The floating toolbar of a canvas, with tool flyouts and a design, prototype and inspect switch.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { designTools, ToolPalette } from "./ui/uicontext/components/canvas/ToolPalette.ts"import { CanvasMode, CanvasTool } from "./ui/uicontext/components/canvas/Viewport.ts"const Message = UiSchema.TaggedEnum({ PickedTool: { tool: CanvasTool }, PickedMode: { mode: CanvasMode } })// Shapes share one slot: the arrow beside it opens the rest, and the slot keeps the last one used.export const EditorTools = Html.component({ name: "EditorTools", model: Schema.Struct({ tool: CanvasTool, mode: CanvasMode, shown: Schema.Record(Schema.String, CanvasTool) }), message: Message,}).make({ init: () => ({ tool: "select" as const, mode: "design" as const, shown: {} }), update: { PickedTool: ($, { tool }) => { const group = designTools.find((g) => g.tools.some((t) => t.tool === tool)) return $.evolve({ tool, shown: (s) => (group === undefined ? s : { ...s, [group.id]: tool }) }) }, PickedMode: ($, { mode }) => $.evolve({ mode }), }, view: (h, m) => ToolPalette(h, { tool: m.tool, groups: designTools, shown: m.shown, onPick: (tool) => Message.PickedTool({ tool }), mode: { value: m.mode, options: ["design", "prototype", "inspect"], onPick: (mode) => Message.PickedMode({ mode }) }, }),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/tool-paletteUsage
import { boardTools, ToolPalette } from "./ui/uicontext/components/canvas/ToolPalette.ts"import { CanvasTool } from "./ui/uicontext/components/canvas/Viewport.ts"const Message = UiSchema.TaggedEnum({ PickedTool: { tool: CanvasTool } })export const Tools = (h: Html.H<typeof Message.Type>, tool: CanvasTool) => ToolPalette(h, { tool, groups: boardTools, onPick: (picked) => Message.PickedTool({ tool: picked }) })API reference
Options
| Name | Type | Description |
|---|---|---|
toolrequired | "select" | "hand" | "frame" | "rect" | "ellipse" | "line" | "arrow" | "text" | "sticky" | "pen" | "highlighter" | "eraser" | "comment" | "laser" | — |
groupsrequired | PaletteGroup[] | — |
onPickrequired | (tool: CanvasTool) => Message | — |
shown | Readonly<Record<string, CanvasTool>> | The tool each group shows, by group id: the caller remembers the last one used |
mode | { readonly value: CanvasMode readonly options: CanvasMode[] readonly onPick: (mode: CanvasMode) => Message } | — |
end | View.Children | Controls after the tools: undo and redo, a zoom |
style | Style | — |