{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "control-panel",
  "title": "Control panel",
  "description": "an inspector drawn from a config (`[default, min, max]`, a hex, a boolean, `{ type }`, nested = a section) · Copy, Paste, Reset all",
  "dependencies": [
    "cn@^0.5.2",
    "lucide-react@^1.52.0"
  ],
  "registryDependencies": [
    "@halftone-web/control-row",
    "@halftone-web/control-section",
    "@halftone-web/control-segmented",
    "@halftone-web/control-select",
    "@halftone-web/control-switch",
    "@halftone-web/curve-editor",
    "@halftone-web/kit-setup",
    "@halftone-web/lib-controls-bezier",
    "@halftone-web/lib-controls-schema",
    "@halftone-web/lib-controls-spring",
    "@halftone-web/lib-controls-transfer",
    "@halftone-web/number-field",
    "@halftone-web/presets",
    "@halftone-web/spring-editor",
    "@halftone-web/swatch",
    "@halftone-web/vec2-pad"
  ],
  "files": [
    {
      "path": "components/controls/control-panel.tsx",
      "content": "'use client';\nimport { cn } from 'cn';\nimport { ClipboardPasteIcon, CopyIcon, RotateCcwIcon } from 'lucide-react';\nimport * as React from 'react';\nimport { ControlRow } from '@/components/controls/control-row';\nimport { CurveEditor } from '@/components/controls/curve-editor';\nimport { NumberField } from '@/components/controls/number-field';\nimport { Presets, type Preset } from '@/components/controls/presets';\nimport { ControlSection } from '@/components/controls/section';\nimport { ControlSegmented } from '@/components/controls/segmented';\nimport { ControlSelect } from '@/components/controls/select';\nimport { SpringEditor } from '@/components/controls/spring-editor';\nimport { HexField } from '@/components/controls/swatch';\nimport { ControlSwitch } from '@/components/controls/switch';\nimport { Vec2Pad } from '@/components/controls/vec2-pad';\nimport type { Bezier } from '@/lib/controls/bezier';\nimport { defaultValues, getAt, isDefault, leaves, parseControls, setAt, type ControlConfig, type ControlNode, type LeafControl, type Values } from '@/lib/controls/schema';\nimport type { Spring } from '@/lib/controls/spring';\nimport { copyText, describePaste, parseSettings, readText, serializeSettings } from '@/lib/controls/transfer';\n\n/**\n * A panel's state from a config: the parsed controls, their values, and setters. Parsed once; pass a config that\n * doesn't change between renders (a module constant).\n */\nexport function useControls(config: ControlConfig, initial?: Values) {\n  const [nodes] = React.useState(() => parseControls(config));\n  const defaults = React.useMemo(() => defaultValues(nodes), [nodes]);\n  const [values, setValues] = React.useState<Values>(() => initial ?? defaults);\n  const set = React.useCallback((path: string, value: unknown) => setValues((v) => setAt(v, path, value)), []);\n  const reset = React.useCallback(() => setValues(defaults), [defaults]);\n  return { nodes, values, defaults, setValues, set, reset };\n}\n\nconst iconButton =\n  'text-muted-foreground hover:text-foreground hover:bg-accent focus-visible:ring-ring/50 inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium outline-none focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50';\n\nconst editedIn = (nodes: ControlNode[], values: Values) => leaves(nodes).filter((n) => !isDefault(n, getAt(values, n.path))).length;\n\n/**\n * A control panel drawn from a config (`lib/controls/schema.ts`): every value gets the control its type says, nested\n * objects become sections, and each control marks itself and offers Reset while it's off its default. The header copies\n * the values as JSON and pastes them back, checked against the config; Reset all goes back to the defaults.\n *\n * Controlled: pass `values` and `onChange` (from `useControls`, or your own state).\n */\nexport function ControlPanel({\n  title,\n  nodes,\n  values,\n  onChange,\n  presets,\n  transfer = true,\n  className,\n}: {\n  title: string;\n  nodes: ControlNode[];\n  values: Values;\n  onChange: (values: Values) => void;\n  presets?: readonly Preset[];\n  /** Copy and Paste in the header. */\n  transfer?: boolean;\n  className?: string;\n}) {\n  const [status, setStatus] = React.useState('');\n  const [pasteBox, setPasteBox] = React.useState(false);\n  const pasteId = React.useId();\n  const defaults = React.useMemo(() => defaultValues(nodes), [nodes]);\n  const edited = editedIn(nodes, values);\n\n  const paste = (text: string) => {\n    const result = parseSettings(text, nodes, values);\n    if (result.ok) onChange(result.values);\n    setStatus(describePaste(result));\n    return result.ok;\n  };\n\n  const onCopy = async () => {\n    const ok = await copyText(serializeSettings(values, title));\n    setStatus(ok ? 'Copied the settings as JSON.' : 'The browser didn’t allow copying. Select the values from the JSON yourself.');\n  };\n\n  const onPaste = async () => {\n    const text = await readText();\n    if (text === null) {\n      setPasteBox(true);\n      setStatus('Paste the settings into the box.');\n    } else paste(text);\n  };\n\n  const leaf = (n: LeafControl) => {\n    const value = getAt(values, n.path);\n    const set = (v: unknown) => onChange(setAt(values, n.path, v));\n    const off = !isDefault(n, value);\n    const reset = () => set(n.default);\n    const id = `ctl-${n.path}`;\n    const row = { label: n.label, edited: off, onReset: reset, hint: n.hint };\n    switch (n.kind) {\n      case 'number':\n        return (\n          <NumberField\n            key={n.path}\n            label={n.label}\n            value={value as number}\n            onChange={set}\n            min={n.min}\n            max={n.max}\n            step={n.step}\n            unit={n.unit}\n            defaultValue={n.default}\n            hint={n.hint}\n          />\n        );\n      case 'colour':\n        return (\n          <ControlRow key={n.path} {...row} htmlFor={id}>\n            <HexField id={id} label={n.label} value={value as string} onChange={set} />\n          </ControlRow>\n        );\n      case 'switch':\n        return <ControlRow key={n.path} {...row} htmlFor={id} trailing={<ControlSwitch id={id} label={n.label} checked={value as boolean} onChange={set} />} />;\n      case 'select':\n        return (\n          <ControlRow key={n.path} {...row} htmlFor={n.display === 'menu' ? id : undefined}>\n            {n.display === 'segmented' ? (\n              <ControlSegmented label={n.label} value={value as string} options={n.options} onChange={set} />\n            ) : (\n              <ControlSelect id={id} label={n.label} value={value as string} options={n.options} onChange={set} />\n            )}\n          </ControlRow>\n        );\n      case 'easing':\n        return (\n          <ControlRow key={n.path} {...row}>\n            <CurveEditor label={n.label} value={value as Bezier} onChange={set} />\n          </ControlRow>\n        );\n      case 'spring':\n        return (\n          <ControlRow key={n.path} {...row}>\n            <SpringEditor label={n.label} value={value as Spring} onChange={set} />\n          </ControlRow>\n        );\n      case 'vec2':\n        return (\n          <ControlRow key={n.path} {...row}>\n            <Vec2Pad label={n.label} value={value as [number, number]} onChange={set} min={n.min} max={n.max} step={n.step} />\n          </ControlRow>\n        );\n    }\n  };\n\n  const render = (list: ControlNode[]): React.ReactNode =>\n    list.map((n) =>\n      n.kind === 'section' ? (\n        <ControlSection key={n.path} title={n.label} defaultOpen={!n.collapsed} edited={editedIn(n.children, values)} className=\"border-border border-t pt-2\">\n          {render(n.children)}\n        </ControlSection>\n      ) : (\n        leaf(n)\n      )\n    );\n\n  return (\n    <div role=\"region\" aria-label={title} className={cn('bg-card text-card-foreground border-border flex min-w-0 flex-col gap-3 rounded-xl border p-4 shadow-sm', className)}>\n      <div className=\"flex min-h-7 items-center gap-1\">\n        <h2 className=\"text-foreground min-w-0 flex-1 truncate text-sm font-semibold\">{title}</h2>\n        {transfer ? (\n          <>\n            <button type=\"button\" onClick={onCopy} className={iconButton} title=\"Copy the settings as JSON\">\n              <CopyIcon className=\"size-3.5\" aria-hidden />\n              Copy\n            </button>\n            <button type=\"button\" onClick={onPaste} className={iconButton} title=\"Paste settings copied from a panel\">\n              <ClipboardPasteIcon className=\"size-3.5\" aria-hidden />\n              Paste\n            </button>\n          </>\n        ) : null}\n        <button type=\"button\" disabled={!edited} onClick={() => onChange(defaults)} className={iconButton} aria-label={edited ? `Reset all ${edited} changed settings` : 'Reset all: nothing changed'}>\n          <RotateCcwIcon className=\"size-3.5\" aria-hidden />\n          Reset\n        </button>\n      </div>\n      {presets?.length ? <Presets presets={presets} values={values} onApply={(p) => onChange(mergeValues(values, p.values))} /> : null}\n      {pasteBox ? (\n        <div className=\"flex flex-col gap-1.5\">\n          <label htmlFor={pasteId} className=\"text-muted-foreground text-xs font-medium\">\n            Settings JSON\n          </label>\n          <textarea\n            id={pasteId}\n            rows={4}\n            autoFocus\n            onPaste={(e) => {\n              if (paste(e.clipboardData.getData('text'))) setPasteBox(false);\n            }}\n            onKeyDown={(e) => {\n              if (e.key === 'Escape') setPasteBox(false);\n            }}\n            className=\"border-border bg-background focus-visible:ring-ring/50 min-w-0 rounded-md border p-2 font-mono text-xs outline-none focus-visible:ring-[3px]\"\n          />\n        </div>\n      ) : null}\n      <p role=\"status\" aria-live=\"polite\" className={cn('text-muted-foreground text-xs', !status && 'sr-only')}>\n        {status}\n      </p>\n      <div className=\"flex min-w-0 flex-col gap-3\">{render(nodes)}</div>\n    </div>\n  );\n}\n\n/** A preset's values over the current ones: what it doesn't set stays. */\nfunction mergeValues(current: Values, patch: Values): Values {\n  let out = current;\n  for (const [k, v] of Object.entries(patch)) {\n    const have = out[k];\n    out =\n      v && typeof v === 'object' && !Array.isArray(v) && have && typeof have === 'object' && !Array.isArray(have) && !('mode' in v)\n        ? { ...out, [k]: mergeValues(have as Values, v as Values) }\n        : { ...out, [k]: v };\n  }\n  return out;\n}\n\nexport { defaultValues, parseControls, type ControlConfig, type Values };\n",
      "type": "registry:component",
      "target": "components/controls/control-panel.tsx"
    }
  ],
  "meta": {
    "exports": [
      "ControlPanel",
      "useControls"
    ],
    "api": "const c = useControls(config); <ControlPanel title nodes={c.nodes} values={c.values} onChange={c.setValues} presets? />"
  },
  "categories": [
    "controls"
  ],
  "type": "registry:component"
}