# Toolbar
A row of controls with one tab stop and arrow-key focus.
Source page: https://dinachi.dev/docs/components/toolbar

## Installation
```bash
npx @dinachi/cli@latest add toolbar
```
Adds to your project:
- `components/ui/toolbar/toolbar.tsx`
- `components/ui/toolbar/index.ts`
Installs:
- `@base-ui/react` ^1.9.0

## Usage
```tsx
import { Toolbar, ToolbarButton, ToolbarGroup, ToolbarSeparator } from "@/components/ui/toolbar"

export function FormatBar() {
  return (
    <Toolbar aria-label="Formatting">
      <ToolbarGroup>
        <ToolbarButton>Bold</ToolbarButton>
        <ToolbarButton>Italic</ToolbarButton>
      </ToolbarGroup>
      <ToolbarSeparator />
      <ToolbarButton>Clear</ToolbarButton>
    </Toolbar>
  )
}
```

## Anatomy
```tsx
<Toolbar>
  <ToolbarGroup>
    <ToolbarButton />
  </ToolbarGroup>
  <ToolbarSeparator />
  <ToolbarLink />
  <ToolbarInput />
</Toolbar>
```
- `Toolbar`: The container. Also `ToolbarRoot`, `Toolbar.Button`, etc.
- `ToolbarButton`: A button item; `render` swaps the control.
- `ToolbarLink`: A link item.
- `ToolbarInput`: A text input in toolbar focus.
- `ToolbarGroup`: Groups related items.
- `ToolbarSeparator`: A line between groups.

## Examples

### Default
Buttons in groups, separators and a link.
```tsx
import { Toolbar, ToolbarButton, ToolbarSeparator, ToolbarGroup, ToolbarLink } from '@/components/ui/toolbar';
import { Bold, Italic, Underline, Link, AlignLeft, AlignCenter, AlignRight } from 'lucide-react';

export function Example() {
  return (
    <Toolbar>
      <ToolbarGroup>
        <ToolbarButton aria-label="Bold">
          <Bold className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton aria-label="Italic">
          <Italic className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton aria-label="Underline">
          <Underline className="h-4 w-4" />
        </ToolbarButton>
      </ToolbarGroup>
      <ToolbarSeparator />
      <ToolbarGroup>
        <ToolbarButton aria-label="Align left">
          <AlignLeft className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton aria-label="Align center">
          <AlignCenter className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton aria-label="Align right">
          <AlignRight className="h-4 w-4" />
        </ToolbarButton>
      </ToolbarGroup>
      <ToolbarSeparator />
      <ToolbarLink href="#">
        <Link className="mr-1 h-4 w-4" />
        Insert Link
      </ToolbarLink>
    </Toolbar>
  );
}
```

### With toggles
Toggles and toggle groups rendered through ToolbarButton.
```tsx
import { Toolbar, ToolbarButton, ToolbarSeparator } from '@/components/ui/toolbar';
import { Toggle } from '@/components/ui/toggle';
import { ToggleGroup } from '@/components/ui/toggle-group';
import { Bold, Italic, Underline, AlignLeft, AlignCenter, AlignRight } from 'lucide-react';

export function Example() {
  return (
    <Toolbar>
      <ToggleGroup defaultValue={["bold"]} multiple>
        <ToolbarButton render={<Toggle value="bold" />} aria-label="Bold">
          <Bold className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton render={<Toggle value="italic" />} aria-label="Italic">
          <Italic className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton render={<Toggle value="underline" />} aria-label="Underline">
          <Underline className="h-4 w-4" />
        </ToolbarButton>
      </ToggleGroup>
      <ToolbarSeparator />
      <ToggleGroup defaultValue={["align-left"]}>
        <ToolbarButton render={<Toggle value="align-left" />} aria-label="Align left">
          <AlignLeft className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton render={<Toggle value="align-center" />} aria-label="Align center">
          <AlignCenter className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton render={<Toggle value="align-right" />} aria-label="Align right">
          <AlignRight className="h-4 w-4" />
        </ToolbarButton>
      </ToggleGroup>
    </Toolbar>
  );
}
```

