Data table
A native table that sorts and scrolls.
quiet warm playful
Open the live demo
npx @susegad/cli add data-table
Stands on: Core, Core: arrivals, Core: components, Core: rupees and the Indian clock, Engine, Tokens
The prompt
the prompt
A ledger for a business: what came in, what is owed, month by month. The register for auditors and finance first; warm and playful after.
A native <table> that sorts and scrolls. Money is written the Indian way, lakh and crore (₹1,25,000, never 125,000), on tabular figures so columns of digits line up. At phone width the table scrolls inside itself and the page never scrolls sideways.
<link rel="stylesheet" href="susegad/components/data-table/data-table.css">
<script type="module" src="susegad/components/data-table/data-table.js"></script>
<sg-data-table>
<table>
<caption>Collections, April to September 2026</caption>
<thead><tr>
<th scope="col" data-sort="month">Month</th>
<th scope="col" data-sort="collected" data-money-col>Collected</th>
<th scope="col">Note</th>
</tr></thead>
<tbody>
<tr><td>April</td><td data-money="245000">₹2,45,000</td><td>Season opens</td></tr>
<tr><td>June</td><td data-money="125000">₹1,25,000</td><td>Monsoon, slow</td></tr>
</tbody>
<tfoot><tr><td>Total</td><td data-money="370000">₹3,70,000</td><td colspan="2"></td></tr></tfoot>
</table>
</sg-data-table>
The prompt
Make a data-table web component around a native <table>: a <caption>, sortable columns marked with data-sort on the th, and money cells marked td[data-money] whose value is the raw number. With JavaScript, wrap the table in a scrollable region so at phone width the table scrolls sideways inside itself and the page never does; turn each sortable header into a real <button> so the sort is the platform's focus and Enter and Space work; write aria-sort on the sorted th (ascending, then descending, then gone on a third click) and reorder only the tbody rows with a stable sort, numbers as numbers and text as text, equal rows keeping the order they were written in. Rewrite every money cell from a pure grouping function that writes 125000 as "1,25,000" and 12500000 as "1,25,00,000" (lakh and crore, the 2-2-3 rhythm), never Western grouping and never a locale library, which would group Western-style on most machines. Set tabular figures on the table and align money columns to the right. Give it three registers. Quiet: hairline rules, generous space, zero ornament; the money column carries the weight. Warm: a hand-drawn rule under the header row, drawn in code with a small seeded wobble, and a hand face on the caption. Playful: a tinted zebra and a coloured sort arrow, still drawn. Under reduced motion nothing moves. Without JavaScript the table reads fully in the order it was written.
Words to code
| When you say | Technique | What happens |
|---|---|---|
| lakh and crore, ₹1,25,000 | pure core | groupIndian(n) groups the integer part 2-2-3 (the last three, then pairs): 125000 → "1,25,000", 12500000 → "1,25,00,000". No Intl, no locale library: those group Western-style on most machines. rupee(n) puts the ₹ in front; a number it cannot read says nothing. |
| what a cell sorts as | cellValue(td) | data-value, then data-money, then the text. Money-like text ("₹1,25,000") becomes a number; anything else stays text. |
| the platform's focus, Enter and Space | native <button> | The th's content is moved into a real button, so the browser does the keyboard. |
| aria-sort | ariaSort(dir) | ascending on the first click, descending on the second, the attribute removed on the third. Only one th carries it. |
| ascending, descending, gone | nextSort(current, id) | The cycle, pure: a column clicked once sorts ascending, twice descending, three times back to the written order; another column starts again at ascending. |
| stable sort | sortRows(rows, dir, value) | Numbers as numbers, text after numbers, text by localeCompare in Kannada collation; equal values keep their order. Only tbody.rows move; the footer never does. |
| scrolls inside itself | .sg-table-wrap | The element wraps the table in a role="region" div with overflow-x: auto and overscroll-behavior-x: contain, named by the caption. The page never scrolls sideways at 390 px; the wrapper does. |
| the money on the page is the money in the code | rewrite | Every td[data-money] is rewritten from groupIndian on connect, so the rendered text and the sort value can never disagree. |
| a hand-drawn rule with a seeded wobble | warm skin | An SVG path under the header row, points every 90 px nudged by rng('table-rule:seed'), redrawn only on resize. |
| tabular figures | CSS | font-variant-numeric: tabular-nums on the table, so digits align down a column; money columns sit on the right. |
Accessibility
- A real
<caption>names the table and names the scroll region. - Sorting is a native button per column: Tab reaches it, Enter and Space sort it, and
aria-sorton the th tells the screen reader the direction. The arrow is a shape as well as a colour change. - The scroll region is focusable, so a keyboard user can scroll a wide table on a phone.
- Without JavaScript the table reads fully, in the order it was written, with the quiet look.
<sg-data-table> is a native <table> that sorts and scrolls. It enhances a <table>.
Markup
| Element | What it is |
|---|---|
<caption> | required. It names the table, and names the scroll region the element wraps it in. |
thead th[data-sort] | a sortable column. data-sort="month" is the column id used in the sg-table-sort event. Without data-sort a column never sorts. |
th[data-money-col] | right-aligns a money or number column's heading with its cells. |
td[data-money="125000"] | a money cell. The attribute holds the raw number; the element rewrites the text as ₹1,25,000 from the pure core. Cells with element children are left alone. |
td[data-value="38"] | the number a cell sorts by, when the text alone is not it. |
tfoot | never sorted. It stays where you wrote it. |
Everything else in the table is yours: th scope="col", links in cells, text.
Attributes and events
register | quiet, warm, playful; inherited like every component's. |
sg-table-sort | event, { id, dir }, on every change. dir is asc, desc or null (the written order restored). |
table.sort('collected') | sorts programmatically; returns false for an unknown column. |
Sorting
One click sorts ascending, a second descending, a third returns the rows to the order they were written in. The sorted th carries aria-sort="ascending" or "descending", and no other th carries it. The sort is stable: equal values keep their written order. Numbers sort as numbers (data attribute or money-like text), text as text after numbers. The footer never moves.
Money, the Indian way
groupIndian() in data-table.core.js groups 2-2-3: the last three digits, then pairs. 125000 → "1,25,000", 1250000 → "12,50,000", 12500000 → "1,25,00,000". It is a pure function with no Intl and no locale library, because a locale library groups Western-style (125,000) on most machines. rupee(n) adds the ₹. A value the code cannot read renders as nothing, never "NaN".
Registers
| Look | |
|---|---|
| quiet | hairline rules, generous space, zero ornament. The money column carries the weight. |
| warm | a hand-drawn rule under the header (a seeded SVG wobble, drawn in code), the caption in the hand face. |
| playful | a tinted zebra, the sorted column in the accent colour; the money stays in ink, because red figures in a ledger read as owed. |
Reduced motion keeps every register still; there is nothing that needs to move.
Accessibility
How this component meets WCAG 2.2 level A and AA, criterion by criterion, how each was checked, and what your page still owns.
- Meets 15
- 1
| Criterion | Status | How it was checked |
|---|---|---|
| 1.1.1 Non-text content | Meets | The sort arrow is a CSS shape on the button (packages/components/data-table/data-table.css, .sg-table-sort::after), and the direction is said by aria-sort, so nothing is lost without the picture. The table holds the author's own cells and images. |
| 1.3.1 Info and relationships | Meets | A native <table> with a <caption>, <th> headers and aria-sort on the sorted one. packages/components/data-table/data-table.check.mjs: "Enter sorts ascending and writes aria-sort", "the scroll region is named by the caption and focusable", and "without JavaScript: no wrapper, no buttons, the written order". Whether <th scope> is right for a complex table is the author's markup. |
| 1.4.1 Use of color | Meets | The sorted column shows an arrow that turns for the direction and aria-sort says it; the sorted header's darker colour only repeats that (packages/components/data-table/data-table.css, th[aria-sort] rules). |
| 1.4.3 Contrast (minimum) | Meets | packages/components/data-table/data-table.check.mjs: "axe, light, all three registers: 0 violations", the same in dark, and for each register on its own in dark, color-contrast included. Text roles are tested at 4.5:1 or more on every surface in packages/tokens/tokens.test.js. |
| 1.4.4 Resize text | Meets | Manual test, 2026-10-06: the demo with the root font size at 200 percent in three registers (1280 px wide), the page does not scroll sideways and no text is clipped (throwaway Playwright script). |
| 1.4.10 Reflow | Meets | A data table is two-dimensional content, which the criterion allows to scroll in both directions, and here only the table's own region scrolls. data-table.check.mjs: "at 390 the page does not scroll sideways" and "the wrapper does scroll sideways, so nothing is cut off". Manual test at 320 CSS px, three registers, 2026-10-06: the page does not scroll sideways. The region is focusable so a keyboard user can scroll it. |
| 1.4.11 Non-text contrast | Meets | The sort arrow is drawn in currentColor or --sg-accent-text (text roles, 4.5:1 or more) and the focus ring is --sg-focus; the row rules are decoration. packages/tokens/tokens.test.js tests the focus ring and --sg-rule-strong at 3:1 or more on every surface for every palette and theme. |
| 1.4.12 Text spacing | Meets | Manual test, 2026-10-06: the WCAG text-spacing values (line height 1.5, letter 0.12em, word 0.16em, paragraph spacing 2em) injected on the demo in three registers, no clipped or overlapping text (throwaway Playwright script). No check in the repository asserts it yet. |
| 2.1.1 Keyboard | Meets | data-table.check.mjs: in each register Tab reaches the first sort button with a focus ring, Enter sorts ascending then descending, Space clears the sort, and a second column starts again at ascending. The scroll region has tabindex="0", checked in "the scroll region is named by the caption and focusable". |
| 2.1.2 No keyboard trap | Meets | Manual test: packages/components/data-table/data-table.js binds no key handler of its own; the sort controls are native buttons and the region takes Tab and Shift+Tab as any focusable element does. |
| 2.4.3 Focus order | Meets | Sort buttons come in header order and the scroll region comes before them in the page; data-table.check.mjs Tabs to the first sort button in each register. |
| 2.4.7 Focus visible | Meets | data-table.check.mjs: "Tab reaches the first sort button (N presses) with a focus ring" in each register; the rules are .sg-table-sort:focus-visible and .sg-table-wrap:focus-visible in packages/components/data-table/data-table.css, and tools/touch.check.mjs requires a focus style as well. |
| 2.4.11 Focus not obscured (minimum) | Manual test: packages/components/data-table/data-table.css has no position: fixed or sticky, so the component covers nothing. A sticky header or banner the page adds can cover a focused sort button or the region. | |
| 2.5.3 Label in name | Meets | The sort button's accessible name is its visible text first, then ", now smallest first" or ", now largest first" once sorted (packages/components/data-table/data-table.js, aria-label built from btn.textContent). |
| 2.5.8 Target size (minimum) | Meets | A sort button is at least 2rem (32 px at 16 px type) tall (min-height in packages/components/data-table/data-table.css) and as wide as its header text. Manual test, 2026-10-06: no focusable control under 24 by 24 CSS px at 1280 px or at 390 px with touch, in all three registers (throwaway Playwright script). |
| 4.1.2 Name, role, value | Meets | Native <button> in the <th>, aria-sort for the state, and the region named by the caption. data-table.check.mjs: axe with 0 violations in light and dark, in each register, and with JavaScript off. |
The page author owns:
- The caption, the column headings and the cell text, in the language of the page, and
langon cells in another language (3.1.2). scopeandheaderson a table with more than one header row or column.- Which columns can be sorted and the
data-valueof cells whose text is not what should sort. - Sticky headers or banners on the page that could cover a focused sort button or the scroll region (2.4.11).
- The colours the page sets behind the table, if it overrides the tokens.
How it behaves
- The table is a native
<table>with a<caption>; the scroll region isrole="region"named by that caption, focusable so a keyboard user can scroll it. - Sorting is a native
<button>in the th: Tab, Enter, Space;aria-sorttells the direction; the arrow is a shape, not only a colour. - The table sets
font-variant-numeric: tabular-nums, so digits align. - Without JavaScript the table reads fully, in the written order, no wrapper, no buttons.
Sizing it
At 390 px the wrapper scrolls and document.documentElement.scrollWidth stays 390. Give the table enough columns that it has something to scroll, and mark the number columns with data-money-col so they keep their alignment while it does.
Arriving and pressing
- Rows arrive once, on the tala: each one lands on its own beat of the sixteen-beat teental cycle (130 ms a beat, nothing on the three silent beats). Only the first five wait for a beat; the rest take the fifth's beat, so the list cascades top to bottom and the longest wait is 520 ms however long it is, and the cards are in the accessibility tree from the start (the fade is visual only). The stagger is CSS, with
sibling-index(); where a browser lacks it,core/arrive.jshands each one the same delay. Warm eases in; playful settles on the spring. Quiet gets no stagger, and nothing moves under reduced motion. - Only arrivals play. A new one that is added later starts again from the first beat, and a sort or a move the reader asked for never replays anything (
data-seenmarks what has arrived). - A press answers within a frame: no transition on the way in, and it eases back in under 150 ms. Quiet settles 1 px, warm shades the colour toward the ink, playful gives a little (scale 0.97) and lets go on the spring. Reduced motion keeps the colour and drops the movement; a held key does not move anything.