Skip to content

SSwitch <s-switch> ​

A toggle switch that turns a setting on or off. SSwitch is the typed Vue 3 wrapper for Shopify's Polaris <s-switch> web component.

ts
import { SSwitch } from 'polaris-vue-elements'

Best practices ​

  • Don't pair switches with a save button. A switch takes effect the moment it's toggled, so adding a save button leaves merchants unsure when their change actually applies.
  • Make the setting obvious. Write a label that tells merchants what the switch controls and what on and off mean, so they don't have to flip it just to find out.
  • Describe the consequences. Use details to explain what changes when the switch is toggled, especially for settings that affect important features or data, so merchants can decide with confidence.
  • Group and order related switches. When a page has several switches, keep related settings together in a logical order so merchants can find and configure them easily.
  • Explain why a switch is unavailable. When a switch is disabled because of permissions, dependencies or plan limits, tell merchants why, whether it's temporary, and what they need to do to unlock it.
vue
<SSwitch label="Send order confirmation emails" details="Customers get an email as soon as they place an order." />

See Shopify's best practices for <s-switch> for the full guidance.

Example ​

On: false

vue
<script setup>
import { ref } from 'vue'
import { SSwitch } from 'polaris-vue-elements'

const value = ref(false)
</script>

<template>
  <SSwitch label="Enable notifications" v-model="value" />
  <p>On: {{ value }}</p>
</template>

v-model ​

v-model on <SSwitch> binds to the checked property and listens for the change DOM event.

template
<SSwitch v-model="value" />

is equivalent to:

template
<SSwitch :checked="value" @change="value = $event.target.checked" />

Props ​

PropTypeDescription
accessibilityLabelstringA 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.
checkedbooleanWhether the control is active.
defaultCheckedbooleanWhether the control is active by default.
detailsanyAdditional 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.
disabledbooleanDisables the control, disallowing any interaction.
erroranyIndicate an error to the user. The field will be given a specific stylistic treatment to communicate problems that have to be resolved immediately.
labelanyVisual content to use as the control label.
labelAccessibilityVisibility"visible" | "exclusive"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.
namestringAn identifier for the control that is unique within the nearest containing Form component.
requiredbooleanWhether the field needs a value. This requirement adds semantic value to the field, but it will not cause an error to appear automatically. If you want to present an error when this field is empty, you can do so with the error property.
valuestringThe value used in form data when the control is checked.

Events ​

EventType
blurFocusEvent
changeEvent
inputEvent

Listen for these with @blur-style listeners on <SSwitch>.

Released under the MIT License. Vue logo by Evan You, CC BY-NC-SA 4.0.