On this page

Checkbox

Live examples of Checkbox, with the exact Vue file that renders each one.

Every example is a complete Vue file. Copy it into an app that has the registry installed, or open it in StackBlitz.

With a label

The label is part of the click area.

CheckboxWithLabel.vue
<script setup lang="ts">
import { ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldLabel } from '@/components/ui/field'

const notify = ref(true)
</script>

<template>
  <Field orientation="horizontal" class="max-w-sm">
    <Checkbox id="notify-failed" v-model="notify" />
    <FieldLabel for="notify-failed">Email me when a sync fails</FieldLabel>
  </Field>
</template>

With a description

A line under the label explains what the option does.

CheckboxWithDescription.vue
<script setup lang="ts">
import { ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldContent, FieldDescription, FieldLabel } from '@/components/ui/field'

const splitShipments = ref(false)
</script>

<template>
  <Field orientation="horizontal" class="max-w-md">
    <Checkbox id="split-shipments" v-model="splitShipments" aria-describedby="split-shipments-help" />
    <FieldContent>
      <FieldLabel for="split-shipments">Split orders across warehouses</FieldLabel>
      <FieldDescription id="split-shipments-help">When one warehouse is short, ship the rest from the next nearest one. The customer may get more than one parcel.</FieldDescription>
    </FieldContent>
  </Field>
</template>

Select all

The parent box shows a dash when only some of its children are checked.

Sync stock to
CheckboxSelectAll.vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldGroup, FieldLabel, FieldLegend, FieldSet } from '@/components/ui/field'

// "All channels" is checked, unchecked or indeterminate (a dash) when only some channels are selected.
const channels = [
  { id: 'channel-web', label: 'Online store' },
  { id: 'channel-marketplace', label: 'Marketplace' },
  { id: 'channel-pos', label: 'Point of sale' },
]
const selected = ref<string[]>(['channel-web'])

const all = computed<boolean | 'indeterminate'>({
  get: () => selected.value.length === channels.length ? true : selected.value.length === 0 ? false : 'indeterminate',
  set: value => { selected.value = value === true ? channels.map(channel => channel.id) : [] },
})

function toggle(id: string, on: boolean | 'indeterminate') {
  selected.value = on === true ? [...selected.value, id] : selected.value.filter(item => item !== id)
}
</script>

<template>
  <FieldSet class="max-w-sm">
    <FieldLegend variant="label">Sync stock to</FieldLegend>
    <FieldGroup data-slot="checkbox-group">
      <Field orientation="horizontal">
        <Checkbox id="channel-all" v-model="all" />
        <FieldLabel for="channel-all">All channels</FieldLabel>
      </Field>
      <FieldGroup data-slot="checkbox-group" class="pl-(--bh-space-300)">
        <Field v-for="channel in channels" :key="channel.id" orientation="horizontal">
          <Checkbox
            :id="channel.id"
            :model-value="selected.includes(channel.id)"
            @update:model-value="value => toggle(channel.id, value)"
          />
          <FieldLabel :for="channel.id">{{ channel.label }}</FieldLabel>
        </Field>
      </FieldGroup>
    </FieldGroup>
  </FieldSet>
</template>