Skip to content

CheckboxGroup

Several yes/no options under one question. The person ticks as many as they want, or none.

import { CheckboxGroup, CheckboxGroupSkeleton } from '@sypnia/ui/react'

On this page
  1. Several of a few
  2. In a row
  3. States
  4. Skeleton
  5. When to use it
  6. For everyone
  7. Props

Several of a few

What a new API key is allowed to do. The hint counts what is ticked.

1 of 4 permissions granted. Give a key only what it needs.

Code
import { useState } from 'react';
import { CheckboxGroup, Field, type Option } from '@sypnia/ui/react';

const permissions: Option[] = [
  { value: 'read', label: 'Read rows', description: 'Queries and exports.' },
  { value: 'write', label: 'Write rows', description: 'Insert, update and delete.' },
  { value: 'schema', label: 'Change tables', description: 'Create, alter and drop tables and columns.' },
  { value: 'billing', label: 'See billing', description: 'Only the owner can grant this.', disabled: true },
];

export default function Basic() {
  const [granted, setGranted] = useState(['read']);
  return (
    <Field label="This key can" hint={`${granted.length} of 4 permissions granted. Give a key only what it needs.`}>
      <CheckboxGroup options={permissions} value={granted} onChange={setGranted} />
    </Field>
  );
}

In a row

Short labels can sit side by side; they wrap when there is no room.

Code
import { CheckboxGroup, Field } from '@sypnia/ui/react';

const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'].map((d) => ({ value: d.toLowerCase(), label: d }));

export default function Horizontal() {
  return (
    <Field label="Back up on">
      <CheckboxGroup orientation="horizontal" options={days} defaultValue={['mon', 'wed', 'fri']} />
    </Field>
  );
}

States

With an error that says how to fix it, and disabled.

Tick at least one channel, or nobody will hear about failures.

Alerts are paused while the project sleeps.

Code
import { CheckboxGroup, Field, FormGrid } from '@sypnia/ui/react';

const channels = [{ value: 'mail', label: 'Mail' }, { value: 'slack', label: 'Slack' }, { value: 'webhook', label: 'Webhook' }];

export default function States() {
  return (
    <FormGrid>
      <Field label="Send alerts by" error="Tick at least one channel, or nobody will hear about failures.">
        <CheckboxGroup options={channels} />
      </Field>
      <Field label="Send alerts by" hint="Alerts are paused while the project sleeps.">
        <CheckboxGroup disabled options={channels} defaultValue={['mail']} />
      </Field>
    </FormGrid>
  );
}

Skeleton

The real piece and its skeleton: the same box, so nothing moves when the data arrives.

Code
import type { CSSProperties } from 'react';
import { CheckboxGroup, CheckboxGroupSkeleton } from '@sypnia/ui/react';

// Options that come from the server. The skeleton has the same box as the real one, so nothing moves when the data arrives.
export default function Skeleton() {
  return (
    <div className="sy-grid" style={{ '--min': '16rem' } as CSSProperties}>
      <CheckboxGroup aria-label="Notify me about" options={[{ value: 'deploys', label: 'Deploys' }, { value: 'errors', label: 'Errors' }, { value: 'billing', label: 'Billing' }]} defaultValue={['errors']} />
      <CheckboxGroupSkeleton items={3} />
    </div>
  );
}

When to use it

Use it when

  • Up to six or so options that do not exclude each other, all worth seeing at once: what to be notified about, what a key may do.
  • Each option can carry a line that explains it.

Do not use it when

  • Only one can be chosen: RadioGroup.
  • There are many options: MultiSelect keeps the form short.
  • It is a single yes/no: Checkbox, or Switch when it applies at once.

For everyone

  • Each option is a native checkbox: Tab moves between them and Space ticks.
  • The group is announced with the question as its name (role="group"), so each option is heard in its context.
  • The whole line, text included, ticks the box. On touch screens each line is 44 px tall.
  • Inside a Field it takes the label, the hint and the error by itself; alone, give it an aria-label.

Props

CheckboxGroupTypeDefault
optionsrequired Option[]
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
className string
defaultValue string[]
disabled boolean
id

Inside a Field it comes from the Field.

string
invalid

Marks the value as wrong. Inside a `<Field error>` it is set for you.

boolean
name

The name of the value when it travels in a form.

string
onChange ((values: string[]) => void)
orientation

vertical: one under another (the default, easier to scan). horizontal: in a row that wraps, for short labels.

"horizontal" | "vertical"
value string[]
CheckboxGroupSkeletonTypeDefault
items number 3
orientation "horizontal" | "vertical"
  1. Several of a few
  2. In a row
  3. States
  4. Skeleton
  5. When to use it
  6. For everyone
  7. Props

    Search pages, sections and terms