PeakUI

Switch

On and off toggle control

Usage

import {Switch} from "@peakui/react";

Anatomy

<Switch>
  <Switch.Content>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    Label text
  </Switch.Content>
</Switch>

For grouping multiple switches, use the SwitchGroup component — a presentational wrapper; each switch keeps its own state.

Examples

Sizes

Disabled

Without Label

With Description

Controlled

Group

Group Horizontal

Customization

Tailwind CSS

<Switch className="w-full">
  <Switch.Content>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    Label text
  </Switch.Content>
</Switch>

Global CSS

.switch {
  gap: 0.75rem;
}

Styling Reference

CSS Classes

ClassDescription
.switchThe field wrapper element
.switch--sm, .switch--md, .switch--lgSize modifiers
.switch__contentThe clickable label wrapping control and text
.switch__controlThe track the thumb slides in
.switch__thumbThe sliding knob
.switch__iconThe optional icon container inside the track
.switch-groupThe group wrapper element
.switch-group--horizontal, .switch-group--verticalGroup orientation modifiers
.switch-group__itemsThe group's items container

Slots

SlotDescription
[data-slot="switch"]The field wrapper element
[data-slot="switch-content"]The clickable label element
[data-slot="switch-control"]The track element
[data-slot="switch-thumb"]The knob element
[data-slot="switch-icon"]The icon container element
[data-slot="switch-group"]The group wrapper element
[data-slot="switch-group-items"]The group's items container element

Interactive States

The component supports both CSS pseudo-classes and data attributes:

StateAttribute
Selected[data-selected="true"] / [aria-checked="true"]
Hovered[data-hovered="true"]
Pressed[data-pressed="true"]
Focused (visible)[data-focus-visible="true"]
Disabled[data-disabled="true"]
Invalid[data-invalid="true"]

API Reference

Switch.Root

PropTypeDefaultDescription
size"sm" | "md" | "lg""md"Track size
isSelectedboolean-Controlled selected state
defaultSelectedboolean-Uncontrolled initial selected state
isDisabledboolean-Disables the switch
isInvalidboolean-Marks the switch invalid
isRequiredboolean-Marks the switch required
validate(isSelected) => boolean | string-Validation function
onChange(isSelected) => void-Called when the state changes
renderDOMRenderFunction<'div', SwitchFieldRenderProps>-Replaces the rendered element
...restEvery React Aria SwitchField attribute is accepted

Switch.Content

PropTypeDefaultDescription
renderDOMRenderFunction<'label', SwitchButtonRenderProps>-Replaces the rendered label element
...restEvery React Aria SwitchButton attribute is accepted

Switch.Control

PropTypeDefaultDescription
renderDOMRenderFunction<'span', undefined>-Replaces the rendered element

Switch.Thumb

PropTypeDefaultDescription
renderDOMRenderFunction<'span', undefined>-Replaces the rendered element

Switch.Icon

PropTypeDefaultDescription
renderDOMRenderFunction<'span', undefined>-Replaces the rendered element

SwitchGroup.Root

PropTypeDefaultDescription
orientation"horizontal" | "vertical""vertical"Layout direction
renderDOMRenderFunction<'div', undefined>-Replaces the rendered element
...restEvery div attribute is accepted

Render Props

children and className of Switch.Root and Switch.Content accept a function receiving the respective React Aria render props.

On this page