Blocks / Canvas toolbar

Canvas toolbar

The tool dock of an editor: grouped tools, an active one, variants in a menu, undo and redo. Horizontal or vertical.

import { CanvasToolbar, formatShortcut } from "@forgeron/ui";
/01 → Default
TOOL SELECT · UNDO 2 · REDO 0 · TRY V H R O P T
import {
  CircleIcon,
  Cursor02Icon,
  HandIcon,
  LineIcon,
  Pen01Icon,
  Square01Icon,
  TextIcon,
  TriangleIcon,
} from "@forgeron/icons";
import { CanvasToolbar } from "@forgeron/ui";
import { useState } from "react";

const tools = [
  { id: "select", label: "Select", icon: Cursor02Icon, shortcut: "v", group: "pointer" },
  { id: "hand", label: "Hand", icon: HandIcon, shortcut: "h", group: "pointer" },
  {
    id: "shape",
    label: "Shape",
    icon: Square01Icon,
    shortcut: "r",
    group: "draw",
    variants: [
      { id: "rect", label: "Rectangle", icon: Square01Icon },
      { id: "ellipse", label: "Ellipse", icon: CircleIcon, shortcut: "o" },
      { id: "triangle", label: "Triangle", icon: TriangleIcon },
      { id: "line", label: "Line", icon: LineIcon, shortcut: "l" },
    ],
  },
  { id: "pen", label: "Pen", icon: Pen01Icon, shortcut: "p", group: "draw" },
  { id: "text", label: "Text", icon: TextIcon, shortcut: "t", group: "draw" },
];

export default function Default() {
  const [tool, setTool] = useState("select");
  const [history, setHistory] = useState({ past: 2, future: 0 });
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 12, alignItems: "flex-start" }}>
      <CanvasToolbar
        label="Drawing tools"
        tools={tools}
        value={tool}
        onChange={setTool}
        canUndo={history.past > 0}
        canRedo={history.future > 0}
        onUndo={() => setHistory(({ past, future }) => ({ past: past - 1, future: future + 1 }))}
        onRedo={() => setHistory(({ past, future }) => ({ past: past + 1, future: future - 1 }))}
      />
      <span style={{ font: "400 12px var(--font-mono)", color: "var(--ink-muted)" }}>
        TOOL {tool.toUpperCase()} · UNDO {history.past} · REDO {history.future} · TRY V H R O P T
      </span>
    </div>
  );
}
/02 → Vertical
import { Cursor02Icon, Eraser01Icon, HandIcon, Pen01Icon, StickyNote02Icon } from "@forgeron/icons";
import { CanvasToolbar } from "@forgeron/ui";

const tools = [
  { id: "select", label: "Select", icon: Cursor02Icon, shortcut: "v" },
  { id: "hand", label: "Hand", icon: HandIcon, shortcut: "h" },
  { id: "pen", label: "Pen", icon: Pen01Icon, shortcut: "p", group: "draw" },
  {
    id: "eraser",
    label: "Eraser",
    icon: Eraser01Icon,
    shortcut: "e",
    group: "draw",
    disabled: true,
  },
  { id: "note", label: "Sticky note", icon: StickyNote02Icon, shortcut: "n", group: "draw" },
];

export default function Vertical() {
  return (
    <CanvasToolbar
      label="Board tools"
      orientation="vertical"
      floating
      tools={tools}
      defaultValue="select"
      onUndo={() => {}}
      onRedo={() => {}}
      canRedo={false}
      shortcuts={false}
    />
  );
}
API → Props
PropTypeDefaultDescription
labelstring—Names the dock: “Drawing tools”.
tools{ id, label, icon, shortcut?, group?, variants?, disabled? }[]—Tools with the same group sit together, a line between groups. variants: { id, label, icon?, shortcut? }[] in a menu beside the tool; the one chosen gives its icon to it.
value / defaultValue / onChangestring; string; (id) => void—The active tool. Controlled or not.
variants / defaultVariants / onVariantChangeRecord<toolId, variantId>; (toolId, variantId) => void—The variant chosen for each tool.
orientation"horizontal" | "vertical""horizontal"Arrows move between tools (one tab stop); vertical puts tooltips and menus to the side.
onUndo / onRedo / canUndo / canRedo() => void; boolean—Undo and redo buttons at the end. ⌘Z and ⇧⌘Z are bound with them.
shortcutsbooleantrueBinds each tool's key on the page (not while typing). Hints show in the tooltip.
macboolean—Reads ⌘ (true) or Ctrl (false) in the hints. Default: from the platform.
floatingboolean—The shadow of a panel lifted off the canvas.
childrenReactNode—More controls at the end of the dock.
classNamestring—Extra classes.