Park UI

Switch

A form control for toggling between on and off states.

Installation

Usage

Shortcuts

The Switch component also provides a set of shortcuts for common use cases.

SwitchControl

The Switch.Control renders the Switch.Thumb within it by default.

This works:

This might be more concise, if you don't need to customize the thumb:

Examples

Sizes

Use the size prop to change the size of the switch.

Controlled

Use the checked and onCheckedChange prop to control the state of the switch.

Disabled

Use the disabled prop to disable the switch.

Invalid

Use the invalid prop to mark the switch as invalid.

Tooltip

Wrap the Switch.Root with a Tooltip to show a tooltip on hover or focus.

Track Indicator

Use the Switch.Indicator component to display different indicators based on the checked state.

Thumb Indicator

Use the Switch.ThumbIndicator component to add an icon to the switch thumb.

Closed Component

Here's how to setup the switch for a closed component composition.

Props

Root

PropDefaultType
variant'solid'
'solid'
size'md'
'xs' | 'sm' | 'md' | 'lg'
value\on\
string | number

The value of switch input. Useful for form submission.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

checked
boolean

The controlled checked state of the switch

disabled
boolean

Whether the switch is disabled.

ids
Partial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string }>

The ids of the elements in the switch. Useful for composition.

invalid
boolean

If `true`, the switch is marked as invalid.

label
string

Specifies the localized strings that identifies the accessibility elements and their states

name
string

The name of the input field in a switch (Useful for form submission).

onCheckedChange
(details: CheckedChangeDetails) => void

Function to call when the switch is clicked.

readOnly
boolean

Whether the switch is read-only

required
boolean

If `true`, the switch input is marked as required,