{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "whiteboard-canvas",
  "title": "Whiteboard Canvas",
  "description": "Interactive Whiteboard Canvas with light and dark themes.",
  "dependencies": [
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/whiteboard-canvas/whiteboard-canvas.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport {\n  MousePointer2,\n  Pencil,\n  Square,\n  Circle,\n  Eraser,\n  Undo2,\n  Redo2,\n  Download,\n  Trash2,\n  ArrowUpRight,\n  Layers,\n} from \"lucide-react\";\nimport { clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport interface WhiteboardShape {\n  id: string;\n  type: \"pen\" | \"rectangle\" | \"ellipse\";\n  color: string;\n  label?: string;\n  points: { x: number; y: number }[];\n}\nexport interface WhiteboardCanvasProps {\n  title?: string;\n  theme?: \"dark\" | \"light\";\n  initialShapes?: WhiteboardShape[];\n  onShapesChange?: (shapes: WhiteboardShape[]) => void;\n  className?: string;\n}\ntype Tool = \"select\" | \"pen\" | \"rectangle\" | \"ellipse\" | \"eraser\";\nconst tools = [\n  { id: \"select\", name: \"Select\", icon: MousePointer2 },\n  { id: \"pen\", name: \"Pen\", icon: Pencil },\n  { id: \"rectangle\", name: \"Rectangle\", icon: Square },\n  { id: \"ellipse\", name: \"Ellipse\", icon: Circle },\n  { id: \"eraser\", name: \"Eraser\", icon: Eraser },\n] as const;\nconst seed: WhiteboardShape[] = [\n  {\n    id: \"a\",\n    label: \"Capture insights\",\n    type: \"rectangle\",\n    color: \"#8b8b95\",\n    points: [\n      { x: 80, y: 160 },\n      { x: 290, y: 280 },\n    ],\n  },\n  {\n    id: \"b\",\n    label: \"Explore directions\",\n    type: \"rectangle\",\n    color: \"#9690da\",\n    points: [\n      { x: 375, y: 160 },\n      { x: 585, y: 280 },\n    ],\n  },\n  {\n    id: \"c\",\n    label: \"Define the next step\",\n    type: \"rectangle\",\n    color: \"#79a798\",\n    points: [\n      { x: 670, y: 160 },\n      { x: 880, y: 280 },\n    ],\n  },\n  {\n    id: \"d\",\n    type: \"pen\",\n    color: \"#62626e\",\n    points: [\n      { x: 305, y: 220 },\n      { x: 358, y: 220 },\n      { x: 352, y: 214 },\n      { x: 358, y: 220 },\n      { x: 352, y: 226 },\n    ],\n  },\n  {\n    id: \"e\",\n    type: \"pen\",\n    color: \"#62626e\",\n    points: [\n      { x: 600, y: 220 },\n      { x: 653, y: 220 },\n      { x: 647, y: 214 },\n      { x: 653, y: 220 },\n      { x: 647, y: 226 },\n    ],\n  },\n];\nfunction geometry(shape: WhiteboardShape) {\n  const a = shape.points[0],\n    b = shape.points[shape.points.length - 1];\n  return {\n    x: Math.min(a.x, b.x),\n    y: Math.min(a.y, b.y),\n    width: Math.abs(b.x - a.x),\n    height: Math.abs(b.y - a.y),\n  };\n}\nfunction Shape({ shape }: { shape: WhiteboardShape }) {\n  if (shape.type === \"pen\")\n    return (\n      <path\n        d={shape.points\n          .map((p, i) => `${i ? \"L\" : \"M\"}${p.x},${p.y}`)\n          .join(\" \")}\n        fill=\"none\"\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      />\n    );\n  const box = geometry(shape);\n  return shape.type === \"rectangle\" ? (\n    <rect {...box} rx={8} fill={`${shape.color}0d`} />\n  ) : (\n    <ellipse\n      cx={box.x + box.width / 2}\n      cy={box.y + box.height / 2}\n      rx={box.width / 2}\n      ry={box.height / 2}\n      fill={`${shape.color}0d`}\n    />\n  );\n}\nexport function WhiteboardCanvas({\n  title = \"Discovery canvas\",\n  theme = \"dark\",\n  initialShapes = seed,\n  onShapesChange,\n  className,\n}: WhiteboardCanvasProps) {\n  const [shapes, setShapes] = React.useState<WhiteboardShape[]>(() =>\n    structuredClone(initialShapes),\n  );\n  const [tool, setTool] = React.useState<Tool>(\"select\");\n  const [color, setColor] = React.useState(\"#9690da\");\n  const [selected, setSelected] = React.useState<string | null>(null);\n  const [past, setPast] = React.useState<WhiteboardShape[][]>([]);\n  const [future, setFuture] = React.useState<WhiteboardShape[][]>([]);\n  const [message, setMessage] = React.useState(\"\");\n  const svg = React.useRef<SVGSVGElement>(null);\n  const patternId = React.useId().replace(/:/g, \"\");\n  const current = React.useRef(shapes);\n  const gesture = React.useRef<{\n    id: string;\n    start: { x: number; y: number };\n    before: WhiteboardShape[];\n  } | null>(null);\n  const dark = theme === \"dark\";\n  const update = (next: WhiteboardShape[]) => {\n    current.current = next;\n    setShapes(next);\n    onShapesChange?.(next);\n  };\n  const record = (before: WhiteboardShape[]) => {\n    setPast((history) => [...history.slice(-49), before]);\n    setFuture([]);\n  };\n  const finish = (cancel = false) => {\n    const g = gesture.current;\n    if (!g) return;\n    gesture.current = null;\n    if (cancel) update(g.before);\n    else if (JSON.stringify(g.before) !== JSON.stringify(current.current))\n      record(g.before);\n  };\n  const point = (event: React.PointerEvent<SVGSVGElement>) => {\n    const p = svg.current!.createSVGPoint();\n    p.x = event.clientX;\n    p.y = event.clientY;\n    const transformed = p.matrixTransform(\n      svg.current!.getScreenCTM()!.inverse(),\n    );\n    return {\n      x: Math.max(0, Math.min(960, transformed.x)),\n      y: Math.max(0, Math.min(480, transformed.y)),\n    };\n  };\n  const undo = () => {\n    if (!past.length) return;\n    finish();\n    const snapshot = current.current;\n    setFuture((history) => [...history, snapshot]);\n    update(past[past.length - 1]);\n    setPast(past.slice(0, -1));\n    setSelected(null);\n  };\n  const redo = () => {\n    if (!future.length) return;\n    const snapshot = current.current;\n    setPast((history) => [...history, snapshot]);\n    update(future[future.length - 1]);\n    setFuture(future.slice(0, -1));\n    setSelected(null);\n  };\n  const remove = () => {\n    if (!selected) return;\n    record(current.current);\n    update(current.current.filter((shape) => shape.id !== selected));\n    setSelected(null);\n  };\n  const exportSvg = () => {\n    const copy = svg.current!.cloneNode(true) as SVGSVGElement;\n    copy.setAttribute(\"xmlns\", \"http://www.w3.org/2000/svg\");\n    copy.setAttribute(\"width\", \"960\");\n    copy.setAttribute(\"height\", \"480\");\n    copy.removeAttribute(\"class\");\n    copy.removeAttribute(\"tabindex\");\n    copy.style.background = dark ? \"#0e0e10\" : \"#f6f6f8\";\n    copy\n      .querySelectorAll('[data-selection=\"true\"]')\n      .forEach((node) => node.remove());\n    const url = URL.createObjectURL(\n      new Blob([new XMLSerializer().serializeToString(copy)], {\n        type: \"image/svg+xml\",\n      }),\n    );\n    const a = document.createElement(\"a\");\n    a.href = url;\n    a.download = \"whiteboard.svg\";\n    a.click();\n    setTimeout(() => URL.revokeObjectURL(url), 1000);\n    setMessage(\"Board exported as SVG.\");\n  };\n  const active = shapes.find((shape) => shape.id === selected);\n  const bounds = active\n    ? {\n        x: Math.min(...active.points.map((p) => p.x)),\n        y: Math.min(...active.points.map((p) => p.y)),\n        width:\n          Math.max(...active.points.map((p) => p.x)) -\n          Math.min(...active.points.map((p) => p.x)),\n        height:\n          Math.max(...active.points.map((p) => p.y)) -\n          Math.min(...active.points.map((p) => p.y)),\n      }\n    : null;\n  return (\n    <section\n      aria-label=\"Whiteboard canvas\"\n      className={twMerge(\n        clsx(\n          \"w-full overflow-hidden rounded-xl border font-sans antialiased\",\n          className,\n        ),\n      )}\n      style={{\n        background: dark ? \"#111113\" : \"#fcfcfd\",\n        color: dark ? \"#ededef\" : \"#27282c\",\n        borderColor: dark ? \"#ffffff12\" : \"#18181b16\",\n      }}\n    >\n      <header className=\"flex flex-wrap items-center justify-between gap-4 border-b border-current/10 px-6 py-3\">\n        <div className=\"flex items-center gap-3\">\n          <span className=\"flex size-7 items-center justify-center rounded-md border border-current/10\">\n            <Layers size={15} className=\"opacity-60\" />\n          </span>\n          <div>\n            <h2 className=\"text-sm font-medium tracking-tight\">{title}</h2>\n            <p className=\"mt-0.5 text-[10px] opacity-40\">\n              Workspace / Product discovery\n            </p>\n          </div>\n        </div>\n        <button\n          onClick={exportSvg}\n          className=\"flex items-center gap-2 rounded-md border border-current/10 px-3 py-1.5 text-[11px] hover:bg-current/5\"\n        >\n          <Download size={12} /> Export SVG\n        </button>\n      </header>\n      <div className=\"flex flex-wrap items-end justify-between gap-4 px-7 pb-6 pt-7\">\n        <div>\n          <p className=\"mb-2 text-[10px] tracking-[0.12em] opacity-35\">\n            PROJECT / CANVAS\n          </p>\n          <h3 className=\"text-[23px] font-medium tracking-[-0.035em]\">\n            From a thought to a direction.\n          </h3>\n          <p className=\"mt-2 text-xs opacity-45\">\n            Connect the pieces. Find the next thing worth building.\n          </p>\n        </div>\n        <span className=\"rounded-full border border-current/10 px-2.5 py-1 text-[10px] opacity-50\">\n          Exploration\n        </span>\n      </div>\n      <div className=\"flex flex-wrap items-center justify-between gap-3 border-y border-current/[0.07] px-6 py-2.5\">\n        <div\n          role=\"group\"\n          aria-label=\"Drawing tools\"\n          className=\"flex gap-1 rounded-lg border border-current/[0.07] p-1\"\n        >\n          {tools.map(({ id, name, icon: Icon }) => (\n            <button\n              key={id}\n              aria-label={name}\n              title={name}\n              aria-pressed={tool === id}\n              onClick={() => {\n                finish();\n                setTool(id);\n              }}\n              className=\"rounded-md p-2 focus-visible:outline-2 focus-visible:outline-offset-2\"\n              style={{\n                background:\n                  tool === id ? (dark ? \"#2c2940\" : \"#eeecf8\") : undefined,\n                color: tool === id ? (dark ? \"#c2b5ff\" : \"#6552a0\") : undefined,\n              }}\n            >\n              <Icon size={16} />\n            </button>\n          ))}\n        </div>\n        <div className=\"flex items-center gap-3\">\n          <div role=\"group\" aria-label=\"Stroke color\" className=\"flex gap-2\">\n            {[\"#9690da\", \"#79a798\", \"#bda478\", \"#b88795\"].map((value) => (\n              <button\n                key={value}\n                aria-label={`Color ${value}`}\n                aria-pressed={color === value}\n                onClick={() => {\n                  setColor(value);\n                  if (selected) {\n                    record(current.current);\n                    update(\n                      current.current.map((shape) =>\n                        shape.id === selected\n                          ? { ...shape, color: value }\n                          : shape,\n                      ),\n                    );\n                  }\n                }}\n                className=\"size-4 rounded-full outline-offset-2 focus-visible:outline-2\"\n                style={{\n                  background: value,\n                  outline: color === value ? `1px solid ${value}` : undefined,\n                }}\n              />\n            ))}\n          </div>\n          <div className=\"h-5 border-l border-current/15\" />\n          <button\n            aria-label=\"Undo\"\n            disabled={!past.length}\n            onClick={undo}\n            className=\"rounded p-1.5 disabled:opacity-20\"\n          >\n            <Undo2 size={15} />\n          </button>\n          <button\n            aria-label=\"Redo\"\n            disabled={!future.length}\n            onClick={redo}\n            className=\"rounded p-1.5 disabled:opacity-20\"\n          >\n            <Redo2 size={15} />\n          </button>\n          <button\n            aria-label=\"Delete selected shape\"\n            disabled={!selected}\n            onClick={remove}\n            className=\"rounded p-1.5 disabled:opacity-20\"\n          >\n            <Trash2 size={14} />\n          </button>\n        </div>\n      </div>\n      <svg\n        ref={svg}\n        viewBox=\"0 0 960 480\"\n        role=\"img\"\n        aria-label=\"Interactive drawing surface. Choose a tool to draw; select shapes to move them. Use the shape list below for keyboard selection.\"\n        tabIndex={0}\n        className=\"block aspect-[2/1] min-h-[260px] w-full touch-none outline-offset-[-3px] focus-visible:outline-2\"\n        style={{\n          cursor: tool === \"select\" ? \"default\" : \"crosshair\",\n          background: dark\n            ? \"radial-gradient(ellipse at 50% 40%, #9690da06, transparent 65%), #0e0e10\"\n            : \"#f6f6f8\",\n        }}\n        onKeyDown={(event) => {\n          if (\n            (event.metaKey || event.ctrlKey) &&\n            event.key.toLowerCase() === \"z\"\n          ) {\n            event.preventDefault();\n            if (event.shiftKey) redo();\n            else undo();\n          }\n          if (event.key === \"Escape\") {\n            finish(true);\n            setSelected(null);\n          }\n          if (event.key === \"Delete\" || event.key === \"Backspace\") {\n            event.preventDefault();\n            remove();\n          }\n          const delta: Record<string, number[]> = {\n            ArrowLeft: [-5, 0],\n            ArrowRight: [5, 0],\n            ArrowUp: [0, -5],\n            ArrowDown: [0, 5],\n          };\n          if (selected && delta[event.key]) {\n            event.preventDefault();\n            record(current.current);\n            const [x, y] = delta[event.key];\n            update(\n              current.current.map((shape) =>\n                shape.id === selected\n                  ? {\n                      ...shape,\n                      points: shape.points.map((p) => ({\n                        x: p.x + x,\n                        y: p.y + y,\n                      })),\n                    }\n                  : shape,\n              ),\n            );\n          }\n        }}\n        onPointerDown={(event) => {\n          if (event.button !== 0) return;\n          event.preventDefault();\n          event.currentTarget.focus();\n          event.currentTarget.setPointerCapture(event.pointerId);\n          const start = point(event);\n          const target = (event.target as Element)\n            .closest(\"[data-shape-id]\")\n            ?.getAttribute(\"data-shape-id\");\n          if (tool === \"eraser\") {\n            if (target) {\n              record(current.current);\n              update(current.current.filter((shape) => shape.id !== target));\n              setSelected(null);\n            }\n            return;\n          }\n          if (tool === \"select\") {\n            setSelected(target ?? null);\n            if (target)\n              gesture.current = { id: target, start, before: current.current };\n            return;\n          }\n          const id = crypto.randomUUID();\n          gesture.current = { id, start, before: current.current };\n          update([\n            ...current.current,\n            {\n              id,\n              type: tool,\n              color,\n              points: [start, { x: start.x + 0.1, y: start.y + 0.1 }],\n            },\n          ]);\n          setSelected(null);\n        }}\n        onPointerMove={(event) => {\n          const g = gesture.current;\n          if (!g) return;\n          const p = point(event);\n          if (tool === \"select\") {\n            const original = g.before.find((shape) => shape.id === g.id)!;\n            update(\n              current.current.map((shape) =>\n                shape.id === g.id\n                  ? {\n                      ...shape,\n                      points: original.points.map((a) => ({\n                        x: a.x + p.x - g.start.x,\n                        y: a.y + p.y - g.start.y,\n                      })),\n                    }\n                  : shape,\n              ),\n            );\n          } else\n            update(\n              current.current.map((shape) =>\n                shape.id === g.id\n                  ? {\n                      ...shape,\n                      points:\n                        tool === \"pen\" ? [...shape.points, p] : [g.start, p],\n                    }\n                  : shape,\n              ),\n            );\n        }}\n        onPointerUp={() => finish()}\n        onPointerCancel={() => finish(true)}\n      >\n        <defs>\n          <pattern\n            id={patternId}\n            width=\"24\"\n            height=\"24\"\n            patternUnits=\"userSpaceOnUse\"\n          >\n            <circle\n              cx=\"1\"\n              cy=\"1\"\n              r=\"0.65\"\n              fill={dark ? \"#ffffff0e\" : \"#18181b16\"}\n            />\n          </pattern>\n        </defs>\n        <rect\n          width=\"960\"\n          height=\"480\"\n          fill={`url(#${patternId})`}\n          pointerEvents=\"none\"\n        />\n        {shapes.map((shape) => (\n          <g\n            key={shape.id}\n            data-shape-id={shape.id}\n            stroke={dark ? shape.color : `color-mix(in srgb, ${shape.color} 65%, #18181b)`}\n            strokeWidth={shape.type === \"pen\" ? 1.5 : 1}\n            style={{ pointerEvents: \"visiblePainted\" }}\n          >\n            <Shape shape={shape} />\n            {shape.label && (\n              <text\n                x={geometry(shape).x + geometry(shape).width / 2}\n                y={geometry(shape).y + geometry(shape).height / 2 + 5}\n                textAnchor=\"middle\"\n                fill={dark ? shape.color : `color-mix(in srgb, ${shape.color} 65%, #18181b)`}\n                stroke=\"none\"\n                fontSize=\"13\"\n                fontFamily=\"sans-serif\"\n                pointerEvents=\"none\"\n              >\n                {shape.label}\n              </text>\n            )}\n          </g>\n        ))}\n        {bounds && (\n          <rect\n            data-selection=\"true\"\n            x={bounds.x - 7}\n            y={bounds.y - 7}\n            width={bounds.width + 14}\n            height={bounds.height + 14}\n            fill=\"none\"\n            stroke=\"#9690da\"\n            strokeWidth=\"1\"\n            strokeDasharray=\"4 4\"\n            pointerEvents=\"none\"\n          />\n        )}\n      </svg>\n      <footer className=\"flex flex-wrap items-center justify-between gap-2 border-t border-current/10 px-5 py-3\">\n        <div className=\"flex items-center gap-2 text-[10px] opacity-50\">\n          <span className=\"size-1.5 rounded-full bg-[#79a798]\" />\n          <span role=\"status\">\n            {message ||\n              `${shapes.length} objects · ${tool === \"select\" ? \"Select and move\" : `Draw with ${tool}`}`}\n          </span>\n        </div>\n        <span className=\"flex items-center gap-1 text-[10px] opacity-35\">\n          Built for early thinking. <ArrowUpRight size={11} />\n        </span>\n      </footer>\n      <div className=\"flex flex-wrap items-center gap-2 border-t border-current/10 px-5 py-2 text-[10px]\">\n        <span className=\"opacity-35\">Objects</span>\n        {shapes.map((shape, i) => (\n          <button\n            key={shape.id}\n            aria-label={`Select ${shape.type} ${i + 1}`}\n            aria-pressed={selected === shape.id}\n            onClick={() => {\n              setTool(\"select\");\n              setSelected(shape.id);\n              svg.current?.focus();\n            }}\n            className=\"rounded border border-current/10 px-2 py-1 hover:bg-current/5\"\n            style={{\n              background:\n                selected === shape.id\n                  ? dark\n                    ? \"#323039\"\n                    : \"#e6e3ef\"\n                  : undefined,\n            }}\n          >\n            {shape.type} {i + 1}\n          </button>\n        ))}\n      </div>\n    </section>\n  );\n}\nexport default WhiteboardCanvas;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}