Match your design system
Map your design tokens onto the grid's --lq-* custom properties to build a named theme with light and dark variants, density and accessible contrast.
Your app already has tokens for colour, type and spacing. Instead of overriding grid selectors one by one, map those tokens onto the grid's CSS custom properties in one named theme. The grid then follows your design system, including dark mode, without JavaScript.
1. Start from your tokens#
Assume a design system that exposes tokens as custom properties on :root:
:root {
--ds-font-sans: 'Inter', system-ui, sans-serif;
--ds-text-sm: 13px;
--ds-space-3: 12px;
--ds-radius-md: 6px;
--ds-color-text: #101828;
--ds-color-text-subtle: #475467;
--ds-color-surface: #ffffff;
--ds-color-surface-raised: #f9fafb;
--ds-color-surface-sunken: #f2f4f7;
--ds-color-border: #d0d5dd;
--ds-color-border-subtle: #eaecf0;
--ds-color-brand: #6941c6;
--ds-color-on-brand: #ffffff;
--ds-color-brand-tint: rgba(105, 65, 198, 0.12);
--ds-color-danger: #b42318;
}
:root.dark {
--ds-color-text: #f5f5f6;
--ds-color-text-subtle: #94969c;
--ds-color-surface: #0c111d;
--ds-color-surface-raised: #161b26;
--ds-color-surface-sunken: #1f242f;
--ds-color-border: #333741;
--ds-color-border-subtle: #1f242f;
--ds-color-brand: #9e77ed;
--ds-color-on-brand: #0c111d;
--ds-color-brand-tint: rgba(158, 119, 237, 0.2);
}2. Map them onto a grid theme#
The grid root defines every --lq-* property on itself, so set them on .lq-grid.lq-theme-<name>, not on an ancestor. Because the values reference your tokens with var(), the theme switches to dark automatically when your tokens do:
.lq-grid.lq-theme-acme {
--lq-font-family: var(--ds-font-sans);
--lq-font-size: var(--ds-text-sm);
--lq-cell-padding: var(--ds-space-3);
--lq-radius: var(--ds-radius-md);
--lq-fg: var(--ds-color-text);
--lq-muted-fg: var(--ds-color-text-subtle);
--lq-bg: var(--ds-color-surface);
--lq-header-bg: var(--ds-color-surface-sunken);
--lq-header-fg: var(--ds-color-text);
--lq-row-alt: var(--ds-color-surface-raised);
--lq-row-hover: var(--ds-color-surface-sunken);
--lq-border: var(--ds-color-border);
--lq-border-subtle: var(--ds-color-border-subtle);
--lq-accent: var(--ds-color-brand);
--lq-accent-fg: var(--ds-color-on-brand);
--lq-focus-ring: var(--ds-color-brand);
--lq-selection-bg: var(--ds-color-brand-tint);
--lq-range-bg: var(--ds-color-brand-tint);
--lq-menu-bg: var(--ds-color-surface);
--lq-input-bg: var(--ds-color-surface);
--lq-danger: var(--ds-color-danger);
}
:root.dark .lq-grid.lq-theme-acme {
color-scheme: dark;
}3. Use the theme#
import { createGrid } from '@birchui/core';
import './grid-theme.css';
createGrid(document.querySelector<HTMLElement>('#orders')!, {
columns,
rows: orders,
theme: 'acme',
density: 'compact',
stripes: true,
});theme: 'acme' adds the class lq-theme-acme to the grid root. Properties you do not set keep the light defaults.
4. Style individual parts#
For details tokens cannot express, target the grid's classes with one class more than the built-in rule:
.lq-grid.lq-theme-acme .lq-header-cell {
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.02em;
}The class names are listed in Targeting elements with classes.
Pitfalls#
- Setting properties on
bodydoes nothing. The grid redefines them on its root. Always target.lq-grid. - Check contrast on every surface. Text must reach 4.5:1 on the cell, header, hovered, striped and selected backgrounds, and the accent and focus ring 3:1. A brand tint used as
--lq-selection-bgis the most common failure. See Contrast. - Fonts. Load the web font before the grid measures columns for autosizing, or call
autosizeColumns()afterdocument.fonts.ready. - Specificity. The grid injects its stylesheet after your stylesheets, so equal-specificity rules lose. Use the two-class selectors above.
Related#
- Theming, with the full property table and density.
- How styles are loaded for shipping CSS as a file.