Checkbox Group
Several choices out of one set, held as one array of values in the order the set declares them.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';export default function CheckboxGroupBasic() { return ( <FieldGroup> <FieldGroupLegend>Notify me about failed payments by</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup name="channels" defaultValue={['email', 'in-app']}> <Checkbox value="email">Email</Checkbox> <Checkbox value="sms">Text message</Checkbox> <Checkbox value="in-app">In-app notification</Checkbox> <Checkbox value="slack">Slack, #billing-alerts</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesimport { Checkbox, CheckboxGroup, CheckboxGroupDefaultsProvider } from '@slotted/react';Orientation
A set flows vertically by default. orientation="horizontal" lays the boxes in a row for short labels, such as statuses in a filter bar, and the row wraps instead of scrolling when it runs out of width. Every box stays its own tab stop in both orientations.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';export default function CheckboxGroupOrientation() { return ( <FieldGroup> <FieldGroupLegend>Invoice status</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup orientation="horizontal" defaultValue={['open', 'overdue']}> <Checkbox value="draft">Draft</Checkbox> <Checkbox value="open">Open</Checkbox> <Checkbox value="paid">Paid</Checkbox> <Checkbox value="overdue">Overdue</Checkbox> <Checkbox value="void">Void</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}Sizes
size sets the boxes, the labels and the spacing: sm for dense panels and toolbars, md for most forms, lg for sparse forms. Inside a field or a field group the checkbox group takes their size, so one prop sizes the legend and the boxes together.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';import { Stack } from '@slotted/react/stack';export default function CheckboxGroupSizes() { return ( <Stack gap={5}> <FieldGroup size="sm"> <FieldGroupLegend>Show columns</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup orientation="horizontal" defaultValue={['sku', 'on-hand']}> <Checkbox value="sku">SKU</Checkbox> <Checkbox value="on-hand">On hand</Checkbox> <Checkbox value="reserved">Reserved</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <FieldGroup size="md"> <FieldGroupLegend>Ship from</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup orientation="horizontal" defaultValue={['seattle']}> <Checkbox value="seattle">Seattle</Checkbox> <Checkbox value="tacoma">Tacoma</Checkbox> <Checkbox value="portland">Portland</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <FieldGroup size="lg"> <FieldGroupLegend>Accept payment by</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup orientation="horizontal" defaultValue={['card', 'transfer']}> <Checkbox value="card">Card</Checkbox> <Checkbox value="transfer">Bank transfer</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> </Stack> );}Label placement
labelPlacement="start" puts each label first and the boxes in a column at the end of the row, as in a settings panel where the labels are read first. The placement is set once for the set, so the boxes line up with each other.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';export default function CheckboxGroupLabelPlacement() { return ( <FieldGroup> <FieldGroupLegend>Email reports</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup labelPlacement="start" defaultValue={['weekly']}> <Checkbox value="daily">Daily sales summary</Checkbox> <Checkbox value="weekly">Weekly stock report</Checkbox> <Checkbox value="monthly">Monthly statement</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}Tiles
variant="tile" frames every choice and makes its whole surface the target, for a choice that needs a price or a sentence beside its name, such as add-ons on a plan. The variant is set once for the set. The checkbox group does not arrange the tiles: put a Grid around them for equal widths and equal heights.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';import { Grid } from '@slotted/react/grid';export default function CheckboxGroupTiles() { return ( <FieldGroup> <FieldGroupLegend>Add-ons</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup name="add-ons" variant="tile" defaultValue={['audit-log']}> <Grid minItemWidth="xs"> <Checkbox value="audit-log" description="$29 per month. Every change kept for a year."> Audit log </Checkbox> <Checkbox value="sso" description="$49 per month. SAML and SCIM provisioning."> Single sign-on </Checkbox> <Checkbox value="support" description="$99 per month. Replies within four hours."> Priority support </Checkbox> </Grid> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}Descriptions
A description under one box says what that choice does. In React it is the description prop of Checkbox; in Angular it is a slCheckboxDescription element inside the box. It joins that box’s accessible description only, and pressing it does not toggle the box.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';export default function CheckboxGroupDescriptions() { return ( <FieldGroup> <FieldGroupLegend>When stock runs low</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup defaultValue={['alert']}> <Checkbox value="alert" description="Sent to the buyer for the SKU when on-hand drops below the reorder point." > Alert the buyer </Checkbox> <Checkbox value="draft-po" description="Created for the preferred supplier at the usual order quantity, waiting for approval." > Draft a purchase order </Checkbox> <Checkbox value="hide" description="The product page shows Out of stock instead of a delivery date." > Stop selling online at zero </Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}Select all
A Checkbox with parent checks and unchecks the whole set. It needs allValues, the list of every value the checkbox group holds. The parent box is checked when every value is selected, mixed when some are, and unchecked when none are; you write no state for it. Pressing it replaces the whole selection. allValues also keeps the selection in the order of that list rather than in the order the boxes were pressed.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';const COLUMNS = ['order', 'customer', 'placed', 'total', 'status'];export default function CheckboxGroupParent() { return ( <FieldGroup> <FieldGroupLegend>Columns to export</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup name="columns" allValues={COLUMNS} defaultValue={['order', 'customer', 'total']} > <Checkbox parent>All columns</Checkbox> <Checkbox value="order">Order number</Checkbox> <Checkbox value="customer">Customer</Checkbox> <Checkbox value="placed">Placed on</Checkbox> <Checkbox value="total">Total</Checkbox> <Checkbox value="status">Status</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}Selection count
Inside a field, showCount draws how many values are selected out of allValues at the end of the label row, as 2/4, and adds that count to the group’s accessible description. The field’s label names the group. The count updates as boxes are pressed.
Orders ship from the nearest selected warehouse.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';const WAREHOUSES = ['seattle', 'tacoma', 'portland', 'boise'];export default function CheckboxGroupCount() { return ( <Field> <FieldLabel>Fulfil from</FieldLabel> <CheckboxGroup name="warehouses" allValues={WAREHOUSES} defaultValue={['seattle', 'tacoma']} showCount > <Checkbox value="seattle">Seattle</Checkbox> <Checkbox value="tacoma">Tacoma</Checkbox> <Checkbox value="portland">Portland</Checkbox> <Checkbox value="boise">Boise</Checkbox> </CheckboxGroup> <FieldDescription>Orders ship from the nearest selected warehouse.</FieldDescription> </Field> );}Unavailable option
disabled on one Checkbox takes that choice alone out of the tab order and out of form submission, while the rest of the set stays usable. Its description can say what turns it on.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';export default function CheckboxGroupDisabledOption() { return ( <FieldGroup> <FieldGroupLegend>Send order updates by</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup name="updates" defaultValue={['email']}> <Checkbox value="email">Email</Checkbox> <Checkbox value="sms" disabled description="Add a mobile number to the customer record to turn this on." > Text message </Checkbox> <Checkbox value="webhook">Webhook to the storefront</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> );}States
invalid sets aria-invalid on the group and on every box and draws every box in the danger tone; the field group’s error says what is wrong. Set readOnly and disabled on the field group, which passes them to the checkbox group: read-only keeps every box focusable and refuses every change, and disabled takes the whole set out of the tab order and out of form submission.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupDescription, FieldGroupError, FieldGroupLegend,} from '@slotted/react/field-group';import { Stack } from '@slotted/react/stack';export default function CheckboxGroupStates() { return ( <Stack gap={6}> <FieldGroup required> <FieldGroupLegend>Sales regions</FieldGroupLegend> <FieldGroupError>Choose at least one region for this price list.</FieldGroupError> <FieldGroupContent> <CheckboxGroup invalid orientation="horizontal"> <Checkbox value="us">United States</Checkbox> <Checkbox value="ca">Canada</Checkbox> <Checkbox value="eu">European Union</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <FieldGroup readOnly> <FieldGroupLegend>Tax exemptions</FieldGroupLegend> <FieldGroupDescription> Set by the finance team on the customer record. </FieldGroupDescription> <FieldGroupContent> <CheckboxGroup orientation="horizontal" defaultValue={['resale']}> <Checkbox value="resale">Resale</Checkbox> <Checkbox value="nonprofit">Nonprofit</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <FieldGroup disabled> <FieldGroupLegend>Carriers</FieldGroupLegend> <FieldGroupDescription> Carriers open once a ship-from address is saved. </FieldGroupDescription> <FieldGroupContent> <CheckboxGroup orientation="horizontal" defaultValue={['ups']}> <Checkbox value="ups">UPS</Checkbox> <Checkbox value="fedex">FedEx</Checkbox> <Checkbox value="usps">USPS</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> </Stack> );}Controlled
Pass value and update it from onValueChange in React or (valueChange) in Angular when other parts of the screen depend on the selection. The request carries the new array, its reason (pointer or keyboard) and cancel(), which refuses the change before any box moves. Without value, the checkbox group keeps the selection itself and starts from defaultValue.
import { useState } from 'react';import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';import { Stack } from '@slotted/react/stack';const ORDERS: Record<string, number> = { card: 812, transfer: 145, paypal: 96 };export default function CheckboxGroupControlled() { const [methods, setMethods] = useState<readonly string[]>(['card', 'transfer']); const shown = methods.reduce((sum, method) => sum + (ORDERS[method] ?? 0), 0); return ( <Stack gap={4}> <FieldGroup> <FieldGroupLegend>Payment method</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup orientation="horizontal" value={methods} onValueChange={setMethods}> <Checkbox value="card">Card</Checkbox> <Checkbox value="transfer">Bank transfer</Checkbox> <Checkbox value="paypal">PayPal</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <span>Showing {shown} orders</span> </Stack> );}Validation on submit
A checkbox group inside a required field group carries aria-required, and never the native required, which on a checkbox would demand that one particular box be checked. Validate when the form is submitted, then set invalid and write the field group’s error; live="polite" announces the error once, and reserve keeps its line open so the boxes do not move. In Angular the checkbox group is also a form control: bind formControl, formControlName or ngModel on it, and inside a required field group its validator fails with required while the selection is empty. Submit with nothing checked, then check a channel and submit again.
import { useState, type SubmitEvent } from 'react';import { Button } from '@slotted/react/button';import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupError, FieldGroupLegend,} from '@slotted/react/field-group';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';export default function CheckboxGroupValidation() { const [channels, setChannels] = useState<readonly string[]>([]); const [submitted, setSubmitted] = useState(false); const missing = submitted && channels.length === 0; const submit = (event: SubmitEvent<HTMLFormElement>) => { event.preventDefault(); setSubmitted(true); }; return ( <form noValidate onSubmit={submit}> <Stack gap={4}> <FieldGroup required> <FieldGroupLegend>Send shipping confirmations by</FieldGroupLegend> <FieldGroupError live="polite" reserve> {missing ? 'Choose at least one channel.' : ''} </FieldGroupError> <FieldGroupContent> <CheckboxGroup invalid={missing ? true : undefined} value={channels} onValueChange={setChannels} > <Checkbox value="email">Email</Checkbox> <Checkbox value="sms">Text message</Checkbox> <Checkbox value="in-app">In-app notification</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <Inline gap={3} align="center"> <Button type="submit">Save preferences</Button> {submitted && channels.length > 0 ? <span role="status">Preferences saved.</span> : null} </Inline> </Stack> </form> );}Defaults for a section
A defaults provider sets orientation, labelPlacement, variant and size once for every checkbox group under it. A checkbox group that states a value of its own keeps it, and a field or field group around it decides size before the provider does.
import { Checkbox } from '@slotted/react/checkbox';import { CheckboxGroup, CheckboxGroupDefaultsProvider } from '@slotted/react/checkbox-group';import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';import { Stack } from '@slotted/react/stack';export default function CheckboxGroupDefaults() { return ( <CheckboxGroupDefaultsProvider orientation="horizontal"> <Stack gap={5}> <FieldGroup> <FieldGroupLegend>Fulfilment</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup defaultValue={['unfulfilled', 'partial']}> <Checkbox value="unfulfilled">Unfulfilled</Checkbox> <Checkbox value="partial">Partial</Checkbox> <Checkbox value="fulfilled">Fulfilled</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> <FieldGroup> <FieldGroupLegend>Sales channel</FieldGroupLegend> <FieldGroupContent> <CheckboxGroup defaultValue={['online']}> <Checkbox value="online">Online</Checkbox> <Checkbox value="retail">Retail</Checkbox> <Checkbox value="wholesale">Wholesale</Checkbox> </CheckboxGroup> </FieldGroupContent> </FieldGroup> </Stack> </CheckboxGroupDefaultsProvider> );}Accessibility
- The WAI-ARIA Authoring Practices have no pattern for a group of checkboxes: the Checkbox pattern covers one box, and grouping is a question of naming rather than of keyboard behavior. The sources are the ARIA group role and the Providing Accessible Names and Descriptions practice (w3.org/WAI/ARIA/apg/practices/names-and-descriptions/).
- Every box is its own tab stop, and Space toggles the box that has focus. The platform provides this whole keyboard model. It is what sets a checkbox group apart from a radio group, where the set is a single tab stop and the arrow keys move the choice.
- The role depends on the container. A field group is a fieldset with a legend, which is already a named group to assistive technology; a role="group" inside it, named by the same legend, would announce the set twice. Inside a field group the checkbox group renders no role, and the legend names the set. Inside a field, the field’s label names the group through aria-labelledby.
- The group writes aria-required, never the native required on a box. The native attribute on a checkbox demands that one particular box be checked, which is not what "choose at least one" means, and it turns on browser constraint validation.
- A checkbox has no native read-only state, so the checkbox group enforces one: every box refuses the change on the click that a pointer and Space share, aria-readonly announces it, and every tab stop and focus ring stays.
- The parent box is derived from the set rather than kept as a state of its own, so it always agrees with the boxes it controls. It reports aria-checked="mixed" through the platform’s indeterminate property, and pressing it replaces the selection instead of adding a value of its own.
API
CheckboxGroup
Props
valuereadonly string[]- The controlled selection. When present, the consumer owns the selection for the life of the component. When absent, the checkbox group owns it and starts from defaultValue.
defaultValuereadonly string[]Default[]- The selection an uncontrolled group starts from, read once.
allValuesreadonly string[]- Every value the checkbox group holds. A parent Checkbox needs it: the parent is checked when every value is selected and mixed when some are, which a selection alone cannot tell. It also keeps the selection in the order of this list rather than in the order the boxes were pressed.
variant'plain' | 'tile'Defaultplain- On CheckboxGroup, CheckboxGroupDefaultsProviderHow every choice is drawn, passed to every box. tile frames each choice and makes its whole surface the target. Arranging the tiles is left to Grid.
orientation'vertical' | 'horizontal'Defaultvertical- On CheckboxGroup, CheckboxGroupDefaultsProviderHow the choices flow. A horizontal set wraps instead of scrolling.
size'sm' | 'md' | 'lg'Defaultmd- On CheckboxGroup, CheckboxGroupDefaultsProviderThe type step, passed to every box. A field or field group around the checkbox group decides it first, then the provider.
labelPlacement'end' | 'start'Defaultend- On CheckboxGroup, CheckboxGroupDefaultsProviderWhich side of its label each box is on, set once for the set so the boxes line up.
namestring- The submission name every box shares when it sets none of its own. No name is generated: the platform does not group checkboxes by name the way it groups radios.
disabledbooleanDefault from the field, or false- Unset rather than false. Reaches every box as the native attribute, which takes the boxes out of the tab order and out of form submission.
readOnlybooleanDefault from the field, or false- Unset rather than false. Every box keeps its tab stop, its focus ring and its contrast, writes aria-readonly, and refuses every change.
invalidbooleanDefault from the field, or false- Unset rather than false. Writes aria-invalid on the group and on every box, and data-invalid on the group’s root and on every box’s root.
requiredbooleanDefault from the field, or false- Unset rather than false. Writes aria-required on the group, never the native required, which on a checkbox would demand that one particular box be checked.
showCountbooleanDefaultfalse- Inside a field, draws how many of allValues are selected at the end of the field’s label row, as 2/4, and adds that count to the group’s accessible description.
Events
| Name | Payload | When |
|---|---|---|
onValueChange | (value: readonly string[], request: ChangeRequest<readonly string[]>) | Before the selection moves. request.value is the new array and request.previous the array it replaces. request.reason is pointer or keyboard, recovered from the box’s click; request.source is the originating event; request.cancel() refuses the move before anything commits, including the platform’s own toggle. |
onCheckedChange, on each Checkbox | (checked: boolean, request: ChangeRequest<boolean>) | Before that one box moves, and before the checkbox group builds its own request. Cancelling it also stops the group’s request, so one option can refuse to be chosen without the rest of the set taking part. |
Composition
render(props: CheckboxGroupRootProps) => ReactNode- Replaces the root and hands it every attribute. The props are typed as HTMLAttributes<HTMLElement>, so a set of choices can render as a ul with each box as an li.
useCheckboxGroup()() => CheckboxGroupContextValue | undefined- On inside a CheckboxGroupReturns what a box reads: whether it is selected, its size, and how to request a change. It returns undefined outside a checkbox group, so one Checkbox serves both a single consent box and a member of a set.
CheckboxGroupDefaultsProvider{ size, variant, orientation, labelPlacement }- On a subtreeThe four decisions an application takes once for every checkbox group under it. A field or field group around a checkbox group decides size before the provider.
className / stylestring / CSSProperties- Reach the root. Every other native prop reaches it too, because the root is the only element the checkbox group renders.
rolestringDefault group, or none inside a field group- A role the consumer writes wins. Otherwise the role is group, withdrawn inside a field group, because a fieldset with a legend is already a named group and a second group named by the same legend announces the set twice.
aria-label / aria-labelledbystringDefault the field’s label, inside a field- Names the group. Inside a field, aria-labelledby points at the field’s label unless the consumer writes either attribute. Outside a field and a field group, write one of them, because a group needs a name.
Parts
| Part | Element | Required | Purpose |
|---|---|---|---|
root | div, or the element render returns | Yes | The only part. It arranges the boxes, holds the selection, and carries data-ui="checkbox-group", data-size, data-variant and data-orientation, and data-disabled, data-invalid and data-readonly. Its role is group; inside a field group the role is withdrawn, because the fieldset and its legend already name the set. Inside a field, aria-labelledby points at the field’s label. The boxes inside are Checkbox components with their own stylesheet, which the checkbox group’s stylesheet never selects into. |
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every checkbox group inside it.
Checkbox Group tokens
| Token | Default |
|---|---|
--slotted-checkbox-group-gap | var(--_gap) |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-control-gap-lg | comfortable 8px · compact 6px · dense 6px |
--slotted-control-gap-md | comfortable 8px · compact 6px · dense 4px |
--slotted-control-gap-sm | comfortable 6px · compact 4px · dense 4px |