Toolbar
A data-driven formatting toolbar with toggles, dropdown groups and separators, roving focus, and an overflow menu that collects whatever doesn't fit.
Preview
Launch checklist
- Ship the empty states
- Record the walkthrough with Malik
Use Tab to reach the toolbar, then arrow keys.
Install
Add the editable source with the shadcn CLI. Your project also needs the shared theme tokens this component uses. Setup and requirements. The Pebble installer is not published yet.
npx shadcn@latest add https://pebbleui.com/r/toolbar.jsonProps
| Prop | Type | Default | Description |
|---|---|---|---|
| items | ToolbarItem[] | — | toggle { label, icon, pressed, onPressedChange, shortcut? } · button { label, icon, onClick } · dropdown { label, value, options, onValueChange, showValue? } · separator. Required. |
| aria-label | string | — | Names the toolbar. Required. |
| className | string | — | Merged on the tray. Items that don't fit move into a More menu. |
Build notes
- Fitting: an invisible ruler renders every item at natural width. A ResizeObserver on the root compares the content width (minus padding) with the summed widths plus gaps; if it doesn't all fit, it fills from the start while reserving 36 px for More, and trims a trailing separator. - First render (and SSR) shows everything; the layout effect trims before paint, so there's no visible jump. - Tray: `bg-muted/60` with an inset hairline; pressed: `bg-card shadow-raised-xs ring-hairline inset-shadow-highlight`. - Overflow mapping: toggle → `MenuCheckboxItem` (selection doesn't close the menu), button → `MenuItem` + `MenuShortcut`, dropdown → `MenuSub` with `MenuRadioGroup`. <!-- PEBBLE-LICENSE -->
View source
registry/button/toolbar/toolbar.tsx
"use client";
/* PEBBLE-LICENSE
Built by Pebble. Component: toolbar.
First-party license: MIT
The following first-party terms apply only to original Pebble material.
MIT License
Copyright (c) 2026 Praxys Solutions LLC (Pebble)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
END PEBBLE-LICENSE */
import * as React from "react";
import { Toolbar as ToolbarPrimitive } from "radix-ui";
import { cn } from "@/lib/utils";
import {
Menu,
MenuCheckboxItem,
MenuContent,
MenuItem,
MenuRadioGroup,
MenuRadioItem,
MenuSeparator,
MenuShortcut,
MenuSub,
MenuSubContent,
MenuSubTrigger,
MenuTrigger,
} from "@/registry/overlay/menu/menu";
export type ToolbarItem =
| {
type: "toggle";
id: string;
label: string;
icon: React.ReactNode;
pressed: boolean;
onPressedChange: (pressed: boolean) => void;
shortcut?: string;
disabled?: boolean;
}
| {
type: "button";
id: string;
label: string;
icon: React.ReactNode;
onClick: () => void;
shortcut?: string;
disabled?: boolean;
}
| {
type: "dropdown";
id: string;
label: string;
value: string;
options: { value: string; label: string; icon?: React.ReactNode }[];
onValueChange: (value: string) => void;
/** Show the selected option's label on the trigger (else its icon). */
showValue?: boolean;
}
| { type: "separator"; id: string };
export type ToolbarProps = Omit<React.ComponentProps<"div">, "children" | "dir"> & {
items: ToolbarItem[];
"aria-label": string;
orientation?: "horizontal";
};
const key =
"inline-flex h-8 min-w-8 shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-md px-2 text-[13px] font-medium text-muted-foreground select-none [&_svg]:size-4 [&_svg]:shrink-0 " +
"transition-[background-color,color,box-shadow] duration-(--motion-fast) ease-enter hover:bg-muted hover:text-foreground motion-reduce:transition-none " +
"aria-pressed:bg-card aria-pressed:text-foreground aria-pressed:shadow-raised-xs aria-pressed:ring-1 aria-pressed:ring-hairline aria-pressed:inset-shadow-highlight " +
"data-[state=open]:bg-muted data-[state=open]:text-foreground " +
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none disabled:pointer-events-none disabled:opacity-40";
function Chevron() {
return (
<svg
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className="size-3! opacity-60"
>
<path d="M4.5 6.5 8 10l3.5-3.5" />
</svg>
);
}
function ItemView({ item, measure = false }: { item: ToolbarItem; measure?: boolean }) {
if (item.type === "separator") {
return measure ? (
<span className="mx-1 h-5 w-px shrink-0" />
) : (
<ToolbarPrimitive.Separator
data-slot="toolbar-separator"
className="mx-1 h-5 w-px shrink-0 bg-border"
/>
);
}
if (measure) {
// Same box as the live item, for width measurement only.
return (
<span className={key}>
{item.type === "dropdown" ? (
<>
{item.showValue
? item.options.find((o) => o.value === item.value)?.label
: item.options.find((o) => o.value === item.value)?.icon}
<Chevron />
</>
) : (
item.icon
)}
</span>
);
}
if (item.type === "toggle") {
return (
<ToolbarPrimitive.Button
data-slot="toolbar-item"
aria-label={item.label}
aria-pressed={item.pressed}
aria-keyshortcuts={item.shortcut}
title={item.shortcut ? `${item.label} (${item.shortcut})` : item.label}
disabled={item.disabled}
onClick={() => item.onPressedChange(!item.pressed)}
className={key}
>
{item.icon}
</ToolbarPrimitive.Button>
);
}
if (item.type === "button") {
return (
<ToolbarPrimitive.Button
data-slot="toolbar-item"
aria-label={item.label}
aria-keyshortcuts={item.shortcut}
title={item.shortcut ? `${item.label} (${item.shortcut})` : item.label}
disabled={item.disabled}
onClick={item.onClick}
className={key}
>
{item.icon}
</ToolbarPrimitive.Button>
);
}
const selected = item.options.find((o) => o.value === item.value);
return (
<Menu>
<MenuTrigger asChild>
<ToolbarPrimitive.Button
data-slot="toolbar-item"
aria-label={`${item.label}: ${selected?.label ?? ""}`}
className={cn(key, item.showValue && "text-foreground")}
>
{item.showValue ? selected?.label : selected?.icon}
<Chevron />
</ToolbarPrimitive.Button>
</MenuTrigger>
<MenuContent align="start" className="min-w-40">
<MenuRadioGroup value={item.value} onValueChange={item.onValueChange}>
{item.options.map((o) => (
<MenuRadioItem key={o.value} value={o.value}>
{o.icon}
{o.label}
</MenuRadioItem>
))}
</MenuRadioGroup>
</MenuContent>
</Menu>
);
}
function OverflowItems({ items }: { items: ToolbarItem[] }) {
// Trim separators at the edges of the overflow list.
const list = items.filter(
(item, i) =>
item.type !== "separator" ||
(i > 0 && i < items.length - 1 && items[i - 1].type !== "separator"),
);
return (
<>
{list.map((item) => {
if (item.type === "separator") return <MenuSeparator key={item.id} />;
if (item.type === "toggle")
return (
<MenuCheckboxItem
key={item.id}
checked={item.pressed}
disabled={item.disabled}
onCheckedChange={(v) => item.onPressedChange(v === true)}
onSelect={(e) => e.preventDefault()}
>
{item.label}
{item.shortcut && <MenuShortcut>{item.shortcut}</MenuShortcut>}
</MenuCheckboxItem>
);
if (item.type === "button")
return (
<MenuItem key={item.id} disabled={item.disabled} onSelect={item.onClick}>
{item.icon}
{item.label}
{item.shortcut && <MenuShortcut>{item.shortcut}</MenuShortcut>}
</MenuItem>
);
return (
<MenuSub key={item.id}>
<MenuSubTrigger>{item.label}</MenuSubTrigger>
<MenuSubContent>
<MenuRadioGroup value={item.value} onValueChange={item.onValueChange}>
{item.options.map((o) => (
<MenuRadioItem key={o.value} value={o.value}>
{o.icon}
{o.label}
</MenuRadioItem>
))}
</MenuRadioGroup>
</MenuSubContent>
</MenuSub>
);
})}
</>
);
}
const OVERFLOW_WIDTH = 36;
/**
* A formatting toolbar from data. Items that don't fit collapse, from the
* end, into a "More" menu, measured with a ResizeObserver.
*/
export function Toolbar({ items, className, ...props }: ToolbarProps) {
const rootRef = React.useRef<HTMLDivElement>(null);
const measureRef = React.useRef<HTMLDivElement>(null);
// Server and first client render show everything; the effect trims.
const [visible, setVisible] = React.useState(items.length);
React.useLayoutEffect(() => {
const root = rootRef.current;
const ruler = measureRef.current;
if (!root || !ruler || typeof ResizeObserver === "undefined") return;
const fit = () => {
const widths = Array.from(ruler.children).map((el) => (el as HTMLElement).offsetWidth);
const style = getComputedStyle(root);
const available =
root.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight);
const gap = parseFloat(style.columnGap) || 0;
const total = widths.reduce((sum, w) => sum + w + gap, 0) - gap;
if (total <= available) return setVisible(items.length);
let used = OVERFLOW_WIDTH;
let count = 0;
for (const w of widths) {
if (used + w + gap > available) break;
used += w + gap;
count += 1;
}
// Never end the visible row on a separator.
while (count > 0 && items[count - 1].type === "separator") count -= 1;
setVisible(count);
};
fit();
const observer = new ResizeObserver(fit);
observer.observe(root);
return () => observer.disconnect();
}, [items]);
const shown = items.slice(0, visible);
const hidden = items.slice(visible);
return (
<ToolbarPrimitive.Root
ref={rootRef}
data-slot="toolbar"
className={cn(
"relative flex w-full min-w-0 items-center gap-0.5 overflow-hidden rounded-lg bg-muted/60 p-1 ring-1 ring-hairline ring-inset dark:bg-muted/40",
className,
)}
{...props}
>
{shown.map((item) => (
<ItemView key={item.id} item={item} />
))}
{hidden.some((i) => i.type !== "separator") && (
<Menu>
<MenuTrigger asChild>
<ToolbarPrimitive.Button
data-slot="toolbar-overflow"
aria-label="More formatting"
className={cn(key, "ml-auto")}
>
<svg
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
aria-hidden="true"
>
<path d="M4 8h.01M8 8h.01M12 8h.01" />
</svg>
</ToolbarPrimitive.Button>
</MenuTrigger>
<MenuContent align="end" className="min-w-48">
<OverflowItems items={hidden} />
</MenuContent>
</Menu>
)}
{/* Invisible ruler: every item at its natural width. */}
<div
ref={measureRef}
aria-hidden="true"
className="pointer-events-none invisible absolute top-0 left-0 flex gap-0.5"
>
{items.map((item) => (
<ItemView key={item.id} item={item} measure />
))}
</div>
</ToolbarPrimitive.Root>
);
}
