Back to Shark UI

Switch

docs/components/switch

latest7.0 KB
Original Source

Sections

Components

Utilities

Forms

Hooks

Switch

Copy Markdown

A switch for toggling a binary state.

DocsAPI

PreviewCode

Airplane mode

Installation#

CLIManual

pnpmbunnpmyarn

pnpm dlx shadcn@latest add @shark/switch

Usage#

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

Controlled#

Use the checked and onCheckedChange props to control the switch state programmatically.

PreviewCode

Enable notifications

States#

Invalid#

Use the invalid prop on the Field or in the Switch component to mark the switch as invalid.

PreviewCode

Accept terms and conditions

Examples#

Disabled#

Use the disabled prop to disable the switch.

PreviewCode

Marketing emails

With Description#

Use FieldDescription to add a description to the switch.

PreviewCode

Share across devices

Focus is shared across devices, and turns off when you leave the app.

Card Style#

Use a card-style layout with the switch for a more prominent display.

PreviewCode

Enable notifications

You can enable or disable notifications at any time.

Form Integration#

Integrate the switch into a form with proper validation and error handling.

PreviewCode

Terms and conditions

Please read the terms and conditions carefully.

I agree to the terms

SubmitReset

Customizing Size#

The switch size is controlled by the [--size-*] CSS variable.

PreviewCode

API Reference#

Switch#

Toggle switch for on/off state. Use with a form control.

PropTypeDefault
checkedboolean-
defaultCheckedbooleanfalse
disabledbooleanfalse
invalidbooleanfalse
namestring-
value`stringnumber`
onCheckedChange(details: CheckedChangeDetails) => void-
classNamestring-
AttributeDefault
--size--spacing(4)

For a complete list of props, see the Ark UI documentation.

[

Previous page

Steps ](/docs/components/steps)[

Next page

Table ](/docs/components/table)

On This Page

InstallationUsageControlledStatesInvalidExamplesDisabledWith DescriptionCard StyleForm IntegrationCustomizing SizeAPI ReferenceSwitch