Menu

Components

Toolbar

A row of controls for one job, such as formatting text, with the arrow keys to move between them.

Introduction

A toolbar puts the controls for one task in one place: the buttons above a text editor, the tools beside a canvas. It is more than a row of buttons, because it behaves as a single control: the user tabs into it once, then moves between its buttons with the arrow keys.

  • Built on react-aria's Toolbar, so the toolbar role, the arrow key navigation and the horizontal or vertical orientation come from there.
  • Set the size and look of every button once, on the toolbar. Buttons, toggles and toggle groups inside follow it.
  • Group related controls, divide the groups with a separator, and push the last ones to the end with a spacer.
  • Icon controls can have a tooltip with one prop, so their meaning is visible as well as announced.

For a plain set of related buttons, use Button Group. Use a toolbar when there are many controls that should behave as one.

Installation

centerUI has no CLI or registry yet: components are copied into your project. Toolbar is made of Button, Toggle and Tooltip, so it needs all three.

# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx

# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx

# npm
npm install react-aria-components tailwind-variants tailwind-merge clsx

Add Button, Toggle and Tooltip first (see their Manual installation), then follow the steps below.

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/toolbar-styles.tsShow code
import { tv } from "tailwind-variants";

export const toolbarStyles = tv({
  slots: {
    root: "flex items-center gap-1",
    group: "flex items-center gap-0.5",
    separator: "shrink-0 bg-border",
  },
  variants: {
    // A toolbar with a frame around it, or one that blends into its surroundings.
    variant: {
      outline: { root: "rounded-lg border bg-card p-1 text-card-fg" },
      plain: {},
    },
    orientation: {
      horizontal: {
        root: "w-fit max-w-full flex-row",
        group: "flex-row",
        separator: "mx-1 h-5 w-px",
      },
      vertical: {
        root: "w-fit flex-col",
        group: "flex-col",
        separator: "my-1 h-px w-5 self-center",
      },
    },
    // Lets a long horizontal toolbar continue on the next line.
    isWrap: {
      true: { root: "flex-wrap" },
      false: {},
    },
  },
  defaultVariants: {
    variant: "outline",
    orientation: "horizontal",
    isWrap: false,
  },
});

2. Add the context

It tells groups and separators which way the toolbar runs.

src/types/toolbar-context.tsShow code
import { createContext, useContext } from "react";

/** What a Toolbar shares with its groups and separators. */
export interface ToolbarContextValue {
  orientation: "horizontal" | "vertical";
}

const ToolbarContext = createContext<ToolbarContextValue>({
  orientation: "horizontal",
});

export const ToolbarContextProvider = ToolbarContext.Provider;

/** The enclosing Toolbar's orientation. Horizontal outside of one. */
export function useToolbarContext() {
  return useContext(ToolbarContext);
}

3. Add the Toolbar file

src/components/ui/toolbar/toolbar.tsxShow code
"use client";

import { type ReactNode, useMemo } from "react";
import {
  Toolbar as AriaToolbar,
  type ToolbarProps as AriaToolbarProps,
  Group,
  Separator,
} from "react-aria-components";
import { Button, type ButtonProps } from "@/components/ui/button/button";
import { Toggle, type ToggleProps } from "@/components/ui/toggle/toggle";
import { Tooltip, TooltipContent } from "@/components/ui/tooltip/tooltip";
import { cn } from "@/lib/cn";
import { ButtonGroupProvider } from "@/types/button-group-context";
import {
  ToolbarContextProvider,
  useToolbarContext,
} from "@/types/toolbar-context";
import { toolbarStyles } from "@/types/toolbar-styles";

export interface ToolbarProps
  extends Omit<AriaToolbarProps, "className" | "children"> {
  /** A frame around the toolbar, or none. */
  variant?: "outline" | "plain";
  /** Size of every button in it. A button can override it. */
  size?: "xs" | "sm" | "md" | "lg";
  /** Look of every button in it. A button can override it. Defaults to `ghost`. */
  buttonVariant?: "primary" | "secondary" | "outline" | "ghost";
  /** Let a horizontal toolbar continue on the next line when it runs out of room. */
  isWrap?: boolean;
  /** Disables every button in it. */
  isDisabled?: boolean;
  className?: string;
  children?: ReactNode;
}

