Skip to content

Themes & Density

NextGrid separates color theme (which can switch live via setTheme) from density (which is set at construction so CSS sizes and JS virtualization geometry always agree).

Runtime Theme Switching & Compact Density
import { Grid, type GridDensity, type GridTheme } from 'next-grid-js';
import { createOrders, type OrderRow } from './data/orders';
import type { DemoMountContext } from './types';
import { addButton } from './ui';
export function mount({
stage,
controls,
meta,
}: DemoMountContext): () => void {
let density: GridDensity = 'default';
let theme: GridTheme = 'auto';
let grid: Grid<OrderRow> | null = null;
const rows = createOrders(25);
const mountGrid = () => {
grid?.destroy();
grid = new Grid<OrderRow>({
container: stage,
columnDefs: [
{
key: 'id',
name: 'ID',
width: 96,
isSortable: true,
},
{
key: 'customer',
name: 'Customer',
width: 175,
flex: 1,
isSortable: true,
},
{
key: 'region',
name: 'Region',
width: 105,
},
{
key: 'amount',
name: 'Amount',
width: 115,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
],
rowData: rows,
getRowId: (row) => row.id,
theme,
density,
rowSelection: {
mode: 'multiple',
showCheckbox: true,
},
});
meta.textContent = `Theme: ${grid.getTheme()} · Density: ${density}`;
};
mountGrid();
for (const nextTheme of ['auto', 'light', 'dark'] as const) {
addButton(controls, `Theme: ${nextTheme}`, () => {
theme = nextTheme;
grid?.setTheme(nextTheme);
meta.textContent = `Theme: ${grid?.getTheme()} · Density: ${density}`;
});
}
const densityBtn = addButton(controls, 'Toggle compact', () => {
density = density === 'default' ? 'compact' : 'default';
densityBtn.textContent =
density === 'compact' ? 'Density: compact' : 'Toggle compact';
mountGrid();
});
return () => {
grid?.destroy();
grid = null;
};
}
const grid = new Grid({ container, columnDefs, rowData, theme: 'auto' });
grid.setTheme('dark'); // switches colors without recreating the grid or resetting scroll
grid.getTheme(); // 'dark'
  • 'auto' (default): Sets no theme class on .grid-container; follows the nearest ancestor .next-grid-theme-light or .next-grid-theme-dark class, or falls back to OS prefers-color-scheme.
  • 'light' / 'dark': Forces that theme class on the grid container (color-scheme: light | dark).
  • Accessibility: Token pairs in both themes meet WCAG AA contrast (4.5:1 for text, 3:1 for focus rings and selection borders).
Size 'default' 'compact'
Body row height 26 px 22 px
Header row height 30 px 26 px
Cell padding 0 10px 0 6px
Footer controls 26 px 22 px
Footer gap 8 px 6 px
Menu item padding 6px 8px 4px 8px

Explicit rowHeight and headerRowHeight options override density defaults (headerRowHeight falls back to --next-grid-header-row-height, then density).

Under @media (forced-colors: active), NextGrid maps focus indicators, selection outlines, active editors, copied/pasted cell marks, fill handles, and error cells to system colors (Highlight, HighlightText, Mark). There is no separate high-contrast palette.