Checkbox
The Checkbox component allows users to select or deselect an option. It can be used for binary choices, settings, preferences, and form inputs.
import Checkbox from "rocksolidjs/Checkbox";
import Label from "rocksolidjs/Label";
export default function Example() {
return (
<div class="flex items-center gap-1">
<Checkbox id="notifications" />
<Label for="notifications">
Enable notifications
</Label>
</div>
);
}Examples
Controlled
Use checked together with onChange when the Checkbox state needs to be
controlled by the parent component.
import { createSignal } from "solid-js";
import Checkbox from "rocksolidjs/Checkbox";
export default function Example() {
const [checked, setChecked] = createSignal(false);
return (
<Checkbox
checked={checked()}
onChange={(_, value) => setChecked(value)}
/>
);
}Default Checked
Use the defaultChecked prop to initialize it as selected while allowing the
component to manage its state internally.
import Checkbox from "rocksolidjs/Checkbox";
export default function Example() {
return (
<div class="flex gap-4">
<Checkbox defaultChecked />
</div>
);
}Colors
Use the color prop to communicate the semantic meaning of the Checkbox.
import Checkbox from "rocksolidjs/Checkbox";
export default function Example() {
return (
<div class="flex gap-4">
<Checkbox color="default" defaultChecked />
<Checkbox color="success" defaultChecked />
<Checkbox color="warning" defaultChecked />
<Checkbox color="info" defaultChecked />
<Checkbox color="error" defaultChecked />
</div>
);
}Size
Use the size prop to control the size of the Checkbox.
import Checkbox from "rocksolidjs/Checkbox";
export default function Example() {
return (
<div class="flex flex-col gap-4">
<div class="flex items-center gap-4">
<Checkbox size="small" />
<Checkbox size="medium" />
<Checkbox size="large" />
</div>
<div class="flex items-center gap-4">
<Checkbox size="small" defaultChecked />
<Checkbox size="medium" defaultChecked />
<Checkbox size="large" defaultChecked />
</div>
</div>
);
}Disabled
Use the disabled prop to prevent the Checkbox from being changed.
import Checkbox from "rocksolidjs/Checkbox";
export default function Example() {
return (
<div class="flex gap-4">
<Checkbox disabled />
<Checkbox disabled defaultChecked />
</div>
);
}Customization
Use the slotProps.base to add custom styles to the Checkbox.
import Checkbox from "rocksolidjs/Checkbox";
export default function Example() {
return (
<Checkbox
defaultChecked
slotProps={{
base: {
class: `
text-purple-600
dark:text-purple-400
hover:text-purple-700
dark:hover:text-purple-300
`,
},
}}
/>
);
}Accessibility
- Use a meaningful label for every Checkbox.
- Associate the Checkbox with its label using
idand the corresponding label relationship when necessary. - Do not rely on color alone to communicate the Checkbox state.
- Ensure the Checkbox has sufficient contrast against its background.
- Keep the Checkbox keyboard accessible.
- Use the
disabledstate when an option cannot be changed. - For groups of related choices, provide a clear group label or description.
API
Checkbox
The Checkbox component supports controlled and uncontrolled states, semantic colors, disabled states, custom classes, and input slot customization.
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| checked | boolean |
No | — | Controls whether the Checkbox is checked. |
| class | string |
No | — | Adds a custom CSS class to the Checkbox. |
| color | "default" | "success" | "warning" | "info" | "error" |
No | "default" |
Sets the semantic color of the Checkbox. |
| defaultChecked | boolean |
No | false |
Sets the initial checked state for an uncontrolled Checkbox. |
| disabled | boolean |
No | false |
Disables the Checkbox and prevents user interaction. |
| id | string |
No | — | Sets the id attribute of the underlying input element. |
| onChange | (event: Event, checked: boolean) => void |
No | — | Called when the checked state changes. Provides the event and new checked state. |
| size | "small" | "medium" | "large" |
No | "medium" |
Sets the size of the Checkbox. |
| slotProps | { base?: object } |
No | — | Provides props to customize the Checkbox slots.
|