Checkbox
An enhanced checkbox component with animation variants
Checkbox
The Checkbox component is an enhanced checkbox with beautiful animations and multiple variants. Built on Radix UI primitives for full accessibility.
Installation
npm install @nqlib/nquiImport
import { Checkbox } from '@nqlib/nqui';Basic Usage
import { Checkbox } from '@nqlib/nqui';
export default function Example() {
return <Checkbox>Accept terms and conditions</Checkbox>;
}Examples
Indeterminate
Checkbox in indeterminate state
'use client';import { Checkbox } from '@nqlib/nqui';export default function Example() { return ( <div className="flex items-center gap-2"> <Checkbox checked="indeterminate" /> <span className="text-sm">Indeterminate state</span> </div> );}Square Variant (Default)
Round Variant
Controlled Checkbox
Indeterminate
Checkbox Group
Variants
The Checkbox component supports two variants:
Square Variant (Default)
The square variant features an animated checkmark with a pulse effect and background expansion:
<Checkbox variant="square">Accept terms</Checkbox>Features:
- Animated checkmark with smooth transitions
- Pulse animation on selection
- Background expansion effect
- Hover state with muted background
Round Variant
The round variant features a circular checkbox with a gooey splash animation:
<Checkbox variant="round" />Features:
- Circular checkbox design
- Gooey splash animation on selection
- SVG path animation for checkmark
- Smooth transitions
Controlled Checkbox
Use controlled state for programmatic control:
const [checked, setChecked] = useState(false);
<Checkbox checked={checked} onCheckedChange={setChecked}>
Controlled checkbox
</Checkbox>Uncontrolled Checkbox
Use defaultChecked for uncontrolled state:
<Checkbox defaultChecked>Subscribe to newsletter</Checkbox>Disabled State
<Checkbox disabled>Disabled checkbox</Checkbox>With Label
The square variant includes the label as children. For the round variant, use a separate Label component:
import { Checkbox, Label } from '@nqlib/nqui';
<div className="flex items-center gap-2">
<Checkbox variant="round" id="terms" />
<Label htmlFor="terms">Accept terms</Label>
</div>Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "square" | "round" | "square" | Visual style variant |
checked | boolean | "indeterminate" | - | Controlled checked state |
defaultChecked | boolean | - | Uncontrolled default checked state |
onCheckedChange | (checked: boolean | "indeterminate") => void | - | Callback when checked state changes |
disabled | boolean | false | Whether the checkbox is disabled |
required | boolean | false | Whether the checkbox is required |
name | string | - | Form field name |
value | string | - | Form field value |
className | string | - | Additional CSS classes |
children | React.ReactNode | - | Label text (square variant only) |
Enhanced vs Core Checkbox
nqui exports an enhanced Checkbox component by default, which includes:
- Animated variants (square and round)
- Smooth transitions and animations
- Enhanced visual feedback
- Better focus states
For the base Radix Checkbox (without enhancements), you can import CoreCheckbox:
import { CoreCheckbox } from '@nqlib/nqui';Styling
Checkboxes use CSS variables for theming:
- Primary color:
var(--primary) - Background:
var(--card) - Border:
var(--border) - Foreground:
var(--foreground) - Muted:
var(--muted)
Accessibility
- Full keyboard navigation support
- ARIA attributes for screen readers
- Focus management with visible focus rings
- Proper label association
- Disabled state handling
Best Practices
- Use square variant for forms: The square variant with label text is ideal for form checkboxes
- Use round variant for toggles: The round variant works well for toggle-like interactions
- Always provide labels: Ensure every checkbox has an associated label
- Group related checkboxes: Use FieldSet and FieldLegend for groups
- Handle disabled states: Clearly indicate when checkboxes are disabled
Next Steps
- Learn about RadioGroup for single selection
- Explore Switch for toggle switches
- Check out Field component for form field wrapper
- See Form Components for other form elements