Checkbox
A box to tick: a yes or no that is saved with the rest of a form, or one of several options that can be chosen together.
import { Checkbox, CheckboxSkeleton } from '@sypnia/ui/react'
A yes or no
It changes what the button will do, and nothing happens until the button is pressed.
Code
import { useState } from 'react';
import { Actions, Button, Checkbox } from '@sypnia/ui/react';
// A yes or no that is saved with the form: nothing happens until «Invite».
export default function Single() {
const [notify, setNotify] = useState(true);
return (
<div className="sy-stack">
<Checkbox label="Send the invitation by email" description="Otherwise you get a link to share yourself." checked={notify} onChange={(e) => setNotify(e.target.checked)} />
<Actions align="start"><Button variant="primary">{notify ? 'Invite and send email' : 'Invite and copy link'}</Button></Actions>
</div>
);
} Several, with «all»
Tick some tables: the first box shows a dash while only some are chosen.
Code
import { useEffect, useRef, useState } from 'react';
import { Checkbox, Fieldset } from '@sypnia/ui/react';
const all = ['orders', 'order_items', 'customers', 'products'];
// Some chosen and some not: the box on top shows a dash.
export default function SelectAll() {
const [picked, setPicked] = useState(['orders', 'customers']);
const top = useRef<HTMLInputElement>(null);
useEffect(() => { if (top.current) top.current.indeterminate = picked.length > 0 && picked.length < all.length; }, [picked]);
const toggle = (t: string) => setPicked((p) => (p.includes(t) ? p.filter((x) => x !== t) : [...p, t]));
return (
<Fieldset legend="Tables to export">
<Checkbox ref={top} label={`All tables (${picked.length} of ${all.length})`} checked={picked.length === all.length} onChange={(e) => setPicked(e.target.checked ? all : [])} />
{all.map((t) => <Checkbox key={t} label={<span className="sy-machine">{t}</span>} checked={picked.includes(t)} onChange={() => toggle(t)} />)}
</Fieldset>
);
} States
Ticked, empty, and disabled in both positions. A disabled choice explains itself in the description.
Code
import { Checkbox } from '@sypnia/ui/react';
export default function States() {
return (
<div className="sy-stack">
<Checkbox label="Read rows" defaultChecked />
<Checkbox label="Change rows" />
<Checkbox label="Delete tables" description="Only owners can give this permission." disabled />
<Checkbox label="Sign in to the console" description="Every member has it." disabled defaultChecked />
</div>
);
} Skeleton
The real piece and its skeleton: the same box, so nothing moves when the data arrives.
Code
import { Checkbox, CheckboxSkeleton } from '@sypnia/ui/react';
// A setting whose value is still loading. 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-stack">
<Checkbox label="Send me a weekly summary" description="Every Monday, by email." defaultChecked />
<CheckboxSkeleton description />
</div>
);
} When to use it
Use it when
- A yes/no that takes effect when the form is saved.
- Several options where any number can be chosen: permissions, tables to export.
- Accepting something: terms, a consequence.
Do not use it when
- The change applies at once, with no Save: that is a Switch.
- Only one of the options can be chosen: that is Radio.
- The label would be a negative («Do not send email»): word it so that ticking means yes.
For everyone
- It is the native checkbox: Space ticks it, and pressing the label does too.
- The label is required; the description is read with it.
- A group of checkboxes goes in a Fieldset, so the question they answer is announced.
- The dash (some chosen, some not) is set from code with
indeterminate; screen readers say «mixed».
Props
| Checkbox | Type | Default |
|---|---|---|
labelrequired | ReactNode | |
description | ReactNode |
| CheckboxSkeleton | Type | Default |
|---|---|---|
description | boolean | |
radio | boolean |