# Toolbar

A row of controls that act on one thing, such as the formatting buttons above an editor or the actions above a canvas. It holds Buttons, Toggles and ToggleGroups and takes one Tab stop; the arrow keys move between its controls.

**Dependencies:** npm: 0 direct, 1 total; registry: 3 direct, 3 total.

## Usage

### When to use

- Group the controls that act on one thing, such as text formatting above an editor or tools beside a canvas.
- Give a row of icon buttons one Tab stop, so keyboard users move through it with the arrow keys.

### When not to use

- Pick one option from a few that stays selected. Use ToggleGroup.
- Move between pages or sections. Use Tabs.
- Offer actions for one item in a list. Use DropdownMenu.

## Variants

### orientation

horizontal: a row, moved through with Left and Right. vertical: a column, moved through with Up and Down.

Values: `horizontal`, `vertical`. Default: `horizontal`.

### dir

Writing direction of the arrow keys, read from the page when left out. RTL reverses Left and Right.

Values: `ltr`, `rtl`. Default: `ltr`.

## Examples

### Text formatting

A ToggleGroup for the marks, separate buttons for undo and redo. One Tab stop for the whole row.

```tsx
<Toolbar aria-label="Text formatting">
  <ToggleGroup type="multiple" aria-label="Marks" size="sm" className="min-w-0 max-w-full flex-wrap gap-y-[max(var(--spacing-xs),calc(var(--size-target-min)-var(--size-element-sm)))]">
    <ToggleGroupItem value="bold" aria-label="Bold" iconOnly>
      <IconBold size={16} />
    </ToggleGroupItem>
    <ToggleGroupItem value="italic" aria-label="Italic" iconOnly>
      <IconItalic size={16} />
    </ToggleGroupItem>
    <ToggleGroupItem value="underline" aria-label="Underline" iconOnly>
      <IconUnderline size={16} />
    </ToggleGroupItem>
  </ToggleGroup>
  <ToolbarSeparator />
  <Button variant="ghost" size="sm" iconOnly aria-label="Undo">
    <IconArrowBackUp size={16} />
  </Button>
  <Button variant="ghost" size="sm" iconOnly aria-label="Redo">
    <IconArrowForwardUp size={16} />
  </Button>
</Toolbar>
```

### Vertical

A column of tools beside a canvas, moved through with Up and Down.

```tsx
<Toolbar aria-label="Drawing tools" orientation="vertical">
  <ToggleGroup type="single" defaultValue="select" aria-label="Tool" orientation="vertical" size="sm" className="min-w-0 max-w-full flex-wrap gap-y-[max(var(--spacing-xs),calc(var(--size-target-min)-var(--size-element-sm)))]">
    <ToggleGroupItem value="select" aria-label="Select" iconOnly>
      <IconPointer size={16} />
    </ToggleGroupItem>
    <ToggleGroupItem value="pen" aria-label="Pen" iconOnly>
      <IconPencil size={16} />
    </ToggleGroupItem>
    <ToggleGroupItem value="shape" aria-label="Shape" iconOnly>
      <IconSquare size={16} />
    </ToggleGroupItem>
  </ToggleGroup>
  <ToolbarSeparator />
  <Button variant="ghost" size="sm" iconOnly aria-label="Delete">
    <IconTrash size={16} />
  </Button>
</Toolbar>
```

## Do and don't

### Do

- Give every icon-only control a name and a Tooltip.
- Group related controls with ToolbarGroup and split the groups with ToolbarSeparator.
- Use one size for every control in the toolbar, sm in dense editors.

### Don't

- Do not put a long form in a toolbar. Use a Card or a Dialog.
- Do not use a toolbar for page navigation. Use Tabs or Navbar.
- Do not nest one toolbar inside another.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `orientation` | `enum` | `horizontal`, `vertical` | `horizontal` | horizontal: a row, moved through with Left and Right. vertical: a column, moved through with Up and Down. |
| `loop` | `boolean` |  | `true` | Wraps from the last control to the first and back. |
| `dir` | `enum` | `ltr`, `rtl` | `ltr` | Writing direction of the arrow keys, read from the page when left out. RTL reverses Left and Right. |

### Slots

| Name | Required | Description |
| --- | --- | --- |
| `controls` | yes | Buttons, Toggles, ToggleGroups, ToolbarGroups and ToolbarSeparators, in reading order. Use size sm for a dense toolbar. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The toolbar: a row (or column) of controls with gap-xs. It sizes to its content. |
| `group` | ToolbarGroup: a named cluster of controls with the same gap. |
| `separator` | ToolbarSeparator: a 1px line in the divider color that runs across the toolbar, with spacing-2xs on each side. |

## Accessibility

Role: `toolbar`

| Keys | Action |
| --- | --- |
| `Tab` | Enters the toolbar at the control used last, then leaves it. |
| `ArrowLeft, ArrowRight` | In a horizontal toolbar, moves to the previous or next control, follows the writing direction, skips disabled controls and wraps at the ends. |
| `ArrowUp, ArrowDown` | Does the same in a vertical toolbar. |
| `Home, End` | Moves to the first or last control. |
| `Space, Enter` | Presses the focused control. |

- A toolbar requires aria-label or aria-labelledby.
- Only one control has tabIndex 0: the one focused last. The rest have -1, so Tab leaves the toolbar in one step.
- A ToggleGroup inside leaves its arrow keys to the toolbar, so the whole row stays one Tab stop.
- A text field inside keeps its own arrow keys; Tab moves out of it.
- ToolbarGroup is a named group of controls; ToolbarSeparator is a separator with aria-orientation across the toolbar.

## Install

```bash
npx werewolf-ui add https://www.werewolf-ui.dev/r/toolbar.json
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.
