Tool dock
A design editor's floating tool bar.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add tool-dockOr install the package
Don’t want the file? Import from
asta-ui/tool-dockand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { } from "asta-ui/tool-dock";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const EASE = "cubic-bezier(.16,1,.3,1)"; export interface DockTool { id: string; label: string; icon: ReactNode; /** A single key such as "V" or "R". Shown in the tooltip, and pressed anywhere to pick the tool when `hotkeys` is on. */ shortcut?: string; disabled?: boolean; } export interface ToolDockProps { /** Tools in order. Put the string "separator" between groups. */ tools: (DockTool | "separator")[]; value?: string; defaultValue?: string; onChange?: (id: string) => void; /** Pick a tool by pressing its shortcut, unless the user is typing in a field. Default true. */ hotkeys?: boolean; /** Where the tooltips open. */ tooltip?: "top" | "bottom"; label?: string; className?: string; } const typing = (t: EventTarget | null) => t instanceof HTMLElement && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName)); /** * The floating tool bar of a design editor. A highlight slides to the active tool, each tool shows its name and * shortcut on hover, ← → move between tools, and single-key shortcuts work from anywhere on the page. */ export function ToolDock({ tools, value, defaultValue, onChange, hotkeys = true, tooltip = "top", label = "Tools", className }: ToolDockProps) { const list = tools.filter((t): t is DockTool => t !== "separator"); const [inner, setInner] = useState(defaultValue ?? list[0]?.id); const current = value ?? inner; const [hover, setHover] = useState<string | null>(null); const [pill, setPill] = useState<{ x: number; w: number } | null>(null); const refs = useRef(new Map<string, HTMLButtonElement>()); const box = useRef<HTMLDivElement>(null); const pick = (id: string) => { if (value === undefined) setInner(id); onChange?.(id); }; const pickRef = useRef(pick); useLayoutEffect(() => { pickRef.current = pick; }); // Measure the active button once per change or resize, never per frame. useLayoutEffect(() => { const measure = () => { // The button sits in a positioned wrapper (for its tooltip), so measure the wrapper against the bar. const el = current ? refs.current.get(current)?.parentElement : null; if (el) setPill({ x: el.offsetLeft, w: el.offsetWidth }); }; // ResizeObserver reports once right after layout, so there is no layout read while the page mounts. const ro = new ResizeObserver(measure); if (box.current) ro.observe(box.current); return () => ro.disconnect(); }, [current]); useEffect(() => { if (!hotkeys) return; const keys = new Map(list.filter((t) => t.shortcut && !t.disabled).map((t) => [t.shortcut!.toLowerCase(), t.id])); const onKey = (e: globalThis.KeyboardEvent) => { if (e.metaKey || e.ctrlKey || e.altKey || typing(e.target)) return; const id = keys.get(e.key.toLowerCase()); if (id) pickRef.current(id); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [hotkeys, list.map((t) => `${t.id}:${t.shortcut}:${t.disabled}`).join()]); // eslint-disable-line react-hooks/exhaustive-deps const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => { const enabled = list.filter((t) => !t.disabled); const i = enabled.findIndex((t) => t.id === (document.activeElement as HTMLElement | null)?.dataset.tool); let next = -1; if (e.key === "ArrowRight") next = (i + 1) % enabled.length; else if (e.key === "ArrowLeft") next = (i - 1 + enabled.length) % enabled.length; else if (e.key === "Home") next = 0; else if (e.key === "End") next = enabled.length - 1; if (next < 0) return; e.preventDefault(); refs.current.get(enabled[next].id)?.focus(); }; return ( <div ref={box} role="toolbar" aria-label={label} onKeyDown={onKeyDown} className={cx( "relative inline-flex items-center gap-0.5 rounded-[12px] bg-paper p-1", "shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent),0_12px_32px_-14px_rgb(0_0_0/0.3),0_2px_6px_-3px_rgb(0_0_0/0.12)]", className, )} > {pill && ( <span aria-hidden className="pointer-events-none absolute left-0 top-1 h-[calc(100%-8px)] rounded-[8px] bg-brand motion-reduce:transition-none" style={{ width: pill.w, transform: `translateX(${pill.x}px)`, transition: `transform .32s ${EASE}, width .32s ${EASE}` /* perf-ok: width follows the active tool once per change */ }} /> )} {tools.map((t, i) => t === "separator" ? ( <span key={`sep-${i}`} aria-hidden className="mx-1 h-5 w-px bg-[color-mix(in_srgb,var(--color-ink)_12%,transparent)]" /> ) : ( <span key={t.id} className="relative"> <button ref={(el) => { if (el) refs.current.set(t.id, el); else refs.current.delete(t.id); }} type="button" data-tool={t.id} aria-label={t.label} aria-pressed={current === t.id} aria-keyshortcuts={t.shortcut} disabled={t.disabled} tabIndex={current === t.id ? 0 : -1} onClick={() => pick(t.id)} onPointerEnter={() => setHover(t.id)} onPointerLeave={() => setHover((h) => (h === t.id ? null : h))} onFocus={() => setHover(t.id)} onBlur={() => setHover((h) => (h === t.id ? null : h))} className={cx( "relative grid h-9 w-9 cursor-pointer place-items-center rounded-[8px] outline-none transition-colors duration-150", "focus-visible:shadow-[0_0_0_2px_var(--color-paper),0_0_0_4px_var(--color-brand)] disabled:cursor-not-allowed disabled:opacity-35", current === t.id ? "text-on-brand" : "text-ink-2 hover:bg-[color-mix(in_srgb,var(--color-ink)_7%,transparent)] hover:text-ink", )} > <span className="h-[18px] w-[18px] [&>svg]:h-full [&>svg]:w-full">{t.icon}</span> </button> <span role="tooltip" className={cx( "pointer-events-none absolute left-1/2 z-10 flex items-center gap-2 whitespace-nowrap rounded-[7px] bg-ink px-2 py-1 text-[12px] text-paper", "transition-[opacity,transform] duration-150", tooltip === "top" ? "bottom-full mb-2" : "top-full mt-2", hover === t.id ? "-translate-x-1/2 translate-y-0 opacity-100" : cx("-translate-x-1/2 opacity-0", tooltip === "top" ? "translate-y-1" : "-translate-y-1"), )} > {t.label} {t.shortcut && <span className="rounded-[4px] bg-[color-mix(in_srgb,var(--color-paper)_18%,transparent)] px-1 font-mono text-[10.5px]">{t.shortcut}</span>} </span> </span> ), )} </div> ); }Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Tool dock component to my project. A design editor's floating tool bar. It includes: sliding highlight, tooltips, shortcuts, separators, arrow keys, disabled. Install it: ```sh pnpm dlx asta-ui@latest add tool-dock --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/tool-dock.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { ToolDock, type DockTool } from "@/components/astabyte/tool-dock"; ``` Usage example and props: https://ui.astabyte.com/tool-dock Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- Needs asta.js: aria-pressed, the sliding pill, arrow keys and the V/F/R/P/C hotkeys. --> <div style="display:flex;justify-content:center;padding:48px 16px 16px"> <div role="toolbar" aria-label="Tools" data-asta-tooldock id="ex-dock-one" data-hotkeys class="asta-tool-dock"> <span data-asta-tooldock-pill aria-hidden="true" class="asta-tool-dock-pill"></span> <span class="asta-tool-dock-tool"><button type="button" data-tool="move" aria-label="Move" aria-pressed="true" aria-keyshortcuts="V" data-shortcut="V" tabindex="0" class="asta-tool-dock-btn"><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M3 2.5l9 4-4 1.5-1.5 4z"/></svg></span></button><span aria-hidden="true" class="asta-tool-dock-tip">Move <span>V</span></span></span> <span class="asta-tool-dock-tool"><button type="button" data-tool="frame" aria-label="Frame" aria-pressed="false" aria-keyshortcuts="F" data-shortcut="F" tabindex="-1" class="asta-tool-dock-btn"><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M5 2v12M11 2v12M2 5h12M2 11h12"/></svg></span></button><span aria-hidden="true" class="asta-tool-dock-tip">Frame <span>F</span></span></span> <span class="asta-tool-dock-tool"><button type="button" data-tool="rect" aria-label="Rectangle" aria-pressed="false" aria-keyshortcuts="R" data-shortcut="R" tabindex="-1" class="asta-tool-dock-btn"><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M3 3.5h10v9H3z"/></svg></span></button><span aria-hidden="true" class="asta-tool-dock-tip">Rectangle <span>R</span></span></span> <span aria-hidden="true" class="asta-tool-dock-sep"></span> <span class="asta-tool-dock-tool"><button type="button" data-tool="pen" aria-label="Pen" aria-pressed="false" aria-keyshortcuts="P" data-shortcut="P" tabindex="-1" class="asta-tool-dock-btn"><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M10.5 2.5l3 3-8 8h-3v-3z"/></svg></span></button><span aria-hidden="true" class="asta-tool-dock-tip">Pen <span>P</span></span></span> <span class="asta-tool-dock-tool"><button type="button" data-tool="comment" aria-label="Comment" aria-pressed="false" aria-keyshortcuts="C" data-shortcut="C" tabindex="-1" class="asta-tool-dock-btn"><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M2.5 3.5h11v7.5H7l-3 2.5v-2.5H2.5z"/></svg></span></button><span aria-hidden="true" class="asta-tool-dock-tip">Comment <span>C</span></span></span> </div> </div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { ToolDock, type DockTool } from "@/components/astabyte/tool-dock";
const svg = (d: string) => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.7} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d={d} />
</svg>
);
const TOOLS: (DockTool | "separator")[] = [
{ id: "move", label: "Move", shortcut: "V", icon: svg("M5 3.5 18 10l-5.6 1.9L10 17.5z") },
{ id: "hand", label: "Hand", shortcut: "H", icon: svg("M8 11V6.5a1.5 1.5 0 0 1 3 0V11m0-1.5V5a1.5 1.5 0 0 1 3 0v5.5m0-4a1.5 1.5 0 0 1 3 0V14a6 6 0 0 1-6 6h-.5a6 6 0 0 1-4.6-2.2L4.6 15a1.5 1.5 0 0 1 2.3-1.9L8 14.5") },
"separator",
{ id: "frame", label: "Frame", shortcut: "F", icon: svg("M8 3v18M16 3v18M3 8h18M3 16h18") },
{ id: "rect", label: "Rectangle", shortcut: "R", icon: svg("M4.5 5.5h15v13h-15z") },
{ id: "ellipse", label: "Ellipse", shortcut: "O", icon: svg("M12 4.5a7.5 7.5 0 1 1 0 15 7.5 7.5 0 0 1 0-15z") },
{ id: "pen", label: "Pen", shortcut: "P", icon: svg("m12 19 7-7-3.5-3.5-7 7L7 19zM15.5 8.5 13 3 5 9l2.5 2.5") },
{ id: "text", label: "Text", shortcut: "T", icon: svg("M5 6V4.5h14V6M12 4.5v15M9 19.5h6") },
"separator",
{ id: "comment", label: "Comment", shortcut: "C", icon: svg("M5 5.5h14v10H10l-4 3.5v-3.5H5z") },
];
const HINT: Record<string, string> = {
move: "Click a layer to select it. Drag to move.",
hand: "Drag to pan around the canvas.",
frame: "Drag to draw a frame. Layers inside it are clipped.",
rect: "Drag to draw a rectangle. Hold Shift for a square.",
ellipse: "Drag to draw an ellipse. Hold Shift for a circle.",
pen: "Click to add points. Close the path on the first point.",
text: "Click anywhere to start typing.",
comment: "Click to leave a comment for your team.",
};
export default function ToolDockDemo() {
const [tool, setTool] = useState("move");
return (
<div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
<div className="relative flex h-[340px] w-full max-w-2xl flex-col items-center justify-between overflow-hidden rounded-[14px] bg-paper-2 p-5">
<p className="text-center text-[13px] text-ink-2">{HINT[tool]}</p>
<div className="grid h-28 w-44 place-items-center rounded-[6px] bg-paper text-[12px] text-mute shadow-[0_10px_30px_-18px_rgb(0_0_0/0.4)]">Frame 1</div>
<ToolDock tools={TOOLS} value={tool} onChange={setTool} />
</div>
</div>
);
}