本文へ移動
日本語

仕組み

A card (no outer border) with an optional title row and a rounded scroll window around one real table. A 12px muted header over the card's only hairline; rows have no lines and the hovered row is a soft pill. Freezing uses position: sticky on the cells, so row heights and alignment match and screen readers read a single table. Frozen cells and the header are opaque, since other cells scroll underneath them. Four soft scroll shadows fade in by opacity; scrolling only toggles data attributes on the root through rAF, with no React re-render.

向いている場面

  • Wide tables that do not fit one screen, such as inventory, reconciliation or channel data, where you must keep seeing which row you are on.
  • Many rows in a card or dialog of fixed height: the header stays at the top while scrolling.
  • Rows with an action button at the end: freeze the action column on the right so it is always within reach.

向かない場面

  • Few columns that fit one screen: use a plain table, since freezing and shadows only add noise.
  • Thousands of rows: freezing keeps the header visible but does not fix performance; use paging or virtual scrolling.
  • Phones where only three or four columns matter: use a card list instead of scrolling a wide table sideways.

使い方

import { StickyTable } from './StickyTable.jsx'

// Every column needs a pixel width; sticky chooses the frozen side
const columns = [
  { key: 'id', title: 'Shipment', width: 136, sticky: 'left' },
  { key: 'mode', title: 'Mode', width: 92 },
  { key: 'parcels', title: 'Parcels', width: 72, align: 'right' },
  { key: 'kg', title: 'Weight', width: 88, align: 'right', render: (r) => `${r.kg} kg` },
  { key: 'eta', title: 'ETA', width: 92 },
  { key: 'action', title: 'Action', width: 88, sticky: 'right', render: (r) => <a href={`/shipments/${r.id}`}>Track</a> },
]

const rows = [
  { id: 'SH-4821', mode: 'Road', parcels: 12, kg: 84.5, eta: 'Nov 26' },
  { id: 'SH-4820', mode: 'Rail', parcels: 4, kg: 18.2, eta: 'Nov 25' },
]

export function Shipments() {
  // Narrower than the columns it scrolls sideways with the first and action columns staying put;
  // taller than 320px it scrolls vertically with the header staying at the top
  return <StickyTable title="Shipments" subtitle="This week" columns={columns} rows={rows} rowKey={(r) => r.id} maxHeight={320} />
}

プロパティ

プロパティ型既定値説明
columnsStickyTableColumn[]—Column definitions: key (also the default field to read), title, width (pixels, required), align, sticky ('left' | 'right', otherwise a normal column) and render (custom cell). Left frozen columns go first, right ones last.
rowsobject[]—The data rows.
rowKey(row) => string | number—Returns a unique key for each row.
maxHeightnumber | string320Maximum height of the scroll area: a number is px, or a string such as '60vh'. Shorter content uses its own height.
stickyHeaderbooleantrueWhether the header freezes at the top.
emptyTextReactNode'No data'Text shown when there are no rows, centred in the visible area.
titleReactNode—Card title shown in the title row. A string title also names the table for screen readers when label is not given.
subtitleReactNode—Muted qualifier after the title, such as a period ("This week").
iconReactNode—Icon before the title, drawn in a tinted 32px rounded square. Decorative (aria-hidden).
actionsReactNode—Controls at the right end of the title row (buttons, a switch, a menu).
labelstring—Table name for screen readers (the <caption>) and the name of the scroll region. Defaults to title when title is a string.
labelsobject—Replaces built-in text (table: fallback name when label is missing, scrollable: (name) => string, the scroll area's accessible name). Shallow-merged over the English defaults.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root <div>.

アクセシビリティ

  • The scroll area is role="region" with a name and enters the Tab order when content overflows; arrows, PageUp / PageDown and Home / End scroll it, with a focus ring drawn over the scroll window.
  • It is one real <table> with <th scope="col"> and <caption>; freezing is only visual, so screen readers read normal row and column order.
  • The filler column is aria-hidden.
  • Scroll shadows only hint that there is more and carry no information; with reduced motion they appear without a transition.
  • On touch the header grows to 44px and scrolling has momentum.

モーション

Scroll shadows fade in and out over 160ms and row hover colour takes 160ms; the freezing itself is the browser's sticky positioning, with no scripted movement.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

StickyTable.jsxsticky-table.csstokens.cssREADME.md

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

テーブル
「比較テーブル」は Pro に含まれます。解除するとプレビューが表示されます。
「データバー付きテーブル」は Pro に含まれます。解除するとプレビューが表示されます。