Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(views): share one card-control wrapper so menus open inside board cards (MUL-9295) (#10631)

Jiayuan Zhanga93912904532
diff --git a/packages/views/common/actor-avatar-profile-link.test.tsx b/packages/views/common/actor-avatar-profile-link.test.tsx
index 525dcb4f14..1216e7749f 100644
--- a/packages/views/common/actor-avatar-profile-link.test.tsx
+++ b/packages/views/common/actor-avatar-profile-link.test.tsx
@@ -13,6 +13,7 @@ import { fireEvent, render, screen } from "@testing-library/react";
import { AppLink } from "../navigation/app-link";
import { NavigationProvider } from "../navigation/context";
import type { NavigationAdapter } from "../navigation/types";
+import { CardControl } from "./card-control";
import { DeferredPopup } from "./deferred-popup";
const actorDirectory = vi.hoisted(() => ({ known: true as boolean | undefined }));
@@ -78,23 +79,11 @@ function renderAvatar(adapter: NavigationAdapter) {
);
}
-function PickerWrapper({ children }: { children: React.ReactNode }) {
- const stop = (event: React.SyntheticEvent) => {
- event.stopPropagation();
- event.preventDefault();
- };
- return (
- <div onClick={stop} onMouseDown={stop} onPointerDown={stop}>
- {children}
- </div>
- );
-}
-
function renderCardPicker(adapter: NavigationAdapter) {
return render(
<NavigationProvider value={adapter}>
<AppLink href="/acme/issues/issue-1">
- <PickerWrapper>
+ <CardControl>
<DeferredPopup
trigger={
<span>
@@ -113,7 +102,7 @@ function renderCardPicker(adapter: NavigationAdapter) {
</span>
)}
</DeferredPopup>
- </PickerWrapper>
+ </CardControl>
</AppLink>
</NavigationProvider>,
);
diff --git a/packages/views/common/card-control.test.tsx b/packages/views/common/card-control.test.tsx
new file mode 100644
index 0000000000..f5c4c57b94
--- /dev/null
+++ b/packages/views/common/card-control.test.tsx
@@ -0,0 +1,117 @@
+/**
+ * Controls inside a board card that is both a link and a drag source.
+ *
+ * Menu and popover are deliberately NOT mocked: the regression these guard
+ * against (MUL-9295) only shows with the real Base UI trigger, which opens a
+ * menu on `mousedown`. user-event follows the Pointer Events rule that a
+ * cancelled `pointerdown` suppresses `mousedown`, so it reproduces the bug
+ * where `fireEvent` would not.
+ */
+import { describe, expect, it, vi } from "vitest";
+import { fireEvent, render, screen } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "@multica/ui/components/ui/dropdown-menu";
+import { Popover, PopoverContent, PopoverTrigger } from "@multica/ui/components/ui/popover";
+import { AppLink } from "../navigation/app-link";
+import { NavigationProvider } from "../navigation/context";
+import type { NavigationAdapter } from "../navigation/types";
+import { CardControl } from "./card-control";
+
+function makeAdapter(push: NavigationAdapter["push"]): NavigationAdapter {
+ return {
+ push,
+ replace: vi.fn(),
+ back: vi.fn(),
+ pathname: "/",
+ searchParams: new URLSearchParams(),
+ hash: "",
+ getShareableUrl: (p) => `https://app.example${p}`,
+ };
+}
+
+function renderInCard(control: React.ReactNode) {
+ const push = vi.fn();
+ const card = {
+ onPointerDown: vi.fn(),
+ onMouseDown: vi.fn(),
+ onClick: vi.fn(),
+ };
+ render(
+ <NavigationProvider value={makeAdapter(push)}>
+ <div data-testid="card" {...card}>
+ <AppLink href="/acme/issues/issue-1">
+ <span>Card title</span>
+ <CardControl>{control}</CardControl>
+ </AppLink>
+ </div>
+ </NavigationProvider>,
+ );
+ return { push, card };
+}
+
+describe("CardControl", () => {
+ it("opens a dropdown menu inside a card link and picks an item without navigating", async () => {
+ const user = userEvent.setup();
+ const onSelect = vi.fn();
+ const { push, card } = renderInCard(
+ <DropdownMenu>
+ <DropdownMenuTrigger render={<button type="button">Status</button>} />
+ <DropdownMenuContent>
+ <DropdownMenuItem onClick={onSelect}>Done</DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>,
+ );
+
+ await user.click(screen.getByRole("button", { name: "Status" }));
+ await user.click(await screen.findByRole("menuitem", { name: "Done" }));
+
+ expect(onSelect).toHaveBeenCalledTimes(1);
+ expect(push).not.toHaveBeenCalled();
+ expect(card.onPointerDown).not.toHaveBeenCalled();
+ expect(card.onMouseDown).not.toHaveBeenCalled();
+ expect(card.onClick).not.toHaveBeenCalled();
+ });
+
+ it("opens a popover inside a card link without navigating", async () => {
+ const user = userEvent.setup();
+ const { push, card } = renderInCard(
+ <Popover>
+ <PopoverTrigger render={<button type="button">Assignee</button>} />
+ <PopoverContent>Assignee options</PopoverContent>
+ </Popover>,
+ );
+
+ await user.click(screen.getByRole("button", { name: "Assignee" }));
+
+ expect(await screen.findByText("Assignee options")).toBeInTheDocument();
+ expect(push).not.toHaveBeenCalled();
+ expect(card.onPointerDown).not.toHaveBeenCalled();
+ expect(card.onClick).not.toHaveBeenCalled();
+ });
+
+ it("keeps a press on the control from natively dragging the card link", () => {
+ renderInCard(<button type="button">Status</button>);
+ const link = screen.getByRole("link");
+
+ fireEvent.pointerDown(screen.getByRole("button", { name: "Status" }));
+ expect(fireEvent.dragStart(link)).toBe(false);
+
+ fireEvent.pointerUp(document);
+ expect(fireEvent.dragStart(link)).toBe(true);
+ });
+
+ it("leaves the rest of the card navigating and receiving presses", async () => {
+ const user = userEvent.setup();
+ const { push, card } = renderInCard(<button type="button">Status</button>);
+
+ await user.click(screen.getByText("Card title"));
+
+ expect(push).toHaveBeenCalledWith("/acme/issues/issue-1");
+ expect(card.onPointerDown).toHaveBeenCalled();
+ });
+});
diff --git a/packages/views/common/card-control.tsx b/packages/views/common/card-control.tsx
new file mode 100644
index 0000000000..20c3ff185b
--- /dev/null
+++ b/packages/views/common/card-control.tsx
@@ -0,0 +1,58 @@
+"use client";
+
+import type { PointerEvent, ReactNode, SyntheticEvent } from "react";
+
+const stopPropagation = (event: SyntheticEvent) => event.stopPropagation();
+
+/**
+ * Until the pointer is released, cancels native drags of any element that
+ * contains `control` (the enclosing card link), leaving drags of the
+ * control's own content alone.
+ */
+function blockEnclosingDrag(control: HTMLElement) {
+ const doc = control.ownerDocument;
+ const onDragStart = (event: DragEvent) => {
+ if (event.target instanceof Node && event.target.contains(control)) event.preventDefault();
+ };
+ const release = () => {
+ doc.removeEventListener("dragstart", onDragStart, true);
+ doc.removeEventListener("pointerup", release, true);
+ doc.removeEventListener("pointercancel", release, true);
+ };
+ doc.addEventListener("dragstart", onDragStart, true);
+ doc.addEventListener("pointerup", release, true);
+ doc.addEventListener("pointercancel", release, true);
+}
+
+/**
+ * Wraps a control (picker, menu, button) that sits inside a card which is
+ * itself a link and a drag source, so using the control neither follows the
+ * link nor starts a drag. React events bubble through portals, so this also
+ * covers clicks inside the control's open popup.
+ *
+ * Press events (`pointerdown` / `mousedown`) only stop propagation: they must
+ * keep their default. Cancelling `pointerdown` makes the browser skip the
+ * follow-up `mousedown`, and Base UI menus open on `mousedown`, so a
+ * DropdownMenu inside would never open. With the press default kept, a press
+ * that moves would natively drag the enclosing link, so that `dragstart` is
+ * cancelled instead. `click` is cancelled so the enclosing link does not
+ * navigate.
+ */
+export function CardControl({ children, className }: { children: ReactNode; className?: string }) {
+ return (
+ <div
+ className={className}
+ onPointerDown={(event: PointerEvent<HTMLDivElement>) => {
+ event.stopPropagation();
+ blockEnclosingDrag(event.currentTarget);
+ }}
+ onMouseDown={stopPropagation}
+ onClick={(event) => {
+ event.stopPropagation();
+ event.preventDefault();
+ }}
+ >
+ {children}
+ </div>
+ );
+}
diff --git a/packages/views/issues/components/board-card.tsx b/packages/views/issues/components/board-card.tsx
index e5259c1ae7..1cffcb19bf 100644
--- a/packages/views/issues/components/board-card.tsx
+++ b/packages/views/issues/components/board-card.tsx
@@ -14,6 +14,7 @@ import { descriptionPreview } from "./description-preview";
import { formatDateOnly, isPastDateOnly } from "@multica/core/issues/date";
import { CalendarClock, CalendarDays } from "@multica/ui/icons";
import { ActorAvatar } from "../../common/actor-avatar";
+import { CardControl } from "../../common/card-control";
import { PropertyIcon } from "../../common/property-icon";
import { useWorkspacePaths } from "@multica/core/paths";
import { useActorName } from "@multica/core/workspace/hooks";
@@ -42,19 +43,6 @@ function formatDate(date: string, locale: string): string {
return formatDateOnly(date, { month: "short", day: "numeric" }, locale);
}
-/** Stops event from bubbling to Link/drag handlers */
-function PickerWrapper({ children, className }: { children: React.ReactNode; className?: string }) {
- const stop = (e: React.SyntheticEvent) => {
- e.stopPropagation();
- e.preventDefault();
- };
- return (
- <div onClick={stop} onMouseDown={stop} onPointerDown={stop} className={className}>
- {children}
- </div>
- );
-}
-
export const BoardCardContent = memo(function BoardCardContent({
issue,
editable = false,
@@ -132,7 +120,7 @@ export const BoardCardContent = memo(function BoardCardContent({
const priorityLabel = t(($) => $.priority[issue.priority]);
const priorityIconNode = showPriority ? (
canEdit ? (
- <PickerWrapper className="flex">
+ <CardControl className="flex">
<PriorityPicker
priority={issue.priority}
onUpdate={handleUpdate}
@@ -147,7 +135,7 @@ export const BoardCardContent = memo(function BoardCardContent({
</button>
}
/>
- </PickerWrapper>
+ </CardControl>
) : (
<span
aria-label={priorityLabel}
@@ -184,14 +172,14 @@ export const BoardCardContent = memo(function BoardCardContent({
const assigneeNode = showAssigneeSection ? (
canEdit ? (
- <PickerWrapper className={assigneeContainerClass}>
+ <CardControl className={assigneeContainerClass}>
<AssigneePicker
assigneeType={issue.assignee_type}
assigneeId={issue.assignee_id}
onUpdate={handleUpdate}
trigger={assigneeInner}
/>
- </PickerWrapper>
+ </CardControl>
) : (
<span className={assigneeContainerClass}>{assigneeInner}</span>
)
@@ -279,7 +267,7 @@ export const BoardCardContent = memo(function BoardCardContent({
<div className="ml-auto flex shrink-0 items-center gap-2">
{showStartDate && (
canEdit ? (
- <PickerWrapper className="flex shrink-0">
+ <CardControl className="flex shrink-0">
<StartDatePicker
startDate={issue.start_date}
onUpdate={handleUpdate}
@@ -291,7 +279,7 @@ export const BoardCardContent = memo(function BoardCardContent({
</span>
}
/>
- </PickerWrapper>
+ </CardControl>
) : (
<span className="flex shrink-0 items-center gap-1 text-caption text-muted-foreground">
<CalendarClock className="size-3" />
@@ -301,7 +289,7 @@ export const BoardCardContent = memo(function BoardCardContent({
)}
{showDueDate && (
canEdit ? (
- <PickerWrapper className="flex shrink-0">
+ <CardControl className="flex shrink-0">
<DueDatePicker
dueDate={issue.due_date}
onUpdate={handleUpdate}
@@ -319,7 +307,7 @@ export const BoardCardContent = memo(function BoardCardContent({
</span>
}
/>
- </PickerWrapper>
+ </CardControl>
) : (
<span
className={`flex shrink-0 items-center gap-1 text-caption ${
diff --git a/packages/views/projects/components/project-board.tsx b/packages/views/projects/components/project-board.tsx
index 2b91761ad1..668ca9511f 100644
--- a/packages/views/projects/components/project-board.tsx
+++ b/packages/views/projects/components/project-board.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useState, type ReactNode, type SyntheticEvent } from "react";
+import { useState, type ReactNode } from "react";
import {
DndContext,
DragOverlay,
@@ -22,6 +22,7 @@ import { useWorkspacePaths } from "@multica/core/paths";
import type { Project, UpdateProjectRequest } from "@multica/core/types";
import { AppLink } from "../../navigation";
import { ActorAvatar } from "../../common/actor-avatar";
+import { CardControl } from "../../common/card-control";
import { PAGE_GUTTER } from "../../layout/page-header";
import { useT } from "../../i18n";
import { ProjectStatusBadge, ProjectPriorityBadge } from "./project-badge";
@@ -218,27 +219,6 @@ function ProjectBoardCard({
);
}
-/**
- * Keeps a picker inside the card link from following the link or starting a
- * drag. Menus open on press, so only the click's default is cancelled.
- */
-function CardControl({ children, className }: { children: ReactNode; className?: string }) {
- const stop = (event: SyntheticEvent) => event.stopPropagation();
- return (
- <div
- className={className}
- onPointerDown={stop}
- onMouseDown={stop}
- onClick={(event) => {
- event.stopPropagation();
- event.preventDefault();
- }}
- >
- {children}
- </div>
- );
-}
-
function ProjectBoardCardContent({
project,
onUpdate,