Alert Avatar AvatarGroup Button ButtonGroup Checkbox CheckboxGroup FilePicker Form FormRow Hamburger Input Modal Option Radio RadioGroup Rating Select Spinner Textarea Toast Tooltip Toggle

Checkbox

Checkbox components are used to toggle a single boolean value. They can also be used within a CheckboxGroup to toggle multiple values.


Example

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


Label

A label prop let’s you define a simple String to be used as the checkbox label.

<Checkbox label="I agree to the terms." />

Name

Use the name prop for a single checkbox to specify the FormData key.

<Checkbox label="I agree to the terms." name="approveTerms" />

Error

Display error message below the checkboxes. Also adds error styles.

<Checkbox label="Bauhaus" error="You must like bauhaus" />

Note

A note prop let’s you define a note displayed below the field.

<Checkbox label="Bauhaus" note="Check above to indicate good taste" />

Leading Label

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." />

Checked

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 />

Disabled

Default is true but can also be assigned a Boolean value like disabled={user.isSpammable}.

<Checkbox label="Spam me" disabled />

Value

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" />

Label Named Slot

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>

Leading Label Named Slot

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>

Checkbox Component Props

Name Type Default Description
labelString Text label appears trailing the checkmark
nameString The name property for this Checkbox
errorString The error note displayed
noteString The help note displayed
leadingLabelString Text label appears leading the checkmark
checkedBooleanfalseNormally used with bind:checked directive. Not needed within a CheckboxGroup
disabledBooleanfalse 
classString 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

Checkbox CSS Custom Properties

Name Type Default Description
--ui-checkbox-small-font-sizeNumeric--ui-small-font-size, 0.875remFont size for checkbox label
--ui-checkbox-gapNumeric0.5remFont size for checkbox label
--ui-checkbox-border-widthNumeric--ui-border-width, 1pxBorder width
--ui-checkbox-border-colorColor--ui-border-color, --ui-midtone, #94a3b8Border color
--ui-checkbox-backgroundColor--ui-focus, #3b82f6Background color of checked checkbox

Checkbox Named Slots

Name Description
labelContent to be used for label after checkbox (supercedes label property)
labelLeadingContent to be used for label preceding checkbox (supercedes leadingLabel property)