---
title: ToggleSwitch
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, TokensTable, PropsTable } from '../../src/components'
import { ToggleSwitch } from '@telus-uds/components-web'
import { ToggleSwitch as BaseDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import NoStylisticVariants from '../../partials/_variants-none.mdx'

# ToggleSwitch

<LibraryInfo figmaLink="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Ffile%2F4zJn2Qn6uXOqPP1GVDipy6%2F%25F0%259F%258D%2580-Allium-Components%3Fnode-id%3D1897%253A13965" />

```jsx live
<ToggleSwitch label="Enable data" tooltip="Toggle this on to enable data" />
```

## Introduction

Use ToggleSwitch to control a boolean state.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

- Use `ToggleSwitch` where a user can alternate between easy-to-understand "on" and "off" states.
- Use [`ToggleSwitchGroup`](toggle-switch-group) 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.

```jsx live
<ToggleSwitch
  label="Enable data"
  initialValue={true}
  onChange={(newValue) => alert(`New value is "${newValue}"`)}
/>
```

### 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.

```js live
function ControlledToggleSwitch() {
  const [value, setValue] = useState(false)
  return (
    <StackView space={3}>
      <Typography>Toggle switch is {value ? 'on' : 'off'}.</Typography>
      <ToggleSwitch label="Enable data" value={value} onChange={setValue} />
    </StackView>
  )
}
```

### 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.

```jsx live
<StackView space={3}>
  <Typography variant={{ size: 'h3' }}>Marketing preferences</Typography>
  <Typography>Choose whether to receive marketing emails, such as special offers.</Typography>
  <Typography>
    These will be tailored to your interests and direct from Telus, not from third parties.
  </Typography>
  <ToggleSwitch label="Enable data" accessibilityLabel="Receive marketing emails from Telus" />
</StackView>
```

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

<AvailableOnBoth />

## Props

<PropsTable base={BaseDocs} />

## Tokens

:::warning
Note: `borderWidth` token is deprecated and will be removed in the next major release. Please consider using `borderRightWidth`, `borderLeftWidth`, `borderTopWidth`, and `borderBottomWidth` to achieve the same outcome.
:::

<TokensTable base={BaseDocs} component="ToggleSwitch" />

## Variants

### Toggle Position

`togglePosition` controls the position of the switch in relation to the label. - 'start': switch on the start, before the `label` and `tooltip`. 'end': switch to the end, after the `label` and `tooltip` (default).

```jsx live
<StackView space={3}>
  <ToggleSwitch label="Switch" />
  <ToggleSwitch togglePosition="start" label="Switch start" />
  <ToggleSwitch togglePosition="end" label="Switch end" />
</StackView>
```

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

```jsx live
<StackView space={3}>
  <ToggleSwitch label="Enable data" initialValue={false} inactive />
  <ToggleSwitch label="Enable data" initialValue={true} inactive />
</StackView>
```

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

## Feedback

<Feedback component="ToggleSwitch" />
