Toolbar

A bar of controls that takes a single tab stop, the arrow keys moving between its buttons, toggles and links, each tool transparent at sm by default.

import {
  Toolbar,
  ToolbarButton,
  ToolbarLink,
  ToolbarSeparator,
  ToolbarToggleGroup,
  ToolbarToggleItem,
} from "@produkto/ui/Toolbar";
import { Icon, type IconSvgElement } from "@produkto/ui/Icon";
import {
  TextAlignCenterIcon,
  TextAlignLeftIcon,
  TextAlignRightIcon,
  TextBoldIcon,
  TextItalicIcon,
  TextStrikethroughIcon,
} from "@hugeicons/core-free-icons";

interface Tool {
  value: string;
  label: string;
  icon: IconSvgElement;
}

const marks: Tool[] = [
  { value: "bold", label: "Bold", icon: TextBoldIcon },
  { value: "italic", label: "Italic", icon: TextItalicIcon },
  {
    value: "strikethrough",
    label: "Strikethrough",
    icon: TextStrikethroughIcon,
  },
];

const alignments: Tool[] = [
  { value: "left", label: "Align left", icon: TextAlignLeftIcon },
  { value: "center", label: "Align centre", icon: TextAlignCenterIcon },
  { value: "right", label: "Align right", icon: TextAlignRightIcon },
];

const Example = () => (
  <Toolbar aria-label="Label" className="w-full max-w-md">
    <ToolbarToggleGroup type="multiple">
      {marks.map((mark) => (
        <ToolbarToggleItem
          aria-label={mark.label}
          key={mark.value}
          value={mark.value}
        >
          <Icon icon={mark.icon} />
        </ToolbarToggleItem>
      ))}
    </ToolbarToggleGroup>
    <ToolbarSeparator />
    <ToolbarToggleGroup defaultValue={alignments[1].value} type="single">
      {alignments.map((alignment) => (
        <ToolbarToggleItem
          aria-label={alignment.label}
          key={alignment.value}
          value={alignment.value}
        >
          <Icon icon={alignment.icon} />
        </ToolbarToggleItem>
      ))}
    </ToolbarToggleGroup>
    <ToolbarSeparator />
    <ToolbarLink href="#">Link</ToolbarLink>
    <ToolbarButton className="ms-auto">Button</ToolbarButton>
  </Toolbar>
);

export default Example;

Usage

import {
  Toolbar,
  ToolbarButton,
  ToolbarLink,
  ToolbarSeparator,
  ToolbarToggleGroup,
  ToolbarToggleItem,
} from "@produkto/ui/Toolbar";
import { Undo02Icon } from "@hugeicons/core-free-icons";
<Toolbar
  aria-label="Label"
  tools={[{ tooltip: "Undo", icon: Undo02Icon, onClick: () => {} }]}
>
  <ToolbarButton>Button</ToolbarButton>
  <ToolbarSeparator />
  <ToolbarLink href="#">Link</ToolbarLink>
  <ToolbarToggleGroup type="single" defaultValue="left">
    <ToolbarToggleItem value="left">Item 1</ToolbarToggleItem>
    <ToolbarToggleItem value="right">Item 2</ToolbarToggleItem>
  </ToolbarToggleGroup>
</Toolbar>

Examples

Appearances

appearance paints a ToolbarButton or a ToolbarToggleItem in a Button's appearances, solid, soft, outline or transparent, the default.

import type { ButtonAppearance } from "@produkto/ui/Button";
import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";

const appearances: { appearance: ButtonAppearance; label: string }[] = [
  { appearance: "solid", label: "Solid" },
  { appearance: "soft", label: "Soft" },
  { appearance: "outline", label: "Outline" },
  { appearance: "transparent", label: "Transparent" },
];

const Example = () => (
  <Toolbar aria-label="Label">
    {appearances.map((item) => (
      <ToolbarButton appearance={item.appearance} key={item.appearance}>
        {item.label}
      </ToolbarButton>
    ))}
  </Toolbar>
);

export default Example;

Tones

