⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(tasks): preserve focus when toggling subtasks
Tind6356883c548
diff --git a/apps/web/src/components/list-view/column-section.tsx b/apps/web/src/components/list-view/column-section.tsxnew file mode 100644index 000000000..c3ce8adb0--- /dev/null+++ b/apps/web/src/components/list-view/column-section.tsx@@ -0,0 +1,186 @@+import { type UniqueIdentifier, useDroppable } from "@dnd-kit/core";+import {+ SortableContext,+ verticalListSortingStrategy,+} from "@dnd-kit/sortable";+import { AnimatePresence, motion } from "framer-motion";+import { Archive, ChevronRight, Plus } from "lucide-react";+import { useTranslation } from "react-i18next";+import { cn } from "@/lib/cn";+import { getColumnIcon } from "@/lib/column";+import { flattenSubtaskRows } from "@/lib/subtask-tree";+import type { ProjectWithTasks } from "@/types/project";+import TaskRow from "./task-row";++type Column = ProjectWithTasks["columns"][number];++type ColumnSectionProps = {+ column: Column;+ projectSlug: string;+ activeId: UniqueIdentifier | null;+ overColumnId: string | null;+ isExpanded: boolean;+ expandedTasks: Record<string, boolean>;+ subtaskChildren: Map<string, string[]>;+ tasksById: Map<string, Column["tasks"][number]>;+ relationsLoading: boolean;+ toggleSection: (columnId: string) => void;+ toggleTaskExpanded: (rowId: string) => void;+ onAddTask: (columnId: string) => void;+ handleArchiveClick: (column: Column) => void;+};++// Keep the component identity stable so list updates preserve focus and drag state.+export default function ColumnSection({+ column,+ projectSlug,+ activeId,+ overColumnId,+ isExpanded,+ expandedTasks,+ subtaskChildren,+ tasksById,+ relationsLoading,+ toggleSection,+ toggleTaskExpanded,+ onAddTask,+ handleArchiveClick,+}: ColumnSectionProps) {+ const { t } = useTranslation();+ const { setNodeRef } = useDroppable({+ id: column.id,+ data: {+ type: "column",+ column,+ },+ });++ const showDropIndicator = activeId && overColumnId === column.id;++ // A dragged parent collapses for the duration: moving a row while its+ // children are rendered beneath it has no single correct outcome, and+ // hiding them keeps the drag to the one row the user grabbed.+ // Reserving the toggle column on every row keeps the titles aligned, but+ // only where the group actually has subtasks; a project without any keeps+ // the original left edge.+ const rows = flattenSubtaskRows({+ tasks: column.tasks,+ children: subtaskChildren,+ tasksById,+ // Nothing stays expanded while a drag is in flight. Nested repeats are+ // not in the SortableContext, so they never receive the transforms+ // applied to the top-level rows: dragging any task past an expanded+ // parent would slide the parent while its children stayed put, and the+ // subtree would visibly split. Collapsing happens once, as the drag+ // starts, rather than shifting rows under a moving pointer.+ isExpanded: (rowId) => !activeId && Boolean(expandedTasks[rowId]),+ });++ // Until the relations arrive, every task looks childless. Holding the+ // toggle column open means the chevrons appear in place instead of+ // shifting every title sideways, and aria-busy below says the region is+ // still resolving rather than settled and flat.+ const groupHasSubtasks =+ relationsLoading || rows.some((row) => row.childCount > 0 || row.depth > 0);++ return (+ <div+ className={cn(+ "border-b border-border/50 transition-colors duration-150 overflow-auto",+ showDropIndicator && "border-l-4 border-l-ring bg-accent/35",+ )}+ >+ <div className="flex items-center justify-between py-2 px-4 bg-muted/60 border-b border-border/50">+ <button+ type="button"+ onClick={() => toggleSection(column.id)}+ className="flex items-center gap-2 text-sm font-medium text-foreground hover:text-foreground transition-colors"+ >+ <ChevronRight+ className={cn(+ "w-3 h-3 transition-transform",+ isExpanded && "rotate-90",+ )}+ />+ <div className="flex items-center gap-2 h-4">+ {getColumnIcon(column.id, column.isFinal, column.icon)}+ <div className="flex items-center gap-1">+ <span className="mt-1 mr-1">{column.name}</span>+ <span className="text-xs text-muted-foreground mt-0.5">+ {column.tasks.length}+ </span>+ </div>+ </div>+ </button>++ <div className="flex items-center gap-1">+ <button+ type="button"+ onClick={() => {+ onAddTask(column.id);+ }}+ className="p-1 hover:bg-accent rounded text-muted-foreground hover:text-foreground transition-colors"+ title={t("tasks:listView.addTask")}+ >+ <Plus className="w-3 h-3" />+ </button>++ {column.isFinal && column.tasks.length > 0 && (+ <button+ type="button"+ onClick={() => handleArchiveClick(column)}+ className="p-1 hover:bg-accent rounded text-muted-foreground hover:text-foreground transition-colors"+ title={t("tasks:listView.archiveAllTooltip")}+ >+ <Archive className="w-3 h-3" />+ </button>+ )}+ </div>+ </div>++ {isExpanded && (+ <div+ ref={setNodeRef}+ className="bg-card transition-[translate,opacity] duration-150 ease-out starting:-translate-y-1 starting:opacity-0 motion-reduce:starting:translate-y-0"+ >+ {/* Only the top-level rows are sortable; the nested repeats share+ their task id with one of them. */}+ <SortableContext+ items={column.tasks}+ strategy={verticalListSortingStrategy}+ >+ <AnimatePresence initial={false} mode="popLayout">+ {rows.map((row) => (+ <motion.div+ key={row.rowId}+ initial={{ opacity: 0 }}+ animate={{ opacity: 1 }}+ exit={{ opacity: 0 }}+ transition={{ duration: 0.15, ease: [0.23, 1, 0.32, 1] }}+ >+ <TaskRow+ task={row.task}+ projectSlug={projectSlug}+ reserveToggleSpace={groupHasSubtasks}+ isTaskDragging={activeId === row.task.id}+ depth={row.depth}+ rowId={row.rowId}+ childCount={row.childCount}+ isExpanded={row.isExpanded}+ onToggleExpanded={() => toggleTaskExpanded(row.rowId)}+ />+ </motion.div>+ ))}+ </AnimatePresence>+ </SortableContext>++ {column.tasks.length === 0 && (+ <div className="py-6 px-4 text-center text-xs text-muted-foreground">+ {t("tasks:listView.noTasks")}+ </div>+ )}+ </div>+ )}+ </div>+ );+}diff --git a/apps/web/src/components/list-view/index.test.tsx b/apps/web/src/components/list-view/index.test.tsxnew file mode 100644index 000000000..f647004c0--- /dev/null+++ b/apps/web/src/components/list-view/index.test.tsx@@ -0,0 +1,130 @@+import { cleanup, fireEvent, render, screen } from "@testing-library/react";+import { afterEach, describe, expect, it, vi } from "vitest";+import type { ProjectWithTasks } from "@/types/project";+import ListView from ".";++vi.mock("@tanstack/react-router", () => ({ useNavigate: () => vi.fn() }));+vi.mock("react-i18next", () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));+vi.mock("@/hooks/mutations/task/use-update-task", () => ({+ useUpdateTask: () => ({ mutate: vi.fn() }),+}));+vi.mock("@/hooks/use-keyboard-shortcuts", () => ({+ useRegisterShortcuts: vi.fn(),+}));+vi.mock("@/hooks/queries/task-relation/use-get-project-task-relations", () => ({+ default: () => ({+ isLoading: false,+ data: [+ {+ sourceTaskId: "parent",+ targetTaskId: "child",+ relationType: "subtask",+ },+ {+ sourceTaskId: "child",+ targetTaskId: "grandchild",+ relationType: "subtask",+ },+ ],+ }),+}));+vi.mock("../bulk-selection/bulk-toolbar", () => ({ default: () => null }));+vi.mock("../shared/modals/create-task-modal", () => ({ default: () => null }));+vi.mock("../shared/modals/archive-tasks-modal", () => ({+ ArchiveTasksModal: () => null,+}));+vi.mock("./task-row", () => ({+ default: ({+ rowId,+ childCount,+ isExpanded,+ onToggleExpanded,+ }: {+ rowId: string;+ childCount: number;+ isExpanded: boolean;+ onToggleExpanded: () => void;+ }) => (+ <div>+ {rowId}+ {childCount > 0 && (+ <button+ type="button"+ aria-label={`Toggle ${rowId}`}+ aria-expanded={isExpanded}+ onClick={onToggleExpanded}+ >+ Toggle+ </button>+ )}+ </div>+ ),+}));++const project = {+ id: "project-focus",+ slug: "FOC",+ workspaceId: "workspace-1",+ columns: [+ {+ id: "to-do",+ name: "To Do",+ isFinal: false,+ icon: null,+ tasks: ["parent", "child", "grandchild"].map((id) => ({+ id,+ title: id,+ status: "to-do",+ })),+ },+ ],+} as ProjectWithTasks;++afterEach(() => {+ cleanup();+ localStorage.clear();+});++describe("ListView focus", () => {+ it("retains the focused toggle when expanding and collapsing parent and nested rows", () => {+ render(<ListView project={project} />);++ for (const rowId of ["parent", "parent/child"]) {+ const toggle = screen.getByRole("button", { name: `Toggle ${rowId}` });+ toggle.focus();+ fireEvent.click(toggle);++ expect(toggle).toHaveFocus();+ expect(toggle).toHaveAttribute("aria-expanded", "true");+ }++ for (const rowId of ["parent/child", "parent"]) {+ const toggle = screen.getByRole("button", { name: `Toggle ${rowId}` });+ toggle.focus();+ fireEvent.click(toggle);++ expect(toggle).toHaveFocus();+ expect(toggle).toHaveAttribute("aria-expanded", "false");+ }+ });++ it("retains header focus when collapsing and expanding a status group", () => {+ render(<ListView project={project} />);+ const header = screen.getByRole("button", { name: /^To Do/ });+ header.focus();++ fireEvent.click(header);+ expect(header).toHaveFocus();+ expect(+ screen.queryByRole("button", { name: "Toggle parent" }),+ ).not.toBeInTheDocument();++ fireEvent.click(header);+ expect(header).toHaveFocus();+ expect(+ screen.getByRole("button", { name: "Toggle parent" }),+ ).toBeInTheDocument();+ });+});diff --git a/apps/web/src/components/list-view/index.tsx b/apps/web/src/components/list-view/index.tsxindex 3924a9c25..d0847ec94 100644--- a/apps/web/src/components/list-view/index.tsx+++ b/apps/web/src/components/list-view/index.tsx@@ -9,19 +9,13 @@ import {MouseSensor,TouchSensor,type UniqueIdentifier,- useDroppable,useSensor,useSensors,} from "@dnd-kit/core";import { snapCenterToCursor } from "@dnd-kit/modifiers";-import {- SortableContext,- verticalListSortingStrategy,-} from "@dnd-kit/sortable";import { useNavigate } from "@tanstack/react-router";-import { AnimatePresence, motion } from "framer-motion";import { produce } from "immer";-import { Archive, ChevronRight, Flag, Plus } from "lucide-react";+import { Flag } from "lucide-react";import { useCallback, useEffect, useMemo, useState } from "react";import { useTranslation } from "react-i18next";import { priorityColorsTaskCard } from "@/constants/priority-colors";@@ -29,12 +23,11 @@ import { useUpdateTask } from "@/hooks/mutations/task/use-update-task";import useGetProjectTaskRelations from "@/hooks/queries/task-relation/use-get-project-task-relations";import { useRegisterShortcuts } from "@/hooks/use-keyboard-shortcuts";import { cn } from "@/lib/cn";-import { getColumnIcon } from "@/lib/column";import {readExpandedRows,writeExpandedRows,} from "@/lib/expanded-rows-storage";-import { buildSubtaskChildren, flattenSubtaskRows } from "@/lib/subtask-tree";+import { buildSubtaskChildren } from "@/lib/subtask-tree";import { toast } from "@/lib/toast";import useBulkSelectionStore from "@/store/bulk-selection";import useProjectStore from "@/store/project";@@ -42,7 +35,7 @@ import type { ProjectWithTasks } from "@/types/project";import BulkToolbar from "../bulk-selection/bulk-toolbar";import { ArchiveTasksModal } from "../shared/modals/archive-tasks-modal";import CreateTaskModal from "../shared/modals/create-task-modal";-import TaskRow from "./task-row";+import ColumnSection from "./column-section";type ListViewProps = {project: ProjectWithTasks;@@ -343,151 +336,6 @@ function ListView({ project, disableDragDrop = false }: ListViewProps) {setColumnToArchive(null);};- function ColumnSection({- column,- }: {- column: ProjectWithTasks["columns"][number];- }) {- const { setNodeRef } = useDroppable({- id: column.id,- data: {- type: "column",- column,- },- });-- const showDropIndicator = activeId && overColumnId === column.id;-- // A dragged parent collapses for the duration: moving a row while its- // children are rendered beneath it has no single correct outcome, and- // hiding them keeps the drag to the one row the user grabbed.- // Reserving the toggle column on every row keeps the titles aligned, but- // only where the group actually has subtasks; a project without any keeps- // the original left edge.- const rows = flattenSubtaskRows({- tasks: column.tasks,- children: subtaskChildren,- tasksById,- // Nothing stays expanded while a drag is in flight. Nested repeats are- // not in the SortableContext, so they never receive the transforms- // applied to the top-level rows: dragging any task past an expanded- // parent would slide the parent while its children stayed put, and the- // subtree would visibly split. Collapsing happens once, as the drag- // starts, rather than shifting rows under a moving pointer.- isExpanded: (rowId) => !activeId && Boolean(expandedTasks[rowId]),- });-- // Until the relations arrive, every task looks childless. Holding the- // toggle column open means the chevrons appear in place instead of- // shifting every title sideways, and aria-busy below says the region is- // still resolving rather than settled and flat.- const groupHasSubtasks =- relationsLoading ||- rows.some((row) => row.childCount > 0 || row.depth > 0);-- return (- <div- className={cn(- "border-b border-border/50 transition-colors duration-150 overflow-auto",- showDropIndicator && "border-l-4 border-l-ring bg-accent/35",- )}- >- <div className="flex items-center justify-between py-2 px-4 bg-muted/60 border-b border-border/50">- <button- type="button"- onClick={() => toggleSection(column.id)}- className="flex items-center gap-2 text-sm font-medium text-foreground hover:text-foreground transition-colors"- >- <ChevronRight- className={cn(- "w-3 h-3 transition-transform",- expandedSections[column.id] && "rotate-90",- )}- />- <div className="flex items-center gap-2 h-4">- {getColumnIcon(column.id, column.isFinal, column.icon)}- <div className="flex items-center gap-1">- <span className="mt-1 mr-1">{column.name}</span>- <span className="text-xs text-muted-foreground mt-0.5">- {column.tasks.length}- </span>- </div>- </div>- </button>-- <div className="flex items-center gap-1">- <button- type="button"- onClick={() => {- setIsTaskModalOpen(true);- setActiveColumn(column.id);- }}- className="p-1 hover:bg-accent rounded text-muted-foreground hover:text-foreground transition-colors"- title={t("tasks:listView.addTask")}- >- <Plus className="w-3 h-3" />- </button>-- {column.isFinal && column.tasks.length > 0 && (- <button- type="button"- onClick={() => handleArchiveClick(column)}- className="p-1 hover:bg-accent rounded text-muted-foreground hover:text-foreground transition-colors"- title={t("tasks:listView.archiveAllTooltip")}- >- <Archive className="w-3 h-3" />- </button>- )}- </div>- </div>-- {expandedSections[column.id] && (- <div- ref={setNodeRef}- className="bg-card transition-[translate,opacity] duration-150 ease-out starting:-translate-y-1 starting:opacity-0 motion-reduce:starting:translate-y-0"- >- {/* Only the top-level rows are sortable; the nested repeats share- their task id with one of them. */}- <SortableContext- items={column.tasks}- strategy={verticalListSortingStrategy}- >- <AnimatePresence initial={false} mode="popLayout">- {rows.map((row) => (- <motion.div- key={row.rowId}- initial={{ opacity: 0 }}- animate={{ opacity: 1 }}- exit={{ opacity: 0 }}- transition={{ duration: 0.15, ease: [0.23, 1, 0.32, 1] }}- >- <TaskRow- task={row.task}- projectSlug={project?.slug ?? ""}- reserveToggleSpace={groupHasSubtasks}- isTaskDragging={activeId === row.task.id}- depth={row.depth}- rowId={row.rowId}- childCount={row.childCount}- isExpanded={row.isExpanded}- onToggleExpanded={() => toggleTaskExpanded(row.rowId)}- />- </motion.div>- ))}- </AnimatePresence>- </SortableContext>-- {column.tasks.length === 0 && (- <div className="py-6 px-4 text-center text-xs text-muted-foreground">- {t("tasks:listView.noTasks")}- </div>- )}- </div>- )}- </div>- );- }-if (!project?.columns) {return null;}@@ -511,7 +359,25 @@ function ListView({ project, disableDragDrop = false }: ListViewProps) {<div className="w-full h-full overflow-auto bg-muted/20"><div aria-busy={relationsLoading} className="divide-y divide-border/50">{project.columns.map((column) => (- <ColumnSection key={column.id} column={column} />+ <ColumnSection+ key={column.id}+ column={column}+ projectSlug={project.slug}+ activeId={activeId}+ overColumnId={overColumnId}+ isExpanded={expandedSections[column.id]}+ expandedTasks={expandedTasks}+ subtaskChildren={subtaskChildren}+ tasksById={tasksById}+ relationsLoading={relationsLoading}+ toggleSection={toggleSection}+ toggleTaskExpanded={toggleTaskExpanded}+ onAddTask={(columnId) => {+ setIsTaskModalOpen(true);+ setActiveColumn(columnId);+ }}+ handleArchiveClick={handleArchiveClick}+ />))}</div></div>