Synertech Web/ui v0.57.5 Site Storybook

Documentation / Forms

Switch

Client@synertech/uiOpen in Storybook

The switch: a setting that takes effect as it is turned — notifications, a dark theme, a feature. A choice confirmed afterwards with a button stays a Checkbox.

It is a native <input type="checkbox" role="switch"> under a drawn track, as Checkbox is under its box. Controlled (checked + onCheckedChange) or not (defaultChecked): uncontrolled, its state stays in the input and never in a React state, so a form reset puts it back where it started. With name, it is the form’s field: value (on by default) is sent while it is on, nothing while it is off. Label, description and error message are linked through aria-describedby, in three sizes. Client component ("use client").

Examples

Basic usage

Uncontrolled: defaultChecked sets the initial state, description explains the setting.

Open in a tab
import { Switch } from "@synertech/ui";

export function Basic() {
  return (
    <Switch
      label="Notifications par courriel"
      description="Un message à chaque facture payée."
      defaultChecked
    />
  );
}

Immediate effect

Controlled: onCheckedChange hands back the boolean, which the application applies at once.

Open in a tab
import { useState } from "react";
import { Switch } from "@synertech/ui";

export function Controlled() {
  const [compact, setCompact] = useState(false);

  return (
    <div className="flex flex-col gap-4">
      <Switch label="Affichage compact" checked={compact} onCheckedChange={setCompact} />
      <ul
        className={
          compact
            ? "flex flex-col gap-1 text-sm text-gray-900 dark:text-gray-100"
            : "flex flex-col gap-3 text-base text-gray-900 dark:text-gray-100"
        }
      >
        <li>Facture n° 1042 — payée</li>
        <li>Facture n° 1043 — en attente</li>
        <li>Facture n° 1044 — brouillon</li>
      </ul>
    </div>
  );
}

Settings page

labelPosition="left" puts the switch at the end of the row; className="w-full" stretches it.

Open in a tab
import { useState } from "react";
import { Switch } from "@synertech/ui";

export function SettingsList() {
  const [settings, setSettings] = useState({ emails: true, sms: false, digest: true });
  const toggle = (key: keyof typeof settings) => (checked: boolean) =>
    setSettings((current) => ({ ...current, [key]: checked }));

  return (
    <div className="flex w-full max-w-md flex-col divide-y divide-gray-200 rounded-lg border border-gray-200 bg-white text-gray-900 dark:divide-gray-700 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-100">
      <Switch
        className="w-full p-4"
        labelPosition="left"
        label="Courriels"
        description="Une facture payée, un message."
        checked={settings.emails}
        onCheckedChange={toggle("emails")}
      />
      <Switch
        className="w-full p-4"
        labelPosition="left"
        label="SMS"
        description="Les retards de paiement seulement."
        checked={settings.sms}
        onCheckedChange={toggle("sms")}
      />
      <Switch
        className="w-full p-4"
        labelPosition="left"
        label="Résumé hebdomadaire"
        description="Le lundi matin."
        checked={settings.digest}
        onCheckedChange={toggle("digest")}
      />
    </div>
  );
}

Sizes and states

size sets the track and the texts; disabled, readOnly and error read as on the other fields.

Open in a tab
import { Switch } from "@synertech/ui";

export function SizesAndStates() {
  return (
    <div className="flex flex-col gap-4">
      <Switch size="sm" label="Petit" defaultChecked />
      <Switch size="md" label="Moyen, par défaut" defaultChecked />
      <Switch size="lg" label="Grand" defaultChecked />
      <Switch label="Désactivé" disabled defaultChecked />
      <Switch label="Lecture seule" readOnly defaultChecked />
      <Switch label="Sauvegarde automatique" error="Le réglage n’a pas pu être enregistré." />
    </div>
  );
}

In a form

With name, value is sent while the switch is on; “Reset” puts it back where it started.

Open in a tab
import { useState } from "react";
import { Button, Switch } from "@synertech/ui";

export function InForm() {
  const [sent, setSent] = useState<string[] | null>(null);

  return (
    <form
      className="flex flex-col gap-4"
      onSubmit={(event) => {
        event.preventDefault();
        setSent(new FormData(event.currentTarget).getAll("channels").map(String));
      }}
      onReset={() => setSent(null)}
    >
      <Switch name="channels" value="email" label="Courriel" defaultChecked />
      <Switch name="channels" value="sms" label="SMS" />
      <Switch name="terms" label="J’accepte les conditions" required />
      <div className="flex gap-2">
        <Button type="submit">Enregistrer</Button>
        <Button
          type="reset"
          bgColor="bg-gray-100 hover:bg-gray-200 dark:bg-gray-700 dark:hover:bg-gray-600"
          textColor="text-gray-900 dark:text-gray-100"
        >
          Réinitialiser
        </Button>
      </div>
      {sent && (
        <p className="text-sm text-gray-700 dark:text-gray-300">
          Canaux envoyés : {sent.length > 0 ? sent.join(", ") : "aucun"}
        </p>
      )}
    </form>
  );
}

API reference

Components

Switch

ComponentClient

An on/off setting that takes effect as it is turned: notifications, a dark theme, a feature. A choice the reader confirms with a button is a Checkbox.

A native <input type="checkbox" role="switch"> under a drawn track, as Checkbox is under its box. Uncontrolled, its state stays in the input and never in a React state: the track is drawn by :checked, so a form reset — which React 19 does after every successful action — puts it back, and what is on screen is what the form sends. With name, it is the form's field: value is sent while it is on, nothing while it is off.

The ref is the input, where a form library sends the focus.

import { Switch } from "@synertech/ui";

Props — SwitchProps

PropTypeDefaultDescription
checked boolean —

On or off, held by the application, which changes it in onCheckedChange. Left out, the switch is uncontrolled: the input holds its state, from defaultChecked.

className string —

Classes of the root, which holds the row and the message.

defaultChecked boolean —

The state at mount, uncontrolled. The input holds it afterwards, never a React state: a form reset puts it back.

description ReactNode —

Written under the label, and describes the switch.

disabled boolean false

Neither turned nor submitted, and out of the Tab order.

error boolean | string —

true marks the switch invalid; a message also does, and is written under it. The switch is then aria-invalid and described by the message. false and "" are no error.

label ReactNode —

Names the switch. Without one, give it an aria-label or an aria-labelledby.

labelPosition "left" | "right" "right"

Side of the label. left puts the switch at the end of the row: with className="w-full", the row of a settings page.

name string —

Name in a form: the switch submits value under it while it is on.

onCheckedChange (checked: boolean) => void —

Called with the new state when the reader turns the switch. The native onChange, which a form library hands over, is called before it with the event.

readOnly boolean false

Shown, reached and submitted, but not turned: a press or Space changes nothing.

required boolean false

A form is not sent while the switch is off; an asterisk follows the label.

size "sm" | "md" | "lg" "md"

Size of the switch and of its texts.

value string —

What the form receives under name while the switch is on, on when left out. Nothing is sent while it is off, as for a checkbox.

+ 298 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).