Permits selection from two opposing options

import { CdrSwitch } from '@rei/cedar'
Uses: CdrIcon

Switch is a system-wide control used to quickly change between two possible states. This component is only used for binary actions occurring immediately after the user makes any changes.

When to use

  • Displaying two options (yes or no; on or off)
  • Changing system-level settings, preferences, or other functions
  • The change needs to take immediate effect

When not to use

Choosing the right component

Switches, checkboxes, radio buttons, and toggle buttons are all similar types of selection controls, but are generally not interchangeable. If you're not sure which one to use for a certain scenario, use this comparison table.

CriteriaRadio buttonCheckboxSingle checkboxSwitchToggle button
Selections availableAt least twoAt least oneJust oneOnly two (on or off)At least two (this or that)
Default optionYesNoYesYesYes
ChoicesMutually exclusiveIndependent of each otherMutually exclusiveMutually exclusiveMutually exclusive
Takes effectAfter a user clicks a submit buttonAfter a user clicks a submit buttonAfter a user clicks a submit buttonImmediatelyImmediately
Do
Switch with label that says Prefers reduced motion.

Do use the switch component for preferences, settings or other immediate functions.

Don't
Switch with label that says Show in stock stores first.

Don't use the switch component for filtering or sorting content.

  • Keep labels short and direct
  • The label should describe what the control will do when the switch is on
  • Labels should describe the state of the system when the switch is on (when in doubt, say the label aloud and append "on/off" to the end)
  • Start labels with a capital letter and use sentence case
  • Labels should have one or two words, preferably nouns, describing the functionality of the switch
Do
Switch with label that says Email notifications.

Do use keywords first, forming unique labels.

Don't
Switch with label that says Do you want to receive email notifications from us?

Don't overload the label with too much content.

What Cedar provides

  • References the id of the switch label by using the aria-label property
  • Sets the role of the switch button element to switch
  • Applies the aria-checked attribute

Development responsibilities

When using this component, here's what you are responsible for:

  • Always provide a descriptive label as a slotted element to the cdr-switch element
  • Always provide an id to the label of the switch

CdrSwitch

Props

NameTypeDefault
id

Sets a custom ID for the switch. If this value is not set, it will be auto-generated.

string
size

Sets the size of the switch Possible values: medium, large

string'medium'
fullWidth

Sets the label and switch to expand to the full width of its container with `space-between`

booleanfalse
modelValue
Required

boolean

Slots

Name
default

The label for the switch

Events

NameParameters
update:modelValue

Event emitted by v-model on switch

modelValue