本文へ移動
日本語
「仮想リスト」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

Rows and headers have fixed heights, so the offset of every row is computed once and the window is found with a binary search on each scroll frame; nothing is measured and the scrollbar never jumps. Rows are placed with translateY inside one spacer, and the scroller uses contain: strict, so scrolling 10,000 rows renders about twenty. The pinned header is a separate layer drawn over the top edge and pushed up by the next header.

向いている場面

  • Directories, logs, transactions or any list of thousands of uniform rows.
  • Pickers that must let people scroll the whole set, not just search it.

向かない場面

  • Rows of very different heights: use paging or an infinite list.
  • Under a few hundred rows: a plain list is simpler and supports find-in-page.

使い方

import { useState } from 'react'
import { VirtualList } from './VirtualList.jsx'

export function Directory({ people }) {
  const [picked, setPicked] = useState(null)
  return (
    <VirtualList
      label="Contacts"
      items={people}
      getKey={(p) => p.id}
      getGroup={(p) => p.name[0]}
      rowHeight={56}
      height={400}
      value={picked}
      onSelect={(p) => setPicked(p.id)}
      renderItem={(p) => <span>{p.name}</span>}
    />
  )
}

プロパティ

プロパティ型既定値説明
itemsT[]—All rows, already sorted so that groups are contiguous.
renderItem(item, index, { selected, active }) => ReactNode—Renders one row at rowHeight.
getKey(item, index) => Keyitem.id ?? indexStable key; also what value compares against.
getGroup(item, index) => string—Group label; a header is inserted wherever it changes and pinned while its rows are in view.
rowHeightnumber52Fixed height of every row in px.
headerHeightnumber32Fixed height of group headers in px.
heightnumber400Height of the scroll area in px.
overscannumber6Extra rows rendered above and below the view, so fast scrolling never shows gaps.
valueKey—Key of the selected row.
onSelect(item, index) => void—Called on click, Enter or Space.
onRangeChange({ first, last, rendered }) => void—Called when the rendered window changes; useful for prefetching.
labelstring—Accessible name of the listbox.

アクセシビリティ

  • The scroller is a focusable listbox; rows are options with aria-setsize and aria-posinset, so position is announced as "5,231 of 10,000".
  • Focus stays on the listbox and the active row is aria-activedescendant; that row is always kept in the DOM, even off screen.
  • Keyboard: Up / Down, PageUp / PageDown, Home / End move and scroll the row into view below the pinned header; Enter or Space selects.
  • Group headers are visual only (the pinned copy is aria-hidden). Browser find-in-page cannot see rows that are not rendered; offer search.

モーション

Scrolling is native. The pinned header gains a hairline over 200ms once the list leaves the top; row hover and active fills change in 120ms.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

VirtualList.jsxvirtual-list.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

リスト
「アーチ型カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。
「円柱カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。