Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-table

Progressive enhancement wrapper for HTML tables. Adds sorting, filtering, pagination, row expansion, and selection.

Overview

The <data-table> component wraps a standard HTML table to provide interactive features like column sorting, text filtering, pagination, expandable rows, row selection with bulk actions, responsive card layouts, and sticky headers/columns. All features are opt-in via data attributes and the table remains fully functional without JavaScript.

Basic Usage

Wrap any HTML table with <data-table> and add data attributes to enable features. The table structure remains semantic and accessible.

Sorting

Add data-sort to any <th> element to make that column sortable. The component wraps the header text in a native <button> (the APG sortable table pattern), so headers are reachable with Tab, show a focus ring, and sort with Enter or Space. Activating a header toggles between ascending and descending; activating a different column sorts it ascending (or descending if the rows are already in ascending order, so the change is always visible). The arrow indicator and aria-sort on the <th> always agree.

Sort Types

Value Description
string Alphabetical sorting (case-insensitive)
number Numeric sorting (handles decimals and negatives)
date Date sorting (parses common date formats)

Custom Sort Values

Use data-sort-value on cells when the display value differs from the sort value. This is required for formatted numbers: data-sort="number" parses the cell with parseFloat, so text like $1,234 or 1,234 would not sort numerically without a raw value.

Filtering

Add data-filterable to the table to enable text filtering. A search input is automatically generated above the table. Rows that don't match the filter text are hidden, and the result count is announced to screen readers. When nothing matches, a visible “No matching rows found” message appears below the table; override its text (and the announcement) with data-label-no-results on <data-table>. The other generated strings are covered in Localizing the labels.

Pagination

Add data-paginate="n" to the table to show only n rows per page. Pagination controls are automatically generated below the table.

Row Expansion

Create expandable rows by adding data-expandable to a row and following it with a data-expand-content row. Put your own toggle button in the row with data-action="toggle-expand" (give it an accessible name); the component wires it up, keeps aria-expanded in sync, and keeps the content row attached to its parent when sorting, filtering, or paging.

Row Selection

Add checkboxes to rows for selection, with a "select all" checkbox in the header and a bulk actions bar that appears when rows in that table are selected. The selection count is announced to screen readers. Selected rows get data-state-selected, which uses the same selected tint as a native tr[data-selected], plus a non-colour cue.

Responsive Card Mode

Add data-responsive="card" to transform the table into a card layout on narrow screens. Each cell's label is derived from its column header automatically, and the table roles are restored so screen readers still announce a table. An authored data-label on a cell overrides the derived one.

Horizontal Scroll

For wide tables that should keep their columns, wrap the table in a focusable scroll container labelled by its caption. Add data-table-scroll="block" to also cap the height.

Sticky Headers and Columns

Use data-sticky to keep headers or columns visible while scrolling.

Sticky Options

Value Description
header Keeps the table header row fixed at the top
column Keeps the first column fixed on horizontal scroll
both Both header and first column are sticky

Multiple Sticky Columns

Use data-sticky-column="n" to make the first n columns sticky.

Events

The component dispatches custom events for each interactive feature.

Event Detail Description
data-table:sort { column: number, direction: "asc"|"desc", columnName: string } Fired when a column is sorted.
data-table:filter { query: string, count: number } Fired when the filter is applied (input, setFilter(), refresh(), or a .rows assignment).
data-table:page { page: number } Fired when the page changes.
data-table:expand { row: HTMLTableRowElement, expanded: boolean } Fired when a row is expanded or collapsed.
data-table:selection { count: number, rows: HTMLTableRowElement[] } Fired when row selection changes.

Public API

The component exposes methods for programmatic control.

Method Parameters Returns Description
goToPage(n) n: number void Navigate to a specific page (1-indexed, clamped to the valid range). No-op without data-paginate.
setFilter(query) query: string void Programmatically set the filter text.
refresh() - void Re-scan rows after adding or removing <tr> elements, then re-apply rollups, heatmaps, filtering, sorting, and pagination (the current page is kept when still valid).
getSelectedRows() - HTMLTableRowElement[] Get array of currently selected rows.

Property API (JS-first)

For reactive frameworks pushing live data, data-table exposes .rows and .columns alongside the markup-driven path. Existing <tr> nodes whose id persists across .rows assignments are preserved — selection, expansion, inline-edit state, focus, and CSS animations survive the diff.

