@@ -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;