toggle button

Permits switching two or more options on and off

import { CdrToggleButton, CdrToggleGroup } from '@rei/cedar'

Toggle buttons are a set of two or more options, each of which functions as a button. Use them to switch between alternative views of similar or related content in a contextual setting. For instance, the component allows users to toggle between a grid or list view, or to toggle between centimeters or inches.

When to use

  • Providing choices that affect an object, state, or view
  • Displaying two or more mutually exclusive options
  • Changing contextual information
  • The change needs to take immediate effect

When not to use

  • Displaying related content that isn’t the same type, which should be separated completely. Instead, use tabs
  • Allowing the user to take an action, such as add and remove. Instead, use a button
  • Content can’t fit on the smallest breakpoint (too many options can be hard to scan and navigate). Aim for five options at most. Instead, use radio buttons or select

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

The toggle button contains at least two selections, with a default option selected. You can also extend the component to include more than two. Take care in using toggle buttons that include more than five selections, as space on mobile is limited. Consider checking whether the toggle button will work on Cedar’s XS breakpoint.

Do
A toggle button with two segments labeled day and night.

Do limit the number of segments to aid in usability and scanning.

Don't
A toggle button with three segments labeled morning, afternoon, and night that don't fit the breakpoint.

Don't include more segments than what fit on the smallest breakpoint.

  • Use nouns or noun phrases for labels
  • As much as possible, use content with a similar character count in each segment

Use text-only labels when:

  • Text/copy adequately informs the user of the expected content
  • There is no CdrIcon to properly inform the user of represented content
Do
A toggle button with two segments labeled centimeters and inches.

Do use content types that make sense for each experience.

Don't
A toggle button with a ruler icon on one segment and not on the other.

Don't mix content types in the same toggle button.

What Cedar provides

  • Sets the role of the unordered list to radiogroup
  • Sets the role of the individual toggle buttons to radio
  • Provides an aria-checked property for each toggle button

Development responsibilities

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

Always provide a toggleValue prop. The value of this property will be used as an aria-label attribute. This has the following uses:

  • Spelling out abbreviated values (like "centimeters" instead of "cm") for screen readers
  • Providing a label when only an icon is slotted to the toggle button

The parent CdrToggleGroup contains all the child CdrToggleButton components. This component will track the value of the selected toggle button among all the toggle buttons slotted to it. CdrToggleGroup requires v-model to track the value of the selected child CdrToggleButton.

<template>
   <cdr-toggle-group v-model="units">
      <cdr-toggle-button toggleValue="centimeters">cm</cdr-toggle-button>
      <cdr-toggle-button toggleValue="inches">in</cdr-toggle-button>
    </cdr-toggle-group>
</template>

The toggleValue prop of the selected CdrToggleButton will determine the modelValue of the selected toggle button. If no text or icon is slotted to the toggle button, the string provided for the required toggleValue prop displays as the button text. In cases where the slotted text and the toggleValue are different, as shown in the previous example, the slotted text gets displayed in the browser.

The modelValue of the CdrToggleGroup is always determined by the toggleValue prop of the selected CdrToggleButton. Also, screen readers will choose to read the toggleValue over the slotted text, which is handy in the case of abbreviations.

CdrToggleButton

Props

NameTypeDefault
toggleValue
Required

Sets the value and aria-label for the toggle button. Display can be overridden using default slot

string

Slots

Name
default

CdrToggleButton label content

CdrToggleGroup

Props

NameTypeDefault
modelValue
Required

string|number|boolean|object|array
size

Sets toggle button size Possible values: medium, large

string'medium'

Slots

Name
default

CdrToggleGroup content (CdrToggleButton components)

Events

NameParameters
update:modelValue

Event emitted by v-model

modelValue