PropertyTypeDescription
.rows{ id, [colKey]: value, ... }[]Setter runs a keyed diff against current <tr> children. id is the diff key; other fields populate cells per the column spec.
.columns{ key, label, sort? }[]Read-only: column spec derived from <thead> on upgrade. Each <th>'s data-key attribute becomes the column key (falls back to lowercased text).
.renderRow(row, columns) => ElementOptional custom row renderer. Default builds one <td> per column populated from row[col.key].
EventDetailWhen
data-table:upgraded—Once after first connect; safe signal for the first .rows assignment.
data-table:rows-changed{ rows, added, moved, removed, source: 'api' }Fires after every .rows assignment with diff stats.

See the Data API concepts guide for the full dual-mode contract.

Decision Matrix recipe

Set data-weight="N" on each criterion column header, data-rollup="weighted-sum" on a Score column, and data-heatmap for visual scanning. The component computes the score per row from the weighted criteria, sets data-sort-value so the Score column remains sortable, and tints cells low/mid/high based on relative value within the column.

This recipe replaces a dedicated decision-matrix component — there's no new element needed; the rule is "behaviour on existing element → data-* attribute".

Rollup kinds

  • sum — sum of all sibling numeric cells.
  • weighted-sum — each sibling cell × the data-weight on its column header.
  • product — multiply all sibling numeric cells (e.g. likelihood × impact for risk severity).
  • avg — average of all sibling numeric cells.
  • max — largest sibling numeric value.

Heatmap modes

  • high-good (also auto) — large values tinted green, small values red.
  • low-good — inverted; small values green (e.g. risk severity, defect count).

Tints use the --color-error-subtle, --color-warning-subtle, and --color-success-subtle tokens, which switch with the colour scheme; cell text stays --color-text so contrast holds in light and dark mode. Colour is never the only cue — the number stays in the cell.

Localizing the labels

The component generates some text of its own: the filter input's name and placeholder, the pagination buttons, and the messages it announces to screen readers. Each string has an English default and a data-label-* attribute on <data-table> that replaces it. Column headers, captions and cell content are your markup, so translate them there.

AttributeDefault textPlaceholdersUsed for
data-label-filterFilter table rowsNoneAccessible name of the filter input
data-label-placeholderFilter table...NonePlaceholder of the filter input
data-label-no-resultsNo matching rows foundNoneEmpty-state message and its announcement
data-label-paginationTable paginationNoneAccessible name of the pagination <nav>
data-label-prevPreviousNonePrevious-page button
data-label-nextNextNoneNext-page button
data-label-sortedSorted by {column}, {direction}{column}, {direction}Announcement after a sort
data-label-ascendingascendingNoneWord used for {direction}
data-label-descendingdescendingNoneWord used for {direction}
data-label-filtered-one{count} of {total} row shown{count}, {total}Announcement after filtering, when the total is one row
data-label-filtered-other{count} of {total} rows shown{count}, {total}Announcement after filtering
data-label-all-oneAll {total} row shown{total}Announcement when the filter is cleared, when the total is one row
data-label-all-otherAll {total} rows shown{total}Announcement when the filter is cleared
data-label-selected-one{count} row selected{count}Announcement when one row is selected
data-label-selected-other{count} rows selected{count}Announcement after any other selection change

Things to know:

  • Placeholders. The component fills in the tokens in braces. Keep them in the translation and move them where the grammar needs them. {column} is the header text of the sorted column. {count} is the number of matching or selected rows and {total} is the number of rows in the table.
  • Plurals. The attributes ending in -one and -other are plural categories from Intl.PluralRules. A language with more categories can add -two, -few, -many or -zero. A category you leave out uses your -other text. The filter messages pick the category from {total}, and the selection messages pick it from {count}.
  • Numbers. Counts and page numbers are formatted with Intl.NumberFormat in the language of the nearest lang attribute, so set lang on the component or the page.
  • Changes. The messages, the empty-state text and the pagination buttons read their attribute each time they are used, so a changed attribute applies from the next announcement or render. The filter input's name and placeholder and the pagination name are read once, when the component starts.

The Translation guide covers the same pattern in other components.

Attributes Reference

Host Attributes

