Skip to the content

Controls and surfaces

toolbar

A row of buttons, toggles and links that the keyboard enters once and then moves through with the arrow keys.

Primitives

When to use it

The controls over an editor, a canvas, a table or a player: one Tab stop for the whole row, however many controls it holds. Give it an aria-label. For one group of toggles on its own use toggle-group; for commands in named menus use menubar.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.

Alert dialog

The project is kept.

Collapsible

The options are closed.

Hover card

The first published program was written by Ada Lovelace.

Menubar

Command: none yet. Ruler: hidden. Zoom: 100.

Navigation menu

Toolbar

Styles: none. Saved 0 times.

One-time code

Typed: nothing yet. Sent: nothing yet.

Password

At least twelve characters.

Words for screen readers

Add it

npx shadcn@latest add @rivier-ui/toolbar

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/toolbar.

<Toolbar aria-label="Formatting">
  <ToolbarToggleGroup type="multiple" aria-label="Text style">
    <ToolbarToggleItem value="bold" aria-label="Bold"><Bold strokeWidth={1.5} /></ToolbarToggleItem>
    <ToolbarToggleItem value="underline" aria-label="Underline"><Underline strokeWidth={1.5} /></ToolbarToggleItem>
  </ToolbarToggleGroup>
  <ToolbarSeparator />
  <ToolbarLink href="/help/formatting">Help</ToolbarLink>
  <ToolbarButton className="ms-auto" onClick={save}>Save</ToolbarButton>
</Toolbar>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: class-variance-authority, radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/toolbar.tsx
"use client";
/**
 * @name toolbar
 * @category ui
 * @description A row of buttons, toggles and links that the keyboard enters once and then moves through with the arrow keys.
 * @when The controls over an editor, a canvas, a table or a player: one Tab stop for the whole row, however many controls it holds. Give it an `aria-label`. For one group of toggles on its own use toggle-group; for commands in named menus use menubar.
 * @example
 * <Toolbar aria-label="Formatting">
 *   <ToolbarToggleGroup type="multiple" aria-label="Text style">
 *     <ToolbarToggleItem value="bold" aria-label="Bold"><Bold strokeWidth={1.5} /></ToolbarToggleItem>
 *     <ToolbarToggleItem value="underline" aria-label="Underline"><Underline strokeWidth={1.5} /></ToolbarToggleItem>
 *   </ToolbarToggleGroup>
 *   <ToolbarSeparator />
 *   <ToolbarLink href="/help/formatting">Help</ToolbarLink>
 *   <ToolbarButton className="ms-auto" onClick={save}>Save</ToolbarButton>
 * </Toolbar>
 */
import { Toolbar as ToolbarPrimitive } from "radix-ui";
import type { VariantProps } from "class-variance-authority";
import type { ComponentProps } from "react";
import { buttonVariants } from "@/components/ui/button";
import { toggleVariants } from "@/components/ui/toggle";
import { cn } from "@/lib/cn";

/** The row. It wraps onto a second line rather than overflow; `orientation="vertical"` stacks it and turns the arrow keys. */
export function Toolbar({ className, ...props }: ComponentProps<typeof ToolbarPrimitive.Root>) {
  return (
    <ToolbarPrimitive.Root
      data-slot="toolbar"
      className={cn(
        "flex w-full max-w-full flex-wrap items-center gap-1 rounded-surface border bg-background p-1 data-[orientation=vertical]:w-fit data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-stretch",
        className,
      )}
      {...props}
    />
  );
}

export type ToolbarButtonProps = ComponentProps<typeof ToolbarPrimitive.Button> & VariantProps<typeof buttonVariants>;

/** A button with the look of button: `variant` (ghost by default) and `size`. */
export function ToolbarButton({ className, variant = "ghost", size, ...props }: ToolbarButtonProps) {
  return <ToolbarPrimitive.Button data-slot="toolbar-button" className={cn(buttonVariants({ variant, size }), className)} {...props} />;
}

export function ToolbarLink({ className, ...props }: ComponentProps<typeof ToolbarPrimitive.Link>) {
  return (
    <ToolbarPrimitive.Link
      data-slot="toolbar-link"
      className={cn("inline-flex h-control items-center rounded-control px-3 text-sm font-medium text-foreground underline underline-offset-4 hover:opacity-70", className)}
      {...props}
    />
  );
}

export function ToolbarSeparator({ className, ...props }: ComponentProps<typeof ToolbarPrimitive.Separator>) {
  return (
    <ToolbarPrimitive.Separator
      data-slot="toolbar-separator"
      className={cn("mx-1 shrink-0 bg-border data-[orientation=horizontal]:my-1 data-[orientation=horizontal]:h-px data-[orientation=vertical]:h-5 data-[orientation=vertical]:w-px", className)}
      {...props}
    />
  );
}

/** Toggles that belong together: `type="single"` or `type="multiple"`, with an `aria-label`. */
export function ToolbarToggleGroup({ className, ...props }: ComponentProps<typeof ToolbarPrimitive.ToggleGroup>) {
  return <ToolbarPrimitive.ToggleGroup data-slot="toolbar-toggle-group" className={cn("flex items-center gap-1", className)} {...props} />;
}

export type ToolbarToggleItemProps = ComponentProps<typeof ToolbarPrimitive.ToggleItem> & VariantProps<typeof toggleVariants>;

export function ToolbarToggleItem({ className, variant, size, ...props }: ToolbarToggleItemProps) {
  return <ToolbarPrimitive.ToggleItem data-slot="toolbar-toggle-item" className={cn(toggleVariants({ variant, size }), className)} {...props} />;
}