From fc264d3f5c4c44af705b3ba7e345c58f84db3d63 Mon Sep 17 00:00:00 2001 From: Vadim Lazuk Date: Wed, 11 Nov 2026 16:22:02 +0300 Subject: [PATCH] fix(ui): stop double-clicking tree rows from selecting label text (#2726) The second press of a double-click started the browser's word selection, which in the virtualized tree spilled onto the rows an expanding folder had just inserted. Tree cancels the default of repeated presses; the first press still selects and dblclick still activates. Fixes #2713 Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com> --- src/ui/Tree.test.tsx | 30 ++++++++++++++++++++++++++++++ src/ui/Tree.tsx | 11 +++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/ui/Tree.test.tsx b/src/ui/Tree.test.tsx index 39f001729..c1282f642 100644 --- a/src/ui/Tree.test.tsx +++ b/src/ui/Tree.test.tsx @@ -535,3 +535,33 @@ describe("Tree indents", () => { expect(placeholder?.style.paddingLeft).toBe("calc(24px + 1 * var(--tree-indent, 20px))"); }); }); + +describe("Tree double-click", () => { + it("selects on the first press without cancelling its default", () => { + const { tree } = setup(); + const row = within(tree).getByRole("treeitem", { name: "README.md" }); + expect(fireEvent.mouseDown(row, { button: 0, detail: 1 })).toBe(true); + expect(row).toHaveAttribute("aria-selected", "true"); + }); + + it("cancels the second press so double-clicking cannot select label text, and still activates", () => { + const { tree, onActivate } = setup(); + const row = within(tree).getByRole("treeitem", { name: "README.md" }); + fireEvent.mouseDown(row, { button: 0, detail: 1 }); + // The browser starts its word selection on this press; cancelling it keeps + // the labels unselected (jsdom reports cancellation through dispatchEvent). + expect(fireEvent.mouseDown(row, { button: 0, detail: 2 })).toBe(false); + fireEvent.doubleClick(row); + expect(onActivate).toHaveBeenCalledWith(expect.objectContaining({ id: "README.md" }), expect.anything()); + expect(row).toHaveAttribute("aria-selected", "true"); + }); + + it("still expands a folder on double-click", () => { + const { tree } = setup(); + const row = within(tree).getByRole("treeitem", { name: "src" }); + fireEvent.mouseDown(row, { button: 0, detail: 1 }); + fireEvent.mouseDown(row, { button: 0, detail: 2 }); + fireEvent.doubleClick(row); + expect(labels(tree)).toEqual(["src", "main", "util.ts", "README.md"]); + }); +}); diff --git a/src/ui/Tree.tsx b/src/ui/Tree.tsx index 1064a3fef..b92bbcb88 100644 --- a/src/ui/Tree.tsx +++ b/src/ui/Tree.tsx @@ -590,6 +590,17 @@ export function Tree(props: TreeProps) { return; } if (e.button !== 0) return; + // The second press of a double-click starts the browser's word selection, + // which in a virtualized tree spills onto the rows an expanding folder just + // inserted and highlights neighbouring labels. Prevent it: rows are + // controls, not text (the shared List does the same via `user-select`), the + // first press already selected, and the dblclick below still fires, so + // activation keeps working. Rows can host editors (inline rename), so this + // is scoped to repeated clicks instead of unselecting all row content. + if (e.detail >= 2) { + e.preventDefault(); + return; + } setQuery(""); if (e.shiftKey) selectIndex(i, "range"); else if (e.metaKey || e.ctrlKey) selectIndex(i, "toggle");