Checkbox Primitive
A low-level checkbox component that handles core functionality
Checkbox Primitive
The CheckboxPrimitive
component is a low-level checkbox component that handles all the core functionality. It's useful when you need more control over the styling and behavior of the checkbox.
Basic Usage
Features
- Controlled & uncontrolled modes
- Indeterminate state support
- Keyboard shortcuts
- Custom icons
- Error states & helper text
- Label placement options
- Accessible by default
Props
Prop | Type | Default |
---|---|---|
checked | boolean | - |
defaultChecked | boolean | - |
onChange | ((checked: boolean) => void) | ((event: ChangeEvent<HTMLInputElement>) => void) | - |
disabled | boolean | - |
required | boolean | - |
name | string | - |
value | string | - |
id | string | - |
icon | ReactNode | - |
checkedIcon | ReactNode | - |
children | string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...> | ((props: CheckboxRenderProps) => ReactNode) | - |
indeterminate | boolean | - |
aria-label | string | - |
aria-labelledby | string | - |
aria-describedby | string | - |
aria-invalid | boolean | - |
className | string | - |
wrapperClassName | string | - |
labelClassName | string | - |
iconClassName | string | - |
Examples
Custom Styling
With Render Function
With Custom Icons
Best Practices
-
Component Structure:
- Keep styling separate from logic
- Use semantic HTML elements
- Maintain proper ARIA attributes
-
State Management:
- Choose between controlled and uncontrolled based on needs
- Handle state changes efficiently
- Consider using context for complex state
-
Customization:
- Use className props for styling
- Implement custom icons consistently
- Maintain accessibility when customizing
-
Performance:
- Memoize callbacks when needed
- Avoid unnecessary re-renders
- Use render functions judiciously
Use Cases
The CheckboxPrimitive
component is ideal when you need:
- Complete control over styling
- Custom layout or structure
- Integration with design systems
- Complex state management
- Custom interaction patterns
Related Components
Checkbox
- High-level component with built-in stylingCheckboxGroup
- For managing multiple checkboxesuseCheckbox
- Hook for custom implementations