Ir al contenido
Español
Tabla con columnas ajustables viene con Pro. La vista previa aparece al desbloquearlo.

Cómo funciona

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.

Úsalo para

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

Busca otra opción cuando

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

Uso

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)}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
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.
rowsobject[]—Data rows.
rowKey(row) => string | number—A unique key for each row.
widthsRecord<string, number>—Controlled column widths as { column key: width }. When omitted the component remembers them.
defaultWidthsRecord<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.
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).
labelsobject—Replaces built-in text (handle, pixels, width, handleTitle). Shallow-merged over the English defaults; width is the short readout in the pill while dragging.
labelstring—Table name for screen readers (the <caption>). Defaults to title when title is a string.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root <div>.

Accesibilidad

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

Movimiento

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

Añádelo a tu proyecto

Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.

ResizeTable.jsxresize-table.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Tablas
Tabla comparativa viene con Pro. La vista previa aparece al desbloquearlo.
Tabla con barras de datos viene con Pro. La vista previa aparece al desbloquearlo.