Checkbox
Usage
Use the v-model directive to control whether the checkbox is checked.
<script setup lang="ts">
import { ref } from 'vue'
const signsOutOtherDevices = ref(true)
</script>
<template>
<KCheckbox v-model="signsOutOtherDevices" label="Sign out of all other devices" />
</template>
Label
Use the label prop to set the text next to the box. Clicking the text also checks or unchecks the box.
<KCheckbox v-model="remembersDevice" label="Remember this device" />
Label position
Use the label-position prop to put the label before the box (start) or after it (end, default).
<KCheckbox v-model="remembersDevice" label="Label before the box" label-position="start" />
<KCheckbox v-model="remembersDevice" label="Label after the box" />
Indeterminate
Set indeterminate when only some of the items the checkbox stands for are checked, like a "Select all" box above a list. The box shows a dash, and screen readers announce it as mixed. A click checks or unchecks it as usual: update indeterminate from your own state.
<script setup lang="ts">
import { computed, ref } from 'vue'
const accountNames = ['Checking', 'Savings', 'Visa card']
const selectedAccountNames = ref(['Checking'])
const areAllAccountsSelected = computed({
get: () => selectedAccountNames.value.length === accountNames.length,
set: (selectsAll: boolean) => {
selectedAccountNames.value = selectsAll ? [...accountNames] : []
},
})
const areSomeAccountsSelected = computed(
() => selectedAccountNames.value.length > 0 && !areAllAccountsSelected.value,
)
function toggleAccount(accountName: string, isSelected: boolean) {
selectedAccountNames.value = isSelected
? [...selectedAccountNames.value, accountName]
: selectedAccountNames.value.filter((selectedAccountName) => selectedAccountName !== accountName)
}
</script>
<template>
<KCheckbox v-model="areAllAccountsSelected" label="All accounts" :indeterminate="areSomeAccountsSelected" />
<KCheckbox
v-for="accountName in accountNames"
:key="accountName"
:model-value="selectedAccountNames.includes(accountName)"
:label="accountName"
@update:model-value="(isSelected) => toggleAccount(accountName, isSelected)"
/>
</template>
Hidden label
Set hide-label to show only the box, like in a table row. The label stays for screen readers.
<KCheckbox v-model="isRowSelected" label="Select Corner Coffee" hide-label />
Color
Use the color prop to change the background of the box once it is checked: primary, secondary or neutral (default). It also takes any CSS color, see With a custom color.
<KCheckbox v-model="isPrimaryChecked" label="Primary" color="primary" />
<KCheckbox v-model="isSecondaryChecked" label="Secondary" color="secondary" />
<KCheckbox v-model="isNeutralChecked" label="Neutral" />
Examples
With a custom color
Pass any CSS color to the color prop, like a hex code. Use light-dark() to set one color for the light theme and another for the dark theme.
<KCheckbox v-model="isSkyBlueChecked" label="Sky blue" color="#0ea5e9" />
<KCheckbox
v-model="isAmberOrLimeChecked"
label="Amber in light theme, lime in dark theme"
color="light-dark(#d97706, #a3e635)"
/>
API
Props
| Prop | Type | Default |
|---|---|---|
label | string | |
modelValue | boolean | false |
indeterminate | boolean | false |
hideLabel | boolean | false |
color | 'primary' | 'secondary' | 'neutral' | string | 'neutral' |
labelPosition | 'start' | 'end' | 'end' |
ui | { root?, base?, checkMark?, indeterminateMark? } | |
class | string |
Other attributes, like name, value or required, go to the <input>.
Emits
| Event | Payload |
|---|---|
update:modelValue | boolean |