{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "controllable",
  "title": "Controllable",
  "description": "A value a user may control: theirs when they pass one, kept inside the component when they don’t; and a value a ui-sdk part keeps, followed so the parts beside it can read it.",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/lib/controllable.ts",
      "content": "'use client'\n\nimport { useState } from 'react'\n\n/**\n * Whether `details` is a change its handler called off, after `cancel()`:\n * ui-sdk's change details, whose `canceled` turns true.\n */\nexport function isCanceled(details: unknown): boolean {\n  return (\n    typeof details === 'object' &&\n    details !== null &&\n    'canceled' in details &&\n    details.canceled === true\n  )\n}\n\n/**\n * A value a user may control: theirs when they pass one, kept here when they\n * don't. Setting it tells `onChange` with whatever follows the value, such as\n * ui-sdk's change details, and keeps it here only when it isn't theirs and\n * `onChange` didn't cancel it. Setting the value it already has does nothing.\n */\nexport function useControllable<T, Rest extends unknown[] = []>(\n  value: T | undefined,\n  initial: T,\n  onChange: ((value: T, ...rest: Rest) => void) | undefined,\n) {\n  const [own, setOwn] = useState(initial)\n  const controlled = value !== undefined\n  const current = controlled ? value : own\n  return [\n    current,\n    (next: T, ...rest: Rest) => {\n      if (Object.is(next, current)) return\n      onChange?.(next, ...rest)\n      if (!controlled && !isCanceled(rest[0])) setOwn(next)\n    },\n  ] as const\n}\n",
      "type": "registry:lib",
      "target": "@lib/controllable.ts"
    }
  ],
  "type": "registry:lib"
}