{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-controllable-state",
  "title": "useControllableState",
  "description": "State a parent can control, or leave to the component.",
  "files": [
    {
      "path": "hooks/use-controllable-state.ts",
      "content": "import * as React from \"react\";\n\n// State that the parent can own through `value`, or leave to the component\n// with `defaultValue`.\nfunction useControllableState<T>({\n  value: valueProp,\n  defaultValue,\n  onValueChange,\n}: {\n  value?: T;\n  defaultValue: T;\n  onValueChange?: (value: T) => void;\n}) {\n  const [state, setState] = React.useState(defaultValue);\n  const isControlled = valueProp !== undefined;\n  const value = isControlled ? valueProp : state;\n\n  const onValueChangeRef = React.useRef(onValueChange);\n  React.useEffect(() => {\n    onValueChangeRef.current = onValueChange;\n  });\n\n  const setValue = React.useCallback(\n    (next: T) => {\n      if (!isControlled) setState(next);\n      onValueChangeRef.current?.(next);\n    },\n    [isControlled],\n  );\n\n  return [value, setValue] as const;\n}\n\nexport { useControllableState };\n",
      "type": "registry:hook"
    }
  ],
  "type": "registry:hook"
}