Theme selector
Slant / Forms
A choice of theme, each option painted in its own tokens.
Demo
Usage
import { Field, Slant, ThemeSelector, type ThemeId } from '@tuttn/slant'
// Controlled: pass value and onValueChange, which gets the theme's id.
const [theme, setTheme] = useState<ThemeId>('moon')
<ThemeSelector value={theme} onValueChange={setTheme} />
<Slant theme={theme}>…</Slant>
// Uncontrolled: it starts on the nearest root's theme (or defaultValue), and a
// form submits the chosen id under its name.
<form>
<ThemeSelector name="theme" size="sm" />
</form>
// In a Field, the field's label names the group.
<Field>
<Field.Label>Editor theme</Field.Label>
<ThemeSelector themes={themes.filter(theme => theme.dark)} />
<Field.Description>Dark themes only.</Field.Description>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | ThemeId | — | The chosen theme. Pass it to control the selector; leave it out to let the selector keep its own. |
defaultValue | ThemeId | the nearest Slant root's theme, else 'sage' | The theme an uncontrolled selector starts on. |
onValueChange | (theme: ThemeId) => void | — | Called with the theme's id when a click or an arrow key chooses one. |
themes | readonly Theme[] | all four: Sage, Moon, Ember and Paper | The themes offered, in order. |
label | string | messages.theme | The group's accessible name. Inside a Field, the field's label names it instead. |
name | string | generated | The radios' name, which a form submits the chosen id under. |
form | string | — | The id of the form the radios belong to, when the selector sits outside it. |
disabled | boolean | — | Disables every option. |
size | 'sm' | 'md' | 'md' | `md` fills its row with 44px-high tiles; `sm` hugs its 32px tiles, for a settings row. |
…rest | HTMLAttributes<HTMLDivElement> | — | The ref, className, style and other attributes go on the radiogroup <div>. |
The options are native radios in a radiogroup named by label (“Theme” by default, from messages.theme), or by a Field’s label. Tab reaches the chosen theme, the arrow keys move the choice, and a form submits the chosen id. Each radio is named by its theme’s name; the swatch is decoration.
Each swatch is a nested .slant element with its theme’s data-theme, so it reads that theme’s own --sl-bg, --sl-accent, --sl-surface and radius, and stays true when the tokens change. The ring around the chosen swatch and the focus ring use the selector’s own theme.