Conventions

Slant / Start

The rules every component follows: refs, state props, tone, variant and size, composition, forms and fields, overlays, strings and styles.

Demo

Usage

@tuttn/slant
import { Button, Segmented, Switch, Tabs } from '@tuttn/slant'

// State: value/defaultValue/onValueChange, checked/defaultChecked/onCheckedChange,
// open/defaultOpen/onOpenChange. Pass the value to control it, or a default to let
// the component keep its own.
<Segmented options={periods} value={period} onValueChange={setPeriod} label="Period" />
<Switch defaultChecked onCheckedChange={save} aria-label="Auto-save" />

// Words: tone is what it means, variant how loud it is, size how big.
<Button tone="danger" variant="primary" size="lg">Delete branch</Button>

// Composition: an array for uniform content, parts for rich content.
<Tabs items={items} label="Task details" />
<Tabs defaultValue="files">
  <Tabs.List label="Task details"><Tabs.Tab value="files">Files</Tabs.Tab></Tabs.List>
  <Tabs.Panel value="files"><FileList /></Tabs.Panel>
</Tabs>

// asChild: render the child element with the component's look and behaviour.
<Button asChild><a href="/docs">Docs</a></Button>

// Every DOM component forwards its ref, and takes className, style and
// any other attribute on its root element.
const ref = useRef<HTMLButtonElement>(null)
<Switch ref={ref} className="settings-switch" data-testid="autosave" />

Refs and attributes

Every component that renders an element forwards its ref to that element and passes className, style and every other attribute (id, data-*, aria-*, handlers) to it. Where the interesting element is not the outer one, the page says so: Slider's ref and attributes reach its range input.

State

Stateful components work both ways. Pass the value and a change handler to control them, or a default (or nothing) to let them keep their own. The names are fixed: value, defaultValue and onValueChange for the main value; checked, defaultChecked and onCheckedChange for on and off; open, defaultOpen and onOpenChange for anything that opens. Other state follows the same pattern: Table's sort, defaultSort and onSortChange; Calendar's month. Actions keep plain names: onRun, onSelect, onDismiss.

Words

  • tone is semantic colour: neutral, accent, success, warning or danger. It is set once as data-tone, and the stylesheet turns it into --sl-tone, --sl-tone-soft and --sl-tone-text.
  • variant is emphasis or shape: a primary or ghost button, a bar or pixel meter.
  • size is sm, md or lg, steps on the control-height scale. A root's density moves the whole scale.
  • label is only ever the accessible name, a plain string. Visible text is children, title or a named slot.

Composition

Uniform content takes a data array (items, options, columns), which is short and hard to get wrong. Rich content takes compound parts, Tabs.List, Menu.Item and the like, which accept any children. Where both make sense, the array is shorthand for the parts. asChild swaps a component's element for its single child, so a Button can be a link or a router's Link without losing its look, focus ring or handlers.

Forms

Controls drawn with buttons rather than native inputs (Switch, Segmented, CodeInput, Calendar, Rating, ToggleGroup) render a hidden input when given a name, so they submit with a plain form. A Switch submits only while it is on, as a checkbox does. Controls built on a native input (Checkbox, RadioGroup, FileDrop, Slider) pass name to it, and their ref and attributes reach that input; className and style go on the visible wrapper.

Fields

A Field wires a label, a description and an error to the control inside it, so you never pass ids by hand. Every form control reads it: the control takes the field's id (or keeps its own, which the label then follows), aria-describedby lists the description and, while the field is invalid, the error, and the field's invalid, disabled, required and readOnly reach the control. A prop set on the control itself wins. Group controls (CheckboxGroup, RadioGroup, Rating, ToggleGroup, ThemeSelector) are named by the field's label through aria-labelledby, and the items inside them leave the field alone.

On and off

checked is for controls that are a checkbox underneath: Switch and Checkbox. A ToggleButton is a button that stays pressed, so its state is pressed, defaultPressed and onPressedChange, after the aria-pressed it sets, and a selectable Chip's is selected. Groups of either keep their values as an array: value is the list of pressed or checked items.

Data attributes

Modifiers are data attributes, never extra class names. Booleans are present or absent (data-open=""), and the stylesheet tests presence, so [data-open] works on any element Slant renders.

Strings and direction

Slant writes a few strings itself: "Dismiss", "Previous month", the command bar's hints. They come from the root's messages, so <Slant messages={{ dismiss: 'Fermer' }}> translates them; a prop such as dismissLabel still wins for one instance. <Slant dir="rtl"> mirrors the layout. The stylesheet uses logical properties throughout, and arrow keys swap left and right.

Overlays

Anything that opens over the page follows the same rules. Anchored layers (Menu, Popover, Tooltip, ContextMenu) render in a portal at the end of the body that carries the root's theme, direction and density, so no overflow or stacking context clips them; they flip to the other side when theirs lacks room and slide along it to stay in view. Modal layers (Dialog, Drawer) are native <dialog> elements opened with showModal(), in the browser's top layer with the rest of the page inert, and an anchored layer opened inside one renders inside it. Escape closes only the topmost layer, and closing puts focus back where it was before opening.

Styles

The whole stylesheet sits in @layer slant. Any rule of yours outside a layer wins over it, whatever its specificity, so overriding Slant never needs !important. The other side of that: keep your own element resets, such as button { color: inherit }, in a layer declared before Slant's (@layer base, slant;), or they restyle Slant's buttons too. Slant styles only its root and its own sl-* classes, never bare elements, and never the content you pass in.

New components

A new component follows all of the above, documents every prop with a JSDoc comment (the API tables on these pages are generated from them), and ships with a story whose play function exercises its keyboard behaviour and passes axe in every theme.