Skip to content

Theme ​

The theme is a single CSS file. Import it once in your app entry point.

ts
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
<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.

html
<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).

Live demo — drawn by the real renderer

Change the density ​

GridOptions.density sets the horizontal cell padding. Row and header heights are set separately by rowHeight and headerHeight.

ValueCell 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.

ItemValue in 'spacious'
Default row, header height48px, 52px (an explicit rowHeight or headerHeight wins)
Font size14px
Control size, height20px, 36px
Inner column lines, stripesnone (striped: true brings the stripes back)
Outer frame12px corners, soft shadow, no border
Row lines, hover backgroundlighter 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.

WhereWhat changes
Sort arrowsAscending, descending and unsorted (--mxg-sort-arrow)
Expand arrowsThe default arrows of tree, grouping and detail (your own toggleIcon stays)
Check marksCheckbox and switch renderers, multi-select list, check-box mask
Close ×Group panel chips, filter inputs, filter menu (--mxg-icon-close)
Chevrons and filterInput chevrons, filter-off mark (--mxg-icon-chevron, --mxg-icon-filter-off)
Drag grip and stepperThe grip dots, the stepper ▲▼ or +-
Header dropdownThe right-hand arrow
CalendarPrevious and next arrows, calendar and clock buttons
RatingRatings without shape draw round SVG stars (size from --mxg-rating-size)
  • Icons you set with the icons option and your own toggleIcon always 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 icons stay 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.

MotionWhat happens
Rows move, add, removeRows slide into place on sort, move, expand and collapse
New and removed rowsNew rows fade in, removed rows fade out
Expand arrowTurns 90 degrees
Popups, menus, pickers, tooltipsFade in with a 4px slide, and fade out quickly in reverse when closing (--mxg-motion-fast)
Row dragThe drag shape lifts a little, drop marks fade in
Changed cellThe 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 from popupCreated.
  • 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.

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.

css
.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.

VariableUsed for
--mxg-font, --mxg-font-sizeFont family, font size
--mxg-fg, --mxg-bg, --mxg-fg-mutedBody text and background, muted text
--mxg-bg-header, --mxg-fg-headerHeader
--mxg-bg-row-hoverRow hover
--mxg-bg-row-stripeBackground of striped odd rows (striped: false turns it off)
--mxg-borderDividers
--mxg-cell-paddingHorizontal cell padding (per density)
--mxg-sort-indicatorSort arrow
--mxg-bg-selected, --mxg-fg-selected, --mxg-bg-selected-hoverSelected row
--mxg-bg-cell-selected, --mxg-fg-cell-selectedBackground and text of selected cells (cellSelection)
--mxg-bg-cell-anchor, --mxg-cell-sel-lineBackground of the first-pressed cell, border of the block
--mxg-focus-ring, --mxg-focus-width, --mxg-focus-offsetFocused-cell outline
--mxg-accent, --mxg-accent-fgChecked checkbox, accent color
--mxg-bg-dirty, --mxg-mark-dirtyModified cell background and corner mark
--mxg-bg-added, --mxg-mark-addedNew row background and left bar
--mxg-control-size, --mxg-control-heightCheckbox and radio size, input, select and button height (per density)
--mxg-control-border, --mxg-control-border-hoverBorder and hover border of checkboxes, inputs and selects
--mxg-danger, --mxg-bg-invalidError cell, invalid cell, failed row
--mxg-panel-radius, --mxg-panel-shadowCorner 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-slowDurations for motion: true (120ms, 180ms, 260ms)
--mxg-ease, --mxg-ease-exitEasing curves for entering and leaving with motion: true
--mxg-transition-durationBackground 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.

ts
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(); // undo

With a script tag, load the theme extension after the main bundle. The functions are attached to MaxGrid.themes.

html
<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); radius and density are optional. Colors are #rgb or #rrggbb only (contrast must be calculated, so names such as red are rejected).
  • A name starts with a lowercase letter and uses lowercase letters, digits and hyphens, up to 32 characters; light and dark are not allowed. A font list is at most 200 characters and rejects characters such as url(, ; and parentheses. A wrong value fails at once with RangeError and nothing is created.
  • checkTheme returns the list of violations and, when there are any, also warns once in the console during development (turn that off with checkTheme(theme, { warn: false })). It does not fix colors for you.
  • applyTheme sets data-mxg-theme on 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 throws RangeError. 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.

OptionValueEffect
noncestringPut a CSP nonce on the <style>
layerstarts with a letter, up to 41 charsWrap all styles in @layer name { … }
containerShadowRoot or elementWhere the <style> goes; default is the document head (the root inside shadow DOM)
ts
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).

ts
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.

NameWhere
sortAsc, sortDescSort direction mark
sortNoneSortable header that is not sorted yet (shown when you set it)
expand, collapseCollapsed and expanded rows of tree, group and detail (kept where you set toggleIcon yourself)
menuColumn menu button
filterFilter popup button
ts
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.

ts
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.

PresetAccent (light / dark)CornersRow height (density)
bootstrap#0a58ca / #6ea8fe6pxnormal
tailwind#1d4ed8 / #60a5fa6pxcompact
material#6750a4 / #d0bcff8pxcomfortable

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) ​

html
<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 ​

ts
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:

html
<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 createTheme as 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, margin and similar on div or *) inside itself. It cannot block !important rules or rules that target internal classes such as .mxg-cell directly.
  • 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: reduce the transition time becomes 0. In forced-colors (high contrast) mode the grid uses system colors.