# Preference switch

A compact setting that clearly shows whether it is on or off.

## Usage

Use for a setting that takes effect immediately. Keep checked in parent state and persist it in your application when needed.

Save the JSX and CSS Module in the same folder in a React project with CSS Modules support. Controlled components need parent state and change handlers.

## JSX

```jsx
"use client";
import styles from "./PreferenceSwitch.module.css";
export default function PreferenceSwitch({
  label = "Email updates",
  checked = false,
  onChange,
}) {
  return (
    <button
      type="button"
      role="switch"
      aria-checked={checked}
      className={styles.switch}
      onClick={() => onChange?.(!checked)}
    >
      <span>{label}</span>
      <span className={styles.track} aria-hidden="true">
        <span />
      </span>
    </button>
  );
}

```

## CSS

```css
.switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 28px;
  width: 100%;
  min-height: var(--tdp-control-height, 48px);
  padding: 8px 0;
  background: transparent;
  color: var(--tdp-text, #302e2b);
  border: 0;
  border-radius: var(--tdp-radius, 8px);
  font: 500 15px var(--tdp-font, "DM Sans", system-ui, sans-serif);
  cursor: pointer;
}
.track {
  width: 46px;
  height: 28px;
  background: #a3a3a3;
  border-radius: 30px;
  padding: 4px;
  flex-shrink: 0;
  transition: background 0.2s;
}
.track > span {
  display: block;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 1px 3px #0003;
  transition: transform 0.2s;
}
.switch[aria-checked="true"] .track {
  background: var(--tdp-primary, #1f3934);
}
.switch[aria-checked="true"] .track > span {
  transform: translateX(18px);
}
.switch:focus-visible,
.switch :where(button, input, summary):focus-visible {
  outline: 3px solid var(--tdp-focus, #7851a9);
  outline-offset: 4px;
}
@media (prefers-reduced-motion: reduce) {
  .switch,
  .switch :where(button, input, summary, span) {
    transition: none !important;
    animation: none !important;
  }
}

```

## Props

- label (string): Visible setting name.
- checked (boolean): Current state, owned by the parent.
- onChange (function): Receives the next boolean value.

## Accessibility

The switch exposes aria-checked and a visible label. Native button behavior supports Enter and Space; the thumb position conveys state without relying on color.

Check accessibility again after customizing the component.

[Browse all components](/resources/components/)

Canonical: https://designproject.io/resources/components/preference-switch/

## Keep learning

- [How to create a UI color palette, step by step](https://designproject.io/resources/guides/create-ui-color-palette/)
- [Product design glossary: 44 terms explained](https://designproject.io/resources/glossary/)
- [How to choose font pairings for a product interface](https://designproject.io/resources/guides/choose-font-pairings/)
