Skip to content

Switch

A controlled switch for boolean settings.

Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Loading
Loading
Loading
<VStack gap={4}>
<Switch isSelected={false} label="Off" onChange={() => {}} />
<Switch isSelected label="On" onChange={() => {}} />
<Switch
isDisabled
isSelected={false}
label="Disabled"
onChange={() => {}}
/>
<Switch
isDisabled
isLoading
isSelected
label="Disabled loading"
onChange={() => {}}
/>
<Switch
isLoading
isSelected={false}
label="Loading off"
onChange={() => {}}
/>
<Switch isLoading isSelected label="Loading" onChange={() => {}} />
</VStack>
Loading
Loading
Loading
<VStack gap={4}>
{SIZES.map(size => (
<HStack align="center" gap={4} key={size}>
<Switch
isSelected={false}
label={`Off (${size})`}
onChange={() => {}}
size={size}
/>
<Switch
isSelected
label={`On (${size})`}
onChange={() => {}}
size={size}
/>
<Switch
isLoading
isSelected
label={`Loading (${size})`}
onChange={() => {}}
size={size}
/>
</HStack>
))}
</VStack>
Radio sm
Radio md
Radio lg
<VStack gap={6}>
{SIZES.map(size => (
<HStack align="center" gap={6} key={size}>
<Switch
isSelected
label={`Switch ${size}`}
onChange={() => {}}
size={size}
/>
<CheckboxInput
label={`Checkbox ${size}`}
onChange={() => {}}
size={size}
value
width="auto"
/>
<RadioGroup
isLabelHidden
label={`Radio ${size}`}
onChange={() => {}}
size={size}
value="on">
<RadioGroupItem label={`Radio ${size}`} value="on" />
</RadioGroup>
</HStack>
))}
</VStack>
Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
<VStack gap={4}>
<Switch
isRequired
isSelected
label="Required setting"
onChange={() => {}}
/>
<Switch
isOptional
isSelected={false}
label="Optional setting"
onChange={() => {}}
/>
</VStack>
This longer description explains when the setting applies and wraps onto multiple lines in constrained layouts.
<VStack gap={4}>
<Switch isSelected label="Compact setting" onChange={() => {}} />
<Switch
description="This longer description explains when the setting applies and wraps onto multiple lines in constrained layouts."
isSelected
label="Detailed setting"
onChange={() => {}}
style={{maxWidth: 360}}
/>
</VStack>
Receive product updates and account alerts.
Notification delivery may be delayed.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive product updates and account alerts.
This setting is active and healthy.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />
Receive product updates and account alerts.
function SwitchStory(args: React.ComponentProps<typeof Switch>) {
const [isSelected, setIsSelected] = useState(args.isSelected);
const onChange = args.onChange as SwitchProps['onChange'] | undefined;
return (
<Switch
{...args}
isSelected={isSelected}
onChange={(nextIsSelected, event) => {
setIsSelected(nextIsSelected);
onChange?.(nextIsSelected, event);
}}
/>
);
}
<SwitchStory {...args} />

A controlled switch for boolean settings.

When isOptional: false, isRequired: false

PropTypeDefaultDescription
classNamestringAdditional CSS class names applied to the field root.
data-testidstringTest ID applied to the checkbox input.
descriptionReactNodeSupporting text displayed below the label.
htmlNamestringHTML name attribute for native form submission.
isDisabledbooleanfalseWhether the switch is disabled.
isLabelHiddenbooleanfalseWhether to visually hide the label.
isLoadingbooleanfalseWhether the switch is loading.
isReadOnlybooleanfalseWhether the value is displayed without allowing focus or interaction.
isSelected*booleanWhether the switch is on.
label*stringSwitch label.
labelIconIconComponentContent rendered before the label.
labelPosition"start" | "end"'end'Which side of the switch the label appears on.
labelSpacing"default" | "spread"'default'Spacing behavior between label and switch.
labelTooltipReactNodeTooltip content shown next to the label.
onBlur(event: FocusEvent<HTMLInputElement>) => voidCalled when the switch loses focus.
onChange*(checked: boolean, event: ChangeEvent<HTMLInputElement>) => voidCalled when the checked state changes.
onFocus(event: FocusEvent<HTMLInputElement>) => voidCalled when the switch receives focus.
refRef<HTMLInputElement>Ref forwarded to the checkbox input.
size"sm" | "md" | "lg"'md'Visual size of the switch. Defaults to the ambient size when unset.
statusInputStatusValidation status displayed below the switch.
styleCSSPropertiesInline styles applied to the field root.
isOptionalfalse
isRequiredfalse

When isOptional: true, isRequired: false

PropTypeDefaultDescription
classNamestringAdditional CSS class names applied to the field root.
data-testidstringTest ID applied to the checkbox input.
descriptionReactNodeSupporting text displayed below the label.
htmlNamestringHTML name attribute for native form submission.
isDisabledbooleanfalseWhether the switch is disabled.
isLabelHiddenbooleanfalseWhether to visually hide the label.
isLoadingbooleanfalseWhether the switch is loading.
isReadOnlybooleanfalseWhether the value is displayed without allowing focus or interaction.
isSelected*booleanWhether the switch is on.
label*stringSwitch label.
labelIconIconComponentContent rendered before the label.
labelPosition"start" | "end"'end'Which side of the switch the label appears on.
labelSpacing"default" | "spread"'default'Spacing behavior between label and switch.
labelTooltipReactNodeTooltip content shown next to the label.
onBlur(event: FocusEvent<HTMLInputElement>) => voidCalled when the switch loses focus.
onChange*(checked: boolean, event: ChangeEvent<HTMLInputElement>) => voidCalled when the checked state changes.
onFocus(event: FocusEvent<HTMLInputElement>) => voidCalled when the switch receives focus.
refRef<HTMLInputElement>Ref forwarded to the checkbox input.
size"sm" | "md" | "lg"'md'Visual size of the switch. Defaults to the ambient size when unset.
statusInputStatusValidation status displayed below the switch.
styleCSSPropertiesInline styles applied to the field root.
isOptional*true
isRequiredfalse

When isOptional: false, isRequired: true

PropTypeDefaultDescription
classNamestringAdditional CSS class names applied to the field root.
data-testidstringTest ID applied to the checkbox input.
descriptionReactNodeSupporting text displayed below the label.
htmlNamestringHTML name attribute for native form submission.
isDisabledbooleanfalseWhether the switch is disabled.
isLabelHiddenbooleanfalseWhether to visually hide the label.
isLoadingbooleanfalseWhether the switch is loading.
isReadOnlybooleanfalseWhether the value is displayed without allowing focus or interaction.
isSelected*booleanWhether the switch is on.
label*stringSwitch label.
labelIconIconComponentContent rendered before the label.
labelPosition"start" | "end"'end'Which side of the switch the label appears on.
labelSpacing"default" | "spread"'default'Spacing behavior between label and switch.
labelTooltipReactNodeTooltip content shown next to the label.
onBlur(event: FocusEvent<HTMLInputElement>) => voidCalled when the switch loses focus.
onChange*(checked: boolean, event: ChangeEvent<HTMLInputElement>) => voidCalled when the checked state changes.
onFocus(event: FocusEvent<HTMLInputElement>) => voidCalled when the switch receives focus.
refRef<HTMLInputElement>Ref forwarded to the checkbox input.
size"sm" | "md" | "lg"'md'Visual size of the switch. Defaults to the ambient size when unset.
statusInputStatusValidation status displayed below the switch.
styleCSSPropertiesInline styles applied to the field root.
isOptionalfalse
isRequired*true