# Toolbar

> Toolbar groups buttons, menu buttons and inputs into one APG toolbar with a single tab stop, arrow-key navigation, start / center / end alignment, and overflow into a menu, a scrolling strip or extra rows.

Canonical: https://salmonui.dev/docs/navigation/toolbar

```tsx
import { Button, ToggleButton } from '@salmonui/buttons/button';
import { MenuButton } from '@salmonui/buttons/menu';
import { Toolbar, ToolbarGroup, ToolbarItem, ToolbarSeparator, ToolbarSpacer } from '@salmonui/navigation/toolbar';
import '@salmonui/buttons/button/styles.css';
import '@salmonui/buttons/menu/styles.css'; // the overflow menu
import '@salmonui/navigation/toolbar/styles.css';

<Toolbar aria-label="Formatting">
  <ToggleButton aria-label="Bold" iconOnly><BoldIcon /></ToggleButton>
  <ToggleButton aria-label="Italic" iconOnly><ItalicIcon /></ToggleButton>
  <ToolbarSeparator />
  <ToolbarItem label="Font size">
    <input aria-label="Font size" type="number" defaultValue={14} />
  </ToolbarItem>
  <ToolbarSpacer />
  <ToolbarGroup align="end">
    <Button onClick={save}>Save</Button>
    <MenuButton items={moreItems}>More</MenuButton>
  </ToolbarGroup>
</Toolbar>
```

`Toolbar` takes children instead of an `items` array. Any child works:
Buttons family controls (`Button`, `ToggleButton`, `ToggleButtonGroup`,
`MenuButton`, `SplitButton`), inputs, links or your own content. Each direct
child that is not a toolbar part is wrapped in a measured `ToolbarItem`
automatically; wrap a child yourself to give it a menu `label` / `icon` or
overflow settings.

## Items

| Item need | Salmon UI |
| --- | --- |
| A button with text and icons | A `Button` with its children and `iconStart` / `iconEnd` |
| A separator | `ToolbarSeparator` (`role="separator"`, perpendicular to the bar) |
| An input or custom content | Any content inside `ToolbarItem` |
| Start, center or end alignment | `ToolbarGroup align="start" \| "center" \| "end"`, or `ToolbarSpacer` |
| Disabled or hidden items | The child's own `disabled`; render the child conditionally |
| A tooltip | The child's `title`, or a [Tooltip](https://salmonui.dev/docs/layout/tooltip) around the child |
| Tab order | Not needed: the toolbar is one tab stop (see below) |
| A click handler | The child's own `onClick` |

