Switch

Loading...

Installation

Add the component with the CLI.

npx shadcn@latest add @intentui/switch

Composed components

When you install this component via the CLI, it automatically loads all composed components, so you don’t need to add them individually.

Manual installation

Install the dependencies, then copy the component source into your project.

npm install react-aria-components cn

Anatomy

import { Description } from "@/components/ui/field"
import { SwitchField, Switch } from "@/components/ui/switch"
<>
  <SwitchField value="profileVisible">
    <Switch>Public profile</Switch>
    <Description>Allow others to see your profile.</Description>
  </SwitchField>
  <SwitchField value="searchEngineIndexing">
    <Switch>Search engine indexing</Switch>
    <Description>
      Allow search engines to index your profile.
    </Description>
  </SwitchField>
</>

Examples

Description

You can add a description to the switch by passing the description prop or using the Description component.

Loading...

Controlled

You can manage the switch's state by setting the isSelected prop.

Loading...

Uncontrolled

You can also use the uncontrolled version of the switch.

Loading...

Disabled

A disabled switch cannot be toggled and is non-interactive.

Loading...

Custom layout

Place the switch control on the left and its label on the right.

Loading...

Component API

SwitchField

SwitchField extends the React Aria Components <SwitchField> component.

classNamestring

Switch

Switch extends the React Aria Components <SwitchButton> component.

classNamestring

See the React Aria for the full API reference.

Design