Checkbox
Checkboxes allow users to select one or more items from a set. Can be used to turn an option on or off.
When To Use
- Used for selecting multiple values from several options.
- If you use only one checkbox, it is the same as using Switch to toggle between two states. The difference is that Switch will trigger the state change directly, but Checkbox just marks the state as changed and this needs to be submitted.
Example Usage
Basic usage of checkbox.
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox id="terms" label="Accept terms and conditions" />
<Checkbox id="newsletter" label="Subscribe to newsletter" checked /> Disabled
Disabled checkbox.
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox id="chk-disabled" label="Disabled unchecked" disabled />
<Checkbox id="chk-disabled-checked" label="Disabled checked" checked disabled /> States
Checkboxes can have error states and disabled states.
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox
id="required-chk"
label="Required field"
error="You must check this box"
/> Custom Label and Error with Slots
You can use slots to customize the label and error messages with HTML elements like links.
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox id="chk-slot-label">
I accept the <a href="#" class="text-blue-500 hover:underline">Terms of Service</a>
</Checkbox>
<Checkbox id="chk-slot-error" error>
<span slot="error" class="text-red-500 text-xs mt-1 block">Custom error with <strong>HTML</strong> support</span>
Default slot is the label
</Checkbox> | Property | Description | Type | Default |
|---|---|---|---|
| id | Unique identifier for the input element. An auto-generated string is used if not provided. | string | auto-generated |
| label | The label text to display next to the checkbox. | string | |
| variant | Controls the visual variant of the checkbox. When the 'error' prop/slot is used, this defaults to 'error'. | 'default' | 'error' | 'default' |
| checked | Determines if the checkbox is checked by default. | boolean | false |
| class | Additional CSS classes to apply to the root element. | string | '' |
| error | Indicates an error state or displays an error message | string | boolean | |
| success | Indicates a success state or displays a success message | string | boolean | |
| disabled | Disables the component | boolean | false |