Skip to Content
Cristal

Forms

Switch

Turns a setting on or off, with immediate effect.

<cr-switch>CrSwitch

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 provides aria-checked, with no attribute to keep in sync.
  • label is 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

PropertyTypeDefaultDescription
checkedbooleanfalseÉtat activé.
disabledbooleanfalseDésactive l'interrupteur.
label (obligatoire)string—Libellé affiché à côté de l'interrupteur.
namestring—Nom envoyé avec le formulaire.
valuestring'on'Valeur envoyée quand l'interrupteur est activé.

Events

Reactevent.detailDescription
onCrChangebooleanÉmis quand l'interrupteur change ; detail : l'état activé.

CSS parts

To style it from outside: cr-switch::part(name) { … }

PartDescription
controlLa case native (rôle switch), invisible, posée sur le rail.
labelLe libellé.
trackLe rail.

On this page