Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Command
- Context Menu
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Input Group
- Kbd
- Label
- Menu
- Menubar
- Message
- Meter
- Number Field
- OTP Field
- Pagination
- Popover
- Preview Card
- Progress
- Questionnaire
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Toolbar
- Tooltip
Shared
- Artifact
- Audio Player
- Canvas
- Chain of Thought
- Checkpoint
- Code Block
- Commit
- Connection
- Context
- Controls
- Edge
- Environment Variables
- Mic Selector
- Model Selector
- Node
- Node Toolbar
- Open in Chat
- Panel
- Plan
- Prompt Input
- Queue
- Reasoning
- Sandbox
- Schema Display
- Shimmer
- Snippet
- Sources
- Speech Input
- Stack Trace
- Suggestion
- Tool
- Transcription
- Web Preview
Hooks
Resources
useControllableState
Controlled or uncontrolled state with one API.
A React hook that gives a component controlled/uncontrolled behavior like native inputs. Pass value to control it, or defaultValue to leave it uncontrolled, with onChange notified in both modes.
Installation
pnpm dlx shadcn@latest add @frontal-labs/use-controllable-state
Usage
Uncontrolled by default
import { useControllableState } from "@/hooks/use-controllable-state";
function Counter({ value, defaultValue, onChange }) {
const [count, setCount] = useControllableState({
value,
defaultValue: defaultValue ?? 0,
onChange,
});
return <button onClick={() => setCount((c) => (c ?? 0) + 1)}>{count}</button>;
}Controlled
function Controlled() {
const [count, setCount] = useState(0);
return <Counter value={count} onChange={setCount} />;
}The setter accepts updater functions like useState. Updates that resolve to the same value (via Object.is) are skipped, and onChange fires only on real changes.
API
function useControllableState<T>(props: {
value?: T;
defaultValue?: T;
onChange?: (value: T) => void;
}): [T | undefined, Dispatch<SetStateAction<T | undefined>>];Parameters
| Property | Type | Description |
|---|---|---|
value | T | Controlled value. When defined, the hook never stores state internally. |
defaultValue | T | Initial value for uncontrolled mode. |
onChange | (value: T) => void | Called whenever the resolved value changes, in both modes. |
Return value
A [value, setValue] tuple matching useState. setValue updates internal state only when uncontrolled, but always calls onChange.