Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): anchor suggestion popups to mobile caret (#629)

Gustavo Yeed66b5b5fc94
diff --git a/src/web/src/components/community/messages/composer-suggestion-popups.test.ts b/src/web/src/components/community/messages/composer-suggestion-popups.test.ts
index 507a113fa..2310128a0 100644
--- a/src/web/src/components/community/messages/composer-suggestion-popups.test.ts
+++ b/src/web/src/components/community/messages/composer-suggestion-popups.test.ts
@@ -120,6 +120,61 @@ describe("Composer suggestion popups", () => {
})
+ it("projects mention and channel popups through the same add-once viewport geometry", async () => {
+ Object.assign(window, {
+ visualViewport: {
+ offsetTop: 100,
+ offsetLeft: 20,
+ width: 320,
+ height: 500,
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ },
+ })
+ const mentionState: MentionPopupState = {
+ items: [{ kind: "everyone", id: "everyone", label: "everyone" }],
+ query: "",
+ selectedIndex: 0,
+ command: vi.fn(),
+ getRect: () => rect(400),
+ }
+ const channelState: ChannelRefPopupState = {
+ items: [{
+ id: "channel-1",
+ name: "general",
+ serverId: "server-1",
+ serverName: "One",
+ serverDiscriminator: "0001",
+ }],
+ selectedIndex: 0,
+ command: vi.fn(),
+ getRect: () => rect(400),
+ }
+ let mention!: TestRenderer.ReactTestRenderer
+ let channel!: TestRenderer.ReactTestRenderer
+ await act(async () => {
+ mention = TestRenderer.create(createElement(CommunityMentionList, {
+ state: mentionState,
+ presentation: { status: "ready" },
+ }))
+ channel = TestRenderer.create(createElement(ChannelRefList, {
+ state: channelState,
+ }))
+ })
+
+ const expectedProjection = expect.objectContaining({
+ left: 60,
+ top: 496,
+ transform: "translateY(-100%)",
+ })
+ expect(mention.root.findByProps({
+ "data-testid": "community-mention-popup",
+ }).props.style).toEqual(expectedProjection)
+ expect(channel.root.findByProps({
+ "data-testid": "community-channel-ref-popup",
+ }).props.style).toEqual(expectedProjection)
+ })
+
it("renders virtual/member rows in order and selects on mousedown", async () => {
const command = vi.fn()
const state: MentionPopupState = {
diff --git a/src/web/src/hooks/use-anchored-popover.test.ts b/src/web/src/hooks/use-anchored-popover.test.ts
index fd6d4cdbc..d742c5dd8 100644
--- a/src/web/src/hooks/use-anchored-popover.test.ts
+++ b/src/web/src/hooks/use-anchored-popover.test.ts
@@ -14,16 +14,18 @@ function rect(top: number, left: number, height = 16): AnchorRect {
}
describe("anchoredPopoverStyle", () => {
- it("keeps layout coordinates unchanged inside a non-zero visual viewport", () => {
+ it("projects visual-local coordinates into fixed layout coordinates exactly once", () => {
const style = anchoredPopoverStyle(rect(400, 40), VIEWPORT, 256, 240)
- expect(style.top).toBe(396)
- expect(style.left).toBe(40)
+ expect(style.top).toBe(496)
+ expect(style.left).toBe(60)
+ expect(Number(style.top) - VIEWPORT.top).toBe(396)
+ expect(Number(style.left) - VIEWPORT.left).toBe(40)
expect(style.transform).toBe("translateY(-100%)")
expect(style["--anchored-popover-max-height"]).toBe("240px")
})
it("flips below a caret near the visual viewport top", () => {
- const style = anchoredPopoverStyle(rect(120, 40), VIEWPORT, 256, 240)
+ const style = anchoredPopoverStyle(rect(20, 40), VIEWPORT, 256, 240)
expect(style.top).toBe(140)
expect(Number(style.top) - VIEWPORT.top).toBe(40)
expect(style.transform).toBeUndefined()
@@ -36,26 +38,59 @@ describe("anchoredPopoverStyle", () => {
it("uses the roomier side and reduces list height when neither side fully fits", () => {
const shortViewport = { top: 100, left: 0, width: 320, height: 220 }
- const style = anchoredPopoverStyle(rect(180, 20), shortViewport, 256, 240)
+ const style = anchoredPopoverStyle(rect(80, 20), shortViewport, 256, 240)
expect(style.transform).toBeUndefined()
expect(style["--anchored-popover-max-height"]).toBe("102px")
})
- it("does not add the keyboard-panned viewport offset to the fixed anchor", () => {
+ it("keeps the visible caret gap stable when the keyboard pans the viewport", () => {
const keyboardViewport = { top: 364, left: 0, width: 390, height: 480 }
- const style = anchoredPopoverStyle(rect(797, 76, 21), keyboardViewport, 256, 240)
+ const style = anchoredPopoverStyle(rect(433, 76, 21), keyboardViewport, 256, 240)
expect(style.top).toBe(793)
expect(style.left).toBe(76)
+ expect(Number(style.top) - keyboardViewport.top).toBe(429)
+ expect(433 - (Number(style.top) - keyboardViewport.top)).toBe(4)
expect(style.transform).toBe("translateY(-100%)")
})
it("keeps the left margin when the visible viewport is narrower than the popup", () => {
const narrowViewport = { top: 100, left: 50, width: 200, height: 300 }
- const style = anchoredPopoverStyle(rect(220, 200), narrowViewport, 256, 240)
+ const style = anchoredPopoverStyle(rect(120, 200), narrowViewport, 256, 240)
expect(style.left).toBe(58)
+ expect(Number(style.left) - narrowViewport.left).toBe(8)
expect(style.maxWidth).toBe(184)
})
+ it("reprojects current offsets after hide and reopen without retaining or doubling them", () => {
+ const anchor = rect(400, 70)
+ const hidden = anchoredPopoverStyle(
+ anchor,
+ { top: 0, left: 0, width: 390, height: 844 },
+ 256,
+ 240,
+ )
+ const open = anchoredPopoverStyle(
+ anchor,
+ { top: 300, left: 12, width: 390, height: 480 },
+ 256,
+ 240,
+ )
+ const reopened = anchoredPopoverStyle(
+ anchor,
+ { top: 364, left: 20, width: 390, height: 480 },
+ 256,
+ 240,
+ )
+
+ expect(Number(hidden.top)).toBe(396)
+ expect(Number(open.top)).toBe(696)
+ expect(Number(reopened.top)).toBe(760)
+ expect(Number(open.top) - 300).toBe(Number(hidden.top))
+ expect(Number(reopened.top) - 364).toBe(Number(hidden.top))
+ expect(Number(open.left) - 12).toBe(Number(hidden.left))
+ expect(Number(reopened.left) - 20).toBe(Number(hidden.left))
+ })
+
it("keeps zero-offset desktop geometry unchanged", () => {
const style = anchoredPopoverStyle(
rect(400, 40),
diff --git a/src/web/src/hooks/use-anchored-popover.ts b/src/web/src/hooks/use-anchored-popover.ts
index 3d6cce40d..54f0b3ec1 100644
--- a/src/web/src/hooks/use-anchored-popover.ts
+++ b/src/web/src/hooks/use-anchored-popover.ts
@@ -144,20 +144,23 @@ export function anchoredPopoverStyle(
popupWidth: number,
popupMaxHeight: number,
): AnchoredPopoverStyle {
- const viewportRight = viewport.left + viewport.width
- const viewportBottom = viewport.top + viewport.height
- const minVisibleLeft = viewport.left + VIEWPORT_MARGIN
+ // Caret rectangles are visual-viewport-local, while iOS Safari positions
+ // fixed descendants in layout-viewport coordinates while the keyboard pans
+ // the visual viewport. Keep fit/flip/clamp local, then project the final CSS
+ // coordinates into layout space exactly once.
+ const minVisibleLeft = VIEWPORT_MARGIN
const maxVisibleLeft = Math.max(
minVisibleLeft,
- viewportRight - popupWidth - VIEWPORT_MARGIN,
+ viewport.width - popupWidth - VIEWPORT_MARGIN,
)
- const left = Math.min(
+ const visibleLeft = Math.min(
Math.max(rect.left, minVisibleLeft),
maxVisibleLeft,
)
+ const left = viewport.left + visibleLeft
- const spaceAbove = rect.top - viewport.top
- const spaceBelow = viewportBottom - rect.bottom
+ const spaceAbove = rect.top
+ const spaceBelow = viewport.height - rect.bottom
const fullHeight = popupMaxHeight + POPOVER_CHROME_HEIGHT + POPOVER_GAP + VIEWPORT_MARGIN
const placeBelow = spaceAbove < fullHeight
&& (spaceBelow >= fullHeight || spaceBelow > spaceAbove)
@@ -173,10 +176,10 @@ export function anchoredPopoverStyle(
}
return placeBelow
- ? { ...common, top: rect.bottom + POPOVER_GAP }
+ ? { ...common, top: viewport.top + rect.bottom + POPOVER_GAP }
: {
...common,
- top: rect.top - POPOVER_GAP,
+ top: viewport.top + rect.top - POPOVER_GAP,
transform: "translateY(-100%)",
}
}
diff --git a/src/web/src/test/e2e-ui/31-mention-candidate-pagination.spec.ts b/src/web/src/test/e2e-ui/31-mention-candidate-pagination.spec.ts
index 6049f1615..4342ba58e 100644
--- a/src/web/src/test/e2e-ui/31-mention-candidate-pagination.spec.ts
+++ b/src/web/src/test/e2e-ui/31-mention-candidate-pagination.spec.ts
@@ -28,7 +28,7 @@ function candidate(prefix: string, index: number): Candidate {
}
}
-test("@ candidates page to completion, expose first search page, and keep status anchored on mobile", async ({ asUser }) => {
+test("@ candidates page to completion and shared popups stay anchored through viewport resize signals", async ({ asUser }) => {
const serverId = await seedServer("alice", `Mention pages ${Date.now()}`)
const channelId = await seedChannel("alice", serverId, "mention-pages")
const { page } = await asUser("alice")
@@ -52,7 +52,7 @@ test("@ candidates page to completion, expose first search page, and keep status
configurable: true,
value: viewport,
})
- Object.defineProperty(window, "__setMentionTestVisualViewport", {
+ Object.defineProperty(window, "__setSuggestionTestViewportSize", {
configurable: true,
value: (next: Partial<typeof geometry>) => {
Object.assign(geometry, next)
@@ -186,11 +186,16 @@ test("@ candidates page to completion, expose first search page, and keep status
expectAnchoredToCaret(await readBounds(tid.mentionPopup))
await page.evaluate(() => {
- const setViewport = Reflect.get(window, "__setMentionTestVisualViewport") as
- | ((geometry: { offsetTop: number; height: number }) => void)
+ const setViewport = Reflect.get(window, "__setSuggestionTestViewportSize") as
+ | ((geometry: { height: number }) => void)
| undefined
if (!setViewport) throw new Error("Missing controlled visual viewport")
- setViewport({ offsetTop: 364, height: 480 })
+ // Chromium does not reproduce iOS Safari's fixed/layout viewport split.
+ // Keep this automated journey to resize/scroll refresh and containment,
+ // using a height that still contains Chromium's unpanned caret. Non-zero
+ // offset projection is locked by the geometry unit contract and verified
+ // with the real software keyboard in device QA.
+ setViewport({ height: 820 })
})
await page.evaluate(() => new Promise<void>((resolve) => {
requestAnimationFrame(() => requestAnimationFrame(() => resolve()))