Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/primitives/Grid.tsx
Original file line number Diff line number Diff line change
@@ -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<T> {
Expand All @@ -19,7 +19,13 @@ export interface GridProps<T> extends NewOmit<NodeProps, 'children'> {
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;
}
Expand Down Expand Up @@ -114,9 +120,29 @@ export function Grid<T>(props: GridProps<T>): 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
Expand Down
118 changes: 118 additions & 0 deletions tests/contract-grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => (
<view y={100} width={1920} height={200} clipping>
<Grid
ref={grid}
autofocus
looping
scroll="edge"
y={20}
items={tenItems}
columns={3}
itemWidth={100}
itemHeight={50}
itemOffset={10}
>
{Cell}
</Grid>
</view>
));
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(() => (
<view y={960} width={1920} height={1080}>
<Grid
ref={grid}
autofocus
scroll="edge"
items={tenItems}
columns={3}
itemWidth={100}
itemHeight={50}
itemOffset={10}
>
{Cell}
</Grid>
</view>
));
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(() => (
<view y={100} width={1920} height={200} clipping>
<Grid
ref={grid}
autofocus
scroll="edge"
selected={sel()}
y={20}
items={tenItems}
columns={3}
itemWidth={100}
itemHeight={50}
itemOffset={10}
>
{Cell}
</Grid>
</view>
));
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 () => {
Expand Down
Loading