# Toolbar

> Groups slotted buttons and links into a toolbar with a single tab stop and directional keyboard navigation.

- Kind: ui · Category: Actions · Interactivity: script
- Install: `npx shadcn@latest add @haze/toolbar`
- File: `src/components/haze/ui/toolbar.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/toolbar

## Use when
- Several related actions appear side by side in an editor or workspace.
- Users should be able to move between tools without adding many Tab stops.

## Avoid when
- The controls include text fields that rely on arrow keys for editing.

## Alternatives
- [button-group](https://hazeui.com/ui/button-group.md)
- [menu-bar](https://hazeui.com/ui/menu-bar.md)
- [toggle-group](https://hazeui.com/ui/toggle-group.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| label | `string` | Workspace tools | no | Accessible name for the toolbar. |
| orientation | `"horizontal" \| "vertical"` | horizontal | no | Layout and arrow-key direction. |
| class | `string` |  | no | Classes for the toolbar wrapper. |

## Accessibility
- Add data-toolbar-item to each button or link that joins the keyboard group.
- Unenhanced controls keep their native Tab order. Disabled controls are skipped by arrows.
- Key Tab: Reach the current tool.
- Key Arrow keys: Move among tools along the toolbar’s axis.
- Key Home / End: Focus the first or last available tool.

## Agent prompts

### Install and use

Install the Haze UI component "Toolbar" with `npx shadcn@latest add @haze/toolbar`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Toolbar from "@/components/haze/ui/toolbar.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/toolbar.md

### Customize with real content

Adapt the Haze toolbar ui for our product. Replace the demo values of these props with our real content: label, orientation, class. Keep the layout, the heading level rules and these accessibility details: Add data-toolbar-item to each button or link that joins the keyboard group. Unenhanced controls keep their native Tab order. Disabled controls are skipped by arrows. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using toolbar, check it fits: use it when several related actions appear side by side in an editor or workspace.; users should be able to move between tools without adding many tab stops.. Avoid it when the controls include text fields that rely on arrow keys for editing.. Alternatives: @haze/button-group, @haze/menu-bar, @haze/toggle-group.

## Example requests that should lead here
- Build an arrow-key toolbar for a canvas editor.
- Group view and documentation shortcuts into a compact toolbar.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Props {
  label?: string;
  orientation?: "horizontal" | "vertical";
  class?: string;
}

const { label = "Workspace tools", orientation = "horizontal", class: className } = Astro.props;
---

<div role="group" aria-label={label} data-haze-toolbar data-orientation={orientation} class={cn(
  "inline-flex w-fit min-w-0 max-w-full flex-wrap items-center gap-1 rounded-lg border border-frame bg-muted p-1 text-foreground",
  orientation === "vertical" && "flex-col items-stretch", className,
)}>
  <slot />
</div>

<script>
  function enhance(root: HTMLElement) {
    if (root.hasAttribute("data-enhanced")) return;
    const vertical = root.dataset.orientation === "vertical";
    const items = Array.from(root.querySelectorAll<HTMLElement>("[data-toolbar-item]"))
      .filter((item) => item.closest("[data-haze-toolbar]") === root && !item.closest("[popover]"));
    if (items.length < 2) return;
    const enabled = () => items.filter((item) => !item.matches(":disabled, [aria-disabled=true]") && item.getClientRects().length);
    const activate = (active?: HTMLElement) => items.forEach((item) => { item.tabIndex = item === active ? 0 : -1; });
    root.setAttribute("role", "toolbar");
    root.setAttribute("aria-orientation", vertical ? "vertical" : "horizontal");
    activate(enabled()[0]);
    root.addEventListener("focusin", (event) => {
      if (event.target instanceof HTMLElement && items.includes(event.target)) activate(event.target);
    });
    root.addEventListener("keydown", (event) => {
      if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
      const controls = enabled();
      const index = controls.findIndex((item) => item === event.target);
      if (index < 0) return;
      const rtl = !vertical && getComputedStyle(root).direction === "rtl";
      const delta = rtl ? -1 : 1;
      const wrap = (offset: number) => controls[(index + offset + controls.length) % controls.length];
      const before = vertical ? "ArrowUp" : "ArrowLeft";
      const after = vertical ? "ArrowDown" : "ArrowRight";
      const destinations: Record<string, HTMLElement | undefined> = {
        [before]: wrap(-delta), [after]: wrap(delta), Home: controls[0], End: controls.at(-1),
      };
      const destination = destinations[event.key];
      if (!destination) return;
      event.preventDefault();
      destination.focus();
    });
    root.setAttribute("data-enhanced", "");
  }

  const init = () => document.querySelectorAll<HTMLElement>("[data-haze-toolbar]").forEach(enhance);
  init();
  if (!document.documentElement.hasAttribute("data-haze-toolbar-listener")) {
    document.documentElement.setAttribute("data-haze-toolbar-listener", "");
    document.addEventListener("astro:page-load", init);
  }
</script>
```
