๐ข Checkbox
Basic
use NCheckbox
component to create a checkbox.
Attribute | Type | Default | Description |
---|---|---|---|
v-model:checked | boolean indeterminate | false | Bind the checkbox to a boolean value. |
label | string | '' | Set the label of the checkbox. |
Indeterminate
checked="indeterminate"
- set the checkbox to indeterminate state.
Color
checkbox="{color}"
- change the color of the checkbox.
You can use
breakpoints
such assm:red, xs:green
to change color based on screen size.
You can use any color provided by the Tailwind CSS color palette, the default is
primary
. You can also add your own colors to the palette through the Configuration section.Form Group
You can use the NFormGroup
component to create a checkbox group for the checkbox,
Read more about the
NFormGroup
component here.
Notifications for the conversations you are participating in, or if someone cites you with an @mention. Also for all activity when subscribed to specific events.
Notifications for the conversations you are participating in, or if someone cites you with an @mention. Also for all activity when subscribed to specific events.
You must choose at least one event
Size
size="{size}"
- change the size of the checkbox.
You can freely adjust the size of the checkbox using any size imaginable. No limits exist, and you can use
data-[state]
to adjust the size based on the state of the checkbox.Data state | Description |
---|---|
data-[state=checked] | Only apply the class if the checkbox is checked. |
data-[state=unchecked] | Only apply the class if the checkbox is unchecked. |
data-[state=indeterminate] | Only apply the class if the checkbox is indeterminate. |
Disabled
disabled
- disable the checkbox.
Reverse
reverse
- Switch the position of the checkbox and the label.
Customization
You can customize the checkbox using the una
prop and utility classes.
You can also globally customize the checkbox preset if you want to have a different default style. See Configuration section for more details.
Property | Type | Default | Description |
---|---|---|---|
una.checkboxCheckedIcon | string | i-check | Custom icon of the checkbox when it is checked . |
una.checkboxUncheckedIcon | string | null | Custom icon of the checkbox when it is unchecked . |
una.checkboxIndeterminateIcon | string | i-lucide-minus | Custom icon of the checkbox when it is indeterminate . |
Value: [
true,
true,
true,
true
]
Slots
Name | Description |
---|---|
default | Use this slot to customize the label of the checkbox. |
icon | Use this slot to customize the icon of the checkbox when it is checked |
Props
import type { CheckboxIndicatorProps, CheckboxRootProps } from 'radix-vue'
import type { HTMLAttributes } from 'vue'
import type { NLabelProps } from './label'
interface BaseExtensions {
class?: HTMLAttributes['class']
}
export interface NCheckboxProps extends CheckboxRootProps, NLabelProps, BaseExtensions {
/**
* Disable the checkbox.
*/
disabled?: boolean
/**
* Switch the position of label and checkbox.
*/
reverse?: boolean
/**
* Allows you to add `UnaUI` checkbox preset properties,
* Think of it as a shortcut for adding options or variants to the preset if available.
*/
checkbox?: string
/**
* Add name attribute to the checkbox.
*
* @default null
*/
name?: string
/**
* Manually set the id attribute.
* By default, the id attribute is generated randomly for accessibility reasons.
*
* @default randomId
*/
id?: string
/**
* Display label text.
*
* @default null
*/
label?: string
/**
* Allows you to change the size of the checkbox.
*
* @default size="sm"
*
* @example
* size="sm" | size="2cm" | size="2rem" | size="2px"
*/
size?: string
/**
* `UnaUI` preset configuration
*
* @see https://github.com/una-ui/una-ui/blob/main/packages/preset/src/_shortcuts/checkbox.ts
*/
/**
* Force mount the indicator component.
*
* @default true
*/
forceMount?: CheckboxIndicatorProps['forceMount']
// subcomponents
_checkboxIndicator?: CheckboxIndicatorProps
_label?: NLabelProps
una?: {
checkbox?: HTMLAttributes['class']
checkboxWrapper?: HTMLAttributes['class']
checkboxLabel?: HTMLAttributes['class']
checkboxIndicator?: HTMLAttributes['class']
checkboxIconBase?: HTMLAttributes['class']
checkboxCheckedIcon?: HTMLAttributes['class']
checkboxUncheckedIcon?: HTMLAttributes['class']
checkboxIndeterminateIcon?: HTMLAttributes['class']
}
}
Presets
import type { RuleContext } from '@unocss/core'
import type { Theme } from '@unocss/preset-uno'
import { parseColor } from '@unocss/preset-mini/utils'
type CheckboxPrefix = 'checkbox'
export const staticCheckbox: Record<`${CheckboxPrefix}-${string}` | CheckboxPrefix, string> = {
// base
'checkbox': 'checkbox-primary text-md w-1em h-1em shrink-0 rounded-sm ring-offset-base focus-visible:outline-none disabled:n-disabled border border-brand bg-brand text-inverted focus-visible:(ring-2 ring-brand ring-offset-2) data-[state=unchecked]:(bg-base text-base)',
'checkbox-label': 'block',
'checkbox-reverse': 'flex-row-reverse',
// wrappers
'checkbox-wrapper': 'gap-x-3 relative inline-flex items-center hover:cursor-pointer',
// icon
'checkbox-indicator': 'flex items-center justify-center h-full w-full data-[state=unchecked]:opacity-0 transition-base opacity-100 text-inverted',
'checkbox-icon-base': 'w-1em h-1em',
'checkbox-checked-icon': 'i-check',
'checkbox-unchecked-icon': '',
'checkbox-indeterminate-icon': 'i-lucide-minus',
}
export const dynamicCheckbox = [
[/^checkbox-(.*)$/, ([, body]: string[], { theme }: RuleContext<Theme>) => {
const color = parseColor(body, theme)
if ((color?.cssColor?.type === 'rgb' || color?.cssColor?.type === 'rgba') && color.cssColor.components)
return `n-${body}-600 dark:n-${body}-500`
}],
]
export const checkbox = [
...dynamicCheckbox,
staticCheckbox,
]
Component
<script setup lang="ts">
import type { CheckboxRootEmits } from 'radix-vue'
import type { NCheckboxProps } from '../../types'
import { CheckboxIndicator, CheckboxRoot, useForwardPropsEmits } from 'radix-vue'
import { computed } from 'vue'
import { cn, randomId } from '../../utils'
import Icon from '../elements/Icon.vue'
import Label from '../elements/Label.vue'
const props = withDefaults(defineProps<NCheckboxProps>(), {
forceMount: true,
})
const emits = defineEmits<CheckboxRootEmits>()
const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
return delegated
})
const forwarded = useForwardPropsEmits(delegatedProps, emits)
const id = computed(() => props.id ?? randomId('checkbox'))
</script>
<template>
<div
checkbox="wrapper"
:class="[
una?.checkboxWrapper,
{
'checkbox-reverse': reverse,
},
]"
>
<CheckboxRoot
v-bind="forwarded"
:id="id"
:class="
cn(
'peer checkbox',
props.class,
)"
>
<CheckboxIndicator
:force-mount
:size
:class="cn('checkbox-indicator', una?.checkboxIndicator)"
v-bind="props._checkboxIndicator"
>
<slot name="icon">
<Icon
:name="props.checked === 'indeterminate'
? props.una?.checkboxIndeterminateIcon ?? 'checkbox-indeterminate-icon'
: props.checked
? props.una?.checkboxCheckedIcon ?? 'checkbox-checked-icon'
: props.una?.checkboxUncheckedIcon ?? 'checkbox-unchecked-icon'"
:class="cn('checkbox-icon-base', una?.checkboxIconBase)"
/>
</slot>
</CheckboxIndicator>
</CheckboxRoot>
<Label
v-if="$slots.default || label"
:for="props.for || id"
:class="cn('checkbox-label', una?.checkboxLabel)"
v-bind="props._label"
>
<slot>
{{ label }}
</slot>
</Label>
</div>
</template>