Checkbox
A native checkbox that you can style.
vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Checkbox } from '@vase-ui/vue/checkbox';
import styles from './index.module.css';
const CheckIcon: FunctionalComponent = () =>
h(
'svg',
{
width: '16',
height: '16',
viewBox: '0 0 16 16',
fill: 'none',
stroke: 'currentColor',
'aria-hidden': 'true',
},
[h('path', { d: 'm2.5 8.5 4 4 7-9' })],
);
</script>
<template>
<label :class="styles.Label">
<span :class="styles.Box">
<Checkbox default-checked :class="styles.Checkbox" />
<CheckIcon :class="styles.Indicator" />
</span>
Enable notifications
</label>
</template>css
.Label {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 400;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Box {
position: relative;
display: flex;
flex-shrink: 0;
}
.Checkbox {
appearance: none;
box-sizing: border-box;
width: 1rem;
height: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
padding: 0;
margin: 0;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
&:checked {
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: 2px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Indicator {
position: absolute;
inset: 0;
display: none;
pointer-events: none;
color: white;
@media (prefers-color-scheme: dark) {
color: oklch(14.5% 0 0deg);
}
}
.Checkbox:checked + .Indicator {
display: block;
}vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Checkbox } from '@vase-ui/vue/checkbox';
const CheckIcon: FunctionalComponent = () =>
h(
'svg',
{
width: '16',
height: '16',
viewBox: '0 0 16 16',
fill: 'none',
stroke: 'currentColor',
'aria-hidden': 'true',
},
[h('path', { d: 'm2.5 8.5 4 4 7-9' })],
);
</script>
<template>
<label class="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<span class="relative flex shrink-0">
<Checkbox
default-checked
class="peer m-0 size-4 appearance-none rounded-none border border-neutral-950 bg-white p-0 checked:bg-neutral-950 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:checked:bg-white dark:focus-visible:outline-white"
/>
<CheckIcon
class="pointer-events-none absolute inset-0 hidden text-white peer-checked:block dark:text-neutral-950"
/>
</span>
Enable notifications
</label>
</template>Usage guidelines
- Form controls must have an accessible name: Give the checkbox a
<label>element, or aField.Labelin aField. See Labeling a checkbox and the forms guide.
Anatomy
Import the component:
vue
<script setup lang="ts">
import { Checkbox } from '@vase-ui/vue/checkbox';
</script>
<template>
<Checkbox />
</template>Examples
Labeling a checkbox
A <label> around the checkbox is the simplest pattern to label it:
vue
<script setup lang="ts">
import { Checkbox } from '@vase-ui/vue/checkbox';
</script>
<template>
<label>
<Checkbox name="newsletter" />
Get the newsletter
</label>
</template>A <label> beside the checkbox names it with for and id:
vue
<script setup lang="ts">
import { Checkbox } from '@vase-ui/vue/checkbox';
</script>
<template>
<Checkbox id="newsletter" name="newsletter" />
<label for="newsletter">Get the newsletter</label>
</template>Show a mark
Give the input appearance: none and draw the mark with CSS. Or put an element of your own after the input, and select it with :checked + .mark:
vue
<script setup lang="ts">
import { Checkbox } from '@vase-ui/vue/checkbox';
</script>
<template>
<label class="label">
<span class="box">
<Checkbox class="checkbox" />
<svg class="mark" viewBox="0 0 16 16" aria-hidden="true">
<path d="m2.5 8.5 4 4 7-9" fill="none" stroke="currentColor" />
</svg>
</span>
Get the newsletter
</label>
</template>
<style scoped>
.box {
position: relative;
display: inline-flex;
}
.checkbox {
appearance: none;
width: 1rem;
height: 1rem;
margin: 0;
border: 1px solid;
}
.mark {
position: absolute;
inset: 0;
display: none;
pointer-events: none;
}
.checkbox:checked + .mark {
display: block;
}
</style>The element slot renders the input and gives its state. Render the mark from state.checked or state.indeterminate:
vue
<script setup lang="ts">
import { Checkbox } from '@vase-ui/vue/checkbox';
</script>
<template>
<label>
<Checkbox>
<template #element="{ props, elementRef, state }">
<span class="box">
<input v-bind="props" :ref="elementRef" class="checkbox" />
<svg v-if="state.checked" class="mark" viewBox="0 0 16 16" aria-hidden="true">
<path d="m2.5 8.5 4 4 7-9" fill="none" stroke="currentColor" />
</svg>
</span>
</template>
</Checkbox>
Get the newsletter
</label>
</template>
<style scoped>
.box {
position: relative;
display: inline-flex;
}
.checkbox {
appearance: none;
width: 1rem;
height: 1rem;
margin: 0;
border: 1px solid;
}
.mark {
position: absolute;
inset: 0;
pointer-events: none;
}
</style>API reference
Checkbox
A box that the user can check and clear.
Renders an <input> element with the type checkbox.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultChecked | boolean | undefined | false | The checked state of a checkbox that has no modelValue, at the mount and at a native reset of
the form. |
disabled | boolean | undefined | false | Disables the checkbox. A disabled field, group or fieldset around the checkbox also disables it. |
id | string | undefined | The id of the checkbox. With no id, the checkbox has an id of its own. | |
indeterminate | boolean | undefined | false | Shows a state that is neither checked nor unchecked. A press asks to clear it with
update:indeterminate, so bind it with v-model:indeterminate. |
modelValue | boolean | undefined | The checked state. Bind it with v-model. With no modelValue, the checkbox keeps its own
state. | |
name | string | undefined | The name of the checkbox in the data of the form. The name of a Field.Root around it wins. | |
parent | boolean | undefined | false | The checkbox checks and clears each checkbox of the allValues of its group, and names them in
aria-controls. It has no name. |
readonly | boolean | undefined | false | The user cannot change the state. The checkbox stays in the tab order and is not validated. |
required | boolean | undefined | false | The user must check the checkbox before a submit of the form. |
uncheckedValue | string | undefined | The value in the data of a form while the checkbox is unchecked. The form gets it after the
mount, from an enabled checkbox that has a name. The parent checkbox and a checkbox with a
value in a group ignore it. | |
value | string | undefined | The value in the data of a form. In a group, the value that the group lists. A checkbox with no
value keeps its own state in a group. |
Slots
| Slot | Slot props | Description |
|---|---|---|
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: CheckboxState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
checkedChange | [checked: boolean, details: CheckboxChangeEventDetails] | The user presses the checkbox. details.event is the native click event of the press.
details.cancel() keeps the state as it was. |
update:modelValue | [checked: boolean] | Comes after the change event with the new value, unless a listener cancels the change. v-model uses it. |
update:indeterminate | [indeterminate: false] | A press asks to clear the indeterminate state. v-model:indeterminate uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLInputElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
:checked | The checkbox is checked. |
[data-indeterminate] | The checkbox is indeterminate. After the mount, :indeterminate also matches. |
:disabled | The checkbox is disabled. |
:required | The checkbox is required and not read-only. |
[aria-readonly="true"] | The checkbox is read-only. |
[data-valid] | The field of the checkbox is valid. |
[aria-invalid="true"] | The field of the checkbox is invalid. |
[data-touched] | In a field, the focus left the checkbox. |
[data-dirty] | In a field, the value differs from the first value. |
Differences from Base UI
Vase UI ports Checkbox from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Checkbox.Root | Checkbox is the part: one native <input type="checkbox">, so it needs no nativeButton. Its types are Checkbox.Props, Checkbox.State and Checkbox.ChangeEventReason, and the flat names CheckboxProps, CheckboxState and CheckboxChangeEventReason. readonly is in place of readOnly, and form is a plain attribute. The element of the template ref is the input, in place of inputRef. CSS selects :checked, :disabled, :required, :focus, [aria-readonly="true"] and [aria-invalid="true"], so the input has no data-checked, data-unchecked, data-filled, data-disabled, data-required, data-focused, data-readonly and data-invalid. |
Checkbox.Indicator | CSS on the input, an element of the developer beside the input, or the element slot with state gives the mark. |
Behavior
| Vase UI | Evidence |
|---|---|
A native reset of the form gives a control the v-model value of its mount, or else the default value that it has at the reset, as a native control does. The control emits only update:modelValue, also when v-model controls it. Its field is then not dirty, and it has data-filled only when that value is not empty. | |
A checkbox takes its place in the value of a CheckboxGroup from its value prop only. A checkbox with no value is not an item of the group: it keeps its own checked state, and the group value does not change with it. In a native form, the checkboxes of a group share one name, and value tells them apart, so a name cannot be the value of an item. Base UI uses the name of a checkbox with no value as its value in the group. |
|
A control in a field keeps the id of the developer in the server HTML when no Field.Label of the field renders before it, and a label that renders after the control has that id in for. When a label renders before the control, the control has the generated id of the field until the hydration, because that label names the generated id. Then the control takes its id, and the label follows. In each order, the label names the control in the server HTML. Base UI gives each control in a field the generated id until the hydration, also where no label needs it. |
|
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.