VirtualList
Renders only the items inside the visible window, in fixed-height or dynamic-height mode (the latter locates the window via precomputed offsets and binary search).
Basic usage
Fixed height
Dynamic height
Overscan buffer (overscan)
import { LhVirtualList } from '@lh-ui/react'
const listData = Array.from({ length: 1000 }, (_, i) => `Item ${i + 1}`)
const dynamicSize = (index: number) => (index % 3 === 0 ? 56 : 36)
export default function Demo() {
return (
<div className="demo-col">
{/* Fixed: the row content height matches itemHeight (border-box when height + padding) */}
<LhVirtualList
data={listData}
height={200}
itemHeight={40}
renderItem={(item, index) => (
<div
style={{
height: '100%',
boxSizing: 'border-box',
display: 'flex',
alignItems: 'center',
padding: '0 12px',
}}
>
{`#${index}`} · {item}
</div>
)}
/>
{/* Dynamic: itemSize returns each item's height (may be driven by real measurements) */}
<LhVirtualList
data={listData}
height={200}
itemSize={dynamicSize}
renderItem={(item, index) => (
<div
style={{
height: `${dynamicSize(index)}px`,
padding: '10px 12px',
boxSizing: 'border-box',
}}
>
{`#${index}`} · {item}
</div>
)}
/>
{/* overscan: renders 8 extra rows above/below the window to reduce blank flashes */}
<LhVirtualList
data={listData}
height={200}
itemHeight={40}
overscan={8}
renderItem={(item, index) => (
<div
style={{
height: '100%',
boxSizing: 'border-box',
display: 'flex',
alignItems: 'center',
padding: '0 12px',
}}
>
{`#${index}`} · {item}
</div>
)}
/>
</div>
)
}
VirtualList Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| data | Data source | T[] | — | — |
| height | Container height (numbers are px) | number | string | — | — |
| itemHeight | Fixed-mode item height (choose one of itemHeight / itemSize) | number | — | — |
| itemSize | Dynamic mode: function returning each item's height | (index: number) => number | — | — |
| renderItem | Item render function (scoped args { item, index }) | (item: T, index: number) => ReactNode | — | — |
| overscan | Extra rows rendered above/below the window | number | — | 2 |
| sx | Layout shorthand | SxProps | — | — |
data / height / itemHeight / itemSize / overscan match lh-ui-plus's LhVirtualList;
React has no slots, so plus's #default scoped slot ({ item, index }) is merged into the
renderItem render function (same convention as Tree's renderNode and Table column render).
Passing itemSize switches to dynamic-height mode (the core precomputes offsets and binary-searches
the window); otherwise the fixed-height itemHeight is used (estimated at 40px when omitted). Keep
the rendered row height consistent with the measurement — use box-sizing: border-box when setting
both height and padding, or the real row height will drift from the window math.
The container is overflow: auto and keyboard-scrollable; items outside the window are not in the
DOM and screen readers fetch them on demand (an inherent long-list trade-off — prefer pairing with
search/filters). For a full semantic table use Table (its virtual scrolling is driven by the same
core pure functions).
Styles
import '@lh-ui/react/style/virtual-list'