### With input
A search input inside the toolbar.
```tsx
import { Toolbar, ToolbarButton, ToolbarSeparator, ToolbarGroup, ToolbarLink, ToolbarInput } from '@/components/ui/toolbar';
import { Bold, Italic, Link, Search } from 'lucide-react';

export function Example() {
  return (
    <Toolbar>
      <ToolbarGroup>
        <ToolbarButton aria-label="Bold">
          <Bold className="h-4 w-4" />
        </ToolbarButton>
        <ToolbarButton aria-label="Italic">
          <Italic className="h-4 w-4" />
        </ToolbarButton>
      </ToolbarGroup>
      <ToolbarSeparator />
      <ToolbarLink href="#">
        <Link className="mr-1 h-4 w-4" />
        Insert Link
      </ToolbarLink>
      <ToolbarSeparator />
      <div className="relative flex items-center">
        <Search className="absolute left-2 h-3.5 w-3.5 text-muted-foreground" />
        <ToolbarInput placeholder="Search..." className="h-8 w-40 pl-7" />
      </div>
    </Toolbar>
  );
}
```

### Vertical
A column of buttons with horizontal separators.
```tsx
import { Toolbar, ToolbarButton, ToolbarSeparator } from '@/components/ui/toolbar';
import { Bold, Italic, Underline, AlignLeft, AlignCenter, AlignRight } from 'lucide-react';

export function Example() {
  return (
    <Toolbar orientation="vertical" className="inline-flex flex-col">
      <ToolbarButton aria-label="Bold">
        <Bold className="h-4 w-4" />
      </ToolbarButton>
      <ToolbarButton aria-label="Italic">
        <Italic className="h-4 w-4" />
      </ToolbarButton>
      <ToolbarButton aria-label="Underline">
        <Underline className="h-4 w-4" />
      </ToolbarButton>
      <ToolbarSeparator orientation="horizontal" />
      <ToolbarButton aria-label="Align left">
        <AlignLeft className="h-4 w-4" />
      </ToolbarButton>
      <ToolbarButton aria-label="Align center">
        <AlignCenter className="h-4 w-4" />
      </ToolbarButton>
      <ToolbarButton aria-label="Align right">
        <AlignRight className="h-4 w-4" />
      </ToolbarButton>
    </Toolbar>
  );
}
```

## Notes

### Toggles
Pass a [Toggle](/docs/components/toggle) to `ToolbarButton` through `render`; wrap several in a [ToggleGroup](/docs/components/toggle-group) for a shared value.

```tsx
<ToggleGroup defaultValue={["bold"]} multiple>
  <ToolbarButton render={<Toggle value="bold" />} aria-label="Bold">
    <Bold className="h-4 w-4" />
  </ToolbarButton>
</ToggleGroup>
```

### Vertical toolbars
Separators do not follow the toolbar's orientation. With `orientation="vertical"`, pass `orientation="horizontal"` to each `ToolbarSeparator` and add `className="inline-flex flex-col"`.

## API reference

### Toolbar
The container. Also `ToolbarRoot`, `Toolbar.Button`, etc.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | Which arrow keys move focus. |
| `loopFocus` | `boolean` | `true` | Wraps focus from the last item to the first. |
| `disabled` | `boolean` | `false` | Disables every item. |

### ToolbarButton
A button item; `render` swaps the control.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `ReactElement` | - | Swap the element, keep toolbar focus, for example a Toggle or menu trigger. |
| `disabled` | `boolean` | `false` | Blocks the button. |
| `focusableWhenDisabled` | `boolean` | `true` | Keeps a disabled button reachable with the arrow keys. |

### ToolbarLink
A link item.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `href` | `string` | - | Where the link goes. |

### ToolbarInput
A text input in toolbar focus.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `defaultValue` | `string` | - | Starting text. |
| `disabled` | `boolean` | `false` | Blocks the input. |
| `focusableWhenDisabled` | `boolean` | `true` | Keeps a disabled input reachable with the arrow keys. |

### ToolbarGroup
Groups related items.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | `false` | Disables every item in the group. |

### ToolbarSeparator
A line between groups.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `orientation` | `"horizontal" \| "vertical"` | `"vertical"` | Line direction. Set horizontal in a vertical toolbar. |

## Accessibility
- Role toolbar. Name it with aria-label when a page has several.
- One item is in the tab order; arrow keys reach the rest.
- Icon-only buttons need an aria-label each.
- Disabled buttons and inputs stay focusable by default.
### Keyboard
| Keys | Does |
| --- | --- |
| `←` / `→` | Moves focus between items, or ↑ ↓ when vertical |
| `Tab` | Moves focus into and out of the toolbar as one stop |

