Skip to main content

Switch

The switch component provides a clear way for users to toggle options or settings on and off. Use switch for binary controls that take effect immediately, like enabling features, activating settings, or controlling visibility.

Switches provide instant visual feedback and are ideal for settings that don't require a save action to apply changes. Switch doesn't support error or required. For selections that require validation or explicit submission, use checkbox instead.

Support
Targets (29)

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 currently checked.

Anchor to command
command
'--auto' | '--show' | '--hide' | '--toggle'
Default: '--auto'

Sets the action the commandFor target should take when this component is activated. Available options:

  • '--auto': Performs the default action appropriate for the target component.
  • '--show': Displays the target component if it's currently hidden.
  • '--hide': Conceals the target component from view.
  • '--toggle': Alternates the target component between visible and hidden states.
  • '--copy': Copies the target clipboard item.

The supported actions vary by target component type. Learn more about the command attribute.

Anchor to commandFor
commandFor
string

The ID of the component to control when this component is activated. Pair with the command property to specify what action to perform on the target component. Learn more about the commandFor attribute.

When both commandFor and href are set, commandFor takes precedence. The command runs and the link doesn't navigate.

Anchor to defaultChecked
defaultChecked
boolean
Default: false

Whether the control is checked by default.

Anchor to disabled
disabled
boolean
Default: false

Whether the control is disabled, preventing any user interaction.

string

A unique identifier for the element. Use this to reference the element in JavaScript, link labels to form controls, or target specific elements for styling or scripting.

Anchor to label
label
string

The text displayed as the control label, which identifies the purpose of the control to users. This label is associated with the control for accessibility.

string

The name attribute for the control, used to identify its value when the form is submitted. Must be unique within the nearest containing form.

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
<typeof tagName>

A callback fired when the switch value changes.

Learn more about the change event.


Anchor to Add a shipping insurance toggleAdd a shipping insurance toggle

Let buyers opt in to an optional add-on with a toggle. This example presents an s-switch with a label for shipping insurance.

Add a shipping insurance toggle

A rendered example of the switch component

html

<s-switch label="Shipping insurance"></s-switch>

Anchor to Pre-enable a notification settingPre-enable a notification setting

Start a toggle in its active state on first load for a commonly chosen option. This example shows an s-switch with defaultChecked for order notifications.

html

<s-switch label="Enable order notifications" defaultChecked></s-switch>

Anchor to Disable an unavailable optionDisable an unavailable option

Show a non-interactive toggle when the option isn't available for the current order. This example displays an s-switch with the disabled attribute for express delivery.

html

<s-switch label="Express delivery" disabled></s-switch>

  • Use for immediate effects: Reserve switches for settings that take effect immediately without a save or submit action.
  • Write clear labels: Describe the setting being toggled, such as "Shipping insurance" rather than "Toggle" or "On/Off."
  • Use checkbox for deferred actions: If the setting requires form submission to apply, use checkbox instead.
  • Pre-enable thoughtfully: Only use defaultChecked for options that benefit the buyer by default.

Was this page helpful?