Theme selector

Slant / Forms

A choice of theme, each option painted in its own tokens.

Demo

Appearance

Usage

@tuttn/slant
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

PropTypeDefaultDescription
valueThemeId—The chosen theme. Pass it to control the selector; leave it out to let the selector keep its own.
defaultValueThemeIdthe 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.
themesreadonly Theme[]all four: Sage, Moon, Ember and PaperThe themes offered, in order.
labelstringmessages.themeThe group's accessible name. Inside a Field, the field's label names it instead.
namestringgeneratedThe radios' name, which a form submits the chosen id under.
formstring—The id of the form the radios belong to, when the selector sits outside it.
disabledboolean—Disables every option.
size'sm' | 'md''md'`md` fills its row with 44px-high tiles; `sm` hugs its 32px tiles, for a settings row.
…restHTMLAttributes<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.

Full source