Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): isolate dialog portal events (#651)

Gener0b8f3e0eb138
diff --git a/src/web/src/components/ui/dialog.test.ts b/src/web/src/components/ui/dialog.test.ts
new file mode 100644
index 000000000..3fdeb094b
--- /dev/null
+++ b/src/web/src/components/ui/dialog.test.ts
@@ -0,0 +1,60 @@
+import React from "react"
+import TestRenderer, { act } from "react-test-renderer"
+import { describe, expect, it, vi } from "vitest"
+
+vi.mock("@base-ui/react/dialog", () => ({
+ Dialog: {
+ Root: ({ children, ...props }: React.ComponentProps<"div">) =>
+ React.createElement("mock-dialog-root", props, children),
+ Trigger: ({ children, ...props }: React.ComponentProps<"button">) =>
+ React.createElement("mock-dialog-trigger", props, children),
+ Portal: ({ children }: { children: React.ReactNode }) =>
+ React.createElement(React.Fragment, null, children),
+ Backdrop: (props: React.ComponentProps<"div">) =>
+ React.createElement("mock-dialog-backdrop", props),
+ Popup: ({ children, ...props }: React.ComponentProps<"div">) =>
+ React.createElement("mock-dialog-popup", props, children),
+ Close: ({ children, ...props }: React.ComponentProps<"button">) =>
+ React.createElement("mock-dialog-close", props, children),
+ Title: ({ children, ...props }: React.ComponentProps<"h2">) =>
+ React.createElement("mock-dialog-title", props, children),
+ Description: ({ children, ...props }: React.ComponentProps<"p">) =>
+ React.createElement("mock-dialog-description", props, children),
+ },
+}))
+
+import { DialogContent } from "./dialog"
+
+describe("DialogContent", () => {
+ it("runs popup interaction handlers and stops portal propagation", () => {
+ const handlerNames = [
+ "onClick",
+ "onContextMenu",
+ "onPointerCancel",
+ "onPointerDown",
+ "onPointerMove",
+ "onPointerUp",
+ "onTouchCancel",
+ "onTouchEnd",
+ "onTouchMove",
+ "onTouchStart",
+ ] as const
+ const handlers = Object.fromEntries(handlerNames.map((name) => [name, vi.fn()]))
+ let renderer: TestRenderer.ReactTestRenderer
+
+ act(() => {
+ renderer = TestRenderer.create(
+ React.createElement(DialogContent, handlers, "Dialog body"),
+ )
+ })
+
+ const popup = renderer!.root.findByType("mock-dialog-popup")
+ for (const name of handlerNames) {
+ const event = { stopPropagation: vi.fn() }
+ act(() => popup.props[name](event))
+ expect(handlers[name]).toHaveBeenCalledOnce()
+ expect(handlers[name]).toHaveBeenCalledWith(event)
+ expect(event.stopPropagation).toHaveBeenCalledOnce()
+ }
+ })
+})
diff --git a/src/web/src/components/ui/dialog.tsx b/src/web/src/components/ui/dialog.tsx
index ca39d8744..04374ecea 100644
--- a/src/web/src/components/ui/dialog.tsx
+++ b/src/web/src/components/ui/dialog.tsx
@@ -7,6 +7,15 @@ import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { XIcon } from "lucide-react"
+function stopPropagationAfter<Event extends React.SyntheticEvent>(
+ handler: ((event: Event) => void) | undefined,
+) {
+ return (event: Event) => {
+ handler?.(event)
+ event.stopPropagation()
+ }
+}
+
function Dialog({ ...props }: DialogPrimitive.Root.Props) {
return <DialogPrimitive.Root data-slot="dialog" {...props} />
}
@@ -43,6 +52,16 @@ function DialogContent({
className,
children,
showCloseButton = true,
+ onClick,
+ onContextMenu,
+ onPointerCancel,
+ onPointerDown,
+ onPointerMove,
+ onPointerUp,
+ onTouchCancel,
+ onTouchEnd,
+ onTouchMove,
+ onTouchStart,
...props
}: DialogPrimitive.Popup.Props & {
showCloseButton?: boolean
@@ -57,6 +76,16 @@ function DialogContent({
className
)}
{...props}
+ onClick={stopPropagationAfter(onClick)}
+ onContextMenu={stopPropagationAfter(onContextMenu)}
+ onPointerCancel={stopPropagationAfter(onPointerCancel)}
+ onPointerDown={stopPropagationAfter(onPointerDown)}
+ onPointerMove={stopPropagationAfter(onPointerMove)}
+ onPointerUp={stopPropagationAfter(onPointerUp)}
+ onTouchCancel={stopPropagationAfter(onTouchCancel)}
+ onTouchEnd={stopPropagationAfter(onTouchEnd)}
+ onTouchMove={stopPropagationAfter(onTouchMove)}
+ onTouchStart={stopPropagationAfter(onTouchStart)}
>
{children}
{showCloseButton && (
diff --git a/src/web/src/test/e2e-ui/44-mobile-reaction-details.spec.ts b/src/web/src/test/e2e-ui/44-mobile-reaction-details.spec.ts
index 89e52fa4a..ac06eb466 100644
--- a/src/web/src/test/e2e-ui/44-mobile-reaction-details.spec.ts
+++ b/src/web/src/test/e2e-ui/44-mobile-reaction-details.spec.ts
@@ -65,6 +65,40 @@ async function expectDialogInsideViewport(page: Page, messageId: string) {
expect(geometry.bottom).toBeLessThanOrEqual(geometry.height)
}
+async function clickDialogBodyWithoutOpeningMessageMenu(page: Page, messageId: string) {
+ const dialog = page.getByTestId(tid.reactionDialog(messageId))
+ await dialog.locator('[data-slot="dialog-description"]').click()
+ await expect(dialog).toBeVisible()
+ await expect(page.getByRole("menuitem")).toHaveCount(0)
+}
+
+async function swipeDialogBodyWithoutStartingMessageReply(page: Page, messageId: string) {
+ const messageRow = page.getByTestId(tid.message(messageId)).locator("div.group").first()
+ await messageRow.evaluate((element) => {
+ ;(element as HTMLElement).setPointerCapture = () => {}
+ })
+ const body = page.getByTestId(tid.reactionDialog(messageId))
+ .locator('[data-slot="dialog-description"]')
+ const box = await body.boundingBox()
+ if (!box) throw new Error("reaction dialog body has no box")
+ const start = { x: box.x + box.width / 3, y: box.y + box.height / 2 }
+ const pointer = { pointerType: "touch", pointerId: 43, isPrimary: true, button: 0 }
+ await body.dispatchEvent("pointerdown", { ...pointer, clientX: start.x, clientY: start.y })
+ await body.dispatchEvent("pointermove", {
+ ...pointer,
+ buttons: 1,
+ clientX: start.x + 72,
+ clientY: start.y + 2,
+ })
+ await body.dispatchEvent("pointerup", {
+ ...pointer,
+ clientX: start.x + 72,
+ clientY: start.y + 2,
+ })
+ await expect(page.locator('[data-slot="composer-reply-preview"]')).toHaveCount(0)
+ await expect(page.getByTestId(tid.reactionDialog(messageId))).toBeVisible()
+}
+
test.describe.serial("mobile reaction details", () => {
let serverId: string
let channelId: string
@@ -126,6 +160,8 @@ test.describe.serial("mobile reaction details", () => {
await expect.poll(() => dialog.locator('[data-slot="dialog-description"]').evaluate(
(element) => element.scrollWidth > element.clientWidth,
)).toBe(true)
+ await clickDialogBodyWithoutOpeningMessageMenu(alice.page, messageId)
+ await swipeDialogBodyWithoutStartingMessageReply(alice.page, messageId)
await expect(alice.page.getByTestId(tid.reactionTab("🔥"))).toHaveAttribute("data-active", "")
await expect(alice.page.getByTestId(tid.reactionMember(userId("bob")))).toBeVisible()
await testInfo.attach("390-mobile-reaction-details.png", {
@@ -312,6 +348,7 @@ test.describe.serial("mobile reaction details", () => {
await expect(chip).toBeVisible()
await holdReaction(alice.page, chip)
await expectDialogInsideViewport(alice.page, threadOpenerId)
+ await clickDialogBodyWithoutOpeningMessageMenu(alice.page, threadOpenerId)
await testInfo.attach("390-thread-opener-reaction-details.png", {
body: await alice.page.screenshot(),
contentType: "image/png",
@@ -325,6 +362,7 @@ test.describe.serial("mobile reaction details", () => {
await gotoAfterUserWsAuth(dm.page, `/c/me/${dmId}`)
await holdReaction(dm.page, dm.page.getByTestId(tid.reactionChip(dmMessageId, "👍")))
await expectDialogInsideViewport(dm.page, dmMessageId)
+ await clickDialogBodyWithoutOpeningMessageMenu(dm.page, dmMessageId)
await expect(dm.page.getByTestId(tid.reactionMember(userId("bob")))).toBeVisible()
await dm.page.getByRole("button", { name: "Close" }).click()
await dm.context.close()
@@ -337,6 +375,7 @@ test.describe.serial("mobile reaction details", () => {
await expect(sheet).toBeVisible()
await holdReaction(context.page, sheet.getByTestId(tid.reactionChip(messageId, OVERFLOW_EMOJIS[0])))
await expectDialogInsideViewport(context.page, messageId)
+ await clickDialogBodyWithoutOpeningMessageMenu(context.page, messageId)
})
test("removing the last reaction keeps the dialog open on its empty state", async ({ asUser }, testInfo) => {