`ToolbarItem` props: `label` and `icon` (the overflow menu entry),
`overflow: 'auto' | 'never' | 'always'` and `overflowPriority`, `sizes`
(see [Multi-size items](https://salmonui.dev/docs/navigation/toolbar#multi-size-items)), plus `className`, `style` and
any `div` attribute. Its child can be a function of the item's current size.

## Groups

`ToolbarGroup` keeps a run of items together and aligns it (`align`). With
`label` it shows a visible label under its items and becomes a
`role="group"` named by that label (pass `aria-labelledby` to name it
otherwise); `action` puts a control next to the label, typically a small
icon-only `Button` that opens the group's settings (a launcher). The action
is a toolbar stop after the group's items and never overflows; a labelled
group whose items have all overflowed is hidden. This is how the
[Ribbon](https://salmonui.dev/docs/navigation/ribbon) builds its groups.

## Multi-size items

`sizes` lists the sizes an item may take, largest first, from
`'lg' | 'md' | 'sm'`. The item renders `sizes[0]` on the server and until the
toolbar has measured; the current size is `data-size` on the item and the
argument of a function child:

```tsx
<ToolbarItem label="Paste" sizes={['lg', 'md', 'sm']} overflowPriority={2}>
  {(size) =>
    size === 'sm' ? (
      <Button iconOnly aria-label="Paste"><PasteIcon /></Button>
    ) : (
      <Button size={size === 'lg' ? 'md' : 'sm'} iconStart={<PasteIcon />}>Paste</Button>
    )
  }
</ToolbarItem>
```

With `overflow="menu"`, when the items do not fit, the item with the lowest
`overflowPriority` (ties: the last one) steps down one size at a time until
it reaches its last size, then the next item; items move into the overflow
menu only when every multi-size item is at its last size. A one-entry list
fixes the size. `overflow="scroll"` and `"wrap"` keep `sizes[0]`. In a
`'lg'` item the toolbar stacks a `Button`'s icon over its label.

Widths are measured once per size and cached; the stepping code is loaded
only when an item declares more than one size, so other toolbars do not pay
for it.

Render `ToolbarItem` directly inside the toolbar or group (or call a helper
function that returns it): a component of your own that returns a
`ToolbarItem` is wrapped in a second, measured item, because every direct
child that is not a toolbar part is wrapped.

## Overflow

`overflow` decides what happens when the items do not fit:

- `'menu'` (default): items that do not fit are hidden and listed in a menu
  behind a "more" button at the end of the bar (items of a labelled group
  under the group's label). Items leave from the end;
  a higher `overflowPriority` keeps an item longer. `overflow="never"` keeps
  an item in the bar (form fields default to it, because an input cannot be
  a menu entry), `overflow="always"` puts it in the menu at every width.
  An overflowed control stays in the DOM with `hidden`, keeping its state:
  choosing its menu entry calls `click()` on it, so one `onClick` handles
  both places. The entry shows `ToolbarItem label` / `icon`, else the
  control's `aria-label`, `title` or text. An icon-only button with
  `aria-label` therefore shows as icon in the bar and as text in the menu.
- `'scroll'`: the items scroll in one line; scroll buttons appear at the
  ends while there is more to see and move by `scrollStep` px (default
  120). Touch and trackpads scroll natively; keyboard focus scrolls the
  focused item into view.
- `'wrap'`: items wrap onto more rows.

The overflow is measured with one shared `ResizeObserver` per document:
item sizes are cached, each resize is a read phase only, and React commits
only when the set of hidden items changes. The server and the first client
render show every item; hiding happens after mount by setting `hidden`, so
the hydrated markup never differs. The measuring code and the overflow menu
are loaded on demand, so `@salmonui/navigation/toolbar` stays small.

The overflow menu is the Menu family's `Menu` in a collision-aware popup that
flips and shifts to stay in the viewport. Import
`@salmonui/buttons/menu/styles.css` when you use `overflow="menu"`.

A `MenuButton` child that overflows would open its menu from a hidden
button; give it `overflow="never"` (or a higher `overflowPriority`).

## Keyboard and ARIA

The toolbar follows the WAI-ARIA Authoring Practices Toolbar pattern:
`role="toolbar"`, `aria-orientation`, and an accessible name through
`aria-label` or `aria-labelledby`.

| Key | Action |
| --- | --- |
| Tab / Shift+Tab | Enter the toolbar on the control focused last (the first one the first time); the next Tab leaves the toolbar |
| ArrowRight / ArrowLeft | Next / previous control, wrapping (mirrored under `dir="rtl"`); ArrowDown / ArrowUp with `orientation="vertical"` |
| Home / End | First / last control |
| ArrowDown (horizontal), ArrowRight (vertical), Enter, Space on "more" | Open the overflow menu on its first entry; Escape closes it and returns focus |

Inside a text input, Home / End and the arrows move the caret; ArrowLeft /
ArrowRight leave the input only at the start or end of its text. Selects,
textareas, sliders and number inputs keep every arrow key. Disabled buttons
are skipped. A nested `ToggleButtonGroup` keeps its own arrow keys.

`onKeyDown` runs before the toolbar's keys; call `event.preventDefault()`
to handle a key yourself.

`disabled` disables every control at once (the content is a disabled
`<fieldset>`) and sets `aria-disabled="true"` on the toolbar.

## Orientation

`orientation="vertical"` stacks the items, measures overflow along the block
axis and moves focus with ArrowUp / ArrowDown. Give a vertical toolbar a
height for `overflow="menu"` to have an effect.

## Locale

`localeText` overrides the strings in `TOOLBAR_DEFAULT_LOCALE_TEXT`:
`more` (the overflow button name), `scrollBack` and `scrollForward` (scroll
button titles). `moreIcon` replaces the three-dot icon.

## Styling

`.salmon-toolbar` with `data-orientation`, `data-overflow`,
`data-disabled`, and while scrolling `data-scrollable`, `data-at-start`,
`data-at-end`. Parts: `.salmon-toolbar-content`, `.salmon-toolbar-item` (`data-size`),
`.salmon-toolbar-group` (`data-align`, `data-labelled`),
`.salmon-toolbar-group-items`, `.salmon-toolbar-group-label`,
`.salmon-toolbar-spacer`,
`.salmon-toolbar-separator`, `.salmon-toolbar-more` (`data-open`),
`.salmon-toolbar-scroll-button` (`data-scroll="back" | "forward"`) and
`.salmon-toolbar-menu`. `className` and `style` pass through on every part.
Colors come from the theme variables; layout uses logical properties only.

For an application bar, see the [App Bar](https://salmonui.dev/docs/navigation/app-bar).

## State

The toolbar has no selection or open state of its own, so there is no
controlled / uncontrolled pair: each child keeps its own (a controlled
`ToggleButton pressed`, an input `value`). The overflow is derived from the
measured width and needs no prop.

## Server rendering

The server and the first client render show every item; overflowed items
are hidden only after mount, by setting `hidden`, so hydration never sees a
different tree. Multi-size items render `sizes[0]` until the
toolbar has measured.

## Not included

- An overflow mode that expands a second row on demand is not offered:
it would double the layout modes for one rarely used case. Use
  `overflow="menu"`, giving inputs `ToolbarItem overflow="never"`, or
  `overflow="wrap"` to keep every item visible.
- Per-item tab order is not offered: the APG toolbar has one tab
  stop with roving focus, which a per-item `tabIndex` would break.
- Turning off keyboard navigation is not offered: the APG keys are
  the accessible contract and are always on. To suppress one key, call
  `event.preventDefault()` in the toolbar's `onKeyDown`.
- Imperative methods that add, remove, enable or hide items, or refresh the
  overflow, are not offered: the children belong to the
  application. Change the children and their props
  (`disabled`, conditional rendering); the overflow re-measures on resize
  and on child changes by itself.

## API reference

[`@salmonui/navigation/toolbar`](https://salmonui.dev/docs/reference/navigation/toolbar): `Toolbar`,
`ToolbarItem`, `ToolbarGroup`, `ToolbarSeparator`, `ToolbarSpacer`,
`TOOLBAR_DEFAULT_LOCALE_TEXT` and their types.

### Live demo: Toolbar

Live demo: https://salmonui.dev/demos/navigation/toolbar-basics

A formatting toolbar with toggle buttons, a separator, a number input, a spacer and an end-aligned group with a button and a menu button.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Button, ToggleButton } from '@salmonui/buttons/button';
import { MenuButton } from '@salmonui/buttons/menu';
import { NumberField } from '@salmonui/inputs/number-field';
import { Toolbar, ToolbarGroup, ToolbarItem, ToolbarSeparator, ToolbarSpacer } from '@salmonui/navigation/toolbar';

const icon = (d: string) => (
  <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d={d} />
  </svg>
);

export default function ToolbarBasicsDemo() {
  const [bold, setBold] = useState(false);
  const [italic, setItalic] = useState(false);
  const [last, setLast] = useState('nothing yet');
  return (
    <div>
      <Toolbar aria-label="Formatting" overflow="wrap">
        <ToggleButton aria-label="Bold" iconOnly pressed={bold} onPressedChange={setBold}>
          {icon('M5 3h4a2.5 2.5 0 0 1 0 5H5zM5 8h5a2.5 2.5 0 0 1 0 5H5z')}
        </ToggleButton>
        <ToggleButton aria-label="Italic" iconOnly pressed={italic} onPressedChange={setItalic}>
          {icon('M10 3H7M9 13H6M9 3 7 13')}
        </ToggleButton>
        <ToolbarSeparator />
        <ToolbarItem label="Font size">
          <NumberField aria-label="Font size" defaultValue={14} min={8} max={72} style={{ inlineSize: 96 }} />
        </ToolbarItem>
        <ToolbarSpacer />
        <ToolbarGroup align="end" aria-label="Document">
          <Button title="Save the document" onClick={() => setLast('Save')}>
            Save
          </Button>
          <MenuButton
            items={[
              { id: 'export', label: 'Export…' },
              { id: 'print', label: 'Print' },
            ]}
            onAction={(event) => setLast(event.id)}
          >
            More
          </MenuButton>
        </ToolbarGroup>
      </Toolbar>
      <p className="demo-status">
        Bold {bold ? 'on' : 'off'}, italic {italic ? 'on' : 'off'}. Last action: {last}
      </p>
    </div>
  );
}
```

### Live demo: Toolbar overflow

Live demo: https://salmonui.dev/demos/navigation/toolbar-overflow

One toolbar switching between overflow into a menu, a scrolling strip and extra rows, with a priority item and an item that always lives in the menu.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Select } from '@salmonui/dropdowns/select';
import { Slider } from '@salmonui/inputs/slider';
import { Toolbar, ToolbarItem, ToolbarSeparator, type ToolbarOverflow } from '@salmonui/navigation/toolbar';

const MODES = ['menu', 'scroll', 'wrap'];
const actions = ['Cut', 'Copy', 'Paste', 'Undo', 'Redo', 'Find', 'Replace', 'Print'];

export default function ToolbarOverflowDemo() {
  const [mode, setMode] = useState<ToolbarOverflow>('menu');
  const [width, setWidth] = useState(360);
  const [last, setLast] = useState('nothing yet');
  return (
    <div>
      <div className="demo-toolbar">
        <label>
          Overflow{' '}
          <Select aria-label="Overflow" data={MODES} value={mode} onValueChange={(value) => value != null && setMode(value as ToolbarOverflow)} />
        </label>
        {/* Inline like the Overflow label, so the row's controls share one axis. */}
        <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <label>
            Width
            <Slider min={160} max={720} value={width} onValueChange={setWidth} style={{ inlineSize: 200 }} />
          </label>
          <output>{width}px</output>
        </span>
      </div>
      <Toolbar aria-label="Edit" overflow={mode} scrollStep={100} style={{ inlineSize: width, maxInlineSize: '100%' }}>
        {actions.flatMap((name, index) => [
          index === 3 ? <ToolbarSeparator key="sep" /> : null,
          <ToolbarItem key={name} overflowPriority={name === 'Find' ? 1 : 0}>
            <Button onClick={() => setLast(name)}>{name}</Button>
          </ToolbarItem>,
        ])}
        <ToolbarItem overflow="always">
          <Button onClick={() => setLast('Settings')}>Settings</Button>
        </ToolbarItem>
      </Toolbar>
      <p className="demo-status">Last action: {last}</p>
    </div>
  );
}
```

### Live demo: Toolbar states

Live demo: https://salmonui.dev/demos/navigation/toolbar-states

A paragraph toolbar with icon-only buttons that read as text in the overflow menu, a disabled item, a conditionally rendered item, a native onKeyDown that can block roving keys, the whole-toolbar disabled flag and vertical orientation.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Slider } from '@salmonui/inputs/slider';
import { Toolbar, ToolbarItem, ToolbarSeparator } from '@salmonui/navigation/toolbar';

const icon = (d: string) => (
  <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d={d} />
  </svg>
);

const tools = [
  { name: 'Align left', d: 'M2 4h12M2 8h8M2 12h10' },
  { name: 'Align center', d: 'M2 4h12M4 8h8M3 12h10' },
  { name: 'Align right', d: 'M2 4h12M6 8h8M4 12h10' },
  { name: 'Bulleted list', d: 'M6 4h8M6 8h8M6 12h8M2.5 4h.5M2.5 8h.5M2.5 12h.5' },
  { name: 'Insert link', d: 'M7 9a3 3 0 0 0 4 0l2-2a3 3 0 0 0-4-4L8 4M9 7a3 3 0 0 0-4 0L3 9a3 3 0 0 0 4 4l1-1' },
];

export default function ToolbarStatesDemo() {
  const [vertical, setVertical] = useState(false);
  const [disabled, setDisabled] = useState(false);
  const [showPrint, setShowPrint] = useState(true);
  const [blockHome, setBlockHome] = useState(false);
  const [width, setWidth] = useState(200);
  const [log, setLog] = useState<readonly string[]>([]);
  const add = (entry: string) => setLog((list) => [entry, ...list].slice(0, 5));
  return (
    <div>
      <div className="demo-toolbar">
        <Checkbox checked={vertical} onCheckedChange={(checked) => setVertical(checked)}>
          Vertical
        </Checkbox>
        <Checkbox checked={disabled} onCheckedChange={(checked) => setDisabled(checked)}>
          Disabled
        </Checkbox>
        <Checkbox checked={showPrint} onCheckedChange={(checked) => setShowPrint(checked)}>
          Show Print
        </Checkbox>
        <Checkbox checked={blockHome} onCheckedChange={(checked) => setBlockHome(checked)}>
          Block Home and End
        </Checkbox>
        {/* Inline like the checkboxes, so the row's controls share one axis. */}
        <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <label>
            Width
            <Slider min={120} max={480} value={width} disabled={vertical} onValueChange={setWidth} style={{ inlineSize: 200 }} />
          </label>
          <output>{width}px</output>
        </span>
      </div>
      {/* Pushed to the inline end so the overflow menu has to shift to stay in the viewport. */}
      <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
        <Toolbar
          aria-label="Paragraph"
          orientation={vertical ? 'vertical' : 'horizontal'}
          overflow={vertical ? 'wrap' : 'menu'}
          disabled={disabled}
          style={vertical ? undefined : { inlineSize: width, maxInlineSize: '100%' }}
          onKeyDown={(event) => {
            if (blockHome && (event.key === 'Home' || event.key === 'End')) {
              event.preventDefault();
              add(`${event.key} blocked by onKeyDown`);
            } else {
              add(`key ${event.key}`);
            }
          }}
        >
          {tools.map((tool) => (
            <Button key={tool.name} aria-label={tool.name} iconOnly onClick={() => add(tool.name)}>
              {icon(tool.d)}
            </Button>
          ))}
          <ToolbarSeparator />
          <Button disabled onClick={() => add('Undo')}>
            Undo
          </Button>
          {showPrint ? <Button onClick={() => add('Print')}>Print</Button> : null}
        </Toolbar>
      </div>
      <div className="demo-log" aria-live="polite">
        {log.length === 0 ? (
          <span style={{ color: 'var(--salmon-text-secondary)' }}>No events yet.</span>
        ) : (
          <ol>
            {log.map((entry, index) => (
              <li key={`${index}-${entry}`}>{entry}</li>
            ))}
          </ol>
        )}
      </div>
    </div>
  );
}
```
