Skip to main content

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 / dynamic / overscan

Fixed height

#0 · Item 1
#1 · Item 2
#2 · Item 3
#3 · Item 4

Dynamic height

#0 · Item 1
#1 · Item 2
#2 · Item 3

Overscan buffer (overscan)

#0 · Item 1
#1 · Item 2
#2 · Item 3
#3 · Item 4
#4 · Item 5
#5 · Item 6
#6 · Item 7
#7 · Item 8
#8 · Item 9
#9 · Item 10
#10 · Item 11
#11 · Item 12
#12 · Item 13
#13 · Item 14
#14 · Item 15
#15 · Item 16
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​

PropDescriptionTypeOptionsDefault
dataData sourceT[]——
heightContainer height (numbers are px)number | string——
itemHeightFixed-mode item height (choose one of itemHeight / itemSize)number——
itemSizeDynamic mode: function returning each item's height(index: number) => number——
renderItemItem render function (scoped args { item, index })(item: T, index: number) => ReactNode——
overscanExtra rows rendered above/below the windownumber—2
sxLayout shorthandSxProps——
Vue equivalent

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).

Two modes

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.

Accessibility

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'