Menu

Components

Switch

A control that turns a setting on or off, and takes effect right away. Built on react-aria-components.

Introduction

A switch flips one setting between on and off, and the change is applied at once, the way a light switch works: dark mode, Wi-Fi, notifications. There is no Save step.

  • Keyboard support (Space toggles) and screen reader support come from react-aria-components. It is announced as a switch, not a checkbox.
  • A description under the label, and the label before or after the switch.
  • Works in a form: give it a name and value. Only a switch that is on is submitted.

For a choice that only counts once a form is submitted (accepting terms, picking several items), use Checkboxinstead.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx

# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx

# npm
npm install react-aria-components tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/switch-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Switch. The track, the thumb and the text read the state of the
// switch (selected, focused, pressed...) from data attributes on the root,
// which react-aria sets, through the `group/switch` name.
export const switchStyles = tv({
  slots: {
    root: [
      "group/switch relative flex w-fit cursor-pointer items-start gap-3",
      "data-disabled:cursor-not-allowed data-disabled:opacity-50",
      "data-readonly:cursor-default",
    ],
    track: [
      "flex shrink-0 items-center rounded-full bg-fg/20 p-0.5 transition-colors",
      "group-data-hovered/switch:bg-fg/30",
      "group-data-selected/switch:bg-primary group-data-selected/switch:group-data-hovered/switch:bg-primary/90",
      "group-data-focus-visible/switch:ring-2 group-data-focus-visible/switch:ring-ring group-data-focus-visible/switch:ring-offset-2 group-data-focus-visible/switch:ring-offset-bg",
    ],
    thumb:
      "rounded-full bg-bg shadow-sm transition-transform group-data-selected/switch:group-data-pressed/switch:scale-x-110",
    label: "font-medium leading-snug",
    description: "text-muted-fg leading-snug",
  },
  variants: {
    size: {
      sm: {
        root: "text-sm",
        track: "mt-px h-4 w-7",
        thumb: "size-3 group-data-selected/switch:translate-x-3",
        description: "text-xs",
      },
      md: {
        root: "text-sm",
        track: "h-5 w-9",
        thumb: "size-4 group-data-selected/switch:translate-x-4",
        description: "text-sm",
      },
      lg: {
        root: "text-base",
        track: "h-6 w-11",
        thumb: "size-5 group-data-selected/switch:translate-x-5",
        description: "text-sm",
      },
    },
    // Put the text before the switch instead of after it.
    labelPosition: {
      end: {},
      start: { root: "flex-row-reverse" },
    },
  },
  defaultVariants: { size: "md", labelPosition: "end" },
});

2. Add the Switch file

src/components/ui/switch/switch.tsxShow code
"use client";

import { type ReactNode, useId } from "react";
import {
  Switch as AriaSwitch,
  type SwitchProps as AriaSwitchProps,
} from "react-aria-components";
import { cn } from "@/lib/cn";
import { switchStyles } from "@/types/switch-styles";

export interface SwitchProps
  extends Omit<AriaSwitchProps, "children" | "className"> {
  /** The text next to the switch. */
  children?: ReactNode;
  /** A smaller line under the label. */
  description?: ReactNode;
  size?: "sm" | "md" | "lg";
  /** Put the text after the switch (default) or before it. */
  labelPosition?: "end" | "start";
  className?: string;
}

/**
 * A control that turns a setting on or off, and takes effect right away, like
 * a light switch. Built on react-aria's Switch, so keyboard support (Space
 * toggles), screen reader support (announced as a switch) and form submission
 * (`name`, `value`) come from there. Give it an accessible name: text in
 * `children`, or `aria-label`.
 *
 * For a choice that is only applied when a form is submitted, a `Checkbox` is
 * usually the better fit.
 *
 * @example
 * <Switch defaultSelected description="Get a message when something changes.">
 *   Notifications
 * </Switch>
 */
export function Switch({
  children,
  description,
  size,
  labelPosition,
  className,
  ...props
}: SwitchProps) {
  const styles = switchStyles({ size, labelPosition });
  const descriptionId = useId();

  return (
    <AriaSwitch
      aria-describedby={description ? descriptionId : undefined}
      {...props}
      className={cn(styles.root(), className)}
    >
      <span aria-hidden="true" className={styles.track()}>
        <span className={styles.thumb()} />
      </span>
      {children || description ? (
        <span className="flex min-w-0 flex-col gap-0.5">
          {children ? <span className={styles.label()}>{children}</span> : null}
          {description ? (
            <span id={descriptionId} className={styles.description()}>
              {description}
            </span>
          ) : null}
        </span>
      ) : null}
    </AriaSwitch>
  );
}

3. Use it

import { Switch } from "@/components/ui/switch/switch";

Anatomy

A switch is a track with a thumb, and a label.

<Switch defaultSelected description="Get a message when something changes.">
  Notifications
</Switch>
PartRendered asNotes
TrackspanThe pill. Filled with the primary color when the switch is on.
ThumbspanThe round knob that slides from one end of the track to the other.
LabelspanThe text next to the switch. Clicking it toggles the switch.
DescriptionspanA smaller line under the label.

A switch without text needs an aria-label so a screen reader can name it.

Examples

Live previews with code you can copy.

Usage

Description

A smaller line under the label.

Label position

Put the text after the switch (the default) or before it.

Settings list

A row per setting, with the label at the left and the switch at the right. Set labelPosition to start and stretch the switch with w-full justify-between.

Without a label

Name a switch that has no visible text with aria-label.

Uncontrolled

Let the switch keep its own state. Start with defaultSelected.

Controlled

Keep the state in your own with isSelected and onChange, so you can read it or flip it from outside.

isSelected: true

In a form

With a name and value, a switch that is on is submitted as name=value. A switch that is off sends nothing.

Not submitted yet.

Sizes

Disabled

A disabled switch cannot be changed or focused.

Read only

A read-only switch cannot be changed, but it can be focused, and it is still submitted with a form.

Component API

Switch takes the props below, plus everything the react-aria Switch accepts.

PropTypeDefault
childrenReactNode-
descriptionReactNode-
isSelectedboolean-
defaultSelectedbooleanfalse
onChange(isSelected: boolean) => void-
labelPosition"end" | "start""end"
size"sm" | "md" | "lg""md"
namestring-
valuestring-
autoFocusbooleanfalse
isDisabledbooleanfalse
isReadOnlybooleanfalse
aria-labelstring- (when there is no text)