本文へ移動
日本語

仕組み

The table draws its own card (surface, large radius, card shadow, no border) with an optional title row. The header is 12px muted text over the card's only hairline; rows are 48px with no lines between them, and the hovered row becomes a soft filled pill. A sortable header cell is one button, so a click anywhere in it works; the sorted column's values turn heavier. Each column can set its first click direction and a sort value (sort by tier instead of by label). Sorting is stable, empty values always go last, and strings compare with numeric-aware collation. Rows move with FLIP: positions are measured with offsetTop and played from the old offset back to 0 with Web Animations, only on transform, continuing from mid-flight if clicked again. Above 200 rows the animation is skipped. Set --ds-table-row on an ancestor to change the row height.

向いている場面

  • A list that fits on one page where people compare by a column: products by price, orders by amount or date, agents by volume.
  • When it matters to follow a row to its new place: sliding is easier to track than an instant reshuffle.
  • Server-side sorting: use controlled sort and onSortChange, fetch new data, then pass the rows back in.

向かない場面

  • Data with a natural order where sorting is meaningless (audit logs by time, step lists): list it in that order.
  • Sorting by several columns at once: this sorts by one column; use filters or a dedicated sort panel.
  • Hundreds of rows or more: combine with a paged table and sort per page, or sort on the server.

使い方

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

const columns = [
  { key: 'name', title: 'Member' },
  // Counts: the first click sorts descending, so the busiest come first
  { key: 'tasks', title: 'Tasks', width: 64, align: 'right', firstDirection: 'desc' },
  { key: 'onTime', title: 'On time', width: 80, align: 'right', firstDirection: 'desc', render: (r) => `${r.onTime}%` },
]

const rows = [
  { id: 'm1', name: 'Mara Olsen', tasks: 38, onTime: 96 },
  { id: 'm2', name: 'Tomás Ortega', tasks: 27, onTime: 88 },
  { id: 'm3', name: 'Aiko Tanaka', tasks: 45, onTime: 91 },
]

export function TeamTable() {
  return (
    <SortTable
      title="Team"
      subtitle="This sprint"
      columns={columns}
      rows={rows}
      rowKey={(r) => r.id}
      defaultSort={{ key: 'tasks', direction: 'desc' }}
    />
  )
}

プロパティ

プロパティ型既定値説明
columnsColumn[]—Column definitions: key (also the default field to read), title, width, align, sortable (default true), firstDirection ('asc' by default), sortValue (value to sort by), render.
rowsobject[]—Data rows. Shown in this order when unsorted.
rowKey(row) => string | number—Unique key of each row. The move animation uses it to recognize the same row.
sort{ key, direction: 'asc' | 'desc' } | null—Controlled sort. null means unsorted. If omitted, the component keeps its own.
defaultSort{ key, direction } | nullnullInitial sort when uncontrolled.
onSortChange(sort | null) => void—Called when the sort changes. null means back to unsorted.
animatebooleantrueWhether rows slide when they change position. Skipped automatically above 200 rows.
emptyTextReactNode'No data'Text shown when there are no rows.
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>).
labelsobject—Replaces the announcements after sorting (sortedAscending, sortedDescending, unsorted). The first two are functions receiving the column title. Shallow-merged over the English defaults.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root <div>.

アクセシビリティ

  • Sortable headers are <button>s: Tab moves column by column, Enter or Space toggles, and the focus ring is drawn inside the cell.
  • The sorted column's <th> has aria-sort (ascending or descending) and each change is announced through aria-live, such as "Sorted by "Tasks", descending".
  • Direction is not only colour: the arrow points up or down.
  • It is a real <table> with <th scope="col">, so screen readers can read by row and column; label (or a string title) is the <caption>.
  • With reduced motion rows jump straight to place and the arrow does not spin; on touch the header grows to 44px.

モーション

Row move: translateY from the old to the new position over 360ms ease-in-out (Web Animations, can continue mid-flight). Arrow: pops in from scale 0.4 and turns 180° between directions over 240ms with a slight overshoot. Hover pill and header colour: 160ms standard.

プロジェクトに追加

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

SortTable.jsxsort-table.csstokens.cssREADME.md

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

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