# Toolbar

A row of controls with one tab stop: Tab reaches the toolbar, the arrow keys move between its buttons, Home and End jump to the ends. Built on ui-sdk's Toolbar. Icon buttons show their label as a tooltip; pressed makes a button a toggle. The Rich text field's toolbar is this one.

- Beta: ready to use; its API can still change in a minor release.
- Category: Actions

```tsx
import { Toolbar, ToolbarButton, ToolbarGroup, ToolbarSeparator } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for a row of related commands that should be one tab stop, like the rich text field's formatting buttons.
- Do: Name it with `aria-label`, like Formatting, and give every `ToolbarButton` a `label`. An icon-only button shows its label as a tooltip.
- Do: Pass `shortcut` when the command has one, like `mod+b` for Bold, so the tooltip shows it.
- Do: Split related commands with `ToolbarGroup` and `ToolbarSeparator`, like text styles and lists. Set `pressed` on a button that toggles, like Bold.
- Don't: Use it for two or three standalone actions, like Export as CSV or PDF. Use `ButtonGroup`, where each button is its own tab stop.

## Keyboard

| Keys | What they do |
| --- | --- |
| Tab | Lands on the toolbar once, on the item focused last, and Tab again leaves it. |
| ArrowRight, ArrowLeft | Move through every item, wrapping at the ends. |
| Enter, Space | Press the focused item. Arrow keys never press a toggle. |

## Related

- [Button Group](./button-group.md)
- [Segmented Control](./segmented-control.md)
- [Rich Text Field](./rich-text-field.md)

## Examples

What the examples import:

```tsx
import { AlignCenter, AlignLeft, AlignRight, Bold, Download, Italic, Printer, Underline } from "lucide-react";
import { useState } from "react";
import { Toolbar, ToolbarButton, ToolbarGroup, ToolbarSeparator } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const alignments = [
  { value: "left", icon: <AlignLeft /> },
  { value: "center", icon: <AlignCenter /> },
  { value: "right", icon: <AlignRight /> },
];

function Formatting() {
  const [marks, setMarks] = useState<string[]>(["bold"]);
  const [align, setAlign] = useState("left");
  const toggle = (mark: string) =>
    setMarks(current =>
      current.includes(mark)
        ? current.filter(m => m !== mark)
        : [...current, mark],
    );
  return (
    <Toolbar
      aria-label="Formatting"
      className="border-neutral-border w-fit rounded-md border p-1"
    >
      <ToolbarGroup aria-label="Text style">
        <ToolbarButton
          icon={<Bold />}
          label="Bold"
          shortcut="mod+b"
          pressed={marks.includes("bold")}
          onClick={() => toggle("bold")}
        />
        <ToolbarButton
          icon={<Italic />}
          label="Italic"
          shortcut="mod+i"
          pressed={marks.includes("italic")}
          onClick={() => toggle("italic")}
        />
        <ToolbarButton
          icon={<Underline />}
          label="Underline"
          shortcut="mod+u"
          pressed={marks.includes("underline")}
          onClick={() => toggle("underline")}
        />
      </ToolbarGroup>
      <ToolbarSeparator />
      <ToolbarGroup aria-label="Alignment">
        {alignments.map(({ value, icon }) => (
          <ToolbarButton
            key={value}
            icon={icon}
            label={`Align ${value}`}
            pressed={align === value}
            onClick={() => setAlign(value)}
          />
        ))}
      </ToolbarGroup>
    </Toolbar>
  );
}
```

### Formatting

Two groups of toggles split by a rule. Tab in, then use the arrow keys; Bold, Italic and Underline toggle.

```tsx
<Formatting />
```

### With text

A button with children shows its text and needs no tooltip.

```tsx
<Toolbar aria-label="Document">
  <ToolbarButton icon={<Download />} label="Download">
    Download
  </ToolbarButton>
  <ToolbarButton icon={<Printer />} label="Print">
    Print
  </ToolbarButton>
  <ToolbarSeparator />
  <ToolbarButton icon={<Printer />} label="Print preview" disabled>
    Preview
  </ToolbarButton>
</Toolbar>
```

## Props

### Toolbar

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` (required) | `string` |  | Names the toolbar, like "Formatting". |
| `children` | `ReactNode` |  | What it shows inside. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### ToolbarButton

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `icon` | `ReactNode` |  | The 14 px icon. With no children, the button is icon only. |
| `label` (required) | `string` |  | Names the button. An icon-only button also shows it as its tooltip. |
| `children` | `ReactNode` |  | Visible text after the icon. |
| `pressed` | `boolean` |  | Makes it a toggle that stays pressed, like Bold. |
| `shortcut` | `string` |  | A keyboard shortcut for the tooltip, like "mod+b". |
| `onClick` | `() => void` |  | Runs the command. |
| `disabled` | `boolean` |  | Shows it as unavailable and stops it responding. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### ToolbarGroup

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### ToolbarSeparator

No props of its own.
