Skip to content

Checkbox

An easily stylable checkbox component.

Usage guidelines

  • Form controls must have an accessible name: It can be created using a <label> element or the Field component. See Labeling a checkbox and the forms guide.

Anatomy

Import the checkbox parts and compose them together:

vue
<script setup lang="ts">
import { CheckboxIndicator, CheckboxRoot } from 'base-ui-vue'
</script>

<template>
  <CheckboxRoot>
    <CheckboxIndicator />
  </CheckboxRoot>
</template>

Examples

Labeling a checkbox

The simplest pattern is wrapping the checkbox in a label:

vue
<label>
  <CheckboxRoot />
  Accept terms and conditions
</label>

Rendering as a native button

By default, CheckboxRoot renders a <span> element to support enclosing labels. Prefer rendering the checkbox as a native button when using sibling labels (for/id).

vue
<div>
  <label for="notifications-checkbox">Enable notifications</label>
  <CheckboxRoot
    id="notifications-checkbox"
    as="button"
    :native-button="true"
  >
    <CheckboxIndicator />
  </CheckboxRoot>
</div>

Native buttons with wrapping labels are supported by using renderless mode so the hidden input is placed outside the label:

vue
<script setup lang="ts">
import { CheckboxRoot, Slot } from 'base-ui-vue'
</script>

<template>
  <CheckboxRoot
    v-slot="{ props, ref }"
    :as="Slot"
    :native-button="true"
  >
    <label>
      <button :ref="ref" v-bind="props" />
      Enable notifications
    </label>
  </CheckboxRoot>
</template>

Form integration

Use Field to handle label associations and form integration:

vue
<script setup lang="ts">
import {
  CheckboxIndicator,
  CheckboxRoot,
  Form,
  FieldLabel,
  FieldRoot,
} from 'base-ui-vue'
</script>

<template>
  <Form>
    <FieldRoot name="stayLoggedIn">
      <FieldLabel>
        <CheckboxRoot>
          <CheckboxIndicator />
        </CheckboxRoot>
        Stay logged in for 7 days
      </FieldLabel>
    </FieldRoot>
  </Form>
</template>

API reference

Root

PropTypeDefaultDescription
asstring | Component'span'The element or component used for the checkbox root.
checkedbooleanundefinedWhether the checkbox is currently ticked. To render an uncontrolled checkbox, use the default-checked prop instead.
default-checkedbooleanfalseWhether the checkbox is initially ticked. To render a controlled checkbox, use the checked prop instead.
disabledbooleanfalseWhether the component should ignore user interaction.
read-onlybooleanfalseWhether the user should be unable to tick or untick the checkbox.
requiredbooleanfalseWhether the user must tick the checkbox before submitting a form.
indeterminatebooleanfalseWhether the checkbox is in a mixed state: neither ticked, nor unticked.
namestringundefinedIdentifies the field when a form is submitted.
valuestringundefinedThe value of the selected checkbox.
parentbooleanfalseWhether the checkbox controls a group of child checkboxes.
unchecked-valuestringundefinedThe value submitted with the form when the checkbox is unchecked. By default, unchecked checkboxes do not submit any value, matching native checkbox behavior.
native-buttonbooleanfalseWhether the component renders a native <button> element when replacing it via the as prop. Set to true if the rendered element is a native button.
classany | ((state: State) => any)undefinedCSS class applied to the element, or a function that returns a class based on the component's state.
styleStyleValue | ((state: State) => StyleValue)undefinedStyle applied to the element, or a function that returns a style object based on the component's state.
EmitsTypeDescription
checked-change(checked: boolean, details: EventDetails) => voidEmitted when the checkbox is ticked or unticked.
AttributeDescription
data-checkedPresent when the checkbox is checked.
data-uncheckedPresent when the checkbox is unchecked.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is read-only.
data-requiredPresent when the checkbox is required.
data-validPresent when the checkbox is valid.
data-invalidPresent when the checkbox is invalid.
data-dirtyPresent when the checkbox's value has changed.
data-touchedPresent when the checkbox has been touched.
data-filledPresent when the checkbox is checked.
data-focusedPresent when the checkbox is focused.
data-indeterminatePresent when the checkbox is in an indeterminate state.
data-parentPresent when the checkbox is acting as the parent checkbox for a group.

Indicator

PropTypeDefaultDescription
asstring | Component'span'The element or component used for the indicator.
keep-mountedbooleanfalseWhether to keep the element in the DOM when the checkbox is not checked.
classany | ((state: State) => any)undefinedCSS class applied to the element, or a function that returns a class based on the component's state.
styleStyleValue | ((state: State) => StyleValue)undefinedStyle applied to the element, or a function that returns a style object based on the component's state.
AttributeDescription
data-checkedPresent when the checkbox is checked.
data-uncheckedPresent when the checkbox is unchecked.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is read-only.
data-requiredPresent when the checkbox is required.
data-validPresent when the checkbox is valid.
data-invalidPresent when the checkbox is invalid.
data-dirtyPresent when the checkbox's value has changed.
data-touchedPresent when the checkbox has been touched.
data-filledPresent when the checkbox is checked.
data-focusedPresent when the checkbox is focused.
data-indeterminatePresent when the checkbox is in an indeterminate state.
data-starting-stylePresent when the indicator is animating in.
data-ending-stylePresent when the indicator is animating out.