Theme
The theme is a single CSS file. Import it once in your app entry point.
import '@maxgrid/themes/grid.css';Turn on dark mode
Set data-mxg-theme="dark" on the grid or on any ancestor. Without the attribute, the grid is light. There is no automatic prefers-color-scheme switch; read the media query in your app and set the attribute yourself.
<html data-mxg-theme="dark">
…
</html>Set it on <html> together with your app's own theme attribute and the grid and the Gantt chart switch together. The grid (.mxg), controls, popups, the Gantt chart (.mxg-gantt-root), the Gantt tooltip and the unified split card (.mxg-split-unified) all look for the same attribute, data-mxg-theme="dark" (the value is exactly dark), on themselves or an ancestor. Keep the attribute on <html> to avoid a flash, and set it with a short script that runs before the first paint.
<head>
<script>
// Runs before the CSS. 'app-theme' is an example of the name your app stores.
try {
var t = localStorage.getItem('app-theme');
if (!t) t = matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
document.documentElement.dataset.theme = t; // your app's theme attribute
if (t === 'dark') document.documentElement.setAttribute('data-mxg-theme', 'dark');
} catch (e) {}
</script>
</head>When the theme changes, change both attributes together. Without Gantt color variables the chart follows the grid tokens (--mxg-bg, --mxg-fg and so on), and otherwise uses its own light and dark defaults (see "Theme" in Gantt chart).
Change the density
GridOptions.density sets the horizontal cell padding. Row and header heights are set separately by rowHeight and headerHeight.
| Value | Cell padding (left and right) |
|---|---|
'compact' | 4px |
'normal' (default) | 8px |
'comfortable' | 12px |
'spacious' | 16px |
'spacious' (1.5.0) changes the look too, not only the padding.
| Item | Value in 'spacious' |
|---|---|
| Default row, header height | 48px, 52px (an explicit rowHeight or headerHeight wins) |
| Font size | 14px |
| Control size, height | 20px, 36px |
| Inner column lines, stripes | none (striped: true brings the stripes back) |
| Outer frame | 12px corners, soft shadow, no border |
| Row lines, hover background | lighter colors (contrast 4.5:1 or more in light and dark themes) |
The other three densities keep their old heights and padding.
Style a single cell
To layer a class or styles on one cell from app code, use setCellStyle (1.5.0). It follows the cell through sort and filter. See column options.
Icon style
iconStyle: 'soft' (1.5.0) swaps the built-in icons for round-ended line icons (stroke 1.6). The default is 'default' and draws exactly as before.
| Where | What changes |
|---|---|
| Sort arrows | Ascending, descending and unsorted (--mxg-sort-arrow) |
| Expand arrows | The default arrows of tree, grouping and detail (your own toggleIcon stays) |
| Check marks | Checkbox and switch renderers, multi-select list, check-box mask |
| Close × | Group panel chips, filter inputs, filter menu (--mxg-icon-close) |
| Chevrons and filter | Input chevrons, filter-off mark (--mxg-icon-chevron, --mxg-icon-filter-off) |
| Drag grip and stepper | The grip dots, the stepper ▲▼ or +- |
| Header dropdown | The right-hand arrow |
| Calendar | Previous and next arrows, calendar and clock buttons |
| Rating | Ratings without shape draw round SVG stars (size from --mxg-rating-size) |
- Icons you set with the
iconsoption and your owntoggleIconalways win. - Popups attached elsewhere in the document get the same style.
- The submenu arrow in the cell menu and the added, modified, removed and failed icons in the state column change too (values set with
iconsstay as they are). setOptions({ iconStyle })applies to what is already drawn and to popups opened afterwards. A popup that is already open changes only after you close and reopen it.
Turn on motion
motion: true turns on the effects below. It is off by default and the grid draws exactly as before.
| Motion | What happens |
|---|---|
| Rows move, add, remove | Rows slide into place on sort, move, expand and collapse |
| New and removed rows | New rows fade in, removed rows fade out |
| Expand arrow | Turns 90 degrees |
| Popups, menus, pickers, tooltips | Fade in with a 4px slide, and fade out quickly in reverse when closing (--mxg-motion-fast) |
| Row drag | The drag shape lifts a little, drop marks fade in |
| Changed cell | The flash fades in over 180ms |
- Motion is skipped while scrolling and when more than 60 rows change at once.
- A popup is removed from the DOM when its closing animation ends or after 140 ms. With reduced motion it is removed at once, and the same holds when your app calls
remove()on an element it got frompopupCreated. - There is no header movement when column order or visibility changes in this version.
- Change times and curves with the variables above. With the operating system's reduced-motion setting they are all 0ms.
setOptions({ motion })turns it on and off.
Change colors and fonts
Every value is a --mxg-* variable. Override them in your app CSS.
.mxg {
--mxg-bg-row-hover: #e8f0fe;
--mxg-font: 'Pretendard', system-ui, sans-serif;
}To change a value in dark mode only, use .mxg[data-mxg-theme='dark'] as the selector.
To change only the font, one --mxg-font line is enough. The default is the system font. The font button at the top of our example gallery switches between the default and the site font.
.mxg {
--mxg-font: 'Pretendard', system-ui, sans-serif;
}To pick colors and download a ready-made file instead, use the theme builder. You choose a few colors, it works out the other variables, and it checks text contrast.
| Variable | Used for |
|---|---|
--mxg-font, --mxg-font-size | Font family, font size |
--mxg-fg, --mxg-bg, --mxg-fg-muted | Body text and background, muted text |
--mxg-bg-header, --mxg-fg-header | Header |
--mxg-bg-row-hover | Row hover |
--mxg-bg-row-stripe | Background of striped odd rows (striped: false turns it off) |
--mxg-border | Dividers |
--mxg-cell-padding | Horizontal cell padding (per density) |
--mxg-sort-indicator | Sort arrow |
--mxg-bg-selected, --mxg-fg-selected, --mxg-bg-selected-hover | Selected row |
--mxg-bg-cell-selected, --mxg-fg-cell-selected | Background and text of selected cells (cellSelection) |
--mxg-bg-cell-anchor, --mxg-cell-sel-line | Background of the first-pressed cell, border of the block |
--mxg-focus-ring, --mxg-focus-width, --mxg-focus-offset | Focused-cell outline |
--mxg-accent, --mxg-accent-fg | Checked checkbox, accent color |
--mxg-bg-dirty, --mxg-mark-dirty | Modified cell background and corner mark |
--mxg-bg-added, --mxg-mark-added | New row background and left bar |
--mxg-control-size, --mxg-control-height | Checkbox and radio size, input, select and button height (per density) |
--mxg-control-border, --mxg-control-border-hover | Border and hover border of checkboxes, inputs and selects |
--mxg-danger, --mxg-bg-invalid | Error cell, invalid cell, failed row |
--mxg-panel-radius, --mxg-panel-shadow | Corner radius and shadow of the outer frame (0 and none; spacious uses 12px and a soft shadow) |
--mxg-motion-fast, --mxg-motion-base, --mxg-motion-slow | Durations for motion: true (120ms, 180ms, 260ms) |
--mxg-ease, --mxg-ease-exit | Easing curves for entering and leaving with motion: true |
--mxg-transition-duration | Background color transition |
Selection, focus and edit states are styled through classes the renderer adds: mxg-row-selected, mxg-cell-focused, mxg-cell-dirty, mxg-row-added, mxg-row-failed and mxg-cell-invalid.
Create your own theme (createTheme)
Since 1.2.0
createTheme, applyTheme and checkTheme are available since 1.2.0. In 1.1.0 you override the variables directly, as above.
gridElement is the element that holds the grid (the container you passed when you created it). The CSS import on the first line is for a bundler (Vite, webpack and the like). With a script tag, maxgrid.min.js already includes the CSS, so you do not need it.
Pick a few colors and the rest of the --mxg-* variables are derived and applied to one grid or one region only. It is independent of the site's light and dark setting. To choose colors on screen and download files, use the theme builder.
import '@maxgrid/themes/grid.css';
import { applyTheme, checkTheme, createTheme } from '@maxgrid/themes';
const theme = createTheme({
name: 'acme',
base: 'light', // 'light' or 'dark'
tokens: {
background: '#ffffff',
text: '#1f2933',
accent: '#0b7a45',
border: '#d9dee5',
headerBackground: '#f3f5f8',
selectionBackground: '#d8f1e3',
danger: '#b42318',
font: "system-ui, 'Noto Sans KR', sans-serif",
},
});
const problems = checkTheme(theme); // the places short of 4.5:1 for text or 3:1 for borders and focus (empty if none)
const restore = applyTheme(gridElement, theme); // applies to this element only
// ...
restore(); // undoWith a script tag, load the theme extension after the main bundle. The functions are attached to MaxGrid.themes.
<script src="https://cdn.maxmine.com/maxgrid/1.8.0/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/1.8.0/maxgrid.themes.min.js"></script>
<script>
const { createTheme, applyTheme, checkTheme } = MaxGrid.themes;
</script>- You need the eight core tokens (
background,text,accent,border,headerBackground,selectionBackground,danger,font);radiusanddensityare optional. Colors are#rgbor#rrggbbonly (contrast must be calculated, so names such asredare rejected). - A name starts with a lowercase letter and uses lowercase letters, digits and hyphens, up to 32 characters;
lightanddarkare not allowed. A font list is at most 200 characters and rejects characters such asurl(,;and parentheses. A wrong value fails at once withRangeErrorand nothing is created. checkThemereturns the list of violations and, when there are any, also warns once in the console during development (turn that off withcheckTheme(theme, { warn: false })). It does not fix colors for you.applyThemesetsdata-mxg-themeon the element and adds one style to<head>. The undo function is safe to call twice. Applying the same name with different content at the same time throwsRangeError. Destroying the grid does not remove the theme by itself, so call the undo function yourself.- Computed colors are laid down as well, so browsers without
color-mix()still show the intended colors.
See the gallery example theme-custom-create.
Choose where styles go, CSP, icons and another document
Where styles go
applyTheme accepts layer and container. MaxGrid.config, which injects the default styles, uses the same names.
| Option | Value | Effect |
|---|---|---|
nonce | string | Put a CSP nonce on the <style> |
layer | starts with a letter, up to 41 chars | Wrap all styles in @layer name { … } |
container | ShadowRoot or element | Where the <style> goes; default is the document head (the root inside shadow DOM) |
applyTheme(el, theme, { layer: 'vendor', container: shadowRoot, nonce });
MaxGrid.config({ layer: 'vendor', container: shadowRoot, nonce });Using the same theme name with a different layer at the same time counts as different content and throws RangeError. A badly formed layer or container throws TypeError and adds nothing.
Render into another document (iframe)
The grid works from the document of its container for listeners, focus and popup placement. A container from another window works as it is, and the script may stay in the parent window. The default styles go into that document's head, or into the shadow root when the container is inside shadow DOM. To use a document other than the container's, pass the document option (read at creation only).
const doc = iframe.contentDocument;
MaxGrid.createGrid(doc.getElementById('grid'), { columns, rows, document: doc });Change the icons
icons changes the sort arrows, the expand marks, the menu button and the filter button. A column's icons changes that column only and wins over the global ones (expand and collapse are not allowed on a column). A value is text of at most 4 characters or SVG markup. Anything you leave out keeps today's look.
| Name | Where |
|---|---|
sortAsc, sortDesc | Sort direction mark |
sortNone | Sortable header that is not sorted yet (shown when you set it) |
expand, collapse | Collapsed and expanded rows of tree, group and detail (kept where you set toggleIcon yourself) |
menu | Column menu button |
filter | Filter popup button |
icons: { sortAsc: '↑', sortDesc: '↓', menu: '≡', filter: '<svg viewBox="0 0 16 16"><path d="M2 3h12L9 9v4l-2-1V9z" fill="currentColor"/></svg>' }SVG is read against an allow list only. Elements: svg, g, path, circle, rect, line, polyline, polygon, title. Attributes: xmlns (only with the value http://www.w3.org/2000/svg), d, fill, fill-rule, clip-rule, stroke, stroke-width, stroke-linecap, stroke-linejoin, viewBox, width, height (a number from 1 to 1024, unit px or em only), cx, cy, r, x, y, x1, y1, x2, y2, points, transform, opacity. Any other element or attribute (including style, onload, href), and any value containing url(, javascript: or data:, is an invalid-option and nothing is drawn. Accessible names stay the same.
Get told when a popup is created
hooks.popupCreated runs once for each menu, filter popup, date or time picker, tooltip and editor when it is created, before it is shown. Use it to adjust placement or add classes.
hooks: {
popupCreated: ({ element, kind }) => element.classList.add(`my-${kind}`);
}kind is 'filter', 'column-menu', 'cell-menu', 'picker', 'tooltip' or 'editor'. If the hook throws, the popup still opens and the error is reported through the error event (callback-error).
Right-to-left direction and themes
The theme CSS uses logical properties (inset-inline-start, margin-inline-end and so on) instead of left and right, so it mirrors with direction: 'rtl'. Write your own themes and overrides the same way and RTL needs no extra fixes. The dir attribute on the root sets the direction (.mxg[dir='rtl']).
Bootstrap, Tailwind and Material look presets
Since 1.8.0
Presets are available from 1.8.0. The default theme does not change.
If your site looks like Bootstrap, Tailwind or Material, you can use an optional theme that matches it. Each preset is a light and dark pair and depends on no host library. The default theme does not change.
| Preset | Accent (light / dark) | Corners | Row height (density) |
|---|---|---|---|
bootstrap | #0a58ca / #6ea8fe | 6px | normal |
tailwind | #1d4ed8 / #60a5fa | 6px | compact |
material | #6750a4 / #d0bcff | 8px | comfortable |
Text keeps at least 4.5:1 and borders, focus and marks at least 3:1, tested for each light and dark theme.
As a CSS file (no script)
<link rel="stylesheet" href="…/@maxgrid/themes/grid.css" />
<link rel="stylesheet" href="…/@maxgrid/themes/preset-bootstrap.css" />
<div class="app" data-mxg-theme="bootstrap">…grid…</div>Set data-mxg-theme="bootstrap" (or bootstrap-dark) on the grid or an ancestor. The CSS file does not set a font: the grid inherits your site's font.
As a JS theme
import { applyTheme } from '@maxgrid/themes';
import { presets } from '@maxgrid/themes/presets';
const bootstrap = presets.find((p) => p.name === 'bootstrap')!;
const dark = matchMedia('(prefers-color-scheme: dark)').matches;
const restore = applyTheme(gridElement, dark ? bootstrap.dark : bootstrap.light);The font of a JS theme is the usual font stack of that library (for Bootstrap, system-ui, -apple-system, 'Segoe UI', …). To use your site's own font exactly, use the CSS file.
As a script tag (optional CDN file)
The JS themes are also available as one optional file next to the other bundles. It sets the global MaxGridThemesPresets (presets, bootstrapPreset, tailwindPreset, materialPreset) and is not part of the core bundle. Add the integrity attribute so the browser checks the file:
<script
src="https://cdn.maxmine.com/maxgrid/1.8.0/maxgrid.themes-presets.min.js"
integrity="sha384-yv9Dq92vuowXV8g7TpV+CG7Lakk8mz6CpfE+3tsU4VbCUyMFRJ8Rc10B/EdXN0F/"
crossorigin="anonymous"
></script>
<script>
const preset = MaxGridThemesPresets.presets.find((p) => p.name === 'bootstrap');
// applyTheme comes from the optional themes module (maxgrid.themes.min.js, after maxgrid.min.js)
const restore = MaxGrid.themes.applyTheme(gridElement, preset.light);
</script>The file is about 3.6 KB gzip. The integrity value of each release is in sri.json next to the file.
Good to know
- Presets are separate files (
preset-*.css,@maxgrid/themes/presets), so they add nothing to the bundle if you do not use them. - Size, line count and centering are checked under the reset CSS of customer sites (Bootstrap reboot, Tailwind preflight, MUI CssBaseline).
- To change more colors, use
createThemeas described in "Your own theme" above.
Things that trip people up
- The default colors meet 4.5:1 for text and 3:1 for focus rings and sort arrows. The theme package's tests check these values. Values you override are not checked, so measure them yourself.
- All grid CSS sits under
.mxg, so it does not touch your app's global styles. In the other direction, the grid undoes some of your global rules (box-sizing,marginand similar ondivor*) inside itself. It cannot block!importantrules or rules that target internal classes such as.mxg-celldirectly. - A focused cell inside a selected row keeps an outline of at least 3:1. When you change the selection, focus or modified colors, check that pair too.
- Under
prefers-reduced-motion: reducethe transition time becomes 0. In forced-colors (high contrast) mode the grid uses system colors.