AttributeValuesDefaultDescription
data-label-no-resultsstringNo matching rows foundText for the visible empty state and the live-region announcement when the filter matches no rows.
data-label-filterstringFilter table rowsAccessible name of the filter input.
data-label-placeholderstringFilter table...Placeholder of the filter input.
data-label-paginationstringTable paginationAccessible name of the pagination nav.
data-label-prevstringPreviousText of the previous-page button.
data-label-nextstringNextText of the next-page button.
data-label-sortedstringSorted by {column}, {direction}Announcement after a sort. {column} is replaced by the header text and {direction} by the data-label-ascending or data-label-descending text.
data-label-ascendingstringascendingDirection word used for {direction} in the sort announcement.
data-label-descendingstringdescendingDirection word used for {direction} in the sort announcement.
data-label-filtered-onestring{count} of {total} row shownAnnouncement after filtering when Intl.PluralRules selects "one" for the total. {count} is replaced by the number of matching rows and {total} by the number of rows. Any category works: -two, -few, -many, -zero.
data-label-filtered-otherstring{count} of {total} rows shownAnnouncement after filtering for the "other" category and the fallback for unset categories. {count} and {total} are replaced.
data-label-all-onestringAll {total} row shownAnnouncement when the filter is cleared ("one" category). {total} is replaced by the number of rows; {count} holds the same number.
data-label-all-otherstringAll {total} rows shownAnnouncement when the filter is cleared ("other" category and fallback). {total} is replaced by the number of rows; {count} holds the same number.
data-label-selected-onestring{count} row selectedAnnouncement after a selection change when Intl.PluralRules selects "one" for the count. {count} is replaced by the number of selected rows. Any category works: -two, -few, -many, -zero.
data-label-selected-otherstring{count} rows selectedAnnouncement after a selection change for the "other" category and the fallback for unset categories. {count} is replaced.

Required Structure

ElementRequiredDescription
<table>yesStandard HTML table — the component enhances it in place

Child Attributes

AttributeOnValuesDescription
data-filterabletablebooleanEnable text search filtering (adds a search input above the table)
data-paginatetablenumberEnable pagination with N rows per page
data-sortth"string", "number", "date"Enable sorting on this column with the specified comparator
data-weightthnumberColumn weight; multiplier applied to cell values for weighted-sum rollups
data-rollupth"sum", "weighted-sum", "product", "avg", "max"Compute this column's cells from sibling cells in the same row
data-heatmapth"auto", "low-good", "high-good"Tint this column's cells by relative value bucket (low/mid/high)
data-sort-valuetdstringCustom sort value overriding cell text. Use for formatted values, e.g. <td data-sort-value="1234">$1,234</td> — data-sort="number" parses the cell with parseFloat, so "$1,234" would sort as 0
data-filter-valuetdstringCustom filter value overriding cell text
data-expandabletrbooleanMark row as expandable (followed by a data-expand-content row). Supply the toggle yourself: <button data-action="toggle-expand"> in a cell; the component manages aria-expanded
data-expand-contenttrbooleanHidden row revealed when its preceding expandable row is toggled
data-selectabletrbooleanMark row as selectable via an author-supplied <input type="checkbox" data-action="select-row">; a thead checkbox with data-action="select-all" toggles all

CSS-Only Attributes

These attributes are handled by CSS styling, not JavaScript logic.

AttributeOnValueDescription
data-responsive<table>cardTransform to card layout on narrow screens.
data-sticky<table>header | column | bothMake header and/or first column sticky.
data-sticky-column<table>numberNumber of columns to make sticky (default: 1).
data-label<td>stringLabel shown in responsive card mode.
data-filter-value<td>stringCustom value for filtering instead of cell text.

Accessibility

Keyboard Navigation

Everything interactive is a native control, so the table follows the normal tab order; there is no custom row navigation.

Key Action
Tab / Shift+Tab Move between the filter input, sort header buttons, checkboxes, expand toggles, and pagination buttons
Enter / Space Sort by the focused column, toggle a checkbox, expand/collapse a row, or go to a page

After a pagination button is activated, focus moves to its replacement: Previous/Next keep focus (unless they become disabled), and a page number hands focus to the new current-page button.

ARIA and Semantics

  • aria-sort on sortable <th> headers reflects the current direction; the header's <button> is the control. The visual arrow is CSS content with empty alt text, so it is not read twice.
  • aria-expanded on data-action="toggle-expand" buttons reflects row expansion.
  • Row selection state is carried by the row checkboxes themselves.
  • aria-current="page" marks the current page; the pagination is a <nav aria-label="Table pagination">.

Screen Reader Announcements

A visually hidden role="status" region (aria-live="polite") inside the component announces:

  • Sort changes, e.g. “Sorted by Price, descending”
  • Filter results, e.g. “3 of 12 rows shown”, or the data-label-no-results text when nothing matches
  • Selection count changes, e.g. “2 rows selected”

Each message can be translated. See Localizing the labels.

Reduced Motion

When prefers-reduced-motion: reduce is set, transitions on the filter input and pagination buttons are disabled.

↑