Skip to main content

Switch

The switch component creates a toggle control that allows merchants to turn an option on or off. Use switches for settings that take effect immediately, such as enabling notifications or toggling features.

Switches provide clear visual feedback about the current state and are ideal for binary choices that don't require confirmation.


Configure the following properties on the switch component.

Anchor to accessibilityLabel
accessibilityLabel
string

A label used for users using assistive technologies like screen readers. When set, any children or label supplied will not be announced. This can also be used to display a control without a visual label, while still providing context to users using screen readers.

Anchor to checked
checked
boolean
Default: false

Whether the control is active.

Anchor to defaultChecked
defaultChecked
boolean
Default: false

Whether the control is active by default.

Anchor to details
details
string

Additional text to provide context or guidance for the field. This text is displayed along with the field and its label to offer more information or instructions to the user.

This will also be exposed to screen reader users.

Anchor to disabled
disabled
boolean
Default: false

Disables the control, disallowing any interaction.

Anchor to error
error
string

Indicate an error to the user. The field will be given a specific stylistic treatment to communicate problems that have to be resolved immediately.

Anchor to label
label
string

Visual content to use as the control label.

Anchor to labelAccessibilityVisibility
labelAccessibilityVisibility
'visible' | 'exclusive'
Default: 'visible'

Changes the visibility of the component's label.

  • visible: the label is visible to all users.
  • exclusive: the label is visually hidden but remains in the accessibility tree.
Anchor to value
value
string

The value used in form data when the control is checked.

The switch component provides event callbacks for handling user interactions. Learn more about handling events.

Anchor to change
change
(event: <"s-switch">) => void
Anchor to input
input
(event: <"s-switch">) => void

Anchor to Create a toggle switchCreate a toggle switch

Toggle settings on or off using a switch component.

Create a toggle switch

Toggle settings on or off using a switch component.

Create a toggle switch

<s-switch />
<s-switch defaultChecked />

Anchor to Handle switch change eventsHandle switch change events

Subscribe to the onChange event to respond when merchants toggle the switch. This example shows how to handle state changes and capture the checked value in real time, enabling dynamic behavior based on the switch state.

Preview

Handle switch change events

<s-switch
label="Enable email notifications"
defaultChecked
onChange="(event) => console.log('Switch toggled:', event.currentTarget.checked)"
/>

  • Provide descriptive labels: Use the label prop to clearly describe what the switch controls.
  • Use accessibility labels when needed: If the visual label doesn't provide enough context, use the accessibilityLabel prop to provide additional information for screen readers.
  • Indicate disabled state: When a switch is disabled, ensure the reason is communicated to users through surrounding context or helper text.

Was this page helpful?