Morphing Toolbar

A drawing board with one dark toolbar that turns into whichever tool's options you need.

Pick a tool below and start sketching

Keys: V select, P pen, S shapes, T text, E eraser, Esc to go back. Cmd or Ctrl Z undoes.

A board to sketch on, with a single dark toolbar at the bottom. Pick the pen and the bar grows into the pen's options, ink colour and stroke width, while the pen's own glyph flies across the bar to become the way back. Shapes, text and the eraser each do the same. Colour is a view of its own, and Clear asks before it empties the board.

Draw with a mouse, pen or finger. The toolbar is fully keyboard operable: V selects, P draws, S picks a shape, T types, E erases, Esc steps back, Cmd Z or Ctrl Z undoes, Shift Cmd Z or Ctrl Y redoes, and Delete or Backspace removes the selected object. The keys belong to the board while the pointer is over it or focus is inside it. A focused size slider keeps its own arrow, Home, End and page keys, and every other shortcut still reaches the board. Drawing itself needs a pointer.

How it works

  • Every view is mounted the whole time. They sit stacked in the middle of the bar and only the top of the stack shows, so a change of view is three things on one frame.
  • The bar's width springs to the new view's own width, so the surface grows and shrinks around what it holds.
  • The old view leaves and the new one arrives sideways, a few pixels, with a touch of blur, in the direction the stack moved.
  • Shared buttons fly across. A button that exists in both views, such as the pen glyph, travels from its place in the row to its place in the new view. The flight is a clone driven by the Web Animations API on a spring sampled into linear(), so React never renders per frame.
  • The board is a plain list. canvas.ts holds what is drawn as data and how to paint and hit it. The list is repainted on a 2D canvas whenever it changes, so undo is a copy of the list.
  • Hidden views are inert, so they cannot be focused or clicked while another is showing.
  • Reduced motion replaces the flight and the slide with a short cross-fade.
  • A narrow stage scales the bar down to fit, but never below half its size, so a stage narrower than half the bar still clips it.
  • Resizing the stage carries the drawing with it. The objects are kept in stage pixels, so they and every step of history are scaled when the stage changes size.

Installation

pnpm dlx shadcn@latest add @uitopia/morphing-toolbar

Usage

import MorphingToolbar from "@/components/ui/morphing-toolbar";

<MorphingToolbar />;

The board takes the width of its container and keeps an 8:5 shape, with a minimum height of 19.5rem, so in a very narrow container it grows taller than 8:5. It paints in the current text colour, so it follows the theme. Pass colors to change the ink choices, where a value is any CSS colour or "ink" for the theme's own. The first choice is the starting ink.

<MorphingToolbar
  className="max-w-2xl"
  colors={[
    ["ink", "Ink"],
    ["#e5484d", "Red"],
    ["#0090ff", "Blue"],
  ]}
  hint={false}
/>

API

PropTypeDefaultDescription
colorsreadonly (readonly [string, string])[]seven inksThe ink choices as [value, name].
hintstring | falsethe key listThe line under the board. false hides it.
placeholderstring | falsea short lineWhat the empty board says. false hides it.
classNamestring-Classes for the outer wrapper.