xonecas.io
70160
Ce

cel-tui / commit aff6be5

fix(core): hit testing now accounts for scroll offsets

hitTest() accepts an optional ScrollOffsetResolver callback. When
descending into scrollable containers, the test coordinates are shifted
by the scroll offset so clicks target the visually correct child.

cel.ts passes a resolver that checks both controlled (props.scrollOffset)
and uncontrolled (path-based map) scroll offsets.

Added tests for vertical scroll, horizontal scroll, and no-scroll cases.

xonecas2026-04-04 aff6be51e229613bb1c9d6cfe0849a69cae654de parent 78e4393

3 files changed

packages/core/src/cel.ts+19−1
@@ -426,11 +426,29 @@ function getMaxScrollOffset(target: LayoutNode): number {
426426 }
427427 }
428428
429+/**
430+ * Resolve the current scroll offset for a layout node.
431+ * Checks controlled (props.scrollOffset), then uncontrolled (path-based map).
432+ */
433+function resolveScrollOffset(ln: import("./layout.js").LayoutNode): number {
434+ const node = ln.node;
435+ if (node.type === "text") return 0;
436+ const props = node.props;
437+ if ((props as any).scrollOffset !== undefined)
438+ return (props as any).scrollOffset;
439+ // Check uncontrolled map
440+ const pathKey = getScrollPathKey(ln);
441+ if (pathKey !== null) {
442+ return uncontrolledScrollOffsets.get(pathKey) ?? 0;
443+ }
444+ return 0;
445+}
446+
429447 function handleMouseEvent(event: MouseEvent): void {
430448 // Hit test on topmost layer first
431449 for (let i = currentLayouts.length - 1; i >= 0; i--) {
432450 const layoutRoot = currentLayouts[i]!;
433- const path = hitTest(layoutRoot, event.x, event.y);
451+ const path = hitTest(layoutRoot, event.x, event.y, resolveScrollOffset);
434452 if (path.length === 0) continue;
435453
436454 if (event.type === "click") {
packages/core/src/hit-test.test.ts+51−0
@@ -63,6 +63,57 @@ describe("hitTest", () => {
6363 expect(path.length).toBe(4); // root > HStack > VStack > Text
6464 expect((path[path.length - 1]!.node as any).content).toBe("deep");
6565 });
66+
67+ test("adjusts for vertical scroll offset", () => {
68+ // VStack with 5 children of height 1 in a 3-row viewport
69+ const node = VStack(
70+ { width: 20, height: 3, overflow: "scroll", scrollOffset: 2 },
71+ [
72+ Text("line 0"),
73+ Text("line 1"),
74+ Text("line 2"),
75+ Text("line 3"),
76+ Text("line 4"),
77+ ],
78+ );
79+ const ln = layout(node, 20, 3);
80+
81+ // With scrollOffset=2, clicking at row 0 should hit "line 2"
82+ // (the 3rd child, which is visually at the top)
83+ const path = hitTest(ln, 3, 0);
84+ expect((path[path.length - 1]!.node as any).content).toBe("line 2");
85+
86+ // Row 2 should hit "line 4"
87+ const path2 = hitTest(ln, 3, 2);
88+ expect((path2[path2.length - 1]!.node as any).content).toBe("line 4");
89+ });
90+
91+ test("adjusts for horizontal scroll offset", () => {
92+ const node = HStack(
93+ { width: 10, height: 3, overflow: "scroll", scrollOffset: 5 },
94+ [
95+ VStack({ width: 5 }, [Text("a")]),
96+ VStack({ width: 5 }, [Text("b")]),
97+ VStack({ width: 5 }, [Text("c")]),
98+ ],
99+ );
100+ const ln = layout(node, 10, 3);
101+
102+ // With scrollOffset=5, clicking at col 0 should hit child "b" (starts at x=5 in layout)
103+ const path = hitTest(ln, 0, 0);
104+ expect((path[path.length - 1]!.node as any).content).toBe("b");
105+ });
106+
107+ test("no scroll adjustment without overflow scroll", () => {
108+ const node = VStack({ width: 20, height: 3 }, [
109+ Text("line 0"),
110+ Text("line 1"),
111+ Text("line 2"),
112+ ]);
113+ const ln = layout(node, 20, 3);
114+ const path = hitTest(ln, 3, 0);
115+ expect((path[path.length - 1]!.node as any).content).toBe("line 0");
116+ });
66117 });
67118
68119 describe("findClickHandler", () => {
packages/core/src/hit-test.ts+48−3
@@ -13,6 +13,18 @@ function pointInRect(x: number, y: number, rect: Rect): boolean {
1313 );
1414 }
1515
16+/**
17+ * Callback to resolve the current scroll offset for a scrollable container.
18+ * Used to adjust hit coordinates when testing children of scrolled containers.
19+ */
20+export type ScrollOffsetResolver = (ln: LayoutNode) => number;
21+
22+/** Default resolver: reads scrollOffset from props, defaults to 0. */
23+function defaultScrollResolver(ln: LayoutNode): number {
24+ const props = getProps(ln);
25+ return (props as any)?.scrollOffset ?? 0;
26+}
27+
1628 /**
1729 * Find the path from root to the deepest node at `(x, y)`.
1830 *
@@ -20,25 +32,58 @@ function pointInRect(x: number, y: number, rect: Rect): boolean {
2032 * deepest hit node (last index). Returns empty array if the point is
2133 * outside the root.
2234 *
35+ * For scrollable containers, child coordinates are adjusted by the
36+ * scroll offset so clicks target the visually correct child.
37+ *
2338 * @param root - Root of the layout tree.
2439 * @param x - Column position.
2540 * @param y - Row position.
41+ * @param getScrollOffset - Optional resolver for scroll offsets.
2642 * @returns Path from root to deepest node at the position.
2743 */
28-export function hitTest(root: LayoutNode, x: number, y: number): LayoutNode[] {
44+export function hitTest(
45+ root: LayoutNode,
46+ x: number,
47+ y: number,
48+ getScrollOffset?: ScrollOffsetResolver,
49+): LayoutNode[] {
50+ const resolver = getScrollOffset ?? defaultScrollResolver;
2951 if (!pointInRect(x, y, root.rect)) return [];
3052
3153 const path: LayoutNode[] = [root];
3254
33- // Recurse into children (depth-first, last child wins for overlaps)
55+ // Track accumulated scroll adjustments as we descend.
56+ // When a scrollable container is encountered, subsequent child rect
57+ // checks use coordinates shifted by the scroll offset.
58+ let adjustX = 0;
59+ let adjustY = 0;
3460 let current = root;
3561 let found = true;
3662
3763 while (found) {
3864 found = false;
65+
66+ // Check if current node is scrollable — if so, adjust coordinates
67+ const props = getProps(current);
68+ if (props?.overflow === "scroll" || current.node.type === "textinput") {
69+ const offset = resolver(current);
70+ if (offset !== 0) {
71+ const isVertical =
72+ current.node.type === "vstack" || current.node.type === "textinput";
73+ if (isVertical) {
74+ adjustY += offset;
75+ } else {
76+ adjustX += offset;
77+ }
78+ }
79+ }
80+
81+ // Test children with scroll-adjusted coordinates
82+ const testX = x + adjustX;
83+ const testY = y + adjustY;
3984 for (let i = current.children.length - 1; i >= 0; i--) {
4085 const child = current.children[i]!;
41- if (pointInRect(x, y, child.rect)) {
86+ if (pointInRect(testX, testY, child.rect)) {
4287 path.push(child);
4388 current = child;
4489 found = true;