Components
LazyHGrid
A measured, virtualized horizontal grid. Only the viewport and a small overscan window mount, including when scrolling the page.
Example
Requires React 18.2+ or 19 and shared styles. See setup.
In Scroll View
Loading preview…
Code
'use client';
import { ScrollView } from '@swiftuijs/ui';
import { LazyHGrid } from '@swiftuijs/ui';
export default function Example() {
return ((<ScrollView direction="horizontal" style={{ height: 320 }}>
<LazyHGrid rows={2} spacing={8} estimatedItemWidth={64}>
{Array.from({ length: 1000 }, (_, index) => <div key={index} style={{ padding: 16, minWidth: 120, background: 'var(--sw-color-background-secondary)' }}>Item {index + 1}</div>)}
</LazyHGrid>
</ScrollView>));
}Usage and limitations
- Use a bounded
ScrollViewor any ancestor withoverflow: auto/scroll; otherwise the page is the scrolling surface. estimatedItemWidthdefaults to 48 pixels and is corrected withResizeObservermeasurements. A grid virtualizes complete rows or columns; their size is the largest item in that group.overscandefaults to four extra rows or columns on each side. Focused items and their neighbors remain mounted for keyboard navigation.- Use stable React keys. Keep editable state in your application: off-screen items unmount and their local state resets.
- SSR renders an initial 640-pixel window with estimated dimensions; hydration measures the actual viewport and items. It does not emit every item into server HTML.
- Creating the
childrenarray still costs O(n); virtualization limits mounted components and DOM nodes. For small collections, use ordinary stacks or grids.
API reference
| Prop | Type | Required | Description |
|---|---|---|---|
rows | number | No | Number of rows. Default: 2 |
spacing | number | No | Gap between items in pixels. Default: 0 |
estimatedItemWidth | number | No | Initial column width in pixels, corrected after measurement. Default: 48 |
Inherits additional props from IBaseComponent, Pick<VirtualLayoutOptions, 'overscan'>.