Cómo funciona
The component has no timers and fetches nothing: you pass the latest rows (polling or WebSocket) and it compares old and new data. Rows are ranked by sortBy; those that changed rank slide with FLIP using Web Animations, resuming midway if new data arrives, and an arrow mark with the number of places stays about 1.6 seconds. Numeric cells are odometers: each digit is a 0–9 strip in a one-line window moved with translateY, keyed by position from the right so a new leading digit simply appears. On first view (IntersectionObserver, 30% visible) every digit rolls up from 0 in 900ms, rows 40ms apart; later updates roll in 560ms. A changed cell gets a soft green or red pill behind the number; a row that newly enters the top (with limit) slides up under an accent wash. Rank 1 is a solid accent chip, 2 and 3 a soft one.
Úsalo para
- Leaderboards that change every few seconds and where rank is the point: top sellers today, live sales, support workload, campaign rankings.
- Ops dashboards and wall screens, where it is easy to see which row is moving and in which direction.
- Making data feel alive: new data gets clear feedback instead of the whole table being swapped silently.
Busca otra opción cuando
- Data that rarely changes (daily reports): use a sortable table, no change feedback is needed.
- Very frequent updates (several per second): merge them into one set every 1 to 2 seconds first, otherwise rows are always in motion.
- Tables where users act (check, edit, click): rows move by themselves and are easy to misclick, use a manually refreshed table.
- Hundreds of rows: show the top N with limit and put the full list in a paged table.
Uso
import { useEffect, useState } from 'react'
import { LiveTable } from './LiveTable.jsx'
const columns = [
{ key: 'name', title: 'Product' },
{ key: 'orders', title: 'Orders', width: 72, numeric: true },
{ key: 'revenue', title: 'Revenue', width: 92, numeric: true, format: (n) => `$${n.toLocaleString('en-US')}` },
]
export function TopProducts() {
const [rows, setRows] = useState([])
// Fetch fresh data every 5 seconds; a WebSocket push works the same, just hand the new array to rows
useEffect(() => {
let alive = true
const load = () =>
fetch('/api/top-products')
.then((res) => res.json())
.then((data) => alive && setRows(data))
.catch(() => {})
load()
const timer = window.setInterval(load, 5000)
return () => {
alive = false
window.clearInterval(timer)
}
}, [])
return (
<LiveTable
title="Top products"
subtitle="Today"
live="Live"
columns={columns}
rows={rows}
rowKey={(r) => r.id}
sortBy="orders"
limit={5}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
columns | { key, title, width?, align?, numeric?, format?, render? }[] | — | Column definitions. key is the column id and the field to read; numeric columns align right, roll their digits and glow on change; format turns the number into text; render customizes a cell (no rolling). The rank column is added automatically. |
rows | object[] | — | The latest data. Each time new data arrives, the component compares it with the previous set to play moves and flashes. |
rowKey | (row) => string | number | — | A unique id per row, used to recognize it as the same row. |
sortBy | string | — | Field to rank by (row[sortBy] must be a number). Without it, the order of rows is kept. |
sortDirection | 'asc' | 'desc' | 'desc' | Ranking direction; by default larger values come first. |
showRank | boolean | true | Shows the rank column (number and change marker). |
rankTitle | string | 'Rank' | Header of the rank column. |
flash | boolean | true | Whether a changed cell shows a soft green (up) or red (down) pill behind its number that fades away. |
roll | boolean | true | Numeric cells roll their digits like an odometer to each new value; on first view every digit rolls up from 0. Off: numbers change in place. |
limit | number | — | Maximum rows to show (the top ones after ranking). Omit to show all. |
rowLabel | (row) => string | value of the first column | The name of a row, used in the screen reader announcement when first place changes. |
emptyText | ReactNode | 'No data' | Text shown when there is no data. |
labels | object | — | Replaces built-in text (leader, movedUp, movedDown). Shallow-merged over the English defaults; entries are functions of a name or a number. |
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). |
live | ReactNode | — | Text of a live chip in the title row, such as "Live"; its dot breathes (static with reduced motion). Omit to hide the chip. |
label | string | — | Name of the table for screen readers (<caption>). Defaults to title when title is a string. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
Accesibilidad
- The table has aria-live="off" since numbers change on every refresh; a hidden polite region announces only when first place changes hands, such as “Coffee beans moved up to rank 1”.
- Rolling digits are aria-hidden; each cell also holds the plain value as visually hidden text, so screen readers read “1,286”, not a column of digits.
- Changes are not shown by red and green alone: the mark has an arrow and a number, and screen readers read “Up 2 places”.
- A real <table> with <th scope="col">; label (or a string title) becomes the <caption>.
- With reduced motion, digits show their value at once, rows jump into place, nothing glows or washes, the live dot is still, and marks stay static until the next update.
- The component never moves by itself: with no new data there is no animation. To pause, stop updating rows.
Movimiento
Digits: translateY, 900ms cubic-bezier(0.22, 1, 0.36, 1) on first view (rows 40ms apart, the rightmost digit leading by 24ms per column), then 560ms per update. Row move: translateY 360ms ease-in-out (Web Animations, resumable). Rank mark: in 160ms, stays about 1.6s, out 240ms. Changed cell: soft pill fades over 900ms. New row: rises 10px and fades in over 360ms under an accent wash that fades over 1.4s. Live dot: a ring grows and fades every 1.8s.
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.
LiveTable.jsxlive-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.





