ToggleSwitch
- Library
- UDS Base
- Repository
- github.com/telus/universal-design-system
- Package
- npmjs.com/package/@telus-uds/components-base
- Package version
- 1.6.1
Introduction
import { ToggleSwitch } from '@telus-uds/components-base'
Use ToggleSwitch to control a boolean state.
Guidance
- Use
ToggleSwitchwhere a user can alternate between easy-to-understand "on" and "off" states. - Use
ToggleSwitchGroupif 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.
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.
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.
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.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| accessibilityRole | 'switch' | ||
| accessibilityLabel | label | ||
| tokens | custom | System tokens prop, see tokens for more details | |
| variant | objectOf | System variant prop, see variants for more details | |
| value | bool | If this is a "controlled" component and a parent handles its selected state, tells the switch if it is on (true) or off (false). | |
| initialValue | bool | If 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). | |
| onChange | func | Function 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. | |
| id | string | Input ID. | |
| label | string | An optional label. | |
| inactive | bool | If passed, the switch does not respond to user input and may recieve different theme tokens if the theme supports inactive appearance. | |
| tooltip | string | Content of an optional Tooltip. If set, a tooltip button will be shown next to the label. |