tone colours a ToolbarButton in a Button's tones, neutral by default, accent, destructive, success, warning or info, with every appearance.

import type { ButtonTone } from "@produkto/ui/Button";
import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";

const tones: { tone: ButtonTone; label: string }[] = [
  { tone: "neutral", label: "Neutral" },
  { tone: "accent", label: "Accent" },
  { tone: "destructive", label: "Destructive" },
  { tone: "success", label: "Success" },
  { tone: "warning", label: "Warning" },
  { tone: "info", label: "Info" },
];

const Example = () => (
  <Toolbar aria-label="Label">
    {tones.map((item) => (
      <ToolbarButton key={item.tone} tone={item.tone}>
        {item.label}
      </ToolbarButton>
    ))}
  </Toolbar>
);

export default Example;

Sizes

size sets a tool's height at a Button's heights, xs, sm (the default), base or lg, on a ToolbarButton or a ToolbarToggleItem alike, and a tool holding only an icon is square at each of them.

import { Edit03Icon } from "@hugeicons/core-free-icons";
import type { ButtonSize } from "@produkto/ui/Button";
import { Icon } from "@produkto/ui/Icon";
import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";

const sizes: { size: ButtonSize; label: string }[] = [
  { size: "xs", label: "Extra small" },
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const Example = () => (
  <div className="flex flex-col items-start gap-4">
    <Toolbar aria-label="Label">
      {sizes.map((item) => (
        <ToolbarButton key={item.size} size={item.size}>
          {item.label}
        </ToolbarButton>
      ))}
    </Toolbar>
    <Toolbar aria-label="Label">
      {sizes.map((item) => (
        <ToolbarButton aria-label="Button" key={item.size} size={item.size}>
          <Icon icon={Edit03Icon} />
        </ToolbarButton>
      ))}
    </Toolbar>
  </div>
);

export default Example;

Border radius

borderRadius sets the corners of the bar and of every tool in it on the kit's one scale: none, xs 4px, sm 6px, base 8px (the default), lg 12px, xl 16px, or full to round them; a tool's own borderRadius stands over the bar's.

import { TextBoldIcon, TextItalicIcon } from "@hugeicons/core-free-icons";
import { Icon } from "@produkto/ui/Icon";
import {
  Toolbar,
  ToolbarButton,
  ToolbarSeparator,
  ToolbarToggleGroup,
  ToolbarToggleItem,
  type ToolbarBorderRadius,
} from "@produkto/ui/Toolbar";

const radii: ToolbarBorderRadius[] = [
  "none",
  "xs",
  "sm",
  "base",
  "lg",
  "xl",
  "full",
];

const Example = () => (
  <div className="flex flex-col items-start gap-4">
    {radii.map((borderRadius) => (
      <Toolbar
        aria-label={borderRadius}
        key={borderRadius}
        borderRadius={borderRadius}
      >
        <ToolbarToggleGroup defaultValue={["bold"]} type="multiple">
          <ToolbarToggleItem aria-label="Bold" value="bold">
            <Icon icon={TextBoldIcon} />
          </ToolbarToggleItem>
          <ToolbarToggleItem aria-label="Italic" value="italic">
            <Icon icon={TextItalicIcon} />
          </ToolbarToggleItem>
        </ToolbarToggleGroup>
        <ToolbarSeparator />
        <ToolbarButton>{borderRadius}</ToolbarButton>
      </Toolbar>
    ))}
  </div>
);

export default Example;

Tools

tools lists the bar's tools as data, each a square button named by its tooltip: a button runs its onClick, a menu opens its rows (one without an onClick is off), a panel opens beside the focus, and a choice of alignments shows the current one.

import { TEXT_ALIGNMENTS } from "@produkto/ui/consts/alignments";
import { Toolbar } from "@produkto/ui/Toolbar";
import {
  ClipboardPasteIcon,
  Copy01Icon,
  Delete01Icon,
  MoreHorizontalCircle01Icon,
  Settings01Icon,
  Undo02Icon,
} from "@hugeicons/core-free-icons";
import { useState } from "react";

const Example = () => {
  const [align, setAlign] = useState("left");
  return (
    <Toolbar
      aria-label="Label"
      tools={[
        {
          tooltip: "Undo",
          icon: Undo02Icon,
          onClick: () => {},
        },
        {
          tooltip: "Settings",
          icon: Settings01Icon,
          panel: "Content goes here.",
        },
        {
          tooltip: "Text align",
          choices: TEXT_ALIGNMENTS,
          value: align,
          onValueChange: setAlign,
        },
        {
          tooltip: "Options",
          icon: MoreHorizontalCircle01Icon,
          menu: [
            { label: "Item 1", icon: Copy01Icon, onClick: () => {} },
            { label: "Item 2", icon: ClipboardPasteIcon },
            {
              label: "Item 3",
              icon: Delete01Icon,
              tone: "destructive",
              onClick: () => {},
            },
          ],
        },
      ]}
    />
  );
};

export default Example;

Rect

rect floats the toolbar over that box, such as the text being edited: above it, or below it when there is no room, portalled and kept on screen; without one it sits in place.

Content goes here.

import {
  Copy01Icon,
  Delete01Icon,
  Edit03Icon,
} from "@hugeicons/core-free-icons";
import { Icon, type IconSvgElement } from "@produkto/ui/Icon";
import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";
import { useEffect, useRef, useState } from "react";

const tools: { label: string; icon: IconSvgElement }[] = [
  { label: "Edit", icon: Edit03Icon },
  { label: "Copy", icon: Copy01Icon },
  { label: "Delete", icon: Delete01Icon },
];

const Example = () => {
  const text = useRef<HTMLParagraphElement>(null);
  const [rect, setRect] = useState<DOMRect | null>(null);

  useEffect(() => {
    const node = text.current;
    if (!node) return;
    const place = () => setRect(node.getBoundingClientRect());
    place();
    document.addEventListener("scroll", place, true);
    window.addEventListener("resize", place);
    return () => {
      document.removeEventListener("scroll", place, true);
      window.removeEventListener("resize", place);
    };
  }, []);

  return (
    <>
      <p className="body-sm" ref={text}>
        Content goes here.
      </p>
      {rect && (
        <Toolbar aria-label="Label" rect={rect}>
          {tools.map((tool) => (
            <ToolbarButton aria-label={tool.label} key={tool.label}>
              <Icon icon={tool.icon} />
            </ToolbarButton>
          ))}
        </Toolbar>
      )}
    </>
  );
};

export default Example;

Collision padding

collisionPadding keeps a floating toolbar that far inside the screen's edges, by default 8px inside its container or clear of the app's header; here its top reaches down to the text, so with no room above it the toolbar drops below.

Content goes here.

import {
  Copy01Icon,
  Delete01Icon,
  Edit03Icon,
} from "@hugeicons/core-free-icons";
import { Icon, type IconSvgElement } from "@produkto/ui/Icon";
import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";
import { useEffect, useRef, useState } from "react";

const tools: { label: string; icon: IconSvgElement }[] = [
  { label: "Edit", icon: Edit03Icon },
  { label: "Copy", icon: Copy01Icon },
  { label: "Delete", icon: Delete01Icon },
];

const Example = () => {
  const text = useRef<HTMLParagraphElement>(null);
  const [rect, setRect] = useState<DOMRect | null>(null);

  useEffect(() => {
    const node = text.current;
    if (!node) return;
    const place = () => setRect(node.getBoundingClientRect());
    place();
    document.addEventListener("scroll", place, true);
    window.addEventListener("resize", place);
    return () => {
      document.removeEventListener("scroll", place, true);
      window.removeEventListener("resize", place);
    };
  }, []);

  return (
    <>
      <p className="body-sm" ref={text}>
        Content goes here.
      </p>
      {rect && (
        <Toolbar
          aria-label="Label"
          collisionPadding={{ top: rect.top }}
          rect={rect}
        >
          {tools.map((tool) => (
            <ToolbarButton aria-label={tool.label} key={tool.label}>
              <Icon icon={tool.icon} />
            </ToolbarButton>
          ))}
        </Toolbar>
      )}
    </>
  );
};

export default Example;

Orientation

orientation="vertical" stacks the tools in a column, moved through with the up and down arrow keys, beside a bar in a row as it is by default.

import {
  Copy01Icon,
  Delete01Icon,
  Edit03Icon,
} from "@hugeicons/core-free-icons";
import { Icon, type IconSvgElement } from "@produkto/ui/Icon";
import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";

const tools: { label: string; icon: IconSvgElement }[] = [
  { label: "Edit", icon: Edit03Icon },
  { label: "Copy", icon: Copy01Icon },
  { label: "Delete", icon: Delete01Icon },
];

const Example = () => (
  <div className="flex flex-wrap items-start gap-4">
    <Toolbar aria-label="Horizontal">
      {tools.map((tool) => (
        <ToolbarButton aria-label={tool.label} key={tool.label}>
          <Icon icon={tool.icon} />
        </ToolbarButton>
      ))}
    </Toolbar>
    <Toolbar aria-label="Vertical" orientation="vertical">
      {tools.map((tool) => (
        <ToolbarButton aria-label={tool.label} key={tool.label}>
          <Icon icon={tool.icon} />
        </ToolbarButton>
      ))}
    </Toolbar>
  </div>
);

export default Example;

Disabled

disabled dims a tool and the arrow keys pass it by, beside one as it is by default.

import { Toolbar, ToolbarButton } from "@produkto/ui/Toolbar";

const Example = () => (
  <Toolbar aria-label="Label">
    <ToolbarButton>Button</ToolbarButton>
    <ToolbarButton disabled>Button</ToolbarButton>
  </Toolbar>
);

export default Example;

API Reference

Toolbar

ToolbarProps · components/Toolbar/Toolbar.types.ts
PropTypeDefault
rectToolbarRect-
toolsreadonly (ToolbarTool | false | null | undefined)[]-
borderRadiusToolbarBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full""base"

ToolbarButton

ToolbarButtonProps · also takes the props of Radix Toolbar.Button
PropTypeDefault
loadingboolean-
appearanceButtonAppearance"solid" | "soft" | "outline" | "transparent""transparent"
toneButtonTone"neutral" | "accent" | "destructive" | "success" | "warning" | "info"-
sizeButtonSize"xs" | "sm" | "base" | "lg""sm"
borderRadiusButtonBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full"-

ToolbarLink

ToolbarLinkProps · also takes the props of NextLink
PropTypeDefault
sizeTextSize"xs" | "sm" | "base" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "5xl"-

ToolbarSeparator

ToolbarSeparatorProps · takes the props of Radix Toolbar.Separator

ToolbarToggleGroup

ToolbarToggleGroupProps · takes the props of ToggleGroup

ToolbarToggleItem

ToolbarToggleItemProps · also takes the props of ComponentProps< typeof ToggleItem >
PropTypeDefault
appearanceToggleAppearance"solid" | "soft" | "outline" | "transparent""transparent"
sizeToggleSize"xs" | "sm" | "base" | "lg""sm"
borderRadiusToggleBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full"-

ToolbarRect

used by rect
PropTypeDefault
toprequirednumber-
leftrequirednumber-
widthnumber-
heightrequirednumber-

ToolbarTool

used by tools
PropTypeDefault
onClick() => void-
loadingboolean-
tooltipstring-
iconIconSvgElement-
menureadonly ToolbarMenuRow[]-
onOpenChange(open: boolean) => void-
panelReactNode-
openboolean-
choicesreadonly AlignmentChoice[]-
valuestring-
onValueChange(value: string) => void-

ToolbarMenuRow

used by its parts
PropTypeDefault
labelrequiredstring-
iconIconSvgElement-
toneToolbarMenuRowTone"neutral" | "destructive"-
onClick() => void-

AlignmentChoice

used by its parts
PropTypeDefault
labelrequiredstring-
valuerequiredT-
iconrequiredIconSvgElement-

It also takes the props of Radix Toolbar.Root, Radix Popover.Root, Radix Popover.Content.