Checkbox Group
A Discord-inspired list of checkboxes for several answers, each with a label, an optional description and a mixed state.
A checkbox group is for several answers out of a visible list. Each option can have a description. For one answer out of the list, use a Radio group.
Each option is the Form Fields Checkbox, built on Radix Checkbox in React and Reka UI Checkbox in Vue. The primitive owns the role, the keyboard and the form input. CheckboxGroup holds the list of checked values.
The box is 20px with a 1px edge and the small radius. It fills with the brand colour when checked, and the tick draws itself along its length over --newt-duration-fast. The label is 14px medium; the description is 12px under it. Options sit 12px apart.
newt-checkgroup"use client"
import * as React from "react"
import {
CheckboxGroup,
CheckboxOption,
} from "@/registry/bases/newt/ui/checkbox-group"
export default function CheckboxGroupDemo() {
const [days, setDays] = React.useState<readonly string[]>(["mon", "tue"])
return (
<CheckboxGroup
className="w-full max-w-[360px]"
label="Which days are you free?"
value={days}
onValueChange={setDays}
>
<CheckboxOption value="mon">Monday</CheckboxOption>
<CheckboxOption value="tue">Tuesday</CheckboxOption>
<CheckboxOption value="sat" description="Short notice, but possible">
Saturday
</CheckboxOption>
<CheckboxOption value="sun" disabled>
Sunday
</CheckboxOption>
</CheckboxGroup>
)
}<script setup lang="ts">
import { ref } from "vue"
import {
CheckboxGroup,
CheckboxOption,
} from "~~/registry/bases/newt/ui/checkbox-group"
const days = ref<readonly string[]>(["mon", "tue"])
</script>
<template>
<CheckboxGroup
v-model="days"
class="w-full max-w-[360px]"
label="Which days are you free?"
>
<CheckboxOption value="mon">Monday</CheckboxOption>
<CheckboxOption value="tue">Tuesday</CheckboxOption>
<CheckboxOption value="sat" description="Short notice, but possible">
Saturday
</CheckboxOption>
<CheckboxOption value="sun" disabled>Sunday</CheckboxOption>
</CheckboxGroup>
</template>Installation
npx newtui@latest add checkbox-groupnpx shadcn@latest add @newtui/checkbox-groupnpx shadcn-vue@latest add @newtui/checkbox-grouppnpm dlx newtui@latest add checkbox-grouppnpm dlx shadcn@latest add @newtui/checkbox-grouppnpm dlx shadcn-vue@latest add @newtui/checkbox-groupyarn dlx newtui@latest add checkbox-groupyarn dlx shadcn@latest add @newtui/checkbox-groupyarn dlx shadcn-vue@latest add @newtui/checkbox-groupbunx newtui@latest add checkbox-groupbunx shadcn@latest add @newtui/checkbox-groupbunx shadcn-vue@latest add @newtui/checkbox-groupdeno run -A npm:newtui@latest add checkbox-groupdeno run -A npm:shadcn@latest add @newtui/checkbox-groupdeno run -A npm:shadcn-vue@latest add @newtui/checkbox-groupvlx newtui@latest add checkbox-groupvlx shadcn@latest add @newtui/checkbox-groupvlx shadcn-vue@latest add @newtui/checkbox-groupvp dlx newtui@latest add checkbox-groupvp dlx shadcn@latest add @newtui/checkbox-groupvp dlx shadcn-vue@latest add @newtui/checkbox-groupnubx newtui@latest add checkbox-groupnubx shadcn@latest add @newtui/checkbox-groupnubx shadcn-vue@latest add @newtui/checkbox-groupnlx newtui@latest add checkbox-groupnlx shadcn@latest add @newtui/checkbox-groupnlx shadcn-vue@latest add @newtui/checkbox-groupupx newtui@latest add checkbox-groupupx shadcn@latest add @newtui/checkbox-groupupx shadcn-vue@latest add @newtui/checkbox-group- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install radix-uipnpm add radix-uiyarn add radix-uibun add radix-uideno add npm:radix-uivlt install radix-uivp add radix-uinub add radix-uini radix-uiupm add radix-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
npm install reka-uipnpm add reka-uiyarn add reka-uibun add reka-uideno add npm:reka-uivlt install reka-uivp add reka-uinub add reka-uini reka-uiupm add reka-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Add the components it builds on
Copy the source into your project
"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
import { Checkbox } from "@/registry/bases/newt/ui/form-fields"
/*
* Several answers out of a visible list, where a radio group takes one. The
* group owns the set of chosen values; each row is the form-fields
* `Checkbox`, a Radix Checkbox that owns the role, the space bar and the
* hidden input a form reads.
*/
interface CheckboxGroupContextValue {
readonly values: readonly string[]
readonly toggle: (value: string) => void
readonly name: string | undefined
readonly disabled: boolean
}
const CheckboxGroupContext =
React.createContext<CheckboxGroupContextValue | null>(null)
function useCheckboxGroup(component: string): CheckboxGroupContextValue {
const context = React.useContext(CheckboxGroupContext)
if (context === null) {
throw new Error(`<${component}> must be used inside a <CheckboxGroup>.`)
}
return context
}
export interface CheckboxGroupProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onChange" | "defaultValue"
> {
/** Controlled selection. Leave undefined to let the group own it. */
value?: readonly string[]
defaultValue?: readonly string[]
onValueChange?: (value: readonly string[]) => void
/** Accessible name for the group. */
label?: string
/** Shared `name` of the rows. A form then receives one entry per checked row. */
name?: string
/** Disables every row. */
disabled?: boolean
}
const CheckboxGroup = React.forwardRef<HTMLDivElement, CheckboxGroupProps>(
(
{
className,
value,
defaultValue = [],
onValueChange,
label,
name,
disabled = false,
children,
...props
},
ref
) => {
const [internal, setInternal] =
React.useState<readonly string[]>(defaultValue)
const isControlled = value !== undefined
const current = isControlled ? value : internal
const toggle = React.useCallback(
(option: string) => {
const next = current.includes(option)
? current.filter((each) => each !== option)
: [...current, option]
if (!isControlled) setInternal(next)
onValueChange?.(next)
},
[current, isControlled, onValueChange]
)
const context = React.useMemo<CheckboxGroupContextValue>(
() => ({ values: current, toggle, name, disabled }),
[current, toggle, name, disabled]
)
return (
<CheckboxGroupContext.Provider value={context}>
<div
ref={ref}
role="group"
aria-label={label}
className={cn("flex flex-col gap-[var(--newt-space-3)]", className)}
{...props}
>
{children}
</div>
</CheckboxGroupContext.Provider>
)
}
)
CheckboxGroup.displayName = "CheckboxGroup"
/** What a row shows: ticked, clear, or a mix of the rows it stands for. */
export type CheckboxOptionChecked = boolean | "indeterminate"
type CheckboxOptionBaseProps = Omit<
React.ComponentPropsWithoutRef<typeof Checkbox>,
"checked" | "defaultChecked" | "onCheckedChange" | "value" | "name"
> & {
/** The quieter line under the label, linked with `aria-describedby`. */
description?: React.ReactNode
}
export type CheckboxOptionProps = CheckboxOptionBaseProps &
(
| {
/** What this row stands for in the group's value. */
value: string
checked?: never
onCheckedChange?: never
}
| {
/**
* Submitted under the group's `name` while the row is ticked. A row
* with `checked` stays out of the group's value.
*/
value?: string
/**
* Sets the row's state yourself instead of the group. Pass
* `"indeterminate"` for a row that stands for others of which only
* some are ticked: it draws a dash and reads as mixed.
*/
checked: CheckboxOptionChecked
/** Called with the state a press asks for. */
onCheckedChange?: (checked: boolean) => void
}
)
/* Each id once, in order: the caller's first, then the row's description. */
function joinIds(...lists: readonly (string | undefined)[]) {
const ids = [
...new Set(lists.flatMap((list) => (list ?? "").split(/\s+/))),
].filter((entry) => entry !== "")
return ids.length > 0 ? ids.join(" ") : undefined
}
const CheckboxOption = React.forwardRef<
React.ComponentRef<typeof Checkbox>,
CheckboxOptionProps
>(
(
{
className,
value,
checked: checkedProp,
onCheckedChange,
description,
disabled,
children,
"aria-describedby": describedBy,
...props
},
ref
) => {
const group = useCheckboxGroup("CheckboxOption")
const id = React.useId()
const labelId = `${id}-label`
const descriptionId = `${id}-description`
const isDisabled = group.disabled || disabled === true
const checked =
checkedProp ?? (value !== undefined && group.values.includes(value))
const hasDescription = description !== undefined && description !== null
return (
/*
* The label wraps the box, so a press on the label or the description
* reaches it. `aria-labelledby` keeps the name to the label line: the
* description is linked as the description, not read as part of it.
*/
<label
className={cn(
"flex min-w-0 items-start gap-[var(--newt-space-3)]",
isDisabled ? "cursor-not-allowed opacity-50" : "cursor-pointer",
className
)}
>
<Checkbox
ref={ref}
name={value === undefined ? undefined : group.name}
value={value}
checked={checked}
disabled={isDisabled}
aria-labelledby={labelId}
aria-describedby={joinIds(
describedBy,
hasDescription ? descriptionId : undefined
)}
onCheckedChange={(next) => {
if (checkedProp !== undefined) onCheckedChange?.(next === true)
else if (value !== undefined) group.toggle(value)
}}
/*
* The row carries the dimming, not the box as well. A hovered
* label also hovers the control it labels, so the box lights up
* from anywhere on the row with the Checkbox's own hover rules.
*/
className="disabled:opacity-100"
/*
* Its own id, unless you pass one: inside a `Field`, a `Checkbox`
* would otherwise take the field's id, the same for every row.
*/
id={`${id}-control`}
{...props}
/>
<span className="flex min-w-0 flex-col gap-[2px] [overflow-wrap:anywhere]">
<span
id={labelId}
className="text-[length:var(--newt-font-size-sm)] font-[number:var(--newt-font-weight-medium)] leading-[20px] text-newt-text-primary"
>
{children}
</span>
{hasDescription ? (
<span
id={descriptionId}
className="text-[length:var(--newt-font-size-xs)] font-[number:var(--newt-font-weight-normal)] leading-[16px] text-newt-text-muted"
>
{description}
</span>
) : null}
</span>
</label>
)
}
)
CheckboxOption.displayName = "CheckboxOption"
export { CheckboxGroup, CheckboxOption }<script setup lang="ts">
import { computed, provide, ref, watch, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import { checkboxGroupKey } from "."
/*
* Several answers out of a visible list, where a radio group takes one. The
* group owns the set of chosen values; each row is the form-fields
* `Checkbox`, a Reka Checkbox that owns the role, the space bar and the
* hidden input a form reads.
*/
const {
class: className,
modelValue = undefined,
defaultValue = [],
label,
name,
disabled = false,
} = defineProps<{
class?: HTMLAttributes["class"]
/** Controlled selection (use with v-model). */
modelValue?: readonly string[]
defaultValue?: readonly string[]
/** Accessible name for the group. */
label?: string
/** Shared `name` of the rows. A form then receives one entry per checked row. */
name?: string
/** Disables every row. */
disabled?: boolean
}>()
const emit = defineEmits<{
(e: "update:modelValue", value: readonly string[]): void
}>()
const internal = ref<readonly string[]>(modelValue ?? defaultValue)
watch(
() => modelValue,
(next) => {
if (next !== undefined) internal.value = next
}
)
const values = computed(() => modelValue ?? internal.value)
function toggle(value: string) {
const next = values.value.includes(value)
? values.value.filter((each) => each !== value)
: [...values.value, value]
if (modelValue === undefined) internal.value = next
emit("update:modelValue", next)
}
provide(checkboxGroupKey, {
values,
toggle,
name: computed(() => name),
disabled: computed(() => disabled),
})
</script>
<template>
<div
role="group"
:aria-label="label"
:class="cn('flex flex-col gap-[var(--newt-space-3)]', className)"
>
<slot />
</div>
</template><script setup lang="ts">
import { computed, inject, useId, type HTMLAttributes } from "vue"
import { Checkbox } from "~~/registry/bases/newt/ui/form-fields"
import { cn } from "@/lib/utils"
import { checkboxGroupKey, type CheckboxOptionChecked } from "."
/* Attributes other than `class` land on the checkbox, not on the label. */
defineOptions({ inheritAttrs: false })
const {
class: className,
value,
checked = undefined,
description,
disabled = false,
required = false,
ariaDescribedby,
id: controlIdProp,
} = defineProps<{
class?: HTMLAttributes["class"]
/**
* What this row stands for in the group's value. A row with `checked`
* stays out of the group's value and submits this while ticked.
*/
value?: string
/**
* Sets the row's state yourself instead of the group (use with
* `v-model:checked`). Pass `"indeterminate"` for a row that stands for
* others of which only some are ticked: it draws a dash and reads as mixed.
*/
checked?: CheckboxOptionChecked
/** The quieter line under the label, linked with `aria-describedby`. */
description?: string
disabled?: boolean
required?: boolean
/** Ids of more lines that describe the row, listed before the description. */
ariaDescribedby?: string
/** The checkbox's id. Each row makes its own when this is left out. */
id?: string
}>()
const emit = defineEmits<{
/** The state a press asks for, on a row with `checked`. */
(e: "update:checked", value: boolean): void
}>()
const injected = inject(checkboxGroupKey)
if (injected === undefined) {
throw new Error("<CheckboxOption> must be used inside a <CheckboxGroup>.")
}
const group = injected
if (value === undefined && checked === undefined) {
throw new Error("<CheckboxOption> needs a `value` or a `checked`.")
}
const id = useId()
const labelId = `${id}-label`
const descriptionId = `${id}-description`
/*
* Its own id, unless you pass one: inside a `Field`, a `Checkbox` would
* otherwise take the field's id, the same for every row.
*/
const controlId = computed(() => controlIdProp ?? `${id}-control`)
const state = computed<CheckboxOptionChecked>(
() => checked ?? (value !== undefined && group.values.value.includes(value))
)
const isDisabled = computed(() => group.disabled.value || disabled)
/* Each id once, in order: the caller's first, then the row's description. */
const describedBy = computed(() => {
const ids = [
...new Set(
[
...(ariaDescribedby ?? "").split(/\s+/),
description ? descriptionId : "",
].filter((entry) => entry !== "")
),
]
return ids.length > 0 ? ids.join(" ") : undefined
})
function onUpdate(next: CheckboxOptionChecked) {
if (checked !== undefined) emit("update:checked", next === true)
else if (value !== undefined) group.toggle(value)
}
</script>
<template>
<!--
The label wraps the box, so a press on the label or the description
reaches it. `aria-labelledby` keeps the name to the label line: the
description is linked as the description, not read as part of it.
-->
<label
:class="
cn(
'flex min-w-0 items-start gap-[var(--newt-space-3)]',
isDisabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer',
className
)
"
>
<!--
The row carries the dimming, not the box as well. A hovered label also
hovers the control it labels, so the box lights up from anywhere on the
row with the Checkbox's own hover rules.
-->
<Checkbox
v-bind="$attrs"
:id="controlId"
:name="value === undefined ? undefined : group.name.value"
:value="value"
:model-value="state"
:disabled="isDisabled"
:required="required"
:aria-labelledby="labelId"
:aria-describedby="describedBy"
class="disabled:opacity-100"
@update:model-value="onUpdate"
/>
<span class="flex min-w-0 flex-col gap-[2px] [overflow-wrap:anywhere]">
<span
:id="labelId"
class="text-[length:var(--newt-font-size-sm)] font-[number:var(--newt-font-weight-medium)] leading-[20px] text-newt-text-primary"
>
<slot />
</span>
<span
v-if="description"
:id="descriptionId"
class="text-[length:var(--newt-font-size-xs)] font-[number:var(--newt-font-weight-normal)] leading-[16px] text-newt-text-muted"
>
{{ description }}
</span>
</span>
</label>
</template>import type { InjectionKey, Ref } from "vue"
export { default as CheckboxGroup } from "./CheckboxGroup.vue"
export { default as CheckboxOption } from "./CheckboxOption.vue"
/** What a row shows: ticked, clear, or a mix of the rows it stands for. */
export type CheckboxOptionChecked = boolean | "indeterminate"
/** What a row needs from the group it sits in. */
export interface CheckboxGroupContext {
readonly values: Ref<readonly string[]>
readonly toggle: (value: string) => void
readonly name: Ref<string | undefined>
readonly disabled: Ref<boolean>
}
export const checkboxGroupKey: InjectionKey<CheckboxGroupContext> = Symbol(
"newt-checkbox-group"
)Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Checkbox Group to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Checkbox Group to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { CheckboxGroup } from "@/components/ui/checkbox-group"
export function Example() {
return <CheckboxGroup />
}<script setup lang="ts">
import { CheckboxGroup, CheckboxOption } from "@/components/ui/checkbox-group"
</script>
<template>
<CheckboxGroup />
</template>Examples
With descriptions
Give every option a description when the labels alone do not say what ticking them will do. The description is linked to its checkbox with aria-describedby. This group is uncontrolled: it starts from defaultValue and keeps its own state.
newt-checkgroupimport {
CheckboxGroup,
CheckboxOption,
} from "@/registry/bases/newt/ui/checkbox-group"
export default function CheckboxGroupDescriptionsDemo() {
return (
<CheckboxGroup
className="w-full max-w-[360px]"
label="Notify me about"
defaultValue={["mentions", "events"]}
>
<CheckboxOption
value="mentions"
description="When somebody names you or a role you hold."
>
Mentions
</CheckboxOption>
<CheckboxOption
value="replies"
description="When somebody answers one of your messages."
>
Replies
</CheckboxOption>
<CheckboxOption
value="events"
description="Fifteen minutes before an event you joined starts."
>
Event reminders
</CheckboxOption>
</CheckboxGroup>
)
}<script setup lang="ts">
import {
CheckboxGroup,
CheckboxOption,
} from "~~/registry/bases/newt/ui/checkbox-group"
</script>
<template>
<CheckboxGroup
class="w-full max-w-[360px]"
label="Notify me about"
:default-value="['mentions', 'events']"
>
<CheckboxOption
value="mentions"
description="When somebody names you or a role you hold."
>
Mentions
</CheckboxOption>
<CheckboxOption
value="replies"
description="When somebody answers one of your messages."
>
Replies
</CheckboxOption>
<CheckboxOption
value="events"
description="Fifteen minutes before an event you joined starts."
>
Event reminders
</CheckboxOption>
</CheckboxGroup>
</template>Indeterminate
An option can stand for the others. Pass checked to set its state yourself, and pass "indeterminate" while only some of the others are ticked. The box then draws a dash, and the checkbox has aria-checked="mixed". An option with checked stays out of the group’s value; handle a press with onCheckedChange in React or v-model:checked in Vue.
newt-checkgroup"use client"
import * as React from "react"
import {
CheckboxGroup,
CheckboxOption,
} from "@/registry/bases/newt/ui/checkbox-group"
const NOTIFICATIONS = [
{
value: "mentions",
label: "Mentions",
description: "When somebody names you or a role you hold.",
},
{ value: "replies", label: "Replies" },
{ value: "events", label: "Event reminders" },
]
/*
* The first row stands for the others. It is ticked when all of them are,
* clear when none are, and indeterminate in between: it draws a dash and a
* screen reader hears "mixed". A press on it ticks or clears them all.
*/
export default function CheckboxGroupIndeterminateDemo() {
const [chosen, setChosen] = React.useState<readonly string[]>(["mentions"])
const all =
chosen.length === NOTIFICATIONS.length
? true
: chosen.length === 0
? false
: "indeterminate"
return (
<CheckboxGroup
className="w-full max-w-[360px]"
label="Notify me about"
name="notify"
value={chosen}
onValueChange={setChosen}
>
<CheckboxOption
checked={all}
onCheckedChange={(next) =>
setChosen(next ? NOTIFICATIONS.map((each) => each.value) : [])
}
>
All notifications
</CheckboxOption>
{NOTIFICATIONS.map((each) => (
<CheckboxOption
key={each.value}
className="ps-[var(--newt-space-8)]"
value={each.value}
description={each.description}
>
{each.label}
</CheckboxOption>
))}
</CheckboxGroup>
)
}<script setup lang="ts">
import { computed, ref } from "vue"
import {
CheckboxGroup,
CheckboxOption,
type CheckboxOptionChecked,
} from "~~/registry/bases/newt/ui/checkbox-group"
const NOTIFICATIONS = [
{
value: "mentions",
label: "Mentions",
description: "When somebody names you or a role you hold.",
},
{ value: "replies", label: "Replies" },
{ value: "events", label: "Event reminders" },
]
/*
* The first row stands for the others. It is ticked when all of them are,
* clear when none are, and indeterminate in between: it draws a dash and a
* screen reader hears "mixed". A press on it ticks or clears them all.
*/
const chosen = ref<readonly string[]>(["mentions"])
const all = computed<CheckboxOptionChecked>(() =>
chosen.value.length === NOTIFICATIONS.length
? true
: chosen.value.length === 0
? false
: "indeterminate"
)
function setAll(next: boolean) {
chosen.value = next ? NOTIFICATIONS.map((each) => each.value) : []
}
</script>
<template>
<CheckboxGroup
v-model="chosen"
class="w-full max-w-[360px]"
label="Notify me about"
name="notify"
>
<CheckboxOption :checked="all" @update:checked="setAll">
All notifications
</CheckboxOption>
<CheckboxOption
v-for="each in NOTIFICATIONS"
:key="each.value"
class="ps-[var(--newt-space-8)]"
:value="each.value"
:description="each.description"
>
{{ each.label }}
</CheckboxOption>
</CheckboxGroup>
</template>Controlled
Hold the value yourself when something outside the group has to read or replace it. Here a count follows the selection and two Buttons rewrite it in one step.
newt-checkgroup"use client"
import * as React from "react"
import { Button } from "@/registry/bases/newt/ui/button"
import {
CheckboxGroup,
CheckboxOption,
} from "@/registry/bases/newt/ui/checkbox-group"
const PERMISSIONS = [
{ value: "read", label: "Read messages" },
{ value: "send", label: "Send messages" },
{ value: "attach", label: "Attach files" },
{ value: "pin", label: "Pin messages" },
]
/*
* With the value held outside, anything else on the page can write it: the
* two buttons replace the whole selection in one step.
*/
export default function CheckboxGroupControlledDemo() {
const [granted, setGranted] = React.useState<readonly string[]>([
"read",
"send",
])
return (
<div className="flex w-full max-w-[360px] flex-col gap-4">
<div className="flex items-center justify-between gap-3">
<span className="text-sm text-newt-text-muted" aria-live="polite">
{granted.length} of {PERMISSIONS.length} granted
</span>
<div className="flex gap-2">
<Button
variant="secondary"
size="sm"
onClick={() => setGranted(PERMISSIONS.map((each) => each.value))}
>
Grant all
</Button>
<Button
variant="secondary"
size="sm"
disabled={granted.length === 0}
onClick={() => setGranted([])}
>
Clear
</Button>
</div>
</div>
<CheckboxGroup
label="Channel permissions"
value={granted}
onValueChange={setGranted}
>
{PERMISSIONS.map((permission) => (
<CheckboxOption key={permission.value} value={permission.value}>
{permission.label}
</CheckboxOption>
))}
</CheckboxGroup>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Button } from "~~/registry/bases/newt/ui/button"
import {
CheckboxGroup,
CheckboxOption,
} from "~~/registry/bases/newt/ui/checkbox-group"
const PERMISSIONS = [
{ value: "read", label: "Read messages" },
{ value: "send", label: "Send messages" },
{ value: "attach", label: "Attach files" },
{ value: "pin", label: "Pin messages" },
]
/*
* With the value held outside, anything else on the page can write it: the
* two buttons replace the whole selection in one step.
*/
const granted = ref<readonly string[]>(["read", "send"])
</script>
<template>
<div class="flex w-full max-w-[360px] flex-col gap-4">
<div class="flex items-center justify-between gap-3">
<span class="text-sm text-newt-text-muted" aria-live="polite">
{{ granted.length }} of {{ PERMISSIONS.length }} granted
</span>
<div class="flex gap-2">
<Button
variant="secondary"
size="sm"
@click="granted = PERMISSIONS.map((each) => each.value)"
>
Grant all
</Button>
<Button
variant="secondary"
size="sm"
:disabled="granted.length === 0"
@click="granted = []"
>
Clear
</Button>
</div>
</div>
<CheckboxGroup v-model="granted" label="Channel permissions">
<CheckboxOption
v-for="permission in PERMISSIONS"
:key="permission.value"
:value="permission.value"
>
{{ permission.label }}
</CheckboxOption>
</CheckboxGroup>
</div>
</template>Inline
The group is a flex column by default. For a few short labels, pass layout classes to lay the options out in a row that wraps.
newt-checkgroupimport {
CheckboxGroup,
CheckboxOption,
} from "@/registry/bases/newt/ui/checkbox-group"
export default function CheckboxGroupInlineDemo() {
return (
<CheckboxGroup
className="flex-row flex-wrap gap-x-6 gap-y-3"
label="Where do you play?"
defaultValue={["desktop"]}
>
<CheckboxOption value="desktop">Desktop</CheckboxOption>
<CheckboxOption value="console">Console</CheckboxOption>
<CheckboxOption value="mobile">Mobile</CheckboxOption>
</CheckboxGroup>
)
}<script setup lang="ts">
import {
CheckboxGroup,
CheckboxOption,
} from "~~/registry/bases/newt/ui/checkbox-group"
</script>
<template>
<CheckboxGroup
class="flex-row flex-wrap gap-x-6 gap-y-3"
label="Where do you play?"
:default-value="['desktop']"
>
<CheckboxOption value="desktop">Desktop</CheckboxOption>
<CheckboxOption value="console">Console</CheckboxOption>
<CheckboxOption value="mobile">Mobile</CheckboxOption>
</CheckboxGroup>
</template>API reference
CheckboxGroup
value / v-model?string[]
The checked values, when you control them. In React, leave it undefined and pass `defaultValue` to let the group own them.
string[]defaultValue?string[]
The values checked on first render, for an uncontrolled group.
string[][]onValueChange?(value: string[]) => void
React only. Called with the new list of values; Vue emits `update:modelValue`.
(value: string[]) => voidlabel?string
Accessible name for the group.
stringname?string
The field name of every option. Inside a `<form>`, each checked option submits its `value` under this name.
stringdisabled?boolean
Disables every option.
booleanfalseclassName / class?string
Additional classes merged with the component defaults.
stringCheckboxOption
value?string
What this option stands for in the group's value, unique within the group. Required unless you pass `checked`; with `checked`, it is submitted while the option is ticked.
stringchecked / v-model:checked?boolean | "indeterminate"
Sets the option's state yourself instead of the group. `"indeterminate"` draws a dash and sets `aria-checked="mixed"`. The option then stays out of the group's value.
boolean | "indeterminate"onCheckedChange?(checked: boolean) => void
React only. Called with the state a press asks for, on an option with `checked`. Vue emits `update:checked`.
(checked: boolean) => voiddescription?ReactNode / string
A quieter line under the label, linked to the checkbox with `aria-describedby`. An `aria-describedby` you pass is kept: its ids come first, then the description's.
ReactNode / stringdisabled?boolean
Dims the option and stops it taking input.
booleanfalserequired?boolean
The form does not submit until this option is checked.
booleanfalsechildren / default slot?React.ReactNode / Vue slot
The label.
React.ReactNode / Vue slotclassName (React) or class (Vue) on CheckboxOption goes to the <label> around the row. Other attributes go to the checkbox button.
Accessibility
The group has role="group". Give it a label so a screen reader says what the options are for. Each option is a button with role="checkbox" and aria-checked, which is "mixed" for an indeterminate option. The button is inside a <label>, so a press on the label or the description toggles it. Its accessible name is the label alone (aria-labelledby), and the description is linked with aria-describedby.
Each option is a tab stop. Space toggles the focused option. A focused option shows a 2px --newt-focus-ring outline 2px outside the box. The tick, the dash and the filled box show the state, so it does not depend on colour alone. The tick and the dash do not animate when the system asks for reduced motion.
In plain HTML, each option is a native <input type="checkbox"> that works with no script. indeterminate is a DOM property, so checkbox-group.js sets it: an input with data-newt-checkgroup-all ticks or clears the other options of its group and turns indeterminate while only some are ticked. A browser announces an indeterminate input as mixed.
Tokens
Checkbox Group reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-checkgroup.
--newt-space-3--newt-space-5--newt-border-strong--newt-border--newt-radius-sm--newt-bg-input--newt-on-accent--newt-duration-instant--newt-ease-in--newt-text-muted--newt-duration-base--newt-ease-out--newt-brand--newt-border-subtle--newt-brand-hover--newt-duration-fast--newt-focus-ring--newt-text-link--newt-text-primary--newt-font-size-sm--newt-font-weight-medium--newt-font-size-xs--newt-font-weight-normal