Switch
A switch turns one setting on or off, and the change takes effect at once. It renders a native <button> with role="switch".
When to use
- Use a switch to turn a setting on or off. The change applies immediately, with no extra Save or Submit step.
- If the choice is only marked and sent later with a form, or picks items in a list, a checkbox is the better fit.
- Use
loadingwhile the change is being saved, and a function binding when the parent has to confirm it.
Examples
Notifications are on no change yet
<script lang="ts">
import { Switch } from 'svelte-ant-design';
let on = $state(true);
let last = $state('no change yet');
</script>
<Switch
bind:checked={on}
aria-label="Notifications"
onchange={(checked) => (last = `onchange(${checked})`)}
/>
<span>Notifications are {on ? 'on' : 'off'}</span>
<span role="status">{last}</span>
<script lang="ts">
import { Button, Switch } from 'svelte-ant-design';
let disabled = $state(true);
</script>
<Switch checked {disabled} aria-label="Disabled and on" />
<Switch {disabled} aria-label="Disabled and off" />
<Button onclick={() => (disabled = !disabled)}>Toggle disabled</Button>
Nothing around the switches moves when you toggle them.
<script lang="ts">
import { Switch } from 'svelte-ant-design';
</script>
<!-- Plain text for the two states. -->
<Switch checked checkedChildren="On" unCheckedChildren="Off" aria-label="Power" />
<Switch checkedChildren="On" unCheckedChildren="Off" aria-label="Power, off" />
<!-- The texts differ in width, yet the switch keeps the width of the longer one. -->
<Switch checkedChildren="Happy" unCheckedChildren="Sad" aria-label="Mood" />
<!-- A snippet for icons and other markup. -->
{#snippet check()}
<svg viewBox="0 0 12 12" width="10" height="10" fill="none" stroke="currentColor" stroke-width="2">
<path d="M2 6.5 5 9l5-6" />
</svg>
{/snippet}
{#snippet cross()}
<svg viewBox="0 0 12 12" width="10" height="10" fill="none" stroke="currentColor" stroke-width="2">
<path d="m2.5 2.5 7 7m0-7-7 7" />
</svg>
{/snippet}
<Switch checked checkedChildren={check} unCheckedChildren={cross} aria-label="Approved" />
<Switch checkedChildren={check} unCheckedChildren={cross} aria-label="Approved, off" />
<Switch size="small" checked checkedChildren="On" unCheckedChildren="Off" aria-label="Small power" />
<span>Nothing around the switches moves when you toggle them.</span>
<script lang="ts">
import { Switch } from 'svelte-ant-design';
</script>
<Switch checked aria-label="Medium" />
<Switch checked size="medium" aria-label="Medium, explicit" />
<Switch checked size="small" aria-label="Small" />
<!-- `default` is an alias of `medium` for people coming from Ant Design Vue or NG-ZORRO. -->
<Switch checked size="default" aria-label="Default, same as medium" />
<script lang="ts">
import { Switch } from 'svelte-ant-design';
let on = $state(false);
let saving = $state(false);
function save() {
saving = true;
setTimeout(() => (saving = false), 2000);
}
</script>
<Switch bind:checked={on} loading={saving} onchange={save} aria-label="Auto-save" />
<Switch loading checked aria-label="Loading and on" />
<Switch loading aria-label="Loading and off" />
<Switch loading size="small" checked aria-label="Small, loading and on" />
disabled, refused 0 times
<script lang="ts">
import { Switch } from 'svelte-ant-design';
let agreed = $state(false);
let enabled = $state(false);
let locked = $state(false);
let refused = $state(0);
</script>
<label>
<input type="checkbox" bind:checked={agreed} />
I accept the terms
</label>
<!-- A function binding: the setter decides whether a change is accepted. -->
<Switch
aria-label="Enable the feature"
bind:checked={
() => enabled,
(next) => {
if (next && !agreed) {
refused++;
return;
}
enabled = next;
}
}
/>
<span role="status">{enabled ? 'enabled' : 'disabled'}, refused {refused} times</span>
<!-- preventDefault() in onclick cancels the toggle and onchange. -->
<Switch
bind:checked={locked}
aria-label="Lock the setting"
onclick={(event) => {
if (!agreed) event.preventDefault();
}}
/>
<script lang="ts">
import { Switch } from 'svelte-ant-design';
</script>
<!-- class goes to the root; classNames adds classes to the root, content and indicator parts. -->
<Switch
checked
aria-label="With classes"
class="spaced"
classNames={{ root: 'halo', indicator: 'handle-outline' }}
/>
<!-- styles are inline styles per part; custom properties work too. -->
<Switch
checked
aria-label="With styles"
styles={{
root: { '--ads-switch-track-on': '#237804', '--ads-switch-track-on-hover': '#135200' },
indicator: { 'box-shadow': '0 0 0 2px #fff' }
}}
/>
<!-- style is the inline style of the root; styles.root is written after it and wins. -->
<Switch
aria-label="With style"
style="outline: 2px dashed #8c8c8c; outline-offset: 2px"
styles={{ root: { opacity: 0.85 } }}
/>
<style>
:global(.spaced) {
margin-inline-end: 12px;
}
:global(.halo) {
box-shadow: 0 0 0 4px rgb(22 104 220 / 25%);
}
:global(.handle-outline) {
outline: 1px solid rgb(0 0 0 / 35%);
}
</style>
Bluetooth Needed for headphones
<script lang="ts">
import { Switch } from 'svelte-ant-design';
</script>
<!-- A label element: clicking the text toggles the switch. -->
<label for="wifi">Wi-Fi</label>
<Switch id="wifi" checked />
<!-- aria-label for a switch that has no visible text. -->
<Switch aria-label="Airplane mode" title="Airplane mode" />
<!-- aria-labelledby points at any element that holds the name. -->
<span id="bluetooth-name">Bluetooth</span>
<Switch aria-labelledby="bluetooth-name" aria-describedby="bluetooth-hint" />
<small id="bluetooth-hint">Needed for headphones</small>
API
| Property | Description | Type | Default |
|---|---|---|---|
| checked | Whether the switch is on. Use bind:checked for two-way binding; a plain value is the initial state. | boolean | false |
| onchange | Called once per user toggle, after the state flips, with the new value. Not called when the parent changes checked, nor while disabled or loading. | (checked: boolean, event: MouseEvent) => void | — |
| onclick | Native click callback. It runs before the state flips; event.preventDefault() cancels the toggle. Not called while disabled or loading. | (event: MouseEvent) => void | — |
| disabled | Native disabled: not focusable, ignores clicks and keys. | boolean | false |
| loading | Shows a spinner in the handle and ignores activation. The switch keeps focus and gets aria-busy and aria-disabled. | boolean | false |
| size | Size. default is a compatibility alias of medium. | 'medium' | 'small' | 'default' | 'medium' |
| checkedChildren | Content shown while the switch is on: text, or a snippet for icons and markup. Hidden from assistive technology. It stays mounted while the switch is off, so the switch keeps the width of the longer content (avoid ids and side effects inside a snippet). | string | Snippet | — |
| unCheckedChildren | Content shown while the switch is off: text, or a snippet for icons and markup. Hidden from assistive technology. It stays mounted while the switch is on, so the switch keeps the width of the longer content (avoid ids and side effects inside a snippet). | string | Snippet | — |
| classNames | Extra classes for the parts root, content and indicator (the handle), appended to the component's own. | Partial<Record<'root' | 'content' | 'indicator', string>> | — |
| styles | Inline styles for the same parts. Property names are kebab-case or custom properties, numbers are written as they are (no automatic px). They win over the component's styles. | Partial<Record<'root' | 'content' | 'indicator', Record<string, string | number>>> | — |
| class | An additional CSS class for the root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| native attributes | Native attributes of the button (id, title, tabindex, aria-*, data-*, other handlers) are forwarded to it. type, role and aria-checked belong to the component; its loading state takes priority over a consumer aria-busy and aria-disabled. | HTMLAttributes<HTMLButtonElement> | — |
Differences from Ant Design:
onclick receives only the event (read the new value in onchange); there is no defaultChecked, value, checkedValue or unCheckedValue, because bind:checked covers them.Methods
| Method | Description | Type |
|---|---|---|
| focus | Moves focus to the switch. | (options?: FocusOptions) => void |
| blur | Removes focus from the switch. | () => void |
Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-switch-height | Height of a medium switch. | 22px |
| --ads-switch-height-sm | Height of a small switch. | 16px |
| --ads-switch-min-width | Minimum width of a medium switch. | 44px |
| --ads-switch-min-width-sm | Minimum width of a small switch. | 28px |
| --ads-switch-handle-size | Diameter of the handle of a medium switch. | 18px |
| --ads-switch-handle-size-sm | Diameter of the handle of a small switch. | 12px |
| --ads-switch-padding | Gap between the handle and the edge of the track. | 2px |
| --ads-switch-content-margin-min | Margin of the text on the side away from the handle (medium). | 9px |
| --ads-switch-content-margin-max | Margin of the text on the side of the handle (medium). | 24px |
| --ads-switch-content-margin-min-sm | The same margin for a small switch. | 6px |
| --ads-switch-content-margin-max-sm | The same margin for a small switch. | 18px |
| --ads-switch-font-size | Font size of the text of a medium switch. | 12px |
| --ads-switch-font-size-sm | Font size of the text of a small switch. | 10px |
| --ads-switch-spinner-border-width | Thickness of the loading spinner. | 2px |
| --ads-switch-handle-shadow | Shadow of the handle. | 0 2px 4px 0 rgb(0 35 11 / 20%) |
| --ads-switch-loading-opacity | Opacity of a loading switch. tokens-accessible.css sets it to 1. | 0.65 |
| --ads-switch-disabled-opacity | Opacity of a disabled switch. | 0.4 |
| --ads-switch-track-off | Track when off. tokens-accessible.css darkens it to #737373 to keep 3:1 against the page. | rgba(0, 0, 0, 0.25) |
| --ads-switch-track-off-hover | Track when off, on hover. | rgba(0, 0, 0, 0.45) |
| --ads-switch-track-off-active | Track when off, while pressed (the same as on hover). | rgba(0, 0, 0, 0.45) |
| --ads-switch-track-on | Track when on. tokens-accessible.css darkens it to #1668dc. | #1677ff |
| --ads-switch-track-on-hover | Track when on, on hover. | #4096ff |
| --ads-switch-track-on-active | Track when on, while pressed (the same as on hover). | #4096ff |
| --ads-switch-handle-bg | Fill of the handle. | #ffffff |
| --ads-switch-content-color | Color of the text and icons inside the track. | #ffffff |
Accessibility
- The switch is a native
<button>withrole="switch"andaria-checked. Enter and Space toggle it; arrow keys do nothing. - It needs an accessible name:
aria-label,aria-labelledbyor a<label for>. The on and off text is hidden from assistive technology, because as a name it would only repeat the state. - A loading switch stays focusable and exposes
aria-busyandaria-disabled; clicks, Enter and Space are ignored. A disabled switch is natively disabled. - By default the colours are Ant Design's own, and a loading switch is dimmed. With
tokens-accessible.css, and in the dark theme, the track and the handle keep a contrast of at least 3:1, and the text 4.5:1, in every state: the off track is darker and a loading switch is not dimmed. - In forced-colors modes such as Windows High Contrast the switch is drawn with system colours: an outlined track when off, a filled one when on, and the disabled colour for a disabled switch.