Checkbox components are used to toggle a single boolean value. They can also be used within a CheckboxGroup to toggle multiple values.
Check the checkbox to see the value change.
<script>
import { Checkbox } from '$lib/ui';
let isChecked = $state(false);
</script>
<Checkbox bind:checked={isChecked} />
<p>Checked: {isChecked}</p>
Checked: false
A label
prop let’s you define a simple String to be used as the checkbox label.
<Checkbox label="I agree to the terms." />
Use the name
prop for a single checkbox to specify the FormData key.
<Checkbox label="I agree to the terms." name="approveTerms" />
Display error message below the checkboxes. Also adds error styles.
<Checkbox label="Bauhaus" error="You must like bauhaus" />
A note
prop let’s you define a note displayed below the field.
<Checkbox label="Bauhaus" note="Check above to indicate good taste" />
Labels normally appear to the right of the checkbox. Use the leadingLabel
prop instead to place it
on the left.
<Checkbox leadingLabel="I agree to the terms." />
Not normally used because the CheckboxGroup value
prop will set which Checkboxes are checked (and
overrides this property). If using outside of a CheckboxGroup, then use with the bind:checked
directive as shown in the example at the top of this page. The checked
prop can also be assigned a
Boolean value like checked={user.wantsSpam}
.
<Checkbox label="Spam me" checked />
Default is true but can also be assigned a Boolean value like disabled={user.isSpammable}
.
<Checkbox label="Spam me" disabled />
When used within a CheckboxGroup, this is the value that will be assigned to the
CheckboxGroup’s value
array when checked.
<Checkbox label="Spam me" value="spamMe" />
Since the label
prop can only be a String, you can use the label
slot instead to pass in a more
complex label.
<Checkbox>
<span slot="label">
Don't you just <HeartIcon /> spam?
</span>
</Checkbox>
Since the leadingLabel
prop can only be a String, you can use the leadingLabel
slot instead to
pass in a more complex leading label.
<Checkbox>
<span slot="leadingLabel">
I hereby confirm that I <HeartIcon /> spam.
</span>
</Checkbox>
Name | Type | Default | Description |
---|---|---|---|
label | String | Text label appears trailing the checkmark | |
name | String | The name property for this Checkbox | |
error | String | The error note displayed | |
note | String | The help note displayed | |
leadingLabel | String | Text label appears leading the checkmark | |
checked | Boolean | false | Normally used with bind:checked directive. Not needed within a CheckboxGroup |
disabled | Boolean | false | |
class | String | CSS classes declared in global scope can be applied to the outermost element | |
... | Additional props will be passed through to the HTML element enabling support for things
like on:click , etc |
Name | Type | Default | Description |
---|---|---|---|
--ui-checkbox-small-font-size | Numeric | --ui-small-font-size , 0.875rem | Font size for checkbox label |
--ui-checkbox-gap | Numeric | 0.5rem | Font size for checkbox label |
--ui-checkbox-border-width | Numeric | --ui-border-width , 1px | Border width |
--ui-checkbox-border-color | Color | --ui-border-color , --ui-midtone , #94a3b8 | Border color |
--ui-checkbox-background | Color | --ui-focus , #3b82f6 | Background color of checked checkbox |
Name | Description |
---|---|
label | Content to be used for label after checkbox (supercedes label property) |
labelLeading | Content to be used for label preceding checkbox (supercedes leadingLabel property) |