- Frames
- 0
- Active effects
- 0
- Renderer
- none
checkbox.ts
import "@shader-ui/core/styles.css";
import { watchForm } from "@shader-ui/core";
const subscribe = () => new Promise<void>((resolve) => setTimeout(resolve, 1400));
document.body.insertAdjacentHTML(
"beforeend",
`<form id="newsletter">
<fieldset>
<legend>Topics (optional)</legend>
<label><input type="checkbox" name="topics" value="news"> Library news</label>
<label><input type="checkbox" name="topics" value="a11y"> Accessibility articles</label>
</fieldset>
<label><input type="checkbox" required> I accept the privacy policy</label>
<button type="submit">Subscribe</button>
</form>`,
);
watchForm(document.querySelector<HTMLFormElement>("#newsletter")!, {
guide: true,
onSubmit: (event) => {
event.preventDefault();
return subscribe();
},
});Newsletter.tsx
import { Button, Checkbox, Form } from "@shader-ui/react";
const subscribe = () => new Promise<void>((resolve) => setTimeout(resolve, 1400));
export function Newsletter() {
return (
<Form
guide
onSubmit={(e) => {
e.preventDefault();
return subscribe();
}}
>
<fieldset>
<legend>Topics (optional)</legend>
<label>
<Checkbox name="topics" value="news" /> Library news
</label>
<label>
<Checkbox name="topics" value="a11y" /> Accessibility articles
</label>
</fieldset>
<label>
<Checkbox required /> I accept the privacy policy
</label>
<Button type="submit">Subscribe</Button>
</Form>
);
}When to use it
- For an independent option (newsletter), or a required agreement with required (terms and conditions).
- Not for an exclusive choice: use radio buttons. Not for a setting that applies at once: use a switch (switch variant, coming soon).
Recommendations
- indeterminate for a partly ticked "select all" box.
- To match the ticked color to your brand, use --sui-color-checked, without green, coral or violet: they are reserved for meaning.
Accessibility
The library
- Native <input type="checkbox"> element: label click, space, indeterminate, screen readers.
- Ticked, the box is filled: a shape, not just a color, with 3:1 contrast.
- Click area of at least 24 px (44 px on mobile).
- High contrast: back to the system's native control.
Integration
- A tied label.
- A group of boxes in a <fieldset> with a <legend>. On error: aria-invalid on the <fieldset> and an error text, since a group has no native validation.
WCAG 2.2 criteria: Use of Color · Non-text Contrast · Target Size (Minimum) · Error Identification
Props
| Prop | Type | Purpose |
|---|---|---|
required | boolean | Required: ticked, it lights up green; unticked on submit, it pulses coral. |
aria-invalid | boolean | Error set by your validation. On a <fieldset>: coral line under the legend. |
effects, onEffectEnd, ref | | Same as the input. |
Spec
The normative spec for this component: Formulaire
Availability and resources
- Status
- available · open source
- Packages
- @shader-ui/react (React, Next.js)
@shader-ui/core (TypeScript, plain HTML) - Source code
- choice.ts (core) · Choice.tsx (React)