Forms
Switch
Turns a setting on or off, with immediate effect.
Example
A switch acts right away (turn on notifications, dark mode). For a choice that only applies when a form is submitted, prefer a checkbox.
While sliding, the knob stretches towards its destination then shrinks back, and the cross it carries turns into a check mark.
Notifications: on
'use client';
import { useState } from 'react';
import { CrBox, CrSwitch } from '@cristal-ui/react';
export default function SwitchBasic() {
const [notifications, setNotifications] = useState(true);
return (
<CrBox display="flex" direction="column" gap="lg">
<CrSwitch label="Notifications" name="notifications" checked={notifications} onCrChange={(event) => setNotifications(event.detail)} />
<CrSwitch label="Dark mode" />
<CrSwitch label="Sync (Pro accounts only)" disabled />
<p>Notifications: {notifications ? 'on' : 'off'}</p>
</CrBox>
);
}In a form
When on, the switch submits its value (on by default) under its name; when off, it submits nothing, like a checkbox.
If the user asked their system to reduce motion, the knob jumps straight to the other side without stretching, and the check mark replaces the cross without transition.
Accessibility
- Under the hood, a native checkbox with
role="switch": announced as "switch, on / off", toggled with Space. Its checked state providesaria-checked, with no attribute to keep in sync. labelis required and visible; clicking it toggles the switch.- The state does not rely on colour alone: the knob changes side and carries a check mark or a cross.
- The track stands out from the background with a contrast of at least 3:1, on and off; keyboard focus draws an outline around it.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
checked | boolean | false | État activé. |
disabled | boolean | false | Désactive l'interrupteur. |
label (obligatoire) | string | — | Libellé affiché à côté de l'interrupteur. |
name | string | — | Nom envoyé avec le formulaire. |
value | string | 'on' | Valeur envoyée quand l'interrupteur est activé. |
Events
| React | event.detail | Description |
|---|---|---|
onCrChange | boolean | Émis quand l'interrupteur change ; detail : l'état activé. |
CSS parts
To style it from outside: cr-switch::part(name) { … }
| Part | Description |
|---|---|
control | La case native (rôle switch), invisible, posée sur le rail. |
label | Le libellé. |
track | Le rail. |