## Source
`components/ui/toolbar/toolbar.tsx`
```tsx
import * as React from "react";
import { Toolbar as BaseToolbar } from "@base-ui/react/toolbar";
import { cn } from "@/lib/utils";

const ToolbarRoot = React.forwardRef<
  React.ComponentRef<typeof BaseToolbar.Root>,
  React.ComponentProps<typeof BaseToolbar.Root>
>(({ className, ...props }, ref) => (
  <BaseToolbar.Root
    data-slot="toolbar"
    ref={ref}
    className={cn(
      "flex flex-wrap items-center gap-1 rounded-md border bg-background p-1",
      "data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
      className
    )}
    {...props}
  />
));
ToolbarRoot.displayName = "ToolbarRoot";

const ToolbarButton = React.forwardRef<
  React.ComponentRef<typeof BaseToolbar.Button>,
  React.ComponentProps<typeof BaseToolbar.Button>
>(({ className, ...props }, ref) => (
  <BaseToolbar.Button
    data-slot="toolbar-button"
    ref={ref}
    className={cn(
      "inline-flex shrink-0 items-center justify-center rounded-sm px-3 py-2 text-sm font-medium transition-colors",
      "hover:bg-accent hover:text-accent-foreground",
      "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
      "disabled:pointer-events-none disabled:opacity-50",
      "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
      className
    )}
    {...props}
  />
));
ToolbarButton.displayName = "ToolbarButton";

const ToolbarLink = React.forwardRef<
  React.ComponentRef<typeof BaseToolbar.Link>,
  React.ComponentProps<typeof BaseToolbar.Link>
>(({ className, ...props }, ref) => (
  <BaseToolbar.Link
    data-slot="toolbar-link"
    ref={ref}
    className={cn(
      "inline-flex shrink-0 items-center justify-center rounded-sm px-3 py-2 text-sm font-medium transition-colors",
      "text-primary hover:text-primary/80 hover:underline",
      "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
      "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
      className
    )}
    {...props}
  />
));
ToolbarLink.displayName = "ToolbarLink";

const ToolbarSeparator = React.forwardRef<
  React.ComponentRef<typeof BaseToolbar.Separator>,
  React.ComponentProps<typeof BaseToolbar.Separator>
>(({ className, orientation = "vertical", ...props }, ref) => (
  <BaseToolbar.Separator
    data-slot="toolbar-separator"
    ref={ref}
    orientation={orientation}
    className={cn(
      "shrink-0 bg-border",
      orientation === "horizontal" ? "h-px w-full" : "h-4 w-px",
      className
    )}
    {...props}
  />
));
ToolbarSeparator.displayName = "ToolbarSeparator";

const ToolbarGroup = React.forwardRef<
  React.ComponentRef<typeof BaseToolbar.Group>,
  React.ComponentProps<typeof BaseToolbar.Group>
>(({ className, ...props }, ref) => (
  <BaseToolbar.Group
    data-slot="toolbar-group"
    ref={ref}
    className={cn("flex shrink-0 items-center gap-1", className)}
    {...props}
  />
));
ToolbarGroup.displayName = "ToolbarGroup";

const ToolbarInput = React.forwardRef<
  React.ComponentRef<typeof BaseToolbar.Input>,
  React.ComponentProps<typeof BaseToolbar.Input>
>(({ className, ...props }, ref) => (
  <BaseToolbar.Input
    data-slot="toolbar-input"
    ref={ref}
    className={cn(
      "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors",
      "file:border-0 file:bg-transparent file:text-sm file:font-medium",
      "placeholder:text-muted-foreground",
      "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
      "disabled:cursor-not-allowed disabled:opacity-50",
      "data-[highlighted]:ring-2 data-[highlighted]:ring-ring data-[highlighted]:ring-offset-2",
      className
    )}
    {...props}
  />
));
ToolbarInput.displayName = "ToolbarInput";

const Toolbar = Object.assign(ToolbarRoot, {
  Root: ToolbarRoot,
  Button: ToolbarButton,
  Link: ToolbarLink,
  Separator: ToolbarSeparator,
  Group: ToolbarGroup,
  Input: ToolbarInput,
});

export {
  Toolbar,
  ToolbarRoot,
  ToolbarButton,
  ToolbarLink,
  ToolbarSeparator,
  ToolbarGroup,
  ToolbarInput,
};
```
