Toolbar

Slant / Editor

Tools, actions and separators in one tab stop, with one tool chosen at a time.

Demo

Usage

@tuttn/slant
import { ArrowUpRightIcon, GridIcon, PencilIcon, SettingsIcon, Toolbar, TrashIcon, UndoIcon } from '@tuttn/slant'

const tools = [
  { value: 'select', label: 'Select', icon: <ArrowUpRightIcon />, shortcut: 'V' },
  { value: 'draw', label: 'Draw', icon: <PencilIcon />, shortcut: 'P' },
]

// Uncontrolled: starts at defaultValue (the first enabled tool when omitted).
<Toolbar label="Canvas tools" tools={tools} defaultValue="draw" />

// Controlled, with actions after the tools, floating over a canvas.
<Toolbar label="Canvas tools" tools={tools} value={tool} onValueChange={setTool} variant="floating">
  <Toolbar.Separator />
  <Toolbar.Button label="Undo" icon={<UndoIcon />} onClick={undo} />
  <Toolbar.Button label="Snap to grid" icon={<GridIcon />} pressed={snap} onClick={toggleSnap} />
  <Toolbar.Button label="Delete" icon={<TrashIcon />} tone="danger" onClick={remove} />
</Toolbar>

// The compound form, here as a column of tools with visible text.
<Toolbar label="Transform" orientation="vertical" defaultValue="move">
  <Toolbar.Tools>
    <Toolbar.Tool value="move" label="Move" shortcut="M">Move</Toolbar.Tool>
    <Toolbar.Tool value="scale" label="Scale" shortcut="S">Scale</Toolbar.Tool>
  </Toolbar.Tools>
  <Toolbar.Separator />
  <Toolbar.Button label="Settings" icon={<SettingsIcon />} />
</Toolbar>

Props

PropTypeDefaultDescription
labelstring—The toolbar's accessible name.
orientation'horizontal' | 'vertical'—A row of items (Left and Right move, swapped under RTL) or a column (Up and Down move).
valuestring—The chosen tool, when controlled.
defaultValuestringthe first enabled toolThe tool chosen first, when uncontrolled.
onValueChange(value: string) => void—Called with the tool's value when Enter, Space or a click chooses another tool.
toolsreadonly ToolbarItem[]—Shorthand for uniform tools: a `Toolbar.Tools` of these, rendered first, before the children (actions, separators, more tools).
size'sm' | 'md'—The items' height, and width when they hold only an icon, on the control scale.
variant'framed' | 'floating'—`framed` sits on the surface with a hairline border; `floating` drops the border for a shadow, to sit over a canvas.
Toolbar.Tools labelstringmessages.toolsThe radiogroup's accessible name.
Toolbar.Tool valuestring—What choosing the tool sets the toolbar's value to.
Toolbar.Tool labelstring—The tool's name: its aria-label when it has no visible text, and its tooltip.
Toolbar.Tool iconReactNode—The tool's icon.
Toolbar.Tool shortcutstring—The key that picks the tool, shown in the tooltip and given as aria-keyshortcuts. The app listens for it.
Toolbar.Tool disabledboolean—Greys the tool out; clicks and the arrow keys pass over it.
Toolbar.Button labelstring—The action's name: its aria-label when it has no visible text, and its tooltip. Icon-only buttons need it.
Toolbar.Button iconReactNode—The action's icon.
Toolbar.Button toneTone—Colours the icon: danger for a destructive action, say.
Toolbar.Button pressedboolean—Makes the button a toggle: aria-pressed, and the pressed look while true.
…restHTMLAttributes—className, style, other attributes and the ref go on each part's own element: the toolbar div, the radiogroup div, the tool and action buttons, and the separator div.

Keyboard

The whole toolbar is one tab stop: the item focused last, or before that the chosen tool (the first enabled item when no tool is chosen). The arrows move focus across tools and actions alike, Left and Right in a row (swapped under right-to-left text), Up and Down in a column; Home and End jump to the ends, the ends wrap, and disabled items are skipped. Moving doesn't choose: Enter, Space or a click chooses a tool, as in the APG toolbar pattern. A tool's shortcut is shown and announced only; the app binds the key.

Pointer

A click chooses a tool or runs an action. The selection pill slides to the chosen tool and follows it whenever a tool changes size.

Screen readers

The root is a toolbar named by label, with its orientation. The tools are a radiogroup (named "Tools" unless given a label) of radios, each named by its label and described by its tooltip with the shortcut. Actions are buttons, toggles carry aria-pressed, and separators run across the toolbar.

Reduced motion

The pill jumps instead of sliding. Before its first measurement, on the server and in the first frame, the chosen tool draws its own pill, so the choice shows either way.