/**
 * A row (or column) of controls for one job, such as formatting text. It is
 * one stop in the tab order, and the arrow keys move between its controls.
 * Built on react-aria's Toolbar, so the role, the keyboard support and the
 * orientation come from there. Give it an `aria-label` so a screen reader can
 * tell it from other toolbars.
 *
 * The buttons and toggles inside read their size and look from it, so you set
 * them once. Group related controls with `ToolbarGroup` and divide the groups
 * with `ToolbarSeparator`.
 *
 * @example
 * <Toolbar aria-label="Text formatting">
 *   <ToolbarButton isIcon aria-label="Undo" tooltip="Undo"><UndoIcon /></ToolbarButton>
 *   <ToolbarSeparator />
 *   <ToolbarToggle isIcon aria-label="Bold" tooltip="Bold"><BoldIcon /></ToolbarToggle>
 * </Toolbar>
 */
export function Toolbar({
  variant,
  size = "sm",
  buttonVariant = "ghost",
  isWrap,
  isDisabled,
  orientation = "horizontal",
  className,
  children,
  ...props
}: ToolbarProps) {
  const styles = toolbarStyles({ variant, orientation, isWrap });
  const defaults = useMemo(
    () => ({ variant: buttonVariant, size, isDisabled }),
    [buttonVariant, size, isDisabled],
  );
  const context = useMemo(() => ({ orientation }), [orientation]);

  return (
    <ToolbarContextProvider value={context}>
      <ButtonGroupProvider value={defaults}>
        <AriaToolbar
          {...props}
          orientation={orientation}
          className={cn(styles.root(), className)}
        >
          {children}
        </AriaToolbar>
      </ButtonGroupProvider>
    </ToolbarContextProvider>
  );
}

export interface ToolbarGroupProps {
  /** Names the group for screen readers, when it is not clear from its controls. */
  "aria-label"?: string;
  className?: string;
  children?: ReactNode;
}

/**
 * A set of related controls, such as the text styles or the alignment. It
 * keeps them together and tells screen readers they belong together.
 */
export function ToolbarGroup({ className, ...props }: ToolbarGroupProps) {
  const { orientation } = useToolbarContext();

  return (
    <Group
      {...props}
      className={cn(toolbarStyles({ orientation }).group(), className)}
    />
  );
}

/** A thin line between two groups. It runs across the direction of the toolbar. */
export function ToolbarSeparator({ className }: { className?: string }) {
  const { orientation } = useToolbarContext();

  return (
    <Separator
      orientation={orientation === "horizontal" ? "vertical" : "horizontal"}
      className={cn(toolbarStyles({ orientation }).separator(), className)}
    />
  );
}

/** Empty space that takes the room left over, pushing what follows to the end. */
export function ToolbarSpacer() {
  return <div aria-hidden="true" className="flex-1" />;
}

interface WithTooltip {
  /** A tooltip for the control. Give an icon-only control the same text as its `aria-label`. */
  tooltip?: ReactNode;
}

function withTooltip(control: ReactNode, tooltip: ReactNode) {
  if (!tooltip) return control;
  return (
    <Tooltip>
      {control}
      <TooltipContent>{tooltip}</TooltipContent>
    </Tooltip>
  );
}

export type ToolbarButtonProps = ButtonProps & WithTooltip;

/**
 * A Button for a toolbar, with an optional tooltip. It takes its size and look
 * from the toolbar.
 */
export function ToolbarButton({ tooltip, ...props }: ToolbarButtonProps) {
  return withTooltip(<Button {...props} />, tooltip);
}

export type ToolbarToggleProps = ToggleProps & WithTooltip;

