Theming
Four themes (Sage, Moon, Ember, Paper), how to switch them, and how to add your own.
Demo
Usage
import { Slant, themes } from '@tuttn/slant'
// The theme is a data attribute on the root; a nested root with a theme
// re-themes its subtree, and one without keeps its parent's.
<Slant theme="moon">
<Dashboard />
<Slant theme="paper"><Invoice /></Slant>
</Slant>
// Direction, density and Slant's own strings, for everything inside.
<Slant theme="paper" dir="rtl" density="comfortable" messages={{ dismiss: 'סגירה' }}>
<App />
</Slant>
// `themes` lists the built-in themes for a switcher: id, name, accent, bg, dark.
themes.map(theme => theme.id) // ['sage', 'moon', 'ember', 'paper']Props
| Prop | Type | Default | Description |
|---|---|---|---|
theme | ThemeId | — | The theme for this subtree. A nested root without one keeps its parent's theme; the outermost root falls back to Sage. |
dir | Direction | — | Text direction for the subtree. Inherited from the parent root, else from the document. |
density | Density | 'regular' | Control heights for the subtree: compact, regular or comfortable. Inherited from the parent root. |
messages | Partial<SlantMessages> | — | Replaces any of Slant's built-in English strings; merged over the parent root's. |
as | 'div' | 'section' | 'article' | 'main' | 'aside' | 'span' | 'div' | The element to render. |
A theme is nothing more than a set of --sl-* values on .slant[data-theme]: colours, the three corner radii, three shadows, and the color-scheme that native scrollbars and form controls follow. Sage is the base; Moon rounds corners to 14px, Ember squares them to 4px, and Paper is the light theme, with its own state colours and lighter shadows.
The root also hands its settings to everything inside through React context. A nested root inherits theme, dir and density unless it sets its own, and its messages are merged over its parent's. Overlays such as Menu render at the end of the page, outside your tree, in a host that carries the same theme, direction and density, so they look like they belong where they opened.
The outermost root also renders two hidden live regions, one polite and one assertive. Components that announce something, a toast or a sort order, speak through them, so the announcement doesn't depend on the component's own markup appearing.
To add a theme, declare its tokens in your own stylesheet and set the attribute yourself (the theme prop is typed to the built-in ids). Slant's rules are in @layer slant, so yours win without extra specificity:
.slant[data-theme="forest"] {
color-scheme: dark;
--sl-bg: #0f1a0c; --sl-surface: #162412; --sl-surface-2: #1e3019;
--sl-line: #2a4023; --sl-line-strong: #3b5632;
--sl-text: #eef5ea; --sl-muted: #9fb598;
--sl-accent: #7cc26a; --sl-accent-text: #0f1a0c;
--sl-radius: 8px; --sl-radius-small: 4px; --sl-radius-large: 12px;
}Fonts follow the same rule: set --sl-font-sans and --sl-font-mono on the root to use your own faces. The Tokens page lists every variable, and Conventions covers what every component shares.