Controls / 2 of 66

Controls / Switch

Switch

Material switch

React + CSS Modules

Controlled on/off state with native switch semantics, keyboard activation and a thumb that catches the light.

Warm lighting is off

Click, tap, or focus and press Space.

Real component width, contained in this page. This is not a device emulator.

Use it in your app Setup, usage and what you copy
  1. Save both source files in the same folder. Keep the CSS Module filename unchanged.
  2. Import the named component shown below. In Next.js, keep interactive parent state in a client component.
  3. Pass your own state and callbacks. No package install command or backend is required by these previews.
Start with light and dark tokens

Optional base colors for a wrapper with className="thinker-theme". Set data-theme="light" for light mode. Material-specific colors also have fallbacks in the component CSS. This snippet does not add a theme switch or save a preference.

/* Add to your app stylesheet. Switch data-theme on a wrapper. */
.thinker-theme {
  color-scheme: dark;
  --ui-bg: #191919;
  --ui-text: #f4f1e9;
  --ui-muted: #aaa49a;
  --ui-control: #30302e;
  --ui-well: #222221;
  --ui-focus: #e8d9b5;
  --ui-error: #efb1a3;
  --ui-material-top: #646462;
  --ui-material-bottom: #242423;
  --ui-material-ink: #eee8dc;
  --ui-material-muted: #b5b0a4;
  --ui-material-active: #9b998f88;
}
.thinker-theme[data-theme="light"] {
  color-scheme: light;
  --ui-bg: #eeeae2;
  --ui-text: #292820;
  --ui-muted: #59554b;
  --ui-control: #d9d4c9;
  --ui-well: #e1dcd0;
  --ui-focus: #675b42;
  --ui-error: #a43f31;
  --ui-material-top: #e2ddd0;
  --ui-material-bottom: #b4ad9d;
  --ui-material-ink: #333127;
  --ui-material-muted: #625b4d;
  --ui-material-active: #fff9eb99;
}

Usage.

<MaterialSwitch checked={enabled} onChange={setEnabled} label="Warm lighting" />

Controlled values belong to your app. The preview uses local state and does not save or send data.

What you copy.

Save MaterialSwitch.tsx and switch.module.css together. Requires React, TypeScript and CSS Modules. Shared source modules export related controls, so keep the named export you need. Theme tokens use CSS variables with standalone fallbacks.

Tested in Chrome with keyboard, touch-sized layouts and reduced motion. Broader browser and assistive-technology testing is still ahead. Not an npm release.

MaterialSwitch.tsxReact · TypeScript
"use client";
import { useId } from "react";
import styles from "./switch.module.css";

export function MaterialSwitch({
  checked,
  onChange,
  label = "Material mode",
  description,
}: {
  checked: boolean;
  onChange: (checked: boolean) => void;
  label?: string;
  description?: string;
}) {
  const descriptionId = useId();
  return (
    <span className={styles.control}>
      <button
        type="button"
        role="switch"
        aria-checked={checked}
        aria-label={label}
        aria-describedby={description ? descriptionId : undefined}
        className={styles.switch}
        onClick={() => onChange(!checked)}
      >
        <span className={styles.thumb} />
      </button>
      {description && (
        <span id={descriptionId} className={styles.description}>
          {description}
        </span>
      )}
    </span>
  );
}

Props reference MaterialSwitch · 4 props

From the component TypeScript definition. Required props need a value; callbacks connect it to your app.

MaterialSwitch props
PropTypeRequired
checkedbooleanYes
onChange(checked: boolean) => voidYes
labelstringNo
descriptionstringNo

Item types and defaults are defined in MaterialSwitch.tsx. Shared modules may export more than this component.

Thinker UI · In progress, built in the open.