@devries-cameron20 / t3code Mirror · read-only / commits / b1c1e07ded
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

fix(web): a click no longer lands on the sidebar row that just slid under it (#23517)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Mantas Kantautas committed Nov 13, 2026 at 00:53 UTC b1c1e07ded4524dfb8dd054dc4dd3504ec2f86d7
3 files changed +325 -1
apps/web/src/components/Sidebar.shiftGuard.browser.test.ts new
+180
@@ -0,0 +1,180 @@
1 +import { describe, expect, it, vi } from "vite-plus/test";
2 +import { userEvent } from "vite-plus/test/browser";
3 +import { fakeTimers, render } from "../../test/render";
4 +import { createSidebarShiftGuard } from "./Sidebar.shiftGuard";
5 +
6 +function mountList(container: HTMLElement, names: readonly string[]) {
7 + const list = document.createElement("ul");
8 + list.style.cssText = "position: relative; margin: 0; padding: 0; width: 200px; list-style: none";
9 + const pressed: string[] = [];
10 + const rows = new Map<string, HTMLLIElement>();
11 + const add = (name: string) => {
12 + const row = document.createElement("li");
13 + row.style.height = "40px";
14 + const button = document.createElement("button");
15 + button.textContent = name;
16 + button.addEventListener("click", () => pressed.push(name));
17 + row.append(button);
18 + list.append(row);
19 + rows.set(name, row);
20 + };
21 + for (const name of names) add(name);
22 + container.append(list);
23 + const guard = createSidebarShiftGuard(list);
24 + return {
25 + pressed,
26 + guard,
27 + button: (name: string) => rows.get(name)!.querySelector("button")!,
28 + add,
29 + remove: (name: string) => rows.get(name)!.remove(),
30 + [Symbol.dispose]: () => guard.dispose(),
31 + };
32 +}
33 +
34 +// userEvent cannot hold the mouse, so `act` runs inside the real press, after the guard saw it.
35 +function duringPress(act: () => void) {
36 + const listener = () => act();
37 + window.addEventListener("pointerdown", listener, { capture: true, once: true });
38 + return {
39 + [Symbol.dispose]: () => window.removeEventListener("pointerdown", listener, { capture: true }),
40 + };
41 +}
42 +
43 +describe("sidebar shift guard", () => {
44 + it("drops a press on a row that slid up on its own, then accepts it after the delay", async () => {
45 + await using screen = await render(null);
46 + using list = mountList(screen.container, ["A", "B", "C"]);
47 + using _clock = fakeTimers();
48 +
49 + list.remove("A");
50 + list.guard.update();
51 + await userEvent.click(list.button("B"));
52 + expect(list.pressed).toEqual([]);
53 +
54 + vi.advanceTimersByTime(500);
55 + await userEvent.click(list.button("B"));
56 + expect(list.pressed).toEqual(["B"]);
57 + });
58 +
59 + it("keeps rows that did not move, or barely moved, clickable", async () => {
60 + await using screen = await render(null);
61 + using list = mountList(screen.container, ["A", "B", "C"]);
62 + using _clock = fakeTimers();
63 +
64 + list.remove("C");
65 + list.button("A").closest("ul")!.style.paddingTop = "2px";
66 + list.guard.update();
67 + await userEvent.click(list.button("A"));
68 + await userEvent.click(list.button("B"));
69 + expect(list.pressed).toEqual(["A", "B"]);
70 + });
71 +
72 + it("keeps rows clickable when an empty list first fills", async () => {
73 + await using screen = await render(null);
74 + using list = mountList(screen.container, []);
75 + using _clock = fakeTimers();
76 +
77 + list.add("A");
78 + list.add("B");
79 + list.guard.update();
80 + await userEvent.click(list.button("B"));
81 + expect(list.pressed).toEqual(["B"]);
82 + });
83 +
84 + it("lets the user act on the row their own press moved", async () => {
85 + await using screen = await render(null);
86 + using list = mountList(screen.container, ["A", "B", "C"]);
87 + using _clock = fakeTimers();
88 + list.button("A").addEventListener("click", () => {
89 + list.remove("A");
90 + list.guard.update();
91 + });
92 +
93 + await userEvent.click(list.button("A"));
94 + await userEvent.click(list.button("B"));
95 + expect(list.pressed).toEqual(["A", "B"]);
96 + });
97 +
98 + it("clears an earlier mark when the user's own input moves the row again", async () => {
99 + await using screen = await render(null);
100 + using list = mountList(screen.container, ["A", "B", "C", "D"]);
101 + using _clock = fakeTimers();
102 +
103 + list.remove("A");
104 + list.guard.update();
105 + await userEvent.keyboard("{Escape}");
106 + list.remove("B");
107 + list.guard.update();
108 + await userEvent.click(list.button("C"));
109 + expect(list.pressed).toEqual(["C"]);
110 + });
111 +
112 + it("still guards a move that lands while the user types into a field", async () => {
113 + await using screen = await render(null);
114 + const field = document.createElement("input");
115 + screen.container.append(field);
116 + using list = mountList(screen.container, ["A", "B", "C"]);
117 + using _clock = fakeTimers();
118 +
119 + field.focus();
120 + await userEvent.keyboard("x");
121 + list.remove("A");
122 + list.guard.update();
123 + await userEvent.click(list.button("B"));
124 + expect(list.pressed).toEqual([]);
125 + });
126 +
127 + it("keeps a held press dropped until its release, whatever happens before", async () => {
128 + await using screen = await render(null);
129 + using list = mountList(screen.container, ["A", "B", "C"]);
130 + using _clock = fakeTimers();
131 +
132 + list.remove("A");
133 + list.guard.update();
134 + using _hold = duringPress(() => {
135 + window.dispatchEvent(new KeyboardEvent("keydown", { key: "Shift" }));
136 + const other = { bubbles: true, pointerId: 9 };
137 + screen.container.dispatchEvent(new PointerEvent("pointerdown", other));
138 + screen.container.dispatchEvent(new PointerEvent("pointerup", other));
139 + vi.advanceTimersByTime(1_000);
140 + });
141 + await userEvent.click(list.button("B"));
142 + expect(list.pressed).toEqual([]);
143 + });
144 +
145 + it("lets keyboard activation through while a dropped press is held", async () => {
146 + await using screen = await render(null);
147 + using list = mountList(screen.container, ["A", "B", "C"]);
148 + using _clock = fakeTimers();
149 +
150 + list.remove("A");
151 + list.guard.update();
152 + list
153 + .button("B")
154 + .dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerId: 9 }));
155 + list.button("C").focus();
156 + await userEvent.keyboard("{Enter}");
157 + expect(list.pressed).toEqual(["C"]);
158 + await userEvent.keyboard("{Shift>}{F10}{/Shift}");
159 + expect(
160 + list
161 + .button("C")
162 + .dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, cancelable: true })),
163 + ).toBe(true);
164 + });
165 +
166 + it("ends a dropped press the browser cancels", async () => {
167 + await using screen = await render(null);
168 + using list = mountList(screen.container, ["A", "B", "C"]);
169 + using _clock = fakeTimers();
170 +
171 + list.remove("A");
172 + list.guard.update();
173 + const touch = { bubbles: true, pointerId: 9, pointerType: "touch" };
174 + list.button("B").dispatchEvent(new PointerEvent("pointerdown", touch));
175 + list.button("B").dispatchEvent(new PointerEvent("pointercancel", touch));
176 + vi.advanceTimersByTime(500);
177 + await userEvent.click(list.button("B"));
178 + expect(list.pressed).toEqual(["B"]);
179 + });
180 +});
apps/web/src/components/Sidebar.shiftGuard.ts new
+137
@@ -0,0 +1,137 @@
1 +// How long a row the list just moved ignores presses. A click that was already
2 +// on its way when the row slid under the pointer lands within this window.
3 +const MOVED_ROW_PRESS_DELAY_MS = 500;
4 +// Moves this soon after the user's own press or key are theirs, such as the next
5 +// row sliding up after Settle. Same window as Cumulative Layout Shift's
6 +// hadRecentInput.
7 +const OWN_INPUT_WINDOW_MS = 500;
8 +
9 +const PRESS_FOLLOW_UPS = ["click", "auxclick", "dblclick", "contextmenu"] as const;
10 +
11 +// Editing text cannot move rows. Enter and modified keys in a field can, such as
12 +// sending a message or a shortcut.
13 +function editsText(event: KeyboardEvent) {
14 + const target = event.target;
15 + const editable =
16 + target instanceof HTMLElement &&
17 + (target.isContentEditable ||
18 + target instanceof HTMLInputElement ||
19 + target instanceof HTMLTextAreaElement);
20 + return editable && event.key !== "Enter" && !event.ctrlKey && !event.metaKey && !event.altKey;
21 +}
22 +
23 +function readTops(list: HTMLElement) {
24 + const tops = new Map<HTMLElement, number>();
25 + for (const child of list.children) {
26 + if (child instanceof HTMLElement) tops.set(child, child.offsetTop);
27 + }
28 + return tops;
29 +}
30 +
31 +/**
32 + * Drops presses on rows that something other than the user just moved, so a
33 + * thread settling, waking or arriving above the pointer cannot hand its click
34 + * to the neighbouring row. Call `update` after each commit that can move rows.
35 + * The list must be positioned so every direct child's offsetTop shares its origin.
36 + */
37 +export function createSidebarShiftGuard(list: HTMLElement) {
38 + const view = list.ownerDocument.defaultView;
39 + let tops = readTops(list);
40 + const movedAt = new WeakMap<Element, number>();
41 + let lastInputAt = Number.NEGATIVE_INFINITY;
42 + // Pointers still down on a dropped press. Only their own release or cancel ends the drop, so
43 + // a key or another pointer meanwhile cannot hand that press's click to the row.
44 + const heldDrops = new Set<number>();
45 + let pressDropped = false;
46 + let releasedDrop: number | null = null;
47 +
48 + const rowOf = (target: EventTarget | null) => {
49 + let node = target instanceof Node ? target : null;
50 + while (node !== null && node.parentNode !== list) node = node.parentNode;
51 + return node instanceof Element ? node : null;
52 + };
53 + const swallow = (event: Event) => {
54 + event.preventDefault();
55 + event.stopPropagation();
56 + };
57 + const onPointerDown = (event: PointerEvent) => {
58 + // A new press from the same pointer means its earlier release was missed.
59 + heldDrops.delete(event.pointerId);
60 + if (releasedDrop === event.pointerId) releasedDrop = null;
61 + const now = performance.now();
62 + const row = rowOf(event.target);
63 + const moved = row === null ? undefined : movedAt.get(row);
64 + if (moved !== undefined && now - moved < MOVED_ROW_PRESS_DELAY_MS) {
65 + heldDrops.add(event.pointerId);
66 + pressDropped = true;
67 + swallow(event);
68 + return;
69 + }
70 + pressDropped = false;
71 + lastInputAt = now;
72 + };
73 + const onPointerUp = (event: PointerEvent) => {
74 + const dropped = heldDrops.delete(event.pointerId);
75 + releasedDrop = dropped ? event.pointerId : null;
76 + pressDropped = dropped;
77 + if (dropped) {
78 + if (rowOf(event.target) !== null) swallow(event);
79 + return;
80 + }
81 + lastInputAt = performance.now();
82 + };
83 + const onPointerCancel = (event: PointerEvent) => {
84 + heldDrops.delete(event.pointerId);
85 + };
86 + const onKeyDown = (event: KeyboardEvent) => {
87 + pressDropped = false;
88 + if (!editsText(event)) lastInputAt = performance.now();
89 + };
90 + const onPressFollowUp = (event: MouseEvent) => {
91 + // Keyboard activation clicks carry no click count; only pointer presses are dropped.
92 + if (event.type === "click" && event.detail === 0) return;
93 + const dropped =
94 + event instanceof PointerEvent && event.pointerId >= 0
95 + ? heldDrops.has(event.pointerId) || releasedDrop === event.pointerId
96 + : pressDropped;
97 + if (dropped && rowOf(event.target) !== null) swallow(event);
98 + };
99 +
100 + // Window capture runs before React and dnd-kit see the press.
101 + view?.addEventListener("pointerdown", onPointerDown, { capture: true });
102 + view?.addEventListener("pointerup", onPointerUp, { capture: true });
103 + view?.addEventListener("pointercancel", onPointerCancel, { capture: true });
104 + view?.addEventListener("keydown", onKeyDown, { capture: true });
105 + for (const type of PRESS_FOLLOW_UPS) {
106 + view?.addEventListener(type, onPressFollowUp, { capture: true });
107 + }
108 +
109 + return {
110 + update() {
111 + const next = readTops(list);
112 + const now = performance.now();
113 + // A list filling from empty slid nothing under the pointer.
114 + const ownMove = tops.size === 0 || now - lastInputAt < OWN_INPUT_WINDOW_MS;
115 + for (const [row, top] of next) {
116 + const previous = tops.get(row);
117 + if (previous === top) continue;
118 + // A nudge of under half a row, such as a header settling, still leaves the row the pointer
119 + // aimed at under it.
120 + if (previous !== undefined && Math.abs(top - previous) * 2 < row.offsetHeight) continue;
121 + // The latest move decides: one the user caused clears an earlier mark.
122 + if (ownMove) movedAt.delete(row);
123 + else movedAt.set(row, now);
124 + }
125 + tops = next;
126 + },
127 + dispose() {
128 + view?.removeEventListener("pointerdown", onPointerDown, { capture: true });
129 + view?.removeEventListener("pointerup", onPointerUp, { capture: true });
130 + view?.removeEventListener("pointercancel", onPointerCancel, { capture: true });
131 + view?.removeEventListener("keydown", onKeyDown, { capture: true });
132 + for (const type of PRESS_FOLLOW_UPS) {
133 + view?.removeEventListener(type, onPressFollowUp, { capture: true });
134 + }
135 + },
136 + };
137 +}
apps/web/src/components/Sidebar.tsx
+8 -1
@@ -217,6 +217,7 @@ import {
217 } from "./Sidebar.drag";
218 import { SidebarPointerSensor } from "./Sidebar.pointer";
219 import { createSidebarListMotion } from "./Sidebar.motion";
220 +import { createSidebarShiftGuard } from "./Sidebar.shiftGuard";
221 import {
222 pickSnoozePreset,
223 resolveSnoozePresets,
@@ -1056,6 +1057,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
1057 const sidebarContentRef = useRef<HTMLDivElement | null>(null);
1058 const dragLabelOffsetRef = useRef(0);
1059 const listMotionRef = useRef<ReturnType<typeof createSidebarListMotion> | null>(null);
1060 + const shiftGuardRef = useRef<ReturnType<typeof createSidebarShiftGuard> | null>(null);
1061 const [dragState, setDragState] = useState<{
1062 readonly activeKey: string;
1063 readonly activeSection: SidebarSection;
@@ -1713,13 +1715,17 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
1715 const motion = createSidebarListMotion(node);
1716 listMotionRef.current = motion;
1717 motion.update(false);
1718 + const shiftGuard = createSidebarShiftGuard(node);
1719 + shiftGuardRef.current = shiftGuard;
1720 return () => {
1721 // Search replaces this list while the Sidebar stays mounted, and a drag
1722 // whose rows are gone can never drop. The compiler memoizes this callback
1723 // with no deps (it reads only refs), so the cleanup runs only on unmount.
1724 dragSensorRef.current?.cancel();
1725 motion.dispose();
1726 + shiftGuard.dispose();
1727 if (listMotionRef.current === motion) listMotionRef.current = null;
1728 + if (shiftGuardRef.current === shiftGuard) shiftGuardRef.current = null;
1729 if (threadListRef.current === node) threadListRef.current = null;
1730 };
1731 };
@@ -2025,8 +2031,9 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
2031 ":",
2032 );
2033 const previousShelfExpansionKeyRef = useRef(shelfExpansionKey);
2028 - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tells the list motion controller to animate or not after each layout-affecting commit
2034 + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- records moved rows and updates list motion after each layout-affecting commit
2035 useLayoutEffect(() => {
2036 + shiftGuardRef.current?.update();
2037 // Shelf rows would travel over stationary headers below the bottom-anchored shelf.
2038 const shelvesUnchanged = previousShelfExpansionKeyRef.current === shelfExpansionKey;
2039 previousShelfExpansionKeyRef.current = shelfExpansionKey;