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(tasks): keep keyboard focus on the selected subtask row

Tin54a255349a5f
diff --git a/apps/web/src/components/list-view/index.tsx b/apps/web/src/components/list-view/index.tsx
index 29ec3b074..66ae12e38 100644
--- a/apps/web/src/components/list-view/index.tsx
+++ b/apps/web/src/components/list-view/index.tsx
@@ -98,7 +98,7 @@ function ListView({
return index;
}, [project?.columns]);
- // Per viewer and per project, and only a convenience: a row that cannot be
+ // Per browser and per project, and only a convenience: a row that cannot be
// restored simply starts collapsed.
const projectId = project?.id ?? "";
const [expanded, setExpanded] = useState(() => ({
@@ -149,10 +149,8 @@ function ListView({
[project?.columns, subtaskChildren, tasksById, expandedTasks, activeId],
);
- // j/k, select-all and shift-click ranges follow this order, so it is the
- // order rows are shown in. Selection and focus stay per task, so a task
- // shown both under its parent and in its own place is listed once, where it
- // first appears.
+ // Bulk actions stay per task, even when the list renders several rows for
+ // it. Shift-click ranges use the first appearance in display order.
const shownTaskIds = useCallback(
(column: ProjectWithTasks["columns"][number]) => [
...new Set((rowsByColumn.get(column.id) ?? []).map((row) => row.task.id)),
@@ -305,6 +303,10 @@ function ListView({
project.columns.map((column) => ({
id: column.id,
taskIds: shownTaskIds(column),
+ rows: (rowsByColumn.get(column.id) ?? []).map((row) => ({
+ taskId: row.task.id,
+ rowId: row.rowId,
+ })),
collapsed: !isExpanded(column.id),
})),
// Repeated children follow their parent for navigation, but reordering
diff --git a/apps/web/src/components/list-view/keyboard.test.tsx b/apps/web/src/components/list-view/keyboard.test.tsx
index 06455dd64..237e87d77 100644
--- a/apps/web/src/components/list-view/keyboard.test.tsx
+++ b/apps/web/src/components/list-view/keyboard.test.tsx
@@ -41,7 +41,18 @@ vi.mock("@/hooks/queries/task-relation/use-get-project-task-relations", () => ({
isError: false,
}),
}));
-vi.mock("./task-row", () => ({ default: () => null }));
+vi.mock("./task-row", () => ({
+ default: ({ task, rowId }: { task: { id: string }; rowId: string }) => (
+ <button
+ type="button"
+ data-task-id={task.id}
+ data-task-row-id={rowId}
+ onFocus={() =>
+ useBulkSelectionStore.getState().setFocusedTask(task.id, rowId)
+ }
+ />
+ ),
+}));
vi.mock("../bulk-selection/bulk-toolbar", () => ({ default: () => null }));
vi.mock("../shared/modals/create-task-modal", () => ({ default: () => null }));
vi.mock("../shared/modals/archive-tasks-modal", () => ({
@@ -94,14 +105,34 @@ afterEach(() => {
vi.clearAllMocks();
});
-it("visits a child repeated across status groups once without looping", () => {
+it("visits each rendered occurrence in order without looping", () => {
mount();
const visited: (string | null)[] = [];
- for (let index = 0; index < 5; index++) {
+ for (let index = 0; index < 6; index++) {
press("j", "KeyJ");
- visited.push(useBulkSelectionStore.getState().focusedTaskId);
+ visited.push(useBulkSelectionStore.getState().focusedTaskRowId);
}
- expect(visited).toEqual(["parent", "child", "sibling", "last", "last"]);
+ expect(visited).toEqual([
+ "parent",
+ "parent/child",
+ "sibling",
+ "child",
+ "last",
+ "last",
+ ]);
+});
+
+it("continues from the lower occurrence when a child is focused directly", () => {
+ mount();
+ const row = document.querySelector<HTMLElement>(
+ '[data-task-row-id="child"]',
+ )!;
+ act(() => row.focus());
+ press("j", "KeyJ");
+ expect(useBulkSelectionStore.getState().focusedTaskRowId).toBe("last");
+ act(() => row.focus());
+ press("k", "KeyK");
+ expect(useBulkSelectionStore.getState().focusedTaskRowId).toBe("sibling");
});
it("navigates to a child's own row when its parent group is collapsed", () => {
@@ -122,9 +153,12 @@ it("navigates to a child's own row when its parent group is collapsed", () => {
);
});
-it("reorders a repeated child within its own status group", () => {
+it("reorders a repeated child within its own status group and retains its occurrence", async () => {
mount();
- act(() => useBulkSelectionStore.getState().setFocusedTask("child"));
+ const row = document.querySelector<HTMLElement>(
+ '[data-task-row-id="child"]',
+ )!;
+ act(() => row.focus());
press("ArrowDown", "ArrowDown", true);
expect(
mocks.mutate.mock.calls.map(([task]) => ({
@@ -135,4 +169,6 @@ it("reorders a repeated child within its own status group", () => {
{ id: "last", status: "doing" },
{ id: "child", status: "doing" },
]);
+ await act(() => new Promise((resolve) => requestAnimationFrame(resolve)));
+ expect(row).toHaveFocus();
});
diff --git a/apps/web/src/components/list-view/task-row.test.tsx b/apps/web/src/components/list-view/task-row.test.tsx
index b90190688..4ae8b20d6 100644
--- a/apps/web/src/components/list-view/task-row.test.tsx
+++ b/apps/web/src/components/list-view/task-row.test.tsx
@@ -199,6 +199,44 @@ describe("TaskRow", () => {
},
);
+ it("shows the focus ring only on the focused occurrence of a repeated task", () => {
+ const { container } = render(
+ <>
+ <TaskRow
+ task={task}
+ projectSlug="kan"
+ rowId="parent/task-1"
+ depth={1}
+ />
+ <TaskRow task={task} projectSlug="kan" rowId="task-1" />
+ </>,
+ );
+ act(() =>
+ useBulkSelectionStore.getState().setFocusedTask(task.id, "task-1"),
+ );
+ const rows = container.querySelectorAll<HTMLElement>("[data-task-row-id]");
+ expect(rows[0].parentElement).not.toHaveClass("ring-board-card-focus");
+ expect(rows[1].parentElement).toHaveClass("ring-board-card-focus");
+ });
+
+ it("restores the focused occurrence when repeated rows remount", () => {
+ useBulkSelectionStore.getState().setFocusedTask(task.id, "task-1");
+ const { container } = render(
+ <>
+ <TaskRow
+ task={task}
+ projectSlug="kan"
+ rowId="parent/task-1"
+ depth={1}
+ />
+ <TaskRow task={task} projectSlug="kan" rowId="task-1" />
+ </>,
+ );
+ expect(
+ container.querySelector('[data-task-row-id="task-1"]'),
+ ).toHaveFocus();
+ });
+
it.each(["board", "list"])(
"takes keyboard focus and marks itself as a shortcut surface in the %s view",
(view) => {
diff --git a/apps/web/src/components/list-view/task-row.tsx b/apps/web/src/components/list-view/task-row.tsx
index 9ebdbd5a0..d1c581837 100644
--- a/apps/web/src/components/list-view/task-row.tsx
+++ b/apps/web/src/components/list-view/task-row.tsx
@@ -64,7 +64,7 @@ function TaskRow({
task,
projectSlug,
depth = 0,
- rowId,
+ rowId = task.id,
childCount = 0,
isExpanded = false,
onToggleExpanded,
@@ -104,7 +104,7 @@ function TaskRow({
);
const selectRange = useBulkSelectionStore((state) => state.selectRange);
const setFocusedTask = useBulkSelectionStore((state) => state.setFocusedTask);
- useRestoreTaskFocus(task.id, useBulkSelectionStore);
+ useRestoreTaskFocus(task.id, useBulkSelectionStore, rowId);
const setSelectionAnchor = useBulkSelectionStore(
(state) => state.setSelectionAnchor,
);
@@ -112,7 +112,11 @@ function TaskRow({
state.selectedTaskIds.has(task.id),
);
const isTaskFocused = useBulkSelectionStore(
- (state) => state.focusedTaskId === task.id,
+ (state) =>
+ state.focusedTaskId === task.id &&
+ (state.focusedTaskRowId
+ ? state.focusedTaskRowId === rowId
+ : rowId === task.id),
);
const hoverPrefetch = useTaskHoverPrefetch(task.id);
@@ -166,6 +170,7 @@ function TaskRow({
}
setSelectionAnchor(task.id);
+ setFocusedTask(null, rowId);
openTask();
};
@@ -176,9 +181,8 @@ function TaskRow({
selectRange(task.id);
return;
}
- // A repeated row is not sortable, so it takes none of dnd-kit's keys.
- // Space activates a control with button semantics; the top-level rows
- // hand it to dnd-kit instead, which starts a keyboard drag with it.
+ // Space activates the non-sortable repeat. Top-level rows leave Space to
+ // the view's peekTask shortcut; this view has no keyboard drag sensor.
if (isNestedRepeat && e.key === " ") {
e.preventDefault();
setSelectionAnchor(task.id);
@@ -272,6 +276,7 @@ function TaskRow({
isTaskSelected ? "bg-accent/45" : "group-hover/row:bg-accent/60",
)}
data-task-id={task.id}
+ data-task-row-id={rowId}
data-shortcut-surface
// A repeated row opens its task like any other, so it keeps the
// button semantics but takes none of dnd-kit's attributes: those
@@ -284,7 +289,7 @@ function TaskRow({
onKeyDown={handleKeyDown}
onFocus={(event) => {
if (event.target !== event.currentTarget) return;
- setFocusedTask(isKeyboardFocus(event) ? task.id : null);
+ setFocusedTask(isKeyboardFocus(event) ? task.id : null, rowId);
}}
>
{depth > 0 && (
diff --git a/apps/web/src/components/project/task-view-keys/focus-task-element.ts b/apps/web/src/components/project/task-view-keys/focus-task-element.ts
index 4c78a465e..a0e1d0c49 100644
--- a/apps/web/src/components/project/task-view-keys/focus-task-element.ts
+++ b/apps/web/src/components/project/task-view-keys/focus-task-element.ts
@@ -1,6 +1,12 @@
-export function focusTaskElement(taskId: string, moveFocus: boolean) {
+export function focusTaskElement(
+ taskId: string,
+ moveFocus: boolean,
+ rowId?: string | null,
+) {
const element = document.querySelector<HTMLElement>(
- `[data-task-id="${taskId}"]`,
+ rowId
+ ? `[data-task-id="${taskId}"][data-task-row-id="${rowId}"]`
+ : `[data-task-id="${taskId}"]`,
);
if (!element) return;
if (moveFocus) element.focus({ preventScroll: true });
diff --git a/apps/web/src/components/project/task-view-keys/step-row-focus.ts b/apps/web/src/components/project/task-view-keys/step-row-focus.ts
new file mode 100644
index 000000000..7f5c8fc6e
--- /dev/null
+++ b/apps/web/src/components/project/task-view-keys/step-row-focus.ts
@@ -0,0 +1,21 @@
+import type { FocusDirection, TaskGroup } from "./task-groups";
+
+export function stepRowFocus(
+ groups: TaskGroup[],
+ taskId: string | null,
+ rowId: string | null | undefined,
+ direction: FocusDirection,
+) {
+ if (direction === "left" || direction === "right") return null;
+ const rows = groups
+ .filter((group) => !group.collapsed)
+ .flatMap((group) => group.rows ?? []);
+ if (rows.length === 0) return null;
+ let index = rowId ? rows.findIndex((row) => row.rowId === rowId) : -1;
+ if (index === -1 && taskId) {
+ index = rows.findIndex((row) => row.taskId === taskId);
+ }
+ if (index === -1) return direction === "down" ? rows[0] : rows.at(-1);
+ const next = direction === "down" ? index + 1 : index - 1;
+ return rows[Math.min(Math.max(next, 0), rows.length - 1)];
+}
diff --git a/apps/web/src/components/project/task-view-keys/task-groups.ts b/apps/web/src/components/project/task-view-keys/task-groups.ts
index 9d8ae688e..1c621be10 100644
--- a/apps/web/src/components/project/task-view-keys/task-groups.ts
+++ b/apps/web/src/components/project/task-view-keys/task-groups.ts
@@ -1,6 +1,7 @@
export type TaskGroup = {
id: string;
taskIds: string[];
+ rows?: { taskId: string; rowId: string }[];
collapsed?: boolean;
};
diff --git a/apps/web/src/components/project/task-view-keys/task-selection.ts b/apps/web/src/components/project/task-view-keys/task-selection.ts
index f4fa8371a..40b9f5b42 100644
--- a/apps/web/src/components/project/task-view-keys/task-selection.ts
+++ b/apps/web/src/components/project/task-view-keys/task-selection.ts
@@ -1,8 +1,9 @@
export type TaskSelectionStore = {
getState: () => {
focusedTaskId: string | null;
+ focusedTaskRowId?: string | null;
selectedTaskIds: Set<string>;
- setFocusedTask: (taskId: string | null) => void;
+ setFocusedTask: (taskId: string | null, rowId?: string | null) => void;
toggleSelection: (taskId: string) => void;
selectTasks: (taskIds: string[]) => void;
clearSelection: () => void;
diff --git a/apps/web/src/components/project/task-view-keys/use-restore-task-focus.ts b/apps/web/src/components/project/task-view-keys/use-restore-task-focus.ts
index 5606c4155..8fc0f529d 100644
--- a/apps/web/src/components/project/task-view-keys/use-restore-task-focus.ts
+++ b/apps/web/src/components/project/task-view-keys/use-restore-task-focus.ts
@@ -1,16 +1,30 @@
import { useEffect } from "react";
-type FocusStore = { getState: () => { focusedTaskId: string | null } };
+type FocusStore = {
+ getState: () => {
+ focusedTaskId: string | null;
+ focusedTaskRowId?: string | null;
+ };
+};
-export function useRestoreTaskFocus(taskId: string, store: FocusStore) {
+export function useRestoreTaskFocus(
+ taskId: string,
+ store: FocusStore,
+ rowId?: string,
+) {
useEffect(() => {
- if (store.getState().focusedTaskId !== taskId) return;
+ const state = store.getState();
+ if (state.focusedTaskId !== taskId) return;
+ if (rowId && state.focusedTaskRowId && state.focusedTaskRowId !== rowId)
+ return;
const active = document.activeElement;
if (active && active !== document.body) return;
const element = document.querySelector<HTMLElement>(
- `[data-task-id="${taskId}"]`,
+ rowId && state.focusedTaskRowId
+ ? `[data-task-id="${taskId}"][data-task-row-id="${rowId}"]`
+ : `[data-task-id="${taskId}"]`,
);
element?.focus({ preventScroll: true });
element?.scrollIntoView?.({ block: "nearest", inline: "nearest" });
- }, [taskId, store]);
+ }, [taskId, store, rowId]);
}
diff --git a/apps/web/src/components/project/task-view-keys/use-task-view-shortcuts.ts b/apps/web/src/components/project/task-view-keys/use-task-view-shortcuts.ts
index 9b0aaf8c2..fec34a8d2 100644
--- a/apps/web/src/components/project/task-view-keys/use-task-view-shortcuts.ts
+++ b/apps/web/src/components/project/task-view-keys/use-task-view-shortcuts.ts
@@ -8,6 +8,7 @@ import { prefetchTask } from "@/hooks/queries/task/prefetch-task";
import { useShortcut } from "@/lib/shortcuts/use-shortcut";
import { toast } from "@/lib/toast";
import { focusTaskElement } from "./focus-task-element";
+import { stepRowFocus } from "./step-row-focus";
import {
type FocusDirection,
findTaskGroup,
@@ -65,9 +66,9 @@ export function useTaskViewShortcuts({
} as const)
: null;
- const focus = (taskId: string | null) => {
+ const focus = (taskId: string | null, rowId?: string) => {
if (!taskId) return;
- selection.getState().setFocusedTask(taskId);
+ selection.getState().setFocusedTask(taskId, rowId);
void prefetchTask(queryClient, taskId);
const route = taskRoute(taskId);
if (route) void router.preloadRoute(route).catch(() => undefined);
@@ -75,29 +76,45 @@ export function useTaskViewShortcuts({
if (peeked && peeked !== taskId) {
navigate({ to: ".", search: { taskId }, replace: true });
}
- focusTaskElement(taskId, !peeked);
+ focusTaskElement(taskId, !peeked, rowId);
};
- const step = (direction: FocusDirection) =>
- focus(
- stepFocus(getGroups(), layout, focused() ?? peekedTaskId(), direction),
- );
+ const step = (direction: FocusDirection) => {
+ const groups = getGroups();
+ const taskId = focused() ?? peekedTaskId();
+ if (layout === "list" && groups.some((group) => group.rows)) {
+ const next = stepRowFocus(
+ groups,
+ taskId,
+ selection.getState().focusedTaskRowId,
+ direction,
+ );
+ if (next) focus(next.taskId, next.rowId);
+ return;
+ }
+ focus(stepFocus(groups, layout, taskId, direction));
+ };
const move = (direction: MoveDirection) => {
const taskId = focused();
+ const rowId = selection.getState().focusedTaskRowId;
if (!taskId || !onMove) return;
const overId = moveTarget(getMoveGroups(), layout, taskId, direction);
if (!overId) return;
onMove(taskId, overId);
- requestAnimationFrame(() => focusTaskElement(taskId, !peekedTaskId()));
+ requestAnimationFrame(() =>
+ focusTaskElement(taskId, !peekedTaskId(), rowId),
+ );
};
const withFocusedGroup = (run: (group: TaskGroup) => void) => {
- const found = findTaskGroup(
- getGroups().filter((group) => !group.collapsed),
- focused(),
- );
- if (found) run(found.group);
+ const groups = getGroups().filter((group) => !group.collapsed);
+ const rowId = selection.getState().focusedTaskRowId;
+ const rowGroup =
+ rowId &&
+ groups.find((group) => group.rows?.some((row) => row.rowId === rowId));
+ const group = rowGroup || findTaskGroup(groups, focused())?.group;
+ if (group) run(group);
};
useEffect(() => {
diff --git a/apps/web/src/lib/expanded-rows-storage.ts b/apps/web/src/lib/expanded-rows-storage.ts
index fdf9985a4..5edd26e6d 100644
--- a/apps/web/src/lib/expanded-rows-storage.ts
+++ b/apps/web/src/lib/expanded-rows-storage.ts
@@ -1,5 +1,5 @@
/**
- * Per-viewer, per-project record of which list-view rows are expanded.
+ * Browser-local, per-project record of which list-view rows are expanded.
*
* This is a convenience rather than state the app depends on: a value that
* cannot be read or written simply leaves every row collapsed.
diff --git a/apps/web/src/store/bulk-selection.ts b/apps/web/src/store/bulk-selection.ts
index 02f9a6e90..c5112f881 100644
--- a/apps/web/src/store/bulk-selection.ts
+++ b/apps/web/src/store/bulk-selection.ts
@@ -6,6 +6,7 @@ interface BulkSelectionState {
availableTaskIds: string[];
selectionAnchorId: string | null;
focusedTaskId: string | null;
+ focusedTaskRowId: string | null;
selectTask: (taskId: string) => void;
deselectTask: (taskId: string) => void;
@@ -18,7 +19,7 @@ interface BulkSelectionState {
setAvailableTasks: (taskIds: string[]) => void;
getSelectedCount: () => number;
isSelected: (taskId: string) => boolean;
- setFocusedTask: (taskId: string | null) => void;
+ setFocusedTask: (taskId: string | null, rowId?: string | null) => void;
clearFocus: () => void;
}
@@ -28,6 +29,7 @@ const useBulkSelectionStore = create<BulkSelectionState>((set, get) => ({
availableTaskIds: [],
selectionAnchorId: null,
focusedTaskId: null,
+ focusedTaskRowId: null,
selectTask: (taskId: string) =>
set((state) => ({
@@ -117,14 +119,16 @@ const useBulkSelectionStore = create<BulkSelectionState>((set, get) => ({
return selectedTaskIds.has(taskId);
},
- setFocusedTask: (taskId: string | null) =>
+ setFocusedTask: (taskId, rowId = null) =>
set(() => ({
focusedTaskId: taskId,
+ focusedTaskRowId: rowId,
})),
clearFocus: () =>
set(() => ({
focusedTaskId: null,
+ focusedTaskRowId: null,
})),
}));