{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-controllable-state",
  "title": "useControllableState",
  "description": "A custom React hook for controllable state.",
  "files": [
    {
      "path": "registry/default/hooks/use-controllable-state.ts",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\ninterface UseControllableStateParams<T> {\n  /** Controlled value. When defined, the hook is in controlled mode. */\n  value?: T;\n  /** Initial value for uncontrolled mode. */\n  defaultValue: T;\n  onValueChange?: (value: T) => void;\n}\n\n/**\n * Merges controlled and uncontrolled state into a single `[value, setValue]`\n * tuple, mirroring the pattern Radix and Base UI use internally.\n *\n * `onValueChange` fires synchronously in both modes. Functional updates are\n * safe in both modes: they resolve against an eagerly-advanced ref, so\n * multiple `setValue` calls in one event tick compose. `setValue` is\n * referentially stable across renders.\n *\n * A controlled parent may reject a change by not re-rendering. Asking for the\n * same value again in a later event still notifies it, so a validating parent\n * gets a fresh chance to accept; a repeat within the same tick does not.\n */\nexport function useControllableState<T>({\n  value,\n  defaultValue,\n  onValueChange,\n}: UseControllableStateParams<T>): [T, (next: T | ((prev: T) => T)) => void] {\n  const isControlled = value !== undefined;\n  const [uncontrolled, setUncontrolled] = React.useState<T>(defaultValue);\n  const current = isControlled ? (value as T) : uncontrolled;\n\n  const onChangeRef = React.useRef(onValueChange);\n  // Mirrors `current`. Re-synced after every commit; eagerly advanced by\n  // setValue so multiple calls in one event tick compose.\n  const currentRef = React.useRef(current);\n  // The value as last rendered, which the eager ref above can run ahead of.\n  const renderedRef = React.useRef(current);\n  // Set while a controlled change notified in this tick is still unanswered.\n  const notifiedRef = React.useRef(false);\n  React.useEffect(() => {\n    onChangeRef.current = onValueChange;\n    currentRef.current = current;\n    renderedRef.current = current;\n  });\n\n  const setValue = React.useCallback(\n    (next: T | ((prev: T) => T)) => {\n      const prev = currentRef.current;\n      const resolved =\n        typeof next === \"function\" ? (next as (prev: T) => T)(prev) : next;\n      if (isControlled) {\n        // A parent that rejected `prev` never re-rendered, so the ref still\n        // holds it: skip it only while it is shown or was just sent.\n        if (\n          Object.is(resolved, prev) &&\n          (Object.is(resolved, renderedRef.current) || notifiedRef.current)\n        )\n          return;\n        // The parent owns the state, but advance the ref eagerly so a second\n        // functional setValue in the same tick composes on this one instead of\n        // clobbering it. The ref re-syncs from the prop at the next commit.\n        currentRef.current = resolved;\n        notifiedRef.current = true;\n        queueMicrotask(() => {\n          notifiedRef.current = false;\n        });\n        onChangeRef.current?.(resolved);\n      } else {\n        if (Object.is(resolved, prev)) return;\n        currentRef.current = resolved;\n        setUncontrolled(resolved);\n        onChangeRef.current?.(resolved);\n      }\n    },\n    [isControlled],\n  );\n\n  return [current, setValue];\n}\n",
      "type": "registry:hook",
      "target": "hooks/cubby-ui/use-controllable-state.ts"
    }
  ],
  "type": "registry:hook"
}