Checkbox
A checkbox input component for binary selection with built-in label support and accessibility features.
Overview
The Checkbox component wraps the native HTML <input type="checkbox"> element with optional label support and full semantic accessibility.
Examples
Basic Checkbox
<Checkbox label="I agree to the terms" />
Checked State
<Checkbox label="Subscribe to newsletter" defaultChecked />
Disabled
<Checkbox label="Already completed" disabled defaultChecked />
Without Label
<Checkbox aria-label="Accept terms" />
Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Label text |
checked | boolean | — | Controlled checked state |
defaultChecked | boolean | — | Initial checked state |
disabled | boolean | — | Disable the checkbox |
...props | InputHTMLAttributes | — | All native input attributes |
Usage
import { Checkbox } from "@milfushi/ui";
import { useState } from "react";
export function SignupForm() {
const [terms, setTerms] = useState(false);
const [newsletter, setNewsletter] = useState(false);
return (
<div>
<Checkbox
label="I agree to the terms and conditions"
checked={terms}
onChange={(e) => setTerms(e.target.checked)}
required
/>
<Checkbox
label="Subscribe to our newsletter"
checked={newsletter}
onChange={(e) => setNewsletter(e.target.checked)}
/>
</div>
);
}
Styling
The Checkbox component renders as a native checkbox with optional label:
<div>
<input type="checkbox" id="checkbox-1" />
<label for="checkbox-1">Label text</label>
</div>
Style using standard CSS selectors:
input[type="checkbox"] {
accent-color: var(--color-primary);
width: 20px;
height: 20px;
}
input[type="checkbox"]:disabled {
opacity: 0.5;
cursor: not-allowed;
}
input[type="checkbox"] + label {
cursor: pointer;
user-select: none;
margin-left: 8px;
}
Or import pre-built styles:
@import "@milfushi/ui/dist/components.css";
Accessibility
- Native HTML checkbox for full keyboard support
- Automatic
<label>association for click target expansion - Supports controlled and uncontrolled modes
- Works with screen readers and keyboard navigation
- Proper focus indicators
aria-labelsupport when no text label is provided