Skip to main content

ToggleSwitch

Library
UDS Base
Repository
github.com/telus/universal-design-system
Package
npmjs.com/package/@telus-uds/ds-allium
Package version
1.14.0
Loading...

Introduction

import { ToggleSwitch } from '@telus-uds/ds-allium'

Use ToggleSwitch to control a boolean state.

Guidance

  • Use ToggleSwitch where a user can alternate between easy-to-understand "on" and "off" states.
  • Use ToggleSwitchGroup if you need to combine several switches and / or implement a radio group-like behavior.

Uncontrolled usage with initialValue prop

By default, ToggleSwitch controls its own state. Its default state may be set using initialValue prop, and onChange may also be passed to trigger an action when the value changes.

Loading...

Controlled usage with value prop

Pass a boolean true or false to the value prop and pass a state-setting function to the onChange prop to control the ToggleSwitch from a parent.

This makes the ToggleSwitch not manage its own state and instead trust the parent component to manage it. If a ToggleSwitch uses this pattern, it must always receive a value prop of true or false (never undefined), and its intialValue prop should not be used.

Loading...

Accessibility

If a ToggleSwitch's effect is understood through context in the surrounding text, pass the accessibilityLabel prop so that users of accessibility tools such as screen readers know exactly what the ToggleSwitch will do at the time they will use it.

Loading...

When used as a standalone input, ToggleSwitch has the switch role and declares its on/off state as selected. If a group of ToggleSwitches are used like radio buttons where only one may be selected at a time, setting the radio role via accessibilityRole may be more appropriate.

Platform considerations

The component is available on both native platforms and web.

Props

NameTypeDefaultDescription
accessibilityLabellabel
accessibilityRole'switch'
copy'en' | 'fr''en'Whether the English or French copy will be used (e.g. for accessibility labels).
idstringInput ID.
inactiveboolIf passed, the switch does not respond to user input and may recieve different theme tokens if the theme supports inactive appearance.
initialValueboolIf this is an "uncontrolled" component that handles its own selected state, optionally tells the switch if it should default to on (true) or off (false).
labelstringAn optional label.
onChangefuncFunction called when the switch is toggled, with one boolean argument that is the new position of the switch, on (true) or off (false). For "controlled" components, this should always be passed and used to control the state of the switch.
tokenscustomSystem tokens prop, see tokens for more details
tooltipstringContent of an optional Tooltip. If set, a tooltip button will be shown next to the label.
valueboolIf this is a "controlled" component and a parent handles its selected state, tells the switch if it is on (true) or off (false).
variantobjectOfSystem variant prop, see variants for more details

Tokens

In exceptional circumstances, the following tokens can be passed to this component to override its default styles. Do not do this unless absolutely necessary. Read more about overriding styles.

View Tokens
Prop NameToken PropertyToken Type
tokensborderColorcolor
borderWidthborder
borderRadiusradius
outerBorderColorcolor
outerBorderWidthborder
outerBorderGapsize
outerBackgroundColorcolor
backgroundColorcolor
opacityopacity
paddingLeftsize
paddingRightsize
paddingTopsize
paddingBottomsize
shadowshadow
alignSelfflexAlign
iconicon
widthsize
trackBorderWidthborder
trackBorderColorcolor
trackBorderRadiusradius
iconSizesize
iconColorcolor
labelColorcolor
labelFontNamefontName
labelFontSizefontSize
labelFontWeightfontWeight
labelLineHeightlineHeight
labelMarginLeftsize
switchSizesize
switchColorcolor
switchBorderColorcolor
switchBorderWidthborder
switchBorderRadiusradius
switchShadowshadow

Figma

Variants

This component does not have any stylistic variants.

It does have an inactive appearance state. Use this only when absolutely necessary along with clear messaging (such as Feedback) as to what the user should do.

Loading...

Try to structure user interactions such that inactive inputs are not needed because they intuitively do not become part of the content flow.

Feedback