Toolbar
Groups slotted buttons and links into a toolbar with a single tab stop and directional keyboard navigation.
pnpm dlx shadcn@latest add @haze/toolbar---
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>When to use
- 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.
When not to use
- The controls include text fields that rely on arrow keys for editing.
Installation
Add the Haze registry to components.json once, then install Toolbar with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/toolbarnpx shadcn@latest add @haze/toolbaryarn dlx shadcn@latest add @haze/toolbarbunx --bun shadcn@latest add @haze/toolbarFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | Workspace tools | Accessible name for the toolbar. |
| orientation | "horizontal" | "vertical" | horizontal | Layout and arrow-key direction. |
| class | string | – | Classes for the toolbar wrapper. |
Keyboard interactions
- Tab
- Reach the current tool.
- Arrow keys
- Move among tools along the toolbar’s axis.
- HomeEnd
- Focus the first or last available tool.
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.