// preview in
saved for every page

Switch

Turns a setting on or off, with the effect applied right away. For choices that wait for a Save button, use a Checkbox.

Built on Base UIPattern WAI-ARIA SwitchClient componentSource ↗
minimal · light · #3fa34d
defaultChecked
disabled

Installation

Set up Camaleo once (Installation), then import what you need. Wrap the switch in a Label that names the setting.

import { Switch, Label } from "@camaleo/react";

Anatomy

A track with a thumb that slides to the end when it is on. Both parts have a data-slot you can target from CSS.

thumbAuto-save drafts
[data-slot="switch"][data-slot="switch-thumb"][data-slot="label"] text

Examples

Every example is a real file you can copy. They all wear the skin, mode and brand from the bar above.

minimal · light · #3fa34d

Auto-reply is off.

Applies right away

Click it. A switch takes effect the moment it moves, so confirm what changed in a status line instead of a Save button.

Helps us find crashes. We never collect what you type.

With a description

Put what the setting does below the label and link it with aria-describedby, so the name stays short.

Email me when

A list of settings

Related switches go in a Fieldset with a legend that reads as the start of each label.

Only your team can see your profile.

Saving to the server

Click it. When the change takes a moment, keep the switch focused with readOnly and announce "Saving…" and the result.

Dependent settings

Turn the first one off. Child settings are disabled, not hidden, so people still see what they would get back.

Available on the Business plan.

Disabled, with a reason

Say why it can't be turned on and link the reason with aria-describedby.

In context

Switches belong to settings screens. These are whole pieces of UI you can copy, in the skin you picked.

Notifications

Sent to the store app on your phone.

As soon as a customer pays.

When a product drops below 5 units.

Once a day, at 9 AM.

Notification settingsCard · Switch · Separator

Labs

Try features before they ship. Changes apply right away.

Feature previewsCard · Switch · Badge

Business

$32 per seat / month, billed yearly

Yearly billingCard · Switch · Badge · Button

Guidelines

DoA switch for a change that applies now.

Dark mode turns on as soon as it moves.

Don’tA switch that waits for Save.

It looks done but isn't. In a form you submit, use a Checkbox.

DoName the setting.

The switch itself shows on or off; screen readers announce it from aria-checked.

Don’tA label that says the state.

"Enabled" changes on every click and never says what is enabled.

Props

PropTypeDefaultDescription
checkedboolean—Whether the switch is currently active. To render an uncontrolled switch, use the defaultChecked prop instead.
defaultCheckedbooleanfalseWhether the switch is initially active. To render a controlled switch, use the checked prop instead.
disabledbooleanfalseWhether the component should ignore user interaction.
namestring—Identifies the field when a form is submitted.
onCheckedChange(checked: boolean, details) => void—Event handler called when the switch is activated or deactivated.
readOnlybooleanfalseWhether the user should be unable to activate or deactivate the switch.
requiredbooleanfalseWhether the user must activate the switch before submitting a form.
valuestring—The value submitted with the form when the switch is on. By default, switch submits the "on" value, matching native checkbox behavior.

Native attributes pass through to the track, a <span role="switch"> backed by a hidden input for forms.

Styling hooks

Restyle it without forking: target the attributes, or set the tokens on a parent, a skin or the switch itself.

Attributes

data-slot"switch" · "switch-thumb"
data-checkedpresent when on
data-uncheckedpresent when off
data-disabledpresent when disabled
data-readonlypresent when read-only

Tokens it reads

Color

--cml-accent--cml-border-control--cml-field-bg--cml-field-shadow--cml-focus-offset--cml-focus-ring--cml-focus-width

Shape & size

--cml-font-size-field--cml-gap--cml-radius-pill

Type & motion

--cml-duration-base--cml-ease-standard--cml-font-body

This component only

--cml-check-on-image--cml-corner-shape--cml-disabled-opacity--cml-switch-border--cml-switch-height--cml-switch-knob--cml-switch-knob-bg--cml-switch-knob-on--cml-switch-knob-on-bg--cml-switch-knob-on-shadow--cml-switch-knob-shadow--cml-switch-on--cml-switch-on-shadow--cml-switch-pad--cml-switch-track--cml-switch-travel--cml-switch-width

Accessibility

SpaceTurns the focused switch on or off.
EnterTurns the focused switch on or off.
TabMoves to the next switch; each one is its own tab stop.
Was this page helpful? · Edit this page on GitHub