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>
This commit is contained in:
Vadim Lazuk
2026-11-11 21:22:02 +08:00
committed by GitHub
co-authored by Inf1mum
parent 8e90341b3a
commit fc264d3f5c
2 changed files with 41 additions and 0 deletions
+30
View File
@@ -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"]);
});
});
+11
View File
@@ -590,6 +590,17 @@ export function Tree<T>(props: TreeProps<T>) {
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");