From fa282ffaf42ca1ae8431d0d34faad0e422e895c0 Mon Sep 17 00:00:00 2001 From: Chris Lorenzo Date: Tue, 24 Nov 2026 14:34:40 -0500 Subject: [PATCH] feat(Grid): scroll="edge" moves only when focus leaves the rows that fit The grid stays still while focus moves through the rows that fit between its unscrolled top and the bottom of a clipping parent (or the screen). Past the last one it scrolls just enough to show the focused row; above the first row shown, that row returns to the grid's y. Co-Authored-By: Claude Opus 5.5 --- src/primitives/Grid.tsx | 36 +++++++++-- tests/contract-grid.test.tsx | 118 +++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 5 deletions(-) diff --git a/src/primitives/Grid.tsx b/src/primitives/Grid.tsx index 72b8eb5..ce7900c 100644 --- a/src/primitives/Grid.tsx +++ b/src/primitives/Grid.tsx @@ -1,5 +1,5 @@ import { createSignal, createMemo, createEffect, createRenderEffect, JSX, untrack, Index } from "solid-js"; -import { type NodeProps, ElementNode, NewOmit, hasFocus } from "@solidtv/solid"; +import { type NodeProps, type INode, ElementNode, NewOmit, hasFocus } from "@solidtv/solid"; import { chainRefs } from "./utils/chainFunctions.js"; export interface GridItemProps { @@ -19,7 +19,13 @@ export interface GridProps extends NewOmit { itemOffset?: number; columns?: number; looping?: boolean; - scroll?: "auto" | "none"; + /** How the grid follows focus vertically: + * auto - the focused row moves to the top (default) + * edge - the grid moves only when focus goes past the last row that fits + * (above the screen's bottom, or a clipping parent's) or above the first + * row shown; it then moves just enough to show that row + * none - the grid does not move */ + scroll?: "auto" | "edge" | "none"; selected?: number; onSelectedChanged?: (index: number, grid: ElementNode, elm?: ElementNode) => void; } @@ -114,9 +120,29 @@ export function Grid(props: GridProps): JSX.Element { }); } - const scrollY = createMemo(() => - props.scroll === "none" ? props.y ?? 0 : -Math.floor(focusedIndex() / columns()) * totalHeight() + (props.y || 0) - ); + // edge: the rows that fit between the grid's unscrolled top and the bottom + // of its clipping parent, or of the screen. At least one. + function visibleRows() { + const parent = gridRef.parent; + const parentTop = parent?.absY ?? 0; + const top = parentTop + (props.y ?? 0); + const bottom = parent?.clipping + ? parentTop + (parent.height ?? 0) + : (gridRef.lng as INode).stage.root.h; + return Math.max(1, Math.floor((bottom - top + (props.itemOffset ?? 0)) / totalHeight())); + } + + // edge: the first row shown. + let topRow = 0; + const scrollY = createMemo(() => { + const y = props.y ?? 0; + if (props.scroll === "none") return y; + const row = Math.floor(focusedIndex() / columns()); + if (props.scroll !== "edge") return -row * totalHeight() + y; + if (row < topRow) topRow = row; + else if (row > topRow) topRow = Math.max(topRow, row - visibleRows() + 1); + return -topRow * totalHeight() + y; + }); // Created once. The spread below re-runs on every vertical move (for `y`), // and handlers built inline in it were new functions each time. Each runs diff --git a/tests/contract-grid.test.tsx b/tests/contract-grid.test.tsx index d3766e8..b2b5834 100644 --- a/tests/contract-grid.test.tsx +++ b/tests/contract-grid.test.tsx @@ -346,6 +346,124 @@ v.describe('Grid', () => { v.expect([focusedId(), grid.y]).toEqual(['g6', 20]); }); + v.it( + 'scroll="edge" in a clipping parent: y moves only when focus leaves the rows that fit; looping jumps to show the last row', + async () => { + let grid!: lng.ElementNode; + // (200 - 20 + 10) / 60: 3 rows fit. + dispose = await mount(() => ( + + + {Cell} + + + )); + const seen = await walk( + [ + 'ArrowDown', + 'ArrowDown', + 'ArrowDown', // row 3 does not fit: one row up + 'ArrowUp', + 'ArrowUp', + 'ArrowUp', // row 0 is above the first row shown + 'ArrowUp', // loops to g9 + 'ArrowDown', // loops to g0 + ], + () => [focusedId(), grid.y], + ); + v.expect(seen).toEqual([ + ['g3', 20], + ['g6', 20], + ['g9', -40], + ['g6', -40], + ['g3', -40], + ['g0', 20], + ['g9', -40], + ['g0', 20], + ]); + }, + ); + + v.it( + 'scroll="edge" with no clipping parent: the rows that fit above the screen bottom (1080)', + async () => { + let grid!: lng.ElementNode; + // (1080 - 960 + 10) / 60: 2 rows fit. + dispose = await mount(() => ( + + + {Cell} + + + )); + const seen = await walk( + ['ArrowDown', 'ArrowDown', 'ArrowDown', 'ArrowUp', 'ArrowUp', 'ArrowUp'], + () => [focusedId(), grid.y], + ); + v.expect(seen).toEqual([ + ['g3', 0], + ['g6', -60], + ['g9', -120], + ['g6', -120], + ['g3', -60], + ['g0', 0], + ]); + }, + ); + + v.it( + 'scroll="edge": selected scrolls just enough to show the selected row', + async () => { + let grid!: lng.ElementNode; + const [sel, setSel] = s.createSignal(9); + dispose = await mount(() => ( + + + {Cell} + + + )); + v.expect([focusedId(), grid.y]).toEqual(['g9', -40]); + setSel(4); + await flush(); + v.expect([focusedId(), grid.y]).toEqual(['g4', -40]); + setSel(1); + await flush(); + v.expect([focusedId(), grid.y]).toEqual(['g1', 20]); + }, + ); + v.it( 'onSelectedChanged is called as (idx, grid, elm) with this = grid; once on mount', async () => {