/**
 * A Toggle for a toolbar, with an optional tooltip: a control that stays on or
 * off, like Bold. It takes its size and look from the toolbar.
 */
export function ToolbarToggle({ tooltip, ...props }: ToolbarToggleProps) {
  return withTooltip(<Toggle {...props} />, tooltip);
}

4. Use it

import {
  Toolbar,
  ToolbarButton,
  ToolbarGroup,
  ToolbarSeparator,
  ToolbarSpacer,
  ToolbarToggle,
} from "@/components/ui/toolbar/toolbar";

Anatomy

A frame around groups of controls, with separators between them.

<Toolbar aria-label="Text formatting">
  <ToolbarGroup aria-label="History">
    <ToolbarButton isIcon aria-label="Undo" tooltip="Undo"><UndoIcon /></ToolbarButton>
    <ToolbarButton isIcon aria-label="Redo" tooltip="Redo"><RedoIcon /></ToolbarButton>
  </ToolbarGroup>

  <ToolbarSeparator />

  <ToolbarToggle isIcon aria-label="Bold" tooltip="Bold"><BoldIcon /></ToolbarToggle>

  <ToolbarSpacer />

  <ToolbarButton variant="primary">Share</ToolbarButton>
</Toolbar>
PartRendered asNotes
Toolbardiv, role=toolbarThe frame. One stop in the tab order, with arrow keys between its controls.
ToolbarGroupdiv, role=groupA set of related controls, kept together.
ToolbarSeparatorhrA thin line between groups.
ToolbarSpacerdivTakes the room that is left, pushing what follows to the end.
ToolbarButtonButtonA Button that takes its size and look from the toolbar, with an optional tooltip.
ToolbarToggleToggleA control that stays on or off, such as Bold, with an optional tooltip.

Give the toolbar an aria-label so a screen reader can tell it from other toolbars, and every icon control its own aria-label.

Examples

Click into a toolbar and use the arrow keys to move along it.

Usage

A text editor's toolbar: history, text style, alignment and a Share button at the end. The style controls are toggles, and the alignment is a ToggleGroup, because only one alignment applies. Use the toolbar to change the text under it.

The quick brown fox jumps over the lazy dog. Use the toolbar above to change how this text looks.

Groups and separators

A group keeps related controls together and tells screen readers they belong together. A separator draws a line between groups, and a spacer pushes what follows to the far end.

Tooltips

tooltip on a ToolbarButton or a ToolbarToggle shows a tooltip. An icon has no visible text, so give it an aria-label and the same text as its tooltip: screen reader users hear it, and everyone else can read it.

Vertical

orientation=“vertical” stacks the controls. The Up and Down arrow keys move between them, and separators turn to run across.

Variants

A toolbar has a frame by default. Use plain when it sits inside something that already has one, such as a card header.

Button variants

buttonVariant sets the look of every button in the toolbar at once. A single button can still set its own variant.

Sizes

size sets the size of every control in the toolbar.

Disabled

isDisabled disables every button and toggle in the toolbar.

Wrapping

A toolbar that runs out of room is cut off by default. isWrap lets it continue on the next line. It is narrowed here to show it.

Component API

Toolbar takes the props below, plus the rest of what the react-aria Toolbar accepts.

Toolbar

PropTypeDefault
aria-labelstring- (give it one)
orientation"horizontal" | "vertical""horizontal"
variant"outline" | "plain""outline"
size"xs" | "sm" | "md" | "lg""sm"
buttonVariant"primary" | "secondary" | "outline" | "ghost""ghost"
isWrapbooleanfalse
isDisabledbooleanfalse

ToolbarGroup

PropTypeDefault
aria-labelstring-

ToolbarButton and ToolbarToggle

PropTypeDefault
tooltipReactNode-
isIconbooleanfalse
aria-labelstring- (needed with isIcon)

They take every Button or Toggle prop, plus tooltip. ToolbarSeparator and ToolbarSpacer take no props besides className.