Trunk / /
Review 13
AK
Switch7 variants
Checked · off
Checked · on
State · empty
State · loading
State · data
State · error
Use the new regime
Not set yet
Use the new regime
Saving…
Use the new regime
Saving…
Use the new regime
Saves ₹12,400
Use the new regime
Saves ₹12,400
Use the new regime
Couldn’t save. Try again.
Use the new regime
Couldn’t save. Try again.
On click
100%
All states
K
ViewEdit
Get code · SwitchReact · CSS Modules · TypeScript
Props and events · from its properties and interactions
PropTypeDefaultFrom
labelstring"Use the new regime"Label · text
descriptionstring—Description · text
checkedbooleanfalseChecked · variant
state"empty" | "loading" | "data" | "error""data"State
onChange(checked: boolean) => void—Root · On click · Emit
onRetry() => void—Description · On click · Emit
Switch.tsxCopy
export interface SwitchProps {
  label: string;
  description?: string;
  checked?: boolean;
  state?: "empty" | "loading" | "data" | "error";
  onChange?: (checked: boolean) => void;
  onRetry?: () => void;
}

export function Switch({ label, description, checked = false, state = "data", onChange, onRetry }: SwitchProps) {
  const busy = state === "loading";
  return (
    <label className={styles.root}>
      <button role="switch" aria-checked={checked} disabled={busy}
        className={styles.track} data-on={checked}
        onClick={() => onChange?.(!checked)} />
      <span className={styles.label}>{label}</span>
      {state === "error"
        ? <button className={styles.error} onClick={onRetry}>Couldn’t save. Try again.</button>
        : description && <span className={styles.description}>{description}</span>}
    </label>
  );
}
trunk code Switch --framework react --styling css-modules --ts