仕組み
A card with no outer border, a muted 12px header over the only hairline, no column rules and rows as soft pills on hover. Each resizable boundary shows a short grip in the header; the handle around it is a wider hit area. Uses table-layout: fixed with a <colgroup>, so widths come only from <col> and overflowing text is truncated. While dragging, the <col> width is written directly (no React render), the column being resized is tinted with a soft accent band, a 2px accent line sits on the boundary and an ink pill on the line reads out the width; all three are positioned by the script, and state and the callback update once on release. Movement is converted by any ancestor scale, so it still tracks 1:1 when shown scaled down. Each column has a minimum width (default 56px) and an optional maximum. Double-click or Enter resets a column to its default. In free mode the table scrolls sideways inside the card.
向いている場面
- Columns with content of very different lengths where no default width suits everyone: item names, order notes, addresses, message summaries.
- A few columns that should always fill the container: use the default neighbor mode, which never adds a horizontal scrollbar.
- Wide tables with many columns: use free mode so each column can be widened as needed, scrolling sideways.
向かない場面
- Two or three columns with short content: fixed widths are fine and handles only add noise.
- Narrow phone lists: there is little room to adjust, so use cards.
- Changing the order of columns: that is column drag-and-drop, not resizing.
使い方
import { ResizeTable } from './ResizeTable.jsx'
// Every column needs a default width; when they add up to the container's inner width, the values are pixels
const columns = [
{ key: 'project', title: 'Project', width: 120 },
{ key: 'notes', title: 'Latest note', width: 200, minWidth: 80 },
{ key: 'lead', title: 'Lead', width: 100, minWidth: 80 },
]
const rows = [
{ id: 'p1', project: 'Atlas redesign', notes: 'Waiting on final copy from marketing before the handoff', lead: 'Mara' },
{ id: 'p2', project: 'Billing 2.0', notes: 'Invoices ship Tuesday; tax rules still need a review', lead: 'Tomás' },
]
export function ProjectTable() {
return (
<ResizeTable
title="Projects"
subtitle="2 active"
columns={columns}
rows={rows}
rowKey={(r) => r.id}
// To remember the widths, store them here and pass them back as defaultWidths
onWidthsChange={(widths) => console.log(widths)}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
columns | { key, title, width, minWidth?, maxWidth?, align?, resizable?, render? }[] | — | Column definitions: key (also the default field to read), title, width (default, required), minWidth (default 56), maxWidth, align, resizable (whether the line on its right can be dragged) and render. |
rows | object[] | — | Data rows. |
rowKey | (row) => string | number | — | A unique key for each row. |
widths | Record<string, number> | — | Controlled column widths as { column key: width }. When omitted the component remembers them. |
defaultWidths | Record<string, number> | — | Initial widths when uncontrolled, and what “reset” returns to. Columns not listed use their width. |
onWidthsChange | (widths) => void | — | Called once after a release, keyboard adjustment or double-click reset, with the new widths of all columns. |
mode | 'neighbor' | 'free' | 'neighbor' | neighbor: two adjacent columns trade width and the total stays the same. free: only the dragged column changes and the table scrolls sideways when it overflows. |
emptyText | ReactNode | 'No data' | Text shown when there are no rows. |
title | ReactNode | — | Card title shown in the title row. A string title also names the table for screen readers when label is not given. |
subtitle | ReactNode | — | Muted qualifier after the title, such as a period ("This week"). |
icon | ReactNode | — | Icon before the title, drawn in a tinted 32px rounded square. Decorative (aria-hidden). |
actions | ReactNode | — | Controls at the right end of the title row (buttons, a switch, a menu). |
labels | object | — | Replaces built-in text (handle, pixels, width, handleTitle). Shallow-merged over the English defaults; width is the short readout in the pill while dragging. |
label | string | — | Table name for screen readers (the <caption>). Defaults to title when title is a string. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
アクセシビリティ
- Each handle is a focusable role="separator" (vertical) named “Resize column 'Item'”, with aria-valuenow, min, max and an aria-valuetext such as “126 pixels”.
- Keyboard: Tab to a handle, Left and Right change it by 8px (24px with Shift), Enter or Home resets it; it stops at the minimum or maximum.
- The hit area is wider than the visible grip (9px, 24px on touch); on hover and focus the grip turns accent and grows, with a focus ring inside the handle.
- Truncated cells carry a title, so hovering shows the full text. It is a real <table> with <th scope="col"> and the label as <caption>.
- Resizing only changes visual width, not content or reading order. The band, line and width pill are aria-hidden; screen readers get the width through aria-valuetext. With reduced motion the transitions are removed while dragging still follows 1:1.
モーション
The grip turns accent in 160ms and grows with a light spring over 240ms. On drag start the band and line fade in over 160ms and the width pill pops up with a spring over 240ms; during the drag they move 1:1 with no transition (transform, plus the band's width).
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
ResizeTable.jsxresize-table.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





