Theming
Style the grid with --lq-* CSS custom properties, switch between light, dark and auto themes, pick a density, and ship styles as a CSS file when you need to.
The grid is styled entirely through CSS custom properties prefixed with --lq-. There is no Shadow DOM, every element has a class prefixed with lq-, and the built-in selectors are kept to one or two classes, so your own CSS can reach and override anything.
Loading demo…
View source
Sample data and small UI helpers are imported from the docs' demos/_shared.ts and demos/_scheduler-data.ts files.
import { createGrid, type ColumnDef, type GridOptions } from '@birchui/core';
import {
docsTheme,
field,
formatCurrency,
layout,
makeOrders,
onDocsThemeChange,
revenueOf,
type Order,
} from './_shared';
export const meta = { title: 'Themes, density and custom properties', height: 460 };
const columns: ColumnDef<Order>[] = [
{ field: 'id', header: 'Order', width: 110 },
{ field: 'customer', header: 'Customer', flex: 1, minWidth: 170 },
{ field: 'status', header: 'Status', width: 120 },
{ field: 'quantity', header: 'Qty', type: 'number', width: 80 },
{ id: 'revenue', header: 'Revenue', type: 'number', width: 120, valueGetter: revenueOf, format: formatCurrency },
];
const ACCENTS: Record<string, string> = { Default: '', Teal: '#0f766e', Plum: '#7e22ce', Amber: '#b45309' };
function select(values: string[], initial: string): HTMLSelectElement {
const el = document.createElement('select');
for (const value of values) el.add(new Option(value, value, value === initial, value === initial));
return el;
}
export default function mount(el: HTMLElement): () => void {
const { toolbar, body } = layout(el);
const theme = select(['Follow docs', 'light', 'dark'], 'Follow docs');
const density = select(['compact', 'normal', 'comfortable'], 'normal');
const accent = select(Object.keys(ACCENTS), 'Default');
const stripes = document.createElement('input');
stripes.type = 'checkbox';
toolbar.append(field('Theme', theme), field('Density', density), field('Accent', accent), field('Stripes', stripes));
const grid = createGrid<Order>(body, {
columns,
rows: makeOrders(300, 11),
getRowId: (order) => order.id,
checkboxSelection: true,
rowSelection: 'multiple',
theme: docsTheme(),
ariaLabel: 'Theme playground',
});
const resolvedTheme = () => (theme.value === 'Follow docs' ? docsTheme() : (theme.value as 'light' | 'dark'));
const apply = () => {
grid.update({
theme: resolvedTheme(),
density: density.value as GridOptions['density'],
stripes: stripes.checked,
});
const color = ACCENTS[accent.value];
for (const name of ['--lq-accent', '--lq-focus-ring']) {
if (color) grid.element.style.setProperty(name, color);
else grid.element.style.removeProperty(name);
}
if (color) grid.element.style.setProperty('--lq-selection-bg', `color-mix(in srgb, ${color} 14%, transparent)`);
else grid.element.style.removeProperty('--lq-selection-bg');
};
for (const control of [theme, density, accent, stripes]) control.addEventListener('change', apply);
const stopTheme = onDocsThemeChange(() => {
if (theme.value === 'Follow docs') apply();
});
return () => {
for (const control of [theme, density, accent, stripes]) control.removeEventListener('change', apply);
stopTheme();
grid.destroy();
};
}Built-in themes#
import { createGrid } from '@birchui/core';
const grid = createGrid(document.querySelector('#orders')!, {
columns,
rows: orders,
theme: 'auto',
});
document.querySelector<HTMLSelectElement>('#theme')!.addEventListener('change', (event) => {
grid.update({ theme: (event.target as HTMLSelectElement).value });
});The theme option becomes a class on the grid root, lq-theme-<theme>:
theme |
Root class | Result |
|---|---|---|
| Not set (default) | None | Inherits from the nearest ancestor with lq-theme-dark or lq-theme-auto; light when there is none |
'light' |
lq-theme-light |
The light palette, even inside a dark ancestor |
'dark' |
lq-theme-dark |
The dark palette, with color-scheme: dark so native inputs and scrollbars match |
'auto' |
lq-theme-auto |
Light, switching to dark when the user's system prefers a dark color scheme |
'harbor' |
lq-theme-harbor |
Your own theme, see A custom theme |
The root also always has lq-grid, a density class (lq-density-normal by default) and lq-stripes when stripes: true.
Inheriting the theme from your page#
Leave theme unset and put the theme class on any ancestor, such as <body> or your app shell. Every grid inside picks it up, and switching the class switches every grid at once without calling update():
<body class="lq-theme-dark">
<div id="orders" style="height: 480px"></div>
</body>createGrid(document.querySelector<HTMLElement>('#orders')!, { columns, rows });
document.body.classList.replace('lq-theme-dark', 'lq-theme-auto');lq-theme-auto on an ancestor follows the operating system's color scheme. A grid with an explicit theme keeps its own palette regardless of its ancestors.
Following your app's dark mode#
If your app already has its own dark mode class and you'd rather not add lq-theme-dark to the page, pass the resolved value instead:
const appTheme = () => (document.documentElement.classList.contains('dark') ? 'dark' : 'light');
const grid = createGrid(container, { columns, rows, theme: appTheme() });
new MutationObserver(() => grid.update({ theme: appTheme() })).observe(document.documentElement, {
attributes: true,
attributeFilter: ['class'],
});CSS custom properties#
The light values are defined on .lq-grid and .lq-theme-light; .lq-theme-dark and .lq-theme-auto (in dark mode) redefine the color properties.
| Property | Light | Dark | Used for |
|---|---|---|---|
--lq-font-family |
system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif |
same | All grid text |
--lq-font-size |
13px |
same | Cell and header text |
--lq-cell-padding |
10px |
same | Horizontal padding in cells and headers |
--lq-fg |
#1f2328 |
#e6edf3 |
Cell text |
--lq-bg |
#ffffff |
#0d1117 |
Grid and cell background |
--lq-muted-fg |
#59636e |
#9198a1 |
Secondary text, empty message, icons |
--lq-border |
#d1d9e0 |
#30363d |
Outer border, header and pinned-column edges |
--lq-border-subtle |
#e6eaef |
#21262d |
Row separators, filter row, menu separators |
--lq-header-bg |
#f6f8fa |
#161b22 |
Header, filter row, top and bottom slots, pager |
--lq-header-fg |
#1f2328 |
#e6edf3 |
Header text |
--lq-row-alt |
#f9fafb |
#11161d |
Odd rows when stripes: true |
--lq-row-hover |
#eef3f9 |
#1a2230 |
Hovered rows and menu items |
--lq-accent |
#1f6feb |
#4493f8 |
Sort indicators, active filters, resize and drop indicators |
--lq-accent-fg |
#ffffff |
#0d1117 |
Text on accent backgrounds |
--lq-selection-bg |
rgba(31, 111, 235, 0.12) |
rgba(68, 147, 248, 0.18) |
Selected rows |
--lq-range-bg |
rgba(31, 111, 235, 0.14) |
rgba(68, 147, 248, 0.2) |
Selected cell ranges (Grid Pro) |
--lq-focus-ring |
#1f6feb |
#4493f8 |
Focus ring on cells, headers, filters and pager buttons |
--lq-pinned-shadow |
rgba(15, 23, 42, 0.12) |
rgba(0, 0, 0, 0.45) |
Shadow at the edge of pinned columns |
--lq-menu-bg |
#ffffff |
#161b22 |
Column menu background |
--lq-menu-shadow |
0 8px 24px rgba(15, 23, 42, 0.16) |
0 8px 24px rgba(0, 0, 0, 0.5) |
Column menu shadow |
--lq-input-bg |
#ffffff |
#0d1117 |
Floating filter inputs and editors |
--lq-radius |
6px |
same | Outer corners and controls |
Two more properties are set inline on the root from your options: --lq-row-height and --lq-header-height. Read them in your own CSS, for example to size icons in a cell renderer, but change them through rowHeight, headerHeight and density.
Contrast#
Both bundled themes meet WCAG 2.2 AA: at least 4.5:1 for text and 3:1 for non-text UI. Ratios computed from the default colors:
| Pair | Light | Dark | Requirement |
|---|---|---|---|
Text (--lq-fg) on background (--lq-bg) |
15.8:1 | 16.0:1 | 4.5:1 |
Text on a hovered row (--lq-row-hover) |
14.2:1 | 13.5:1 | 4.5:1 |
Text on a striped row (--lq-row-alt) |
15.1:1 | 15.4:1 | 4.5:1 |
| Header text on header background | 14.8:1 | 14.6:1 | 4.5:1 |
Muted text (--lq-muted-fg) on background |
6.1:1 | 6.5:1 | 4.5:1 |
| Muted text on header background | 5.7:1 | 5.9:1 | 4.5:1 |
| Accent (sort indicator, focus ring) on background | 4.6:1 | 6.1:1 | 3:1 |
| Accent on header background | 4.4:1 | 5.6:1 | 3:1 |
| Accent on a hovered row | 4.2:1 | 5.2:1 | 3:1 |
Accent text color (--lq-accent-fg) on accent |
4.6:1 | 6.1:1 | 4.5:1 |
When you build a custom theme, keep text colors at 4.5:1 or more against every background they appear on (cell, header, hovered, striped and selected rows), and keep --lq-accent, --lq-focus-ring and --lq-border used as the only indicator of state at 3:1 or more. Check the combinations, not just text on --lq-bg: muted text on the header background is usually the tightest pair.
Overriding styles#
The grid injects its stylesheet into the document head when the first grid is created, which places it after the stylesheets already on the page. At equal specificity the injected rules win, so write overrides with at least one class more than the rule you are overriding:
#orders .lq-grid {
--lq-accent: #1a7f37;
--lq-selection-bg: rgba(26, 127, 55, 0.14);
}
.lq-grid.lq-theme-light {
--lq-header-bg: #f3f6f9;
}
.lq-grid .lq-row.lq-row-total {
font-weight: 800;
}A custom theme#
Write a rule for .lq-grid.lq-theme-<name> and pass the name as theme:
.lq-grid.lq-theme-harbor {
--lq-font-family: 'Inter', system-ui, sans-serif;
--lq-font-size: 14px;
--lq-fg: #0f2a3d;
--lq-bg: #fbfdff;
--lq-muted-fg: #5b7184;
--lq-border: #d3e1ec;
--lq-border-subtle: #e4edf4;
--lq-header-bg: #eef5fb;
--lq-header-fg: #0f2a3d;
--lq-row-alt: #f5f9fc;
--lq-row-hover: #e6f0f8;
--lq-accent: #0a6c9e;
--lq-accent-fg: #ffffff;
--lq-selection-bg: rgba(10, 108, 158, 0.12);
--lq-range-bg: rgba(10, 108, 158, 0.16);
--lq-focus-ring: #0a6c9e;
--lq-radius: 4px;
}createGrid(container, { columns, rows, theme: 'harbor' });Properties you leave out keep their light defaults, so a theme can be as small as an accent color.
A dark variant of your theme#
Scope the dark values under an ancestor class your app already toggles, such as one on <html>:
.app-dark .lq-grid.lq-theme-harbor {
--lq-fg: #e3eef7;
--lq-bg: #0d1b26;
--lq-muted-fg: #8aa2b6;
--lq-border: #22394b;
--lq-border-subtle: #1a2f3e;
--lq-header-bg: #132634;
--lq-header-fg: #e3eef7;
--lq-row-alt: #102230;
--lq-row-hover: #17303f;
--lq-selection-bg: rgba(77, 171, 222, 0.2);
--lq-focus-ring: #4dabde;
--lq-menu-bg: #132634;
--lq-input-bg: #0d1b26;
color-scheme: dark;
}document.documentElement.classList.toggle('app-dark', isDark);The grid keeps theme: 'harbor'; the cascade does the rest, so no grid update is needed when the mode changes.
Density#
density sets the default row height, header height, font size and cell padding together:
density |
Row height | Header height | Font size | Cell padding |
|---|---|---|---|---|
'compact' |
28px | 32px | 12px | 8px |
'normal' (default) |
34px | 38px | 13px | 10px |
'comfortable' |
42px | 46px | 14px | 14px |
grid.update({ density: 'compact' });An explicit rowHeight number or headerHeight always wins over the density preset. The header height defaults to the row height plus 4 pixels.
Targeting elements with classes#
Every element the grid renders has an lq- class. Useful hooks include lq-header-cell, lq-cell, lq-cell-editable, lq-cell-editing, lq-cell-focused, lq-cell-pinned, lq-row, lq-row-selected, lq-row-odd, lq-row-group, lq-filter-input, lq-pager and lq-empty. For anything tied to your data, add your own classes with cellClass, headerClass and rowClass instead of relying on structure:
const columns: ColumnDef<Order>[] = [
{ field: 'revenue', type: 'number', headerClass: 'col-money', cellClass: 'col-money' },
];.lq-grid .col-money {
font-weight: 600;
letter-spacing: -0.01em;
}The grid already uses tabular figures for all text, so numbers line up without extra CSS. See Columns and Rows.
Sizing the grid#
The root fills its container (width: 100%; height: 100%) with a minimum height of 120 pixels. Give the container an explicit height, or place it in a flex or grid layout that sizes it:
.orders-panel {
display: flex;
flex-direction: column;
height: calc(100vh - 64px);
}
.orders-panel > #orders {
flex: 1;
min-height: 0;
}How styles are loaded#
By default the grid injects its stylesheet, as a <style id="lq-grid-styles"> element in the head of the container's document, the first time a grid is created. You need no CSS import, and server-side rendering is unaffected because nothing happens on import.
If your app has a strict Content Security Policy that forbids inline styles, or you want the CSS in your own bundle, turn injection off and import the stylesheet instead:
import '@birchui/core/styles.css';
import { createGrid } from '@birchui/core';
createGrid(container, { columns, rows, injectStyles: false });With the imported stylesheet, the usual source order applies: put your overrides after the import.
The same CSS is exported as a string, gridStyles, if you need to place it yourself, for example inside a shadow root:
import { gridStyles } from '@birchui/core';
const style = document.createElement('style');
style.textContent = gridStyles;
shadowRoot.append(style);Related#
- Installation covers CSS imports for each bundler.
- Grid Pro features use the same properties; see Grid Pro overview.