Switch

A toggle that turns a single setting on or off, its track filled with the action colour while on and named by the label tied to it.

import { Label } from "@produkto/ui/Label";
import { Switch } from "@produkto/ui/Switch";

const Example = () => (
  <div className="flex items-center gap-2">
    <Switch defaultChecked id="switch-preview" />
    <Label htmlFor="switch-preview">Label</Label>
  </div>
);

export default Example;

Usage

import { Switch } from "@produkto/ui/Switch";
<Switch id="dark-mode" />

Examples

Sizes

size sets the track and its thumb: sm 9px, base 12px (the default) or lg 16px thumbs, each on a track two thumbs wide.

import { Label } from "@produkto/ui/Label";
import { Switch, type SwitchSize } from "@produkto/ui/Switch";

const variants: { size: SwitchSize; label: string }[] = [
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex items-center gap-2" key={variant.size}>
        <Switch id={`switch-size-${variant.size}`} size={variant.size} />
        <Label htmlFor={`switch-size-${variant.size}`}>{variant.label}</Label>
      </div>
    ))}
  </div>
);

export default Example;

Border radius

borderRadius sets the track's corners on the kit's one scale: none, xs 4px, sm 6px, base 8px, lg 12px, xl 16px, or full, the default, to round it, the thumb following one step inside; no corner rounds past half the track's height.

import { Label } from "@produkto/ui/Label";
import { Switch, type SwitchBorderRadius } from "@produkto/ui/Switch";

const variants: { borderRadius: SwitchBorderRadius; label: string }[] = [
  { borderRadius: "none", label: "None" },
  { borderRadius: "xs", label: "xs" },
  { borderRadius: "sm", label: "sm" },
  { borderRadius: "base", label: "base" },
  { borderRadius: "lg", label: "lg" },
  { borderRadius: "xl", label: "xl" },
  { borderRadius: "full", label: "full" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex items-center gap-2" key={variant.borderRadius}>
        <Switch
          id={`switch-radius-${variant.borderRadius}`}
          borderRadius={variant.borderRadius}
        />
        <Label htmlFor={`switch-radius-${variant.borderRadius}`}>
          {variant.label}
        </Label>
      </div>
    ))}
  </div>
);

export default Example;

Disabled

disabled dims the switch and its label and ignores clicks, beside one as it is by default.

import { Label } from "@produkto/ui/Label";
import { Switch } from "@produkto/ui/Switch";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex items-center gap-2">
      <Switch id="switch-enabled" />
      <Label htmlFor="switch-enabled">Label</Label>
    </div>
    <div className="flex items-center gap-2">
      <Switch disabled id="switch-disabled" />
      <Label htmlFor="switch-disabled">Label</Label>
    </div>
  </div>
);

export default Example;

Invalid

error writes what is wrong under the switch and its label, rings the track in the destructive colour and describes it to screen readers, beside one without an error.

import { Label } from "@produkto/ui/Label";
import { Switch } from "@produkto/ui/Switch";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex items-center gap-2">
      <Switch id="switch-valid" />
      <Label htmlFor="switch-valid">Label</Label>
    </div>
    <div className="flex flex-wrap items-center gap-2">
      <Switch error="Description" id="switch-error" />
      <Label htmlFor="switch-error">Label</Label>
    </div>
  </div>
);

export default Example;

API Reference

Switch

SwitchProps · components/Switch/Switch.types.ts
PropTypeDefault
sizeSwitchSize"sm" | "base" | "lg""base"
borderRadiusSwitchBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full""full"
errorReactNode-

It also takes the props of Radix Switch.Root.