Switch

A switch turns one setting on or off, and the change takes effect at once. It renders a native <button> with role="switch".

Import
import { Switch } from 'svelte-ant-design';
Links
src/lib/Switch Report an issue Component README

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 loading while the change is being saved, and a function binding when the parent has to confirm it.

Examples

Notifications are on no change yet

Basic

bind:checked keeps a value in sync with the switch; onchange receives the new value and the click event.

Disabled

A disabled switch is natively disabled: it is not focusable and ignores clicks.

Nothing around the switches moves when you toggle them.

Text and icons

checkedChildren and unCheckedChildren take plain text or a snippet (for icons and markup) and show it in their own state. Both stay mounted and the switch keeps the width of the longer one, so nothing around it moves when you toggle (try the Happy/Sad switch). They are hidden from assistive technology, so the switch still needs a name.

Sizes

medium and small. The value default is accepted as an alias of medium.

Loading

Switch the first one: it shows a spinner for two seconds and keeps focus. A loading switch ignores clicks, Enter and Space.

disabled, refused 0 times

Control by the parent

A function binding lets the parent accept or refuse a change (it needs Svelte 5.9 or newer). preventDefault() in onclick cancels the toggle. Tick the box to allow changes.

Classes and styles

classNames and styles address the root, content and indicator parts; class and style go to the root. Inline styles win over the component's own, and custom properties such as --ads-switch-track-on work.

Bluetooth Needed for headphones

Accessible name

Name a switch with <label for>, aria-label or aria-labelledby. The on and off text never counts as a name.

API

PropertyDescriptionTypeDefault
checkedWhether the switch is on. Use bind:checked for two-way binding; a plain value is the initial state.booleanfalse
onchangeCalled 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—
onclickNative click callback. It runs before the state flips; event.preventDefault() cancels the toggle. Not called while disabled or loading.(event: MouseEvent) => void—
disabledNative disabled: not focusable, ignores clicks and keys.booleanfalse
loadingShows a spinner in the handle and ignores activation. The switch keeps focus and gets aria-busy and aria-disabled.booleanfalse
sizeSize. default is a compatibility alias of medium.'medium' | 'small' | 'default''medium'
checkedChildrenContent 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—
unCheckedChildrenContent 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—
classNamesExtra classes for the parts root, content and indicator (the handle), appended to the component's own.Partial<Record<'root' | 'content' | 'indicator', string>>—
stylesInline 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>>>—
classAn additional CSS class for the root.string—
styleInline style of the root; styles.root is written after it.string—
native attributesNative 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

MethodDescriptionType
focusMoves focus to the switch.(options?: FocusOptions) => void
blurRemoves focus from the switch.() => void

Design tokens

TokenDescriptionDefault
--ads-switch-heightHeight of a medium switch.22px
--ads-switch-height-smHeight of a small switch.16px
--ads-switch-min-widthMinimum width of a medium switch.44px
--ads-switch-min-width-smMinimum width of a small switch.28px
--ads-switch-handle-sizeDiameter of the handle of a medium switch.18px
--ads-switch-handle-size-smDiameter of the handle of a small switch.12px
--ads-switch-paddingGap between the handle and the edge of the track.2px
--ads-switch-content-margin-minMargin of the text on the side away from the handle (medium).9px
--ads-switch-content-margin-maxMargin of the text on the side of the handle (medium).24px
--ads-switch-content-margin-min-smThe same margin for a small switch.6px
--ads-switch-content-margin-max-smThe same margin for a small switch.18px
--ads-switch-font-sizeFont size of the text of a medium switch.12px
--ads-switch-font-size-smFont size of the text of a small switch.10px
--ads-switch-spinner-border-widthThickness of the loading spinner.2px
--ads-switch-handle-shadowShadow of the handle.0 2px 4px 0 rgb(0 35 11 / 20%)
--ads-switch-loading-opacityOpacity of a loading switch. tokens-accessible.css sets it to 1.0.65
--ads-switch-disabled-opacityOpacity of a disabled switch.0.4
--ads-switch-track-offTrack 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-hoverTrack when off, on hover.rgba(0, 0, 0, 0.45)
--ads-switch-track-off-activeTrack when off, while pressed (the same as on hover).rgba(0, 0, 0, 0.45)
--ads-switch-track-onTrack when on. tokens-accessible.css darkens it to #1668dc.#1677ff
--ads-switch-track-on-hoverTrack when on, on hover.#4096ff
--ads-switch-track-on-activeTrack when on, while pressed (the same as on hover).#4096ff
--ads-switch-handle-bgFill of the handle.#ffffff
--ads-switch-content-colorColor of the text and icons inside the track.#ffffff

Accessibility

  • The switch is a native <button> with role="switch" and aria-checked. Enter and Space toggle it; arrow keys do nothing.
  • It needs an accessible name: aria-label, aria-labelledby or 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-busy and aria-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.