Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web): keep card controls and the touch menu out of the drag

Tin9d77111ddcca
diff --git a/apps/web/src/components/kanban-board/task-card.test.tsx b/apps/web/src/components/kanban-board/task-card.test.tsx
index dffa0797f..e42b2eea4 100644
--- a/apps/web/src/components/kanban-board/task-card.test.tsx
+++ b/apps/web/src/components/kanban-board/task-card.test.tsx
@@ -1,3 +1,5 @@
+import { DndContext, TouchSensor, useSensor, useSensors } from "@dnd-kit/core";
+import { SortableContext } from "@dnd-kit/sortable";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import type { ReactNode } from "react";
@@ -5,16 +7,6 @@ import { afterEach, expect, it, vi } from "vite-plus/test";
import type Task from "@/types/task";
import TaskCard from "./task-card";
-const onTouchStart = vi.hoisted(() => vi.fn());
-
-vi.mock("@dnd-kit/sortable", () => ({
- useSortable: () => ({
- attributes: {},
- listeners: { onTouchStart },
- setNodeRef: vi.fn(),
- transform: null,
- }),
-}));
vi.mock("@tanstack/react-router", () => ({ useNavigate: () => vi.fn() }));
vi.mock("react-i18next", () => ({
useTranslation: () => ({
@@ -64,17 +56,50 @@ vi.mock("@/components/task/task-progress-badges", () => ({
vi.mock("@/components/task/task-pull-requests", () => ({
TaskPullRequests: () => null,
}));
+vi.mock("@/hooks/use-workspace-permission", () => ({
+ // Only the priority picker is editable, so it's the one chip under test.
+ useWorkspacePermission: () =>
+ new Proxy({}, { get: (_, key) => () => key === "canUpdateTasks" }),
+}));
+vi.mock("@/hooks/mutations/task/use-update-task-status-priority", () => ({
+ useUpdateTaskPriority: () => ({ mutateAsync: vi.fn() }),
+}));
vi.mock("./task-card-context-menu/task-card-context-menu-content", () => ({
default: () => null,
}));
-afterEach(cleanup);
+afterEach(() => {
+ cleanup();
+ onDragStart.mockClear();
+});
const queryClient = new QueryClient();
+const onDragStart = vi.fn();
+
+function Board({ children }: { children: ReactNode }) {
+ // Same activation constraint as the board.
+ const sensors = useSensors(
+ useSensor(TouchSensor, {
+ activationConstraint: { delay: 250, tolerance: 10 },
+ }),
+ );
+ return (
+ <DndContext sensors={sensors} onDragStart={onDragStart}>
+ <SortableContext items={["task"]}>{children}</SortableContext>
+ </DndContext>
+ );
+}
+
const wrapper = ({ children }: { children: ReactNode }) => (
- <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ <QueryClientProvider client={queryClient}>
+ <Board>{children}</Board>
+ </QueryClientProvider>
);
+const touchAt = { touches: [{ clientX: 0, clientY: 0 }] };
+const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+const holdPastDelay = () => wait(300);
+
const task: Task = {
id: "task",
title: "Open work",
@@ -93,12 +118,49 @@ const task: Task = {
labels: [{ id: "label", name: "Regression label", color: "red" }],
};
-it("passes touches inside the context menu trigger to the touch sensor", () => {
- render(<TaskCard task={task} />, { wrapper });
- fireEvent.touchStart(screen.getByText("Open work"), {
- touches: [{ clientX: 0, clientY: 0 }],
- });
- expect(onTouchStart).toHaveBeenCalledOnce();
+it("starts a drag when the card body is held", async () => {
+ render(<TaskCard task={task} isFinalColumn={false} />, { wrapper });
+ const title = screen.getByText("Open work");
+ fireEvent.touchStart(title, touchAt);
+ await holdPastDelay();
+ fireEvent.touchEnd(title);
+ expect(onDragStart).toHaveBeenCalledOnce();
+ // dnd-kit swallows document clicks for 50ms after a drag ends.
+ await wait(60);
+});
+
+it("opens a chip's picker on tap without starting a drag", async () => {
+ render(<TaskCard task={task} isFinalColumn={false} />, { wrapper });
+ const chip = screen.getByTitle(/^tasks:priority.label/);
+ fireEvent.touchStart(chip, touchAt);
+ fireEvent.touchEnd(chip);
+ fireEvent.click(chip);
+ expect(
+ await screen.findByPlaceholderText("tasks:bulk.priorityPlaceholder"),
+ ).toBeVisible();
+ await holdPastDelay();
+ expect(onDragStart).not.toHaveBeenCalled();
+});
+
+it("doesn't start a drag when a chip is held", async () => {
+ render(<TaskCard task={task} isFinalColumn={false} />, { wrapper });
+ const chip = screen.getByTitle(/^tasks:priority.label/);
+ fireEvent.touchStart(chip, touchAt);
+ await holdPastDelay();
+ fireEvent.touchEnd(chip);
+ expect(onDragStart).not.toHaveBeenCalled();
+});
+
+it("doesn't start a drag when the portaled picker is held", async () => {
+ render(<TaskCard task={task} isFinalColumn={false} />, { wrapper });
+ fireEvent.click(screen.getByTitle(/^tasks:priority.label/));
+ const search = await screen.findByPlaceholderText(
+ "tasks:bulk.priorityPlaceholder",
+ );
+ fireEvent.touchStart(search, touchAt);
+ await holdPastDelay();
+ fireEvent.touchEnd(search);
+ expect(onDragStart).not.toHaveBeenCalled();
});
it("keeps details in the rendered open column despite a final column sharing its slug", () => {
diff --git a/apps/web/src/components/kanban-board/task-card/index.tsx b/apps/web/src/components/kanban-board/task-card/index.tsx
index 158016150..7fc89360d 100644
--- a/apps/web/src/components/kanban-board/task-card/index.tsx
+++ b/apps/web/src/components/kanban-board/task-card/index.tsx
@@ -35,7 +35,7 @@ function TaskCard({
transition: { duration: 200, easing: EASE_OUT_CSS },
});
// The context menu trigger inside the card stops touchstart from bubbling,
- // so the TouchSensor has to receive it in the capture phase.
+ // so the TouchSensor listens on that trigger instead of this wrapper.
const { onTouchStart, ...dragListeners } = listeners ?? {};
const { handleKeyDown } = useTaskCardClick(task);
const hoverPrefetch = useTaskHoverPrefetch(task.id);
@@ -59,7 +59,6 @@ function TaskCard({
data-shortcut-surface
{...attributes}
{...dragListeners}
- onTouchStartCapture={(event) => onTouchStart?.(event)}
{...hoverPrefetch}
role="button"
tabIndex={0}
@@ -75,6 +74,7 @@ function TaskCard({
disableDragDrop={disableDragDrop}
isFinalColumn={isFinalColumn}
isDragging={isDragging}
+ onDragTouchStart={(event) => onTouchStart?.(event)}
/>
</div>
);
diff --git a/apps/web/src/components/kanban-board/task-card/task-card-content.tsx b/apps/web/src/components/kanban-board/task-card/task-card-content.tsx
index 8457dae28..791f7a933 100644
--- a/apps/web/src/components/kanban-board/task-card/task-card-content.tsx
+++ b/apps/web/src/components/kanban-board/task-card/task-card-content.tsx
@@ -1,5 +1,5 @@
import { Trash2Icon } from "lucide-react";
-import { memo, useMemo, useState } from "react";
+import { memo, type TouchEvent, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { TaskPullRequests } from "@/components/task/task-pull-requests";
import {
@@ -36,12 +36,14 @@ import { useUserPreferencesStore } from "@/store/user-preferences";
import type Task from "@/types/task";
import TaskCardContextMenuContent from "../task-card-context-menu/task-card-context-menu-content";
import { useTaskCardClick } from "./use-task-card-click";
+import { useTouchDragMenu } from "./use-touch-drag-menu";
type TaskCardContentProps = {
task: Task;
disableDragDrop: boolean;
isFinalColumn?: boolean;
isDragging: boolean;
+ onDragTouchStart?: (event: TouchEvent<HTMLDivElement>) => void;
};
function TaskCardContent({
@@ -49,6 +51,7 @@ function TaskCardContent({
disableDragDrop,
isFinalColumn,
isDragging,
+ onDragTouchStart,
}: TaskCardContentProps) {
const { t } = useTranslation();
const { project } = useProjectStore();
@@ -91,6 +94,7 @@ function TaskCardContent({
}, [workspaceUsers, task.userId]);
const { handleClick } = useTaskCardClick(task);
+ const touchDragMenu = useTouchDragMenu(isDragging, onDragTouchStart);
const handleDeleteTask = async () => {
try {
@@ -106,7 +110,9 @@ function TaskCardContent({
return (
<>
<ContextMenu
+ open={touchDragMenu.isMenuOpen}
onOpenChange={(open) => {
+ touchDragMenu.setIsMenuOpen(open);
if (open) setHasOpenedMenu(true);
}}
>
@@ -114,6 +120,8 @@ function TaskCardContent({
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events -- the focusable sortable wrapper owns keyboard access */}
<div
onClick={handleClick}
+ onTouchStart={touchDragMenu.onTouchStart}
+ onTouchMove={touchDragMenu.onTouchMove}
onPointerDown={(event) => {
if (isOwnDomEvent(event)) setIsPressed(true);
}}
diff --git a/apps/web/src/components/kanban-board/task-card/use-touch-drag-menu.test.ts b/apps/web/src/components/kanban-board/task-card/use-touch-drag-menu.test.ts
new file mode 100644
index 000000000..e27bda847
--- /dev/null
+++ b/apps/web/src/components/kanban-board/task-card/use-touch-drag-menu.test.ts
@@ -0,0 +1,59 @@
+import { act, renderHook } from "@testing-library/react";
+import type { TouchEvent } from "react";
+import { expect, it, vi } from "vite-plus/test";
+import { useTouchDragMenu } from "./use-touch-drag-menu";
+
+const card = document.createElement("div");
+
+function touchAt(x: number, y: number, target: Node = card) {
+ return {
+ touches: [{ clientX: x, clientY: y }],
+ target,
+ currentTarget: card,
+ } as unknown as TouchEvent<HTMLDivElement>;
+}
+
+function heldWithMenu(isDragging: boolean) {
+ const view = renderHook(() => useTouchDragMenu(isDragging));
+ act(() => {
+ view.result.current.onTouchStart(touchAt(100, 100));
+ view.result.current.setIsMenuOpen(true);
+ });
+ return view;
+}
+
+it("forwards touches on the card to the touch sensor", () => {
+ const onDragTouchStart = vi.fn();
+ const { result } = renderHook(() =>
+ useTouchDragMenu(false, onDragTouchStart),
+ );
+ result.current.onTouchStart(touchAt(0, 0));
+ expect(onDragTouchStart).toHaveBeenCalledOnce();
+});
+
+it("ignores touches from portaled content outside the card", () => {
+ const onDragTouchStart = vi.fn();
+ const { result } = renderHook(() =>
+ useTouchDragMenu(false, onDragTouchStart),
+ );
+ result.current.onTouchStart(touchAt(0, 0, document.createElement("div")));
+ expect(onDragTouchStart).not.toHaveBeenCalled();
+});
+
+it("closes the menu once a lifted card is moved", () => {
+ const { result } = heldWithMenu(true);
+ act(() => result.current.onTouchMove(touchAt(100, 115)));
+ expect(result.current.isMenuOpen).toBe(false);
+});
+
+it("keeps the menu open while the finger stays within the tolerance", () => {
+ const { result } = heldWithMenu(true);
+ act(() => result.current.onTouchMove(touchAt(106, 94)));
+ expect(result.current.isMenuOpen).toBe(true);
+});
+
+it("keeps the menu open when the card isn't being dragged", () => {
+ const { result } = heldWithMenu(false);
+ act(() => result.current.onTouchMove(touchAt(100, 160)));
+ expect(result.current.isMenuOpen).toBe(true);
+});
diff --git a/apps/web/src/components/kanban-board/task-card/use-touch-drag-menu.ts b/apps/web/src/components/kanban-board/task-card/use-touch-drag-menu.ts
new file mode 100644
index 000000000..406b6c369
--- /dev/null
+++ b/apps/web/src/components/kanban-board/task-card/use-touch-drag-menu.ts
@@ -0,0 +1,40 @@
+import { type TouchEvent, useRef, useState } from "react";
+import { isOwnDomEvent } from "@/lib/is-own-dom-event";
+
+// Matches the TouchSensor tolerance and Base UI's long-press threshold.
+const MOVE_TOLERANCE = 10;
+
+type Point = { x: number; y: number };
+
+/**
+ * Coordinates the touch drag with the context menu's long press. Both start
+ * from a held touch; like iPadOS, moving a lifted card means a drag, so it
+ * closes the menu, and releasing without moving keeps the menu open.
+ */
+export function useTouchDragMenu(
+ isDragging: boolean,
+ onDragTouchStart?: (event: TouchEvent<HTMLDivElement>) => void,
+) {
+ const [isMenuOpen, setIsMenuOpen] = useState(false);
+ const start = useRef<Point | null>(null);
+
+ const onTouchStart = (event: TouchEvent<HTMLDivElement>) => {
+ // Portaled pickers inside the card bubble here through React.
+ if (!isOwnDomEvent(event)) return;
+ const touch = event.touches[0];
+ start.current = touch ? { x: touch.clientX, y: touch.clientY } : null;
+ onDragTouchStart?.(event);
+ };
+
+ const onTouchMove = (event: TouchEvent<HTMLDivElement>) => {
+ const touch = event.touches[0];
+ if (!isMenuOpen || !isDragging || !touch || !start.current) return;
+ if (
+ Math.abs(touch.clientX - start.current.x) > MOVE_TOLERANCE ||
+ Math.abs(touch.clientY - start.current.y) > MOVE_TOLERANCE
+ )
+ setIsMenuOpen(false);
+ };
+
+ return { isMenuOpen, setIsMenuOpen, onTouchStart, onTouchMove };
+}