VirtualGrid unstable
A grid that renders only the rows and columns near the visible part of its scroll container, and leaves room for the others so both scrollbars cover the whole grid.
VirtualGrid is a headless component: it keeps a window of rows and a
window of columns, each worked out by Virtualizer on its own axis, and
a mount on grid follows scrolling, resizing and focus. Your view draws
the scroll container, the grid, any header, the body, and a row for each
index in rows with a cell for each index in columns.
Anatomy
| Part | Element |
|---|---|
viewport |
<div>: the element that scrolls, around grid |
grid |
<div>, role="table" unless another role is set: aria-rowcount, aria-colcount and the full width |
body |
<div>, role="rowgroup" unless another role is set: the rows, after any header in grid |
row |
<div>, role="row" unless another role is set |
cell |
<div>, role="cell" unless another role is set |
row and cell take { element, index }. Draw a row for each index
in rows, in order, as children of body, and in each a cell for each
index in columns, in order, keying each element by its index or the
item's own id. A header row goes in grid before body: draw its cells
with cell too, in a row of your own with display: flex, and they line
up with the columns below, as a role="columnheader" set on them stays.
Data attributes
rowandcell:data-index, the index of the row or the column.
Keyboard
Rows and cells say where they are in the whole grid with aria-rowindex
and aria-colindex, and grid says how big it is with aria-rowcount
and aria-colcount. They count from each axis's firstPosition out of
its setSize, so a header row takes firstPosition: 2 on rows. The
cell that has the focus, or holds the element that has it, stays rendered
with the cells around it wherever either axis scrolls, so Tab and Shift+Tab
reach its neighbours in its row, but not columns that aren't rendered.
Removing the focused cell moves focus to the cell now at its row and column,
clamped to the grid, then to viewport, which sets tabindex="0" while
its content overflows and tabindex="-1" otherwise. Arrow keys move
nothing, which is why the roles are table, not grid.
Layout
Sizes are exact: a row is size tall and a column size wide, or
sizes[index] when sizes is given, and nothing is measured. Rows are in
the flow of body and cells in the flow of each flex row, so
position: sticky works on a header. Rows are windowed from the top of
body, so a header above it is accounted for. Columns run from the right
edge in a right-to-left direction.
Scrolling
scrollTo on each axis scrolls to an index, an offset or the end, from
the start of body on that axis. A request waits while the axis has no
items, until it's applied, a newer one replaces it, or the user scrolls.
After each scroll the grid also renders ahead in the direction it moved,
as VirtualList does.
Styling
grid writes inline-size, the width of all columns. body writes
padding-block-start and padding-block-end for the rows left out, and
the first row of a range kept apart from the others margin-block-start.
A row writes display: flex and block-size, a cell inline-size, both
with box-sizing: border-box, and the first cell of each range
margin-inline-start for the columns left out before it. A viewport
needs a bounded size and an overflow that scrolls.
Limits
gridcarries the component's mount, so it can't take a mount of your own.- The grid scrolls with its
viewportonly. - A sticky header covers the top rows of the window, which still renders them.
- On the server, and on a host that doesn't run mounts, the first
initialCountrows and columns are rendered, or those around ascrollTotarget.
Example A grid of ten thousand rows by two hundred columns
import { Html, HtmlServer } from "effect/ui/html"import { VirtualGrid } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Sheet = Html.component({ name: "Sheet", model: Schema.Struct({}), message: Schema.Never}).make({ init: () => ({}), update: ($) => $.model, view: (h) => VirtualGrid(h, { props: { rows: { count: 10_000, size: 32, initialCount: 20 }, columns: { count: 200, size: 120, initialCount: 8 } }, view: (grid) => grid.viewport(h.div.class("scroller")).children( grid.grid(h.div).children( grid.body(h.div).children( grid.rows.map((row) => grid.row({ element: h.div.key(String(row)), index: row }).children( grid.columns.map((column) => grid.cell({ element: h.div.key(String(column)), index: column }) .children(row + ":" + column) ) ) ) ) ) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Sheet, {}))if (!html.includes("19:7") || html.includes("20:0") || !html.includes('aria-colcount="200"')) { throw new Error("expected the first 20 rows and 8 columns of 10000 by 200")}Components
VirtualGrid
unstableA headless virtual grid. Call it with the consumer's h, its props, and
a view closure that draws the viewport, the grid with its body,
and a row for each index in rows holding a cell for each index in
columns.
Details
The parts object also carries rows and columns, the indices to render
in order, which are indices in the whole grid, and scrolling, whether
the grid is scrolling, for cells that skip costly content meanwhile.
row({ element, index }) and cell({ element, index }) return
element. grid, body, row and cell write role only when no
other role is set on the element. row owns aria-rowindex and
data-index, cell owns aria-colindex and data-index, and
viewport owns tabindex. Sizes, padding and margins are added after the
consumer's inline style.
It emits ScrolledRows and ScrolledColumns when the visible rows or
columns, an edge or scrolling changes, so a parent can load more rows
near the end.
Signature
declare const VirtualGrid: Headless<{ readonly columns: { readonly count?: number; readonly firstPosition?: number; readonly initialCount?: number; readonly overscan?: number; readonly scrollTo?: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; }; readonly setSize?: number; readonly size: number; readonly sizes?: readonly Array<number>; }; readonly rows: { readonly count?: number; readonly firstPosition?: number; readonly initialCount?: number; readonly overscan?: number; readonly scrollTo?: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; }; readonly setSize?: number; readonly size: number; readonly sizes?: readonly Array<number>; };}, { readonly _tag: "ScrolledRows"; readonly atEnd: boolean; readonly atStart: boolean; readonly first: number; readonly height: number; readonly last: number; readonly length: number; readonly offset: number; readonly scrolling: boolean;} | { readonly _tag: "ScrolledColumns"; readonly atEnd: boolean; readonly atStart: boolean; readonly first: number; readonly height: number; readonly last: number; readonly length: number; readonly offset: number; readonly scrolling: boolean;}, { body: Part<"div", [], never>; cell: <B extends PartTarget<"div">>(__namedParameters: { readonly element: B; readonly index: number; }) => ReturnType<B["attr"]> & Pick<Part<"div", [index: number], never>, "~R">; columns: Array<number>; grid: Part<"div", [], never>; row: <B extends PartTarget<"div">>(__namedParameters: { readonly element: B; readonly index: number; }) => ReturnType<B["attr"]> & Pick<Part<"div", [index: number], never>, "~R">; rows: Array<number>; scrolling: boolean; viewport: Part<"div", [], never>;}, never, never, never, never, never, never, { readonly columns: { readonly announced: { readonly atEnd: boolean; readonly atStart: boolean; readonly first: number; readonly height: number; readonly last: number; readonly length: number; readonly offset: number; readonly scrolling: boolean; } | null; readonly atEnd: boolean; readonly atStart: boolean; readonly dataset: string | null; readonly epoch: number; readonly focused: { readonly index: number; readonly key: string | null; } | null; readonly gap: number; readonly height: number; readonly lanes: number; readonly lead: number; readonly length: number; readonly offset: number; readonly overflowing: boolean; readonly position: { readonly index: number; readonly key: string | null; readonly offset: number; } | null; readonly scroll: { readonly done: boolean; readonly scrollTo: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; }; } | null; readonly scrolling: boolean; readonly sizes: { [key: string]: number; }; readonly width: number; }; readonly rows: { readonly announced: { readonly atEnd: boolean; readonly atStart: boolean; readonly first: number; readonly height: number; readonly last: number; readonly length: number; readonly offset: number; readonly scrolling: boolean; } | null; readonly atEnd: boolean; readonly atStart: boolean; readonly dataset: string | null; readonly epoch: number; readonly focused: { readonly index: number; readonly key: string | null; } | null; readonly gap: number; readonly height: number; readonly lanes: number; readonly lead: number; readonly length: number; readonly offset: number; readonly overflowing: boolean; readonly position: { readonly index: number; readonly key: string | null; readonly offset: number; } | null; readonly scroll: { readonly done: boolean; readonly scrollTo: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; }; } | null; readonly scrolling: boolean; readonly sizes: { [key: string]: number; }; readonly width: number; };}, { readonly _tag: "Reported"; readonly columns: readonly Array<{ readonly _tag: "MovedViewport"; readonly anchor: { readonly index: number; readonly key: string | null; readonly offset: number; } | null; readonly atEnd: boolean; readonly atStart: boolean; readonly height: number; readonly length: number; readonly overflowing: boolean; readonly scrolled: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; } | null; readonly start: number; } | { readonly _tag: "RequestedScroll"; readonly scrollTo: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; }; } | { readonly _tag: "ScrolledByUser"; } | { readonly _tag: "MeasuredItems"; readonly epoch: number; readonly items: readonly Array<{ readonly index: number; readonly key: string | null; readonly left: number; readonly size: number; readonly top: number; }>; readonly width: number; } | { readonly _tag: "BlurredItems"; } | { readonly _tag: "ScrolledViewport"; readonly height: number; readonly length: number; readonly offset: number; readonly scrolling: boolean; } | { readonly _tag: "FocusedItem"; readonly index: number; readonly key: string | null; }>; readonly rows: readonly Array<{ readonly _tag: "MovedViewport"; readonly anchor: { readonly index: number; readonly key: string | null; readonly offset: number; } | null; readonly atEnd: boolean; readonly atStart: boolean; readonly height: number; readonly length: number; readonly overflowing: boolean; readonly scrolled: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; } | null; readonly start: number; } | { readonly _tag: "RequestedScroll"; readonly scrollTo: { readonly align?: "start" | "end" | "center" | "nearest"; readonly behavior?: "instant" | "smooth"; readonly id?: string | number; readonly target: { readonly _tag: "Offset"; readonly offset: number; } | { readonly _tag: "End"; } | { readonly _tag: "Index"; readonly index: number; } | { readonly _tag: "Key"; readonly key: string; }; }; } | { readonly _tag: "ScrolledByUser"; } | { readonly _tag: "MeasuredItems"; readonly epoch: number; readonly items: readonly Array<{ readonly index: number; readonly key: string | null; readonly left: number; readonly size: number; readonly top: number; }>; readonly width: number; } | { readonly _tag: "BlurredItems"; } | { readonly _tag: "ScrolledViewport"; readonly height: number; readonly length: number; readonly offset: number; readonly scrolling: boolean; } | { readonly _tag: "FocusedItem"; readonly index: number; readonly key: string | null; }>;}, QueryEntries<{}, {}>>Models
VirtualGridAxis
unstableSchema for VirtualGridAxis.
Signature
declare const VirtualGridAxis: Struct<{ readonly count: optional<Number>; readonly firstPosition: optional<Number>; readonly initialCount: optional<Number>; readonly overscan: optional<Number>; readonly scrollTo: optional<Struct<{ readonly align: optional<Literals<readonly ["start", "center", "end", "nearest"]>>; readonly behavior: optional<Literals<readonly ["instant", "smooth"]>>; readonly id: optional<Union<readonly [String, Number]>>; readonly target: TaggedEnum<{ End: TaggedStruct<"End", {}>; Index: TaggedStruct<"Index", { readonly index: Number; }>; Key: TaggedStruct<"Key", { readonly key: String; }>; Offset: TaggedStruct<"Offset", { readonly offset: Number; }>; }>; }>>; readonly setSize: optional<Number>; readonly size: Number; readonly sizes: optional<$Array<Number>>;}>The props of one axis of a VirtualGrid: its rows or its columns.
Signature
type VirtualGridAxis = typeof VirtualGridAxis.TypeVirtualGridEvents
unstableThe events of a VirtualGrid.
Details
ScrolledRows and ScrolledColumns carry VirtualList's Scrolled
fields for their axis, with offsets from the start of body and height
the viewport's size along the axis. Each is emitted when its first,
last, atStart, atEnd or scrolling changes.
Signature
declare const VirtualGridEvents: TaggedEnum<{ ScrolledColumns: TaggedStruct<"ScrolledColumns", { atEnd: Boolean; atStart: Boolean; first: Number; height: Number; last: Number; length: Number; offset: Number; scrolling: Boolean; }>; ScrolledRows: TaggedStruct<"ScrolledRows", { atEnd: Boolean; atStart: Boolean; first: Number; height: Number; last: Number; length: Number; offset: Number; scrolling: Boolean; }>;}>The events of a VirtualGrid.
Signature
type VirtualGridEvents = typeof VirtualGridEvents.TypeVirtualGridProps
unstableSchema for VirtualGridProps.
Signature
declare const VirtualGridProps: Struct<{ readonly columns: Struct<{ readonly count: optional<Number>; readonly firstPosition: optional<Number>; readonly initialCount: optional<Number>; readonly overscan: optional<Number>; readonly scrollTo: optional<Struct<{ readonly align: optional<Literals<readonly ["start", "center", "end", "nearest"]>>; readonly behavior: optional<Literals<readonly ["instant", "smooth"]>>; readonly id: optional<Union<readonly [String, Number]>>; readonly target: TaggedEnum<{ End: TaggedStruct<"End", {}>; Index: TaggedStruct<"Index", { readonly index: ...; }>; Key: TaggedStruct<"Key", { readonly key: ...; }>; Offset: TaggedStruct<"Offset", { readonly offset: ...; }>; }>; }>>; readonly setSize: optional<Number>; readonly size: Number; readonly sizes: optional<$Array<Number>>; }>; readonly rows: Struct<{ readonly count: optional<Number>; readonly firstPosition: optional<Number>; readonly initialCount: optional<Number>; readonly overscan: optional<Number>; readonly scrollTo: optional<Struct<{ readonly align: optional<Literals<readonly ["start", "center", "end", "nearest"]>>; readonly behavior: optional<Literals<readonly ["instant", "smooth"]>>; readonly id: optional<Union<readonly [String, Number]>>; readonly target: TaggedEnum<{ End: TaggedStruct<"End", {}>; Index: TaggedStruct<"Index", { readonly index: ...; }>; Key: TaggedStruct<"Key", { readonly key: ...; }>; Offset: TaggedStruct<"Offset", { readonly offset: ...; }>; }>; }>>; readonly setSize: optional<Number>; readonly size: Number; readonly sizes: optional<$Array<Number>>; }>;}>The props of a VirtualGrid, one VirtualGridAxis for its rows and one for its columns.
Signature
type VirtualGridProps = typeof VirtualGridProps.Type