Switch

<wa-switch> Stable Since 2.0

Switches allow the user to toggle an option on or off.

Switch
<wa-switch>Switch</wa-switch>

This component works with standard <form> elements. Please refer to the section on form controls to learn more about form submission and client-side validation.

Examples

Checked

Use the checked attribute to activate the switch.

Checked
<wa-switch checked>Checked</wa-switch>

The checked attribute is the initial value and does not reflect changes, consistent with native checkboxes. To toggle the checked state with JavaScript, use the checked property instead. To target checked switches with CSS, use the :state(checked) selector.

Disabled

Use the disabled attribute to disable the switch.

Disabled
<wa-switch disabled>Disabled</wa-switch>

Sizes

Use the size attribute to change a switch's size.

Extra Small
Small
Medium
Large
Extra Large
<wa-switch size="xs">Extra Small</wa-switch>
<br />
<wa-switch size="s">Small</wa-switch>
<br />
<wa-switch size="m">Medium</wa-switch>
<br />
<wa-switch size="l">Large</wa-switch>
<br />
<wa-switch size="xl">Extra Large</wa-switch>

Hint

Add descriptive hint to a switch with the hint attribute. For hints that contain HTML, use the hint slot instead.

Label
<wa-switch hint="What should the user know about the switch?">Label</wa-switch>

Custom Styles

Use the available custom properties to change how the switch is styled.

Really big
<wa-switch style="--width: 80px; --height: 40px; --thumb-size: 36px;">Really big</wa-switch>

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.

CDN npm Self-Hosted React

Import this component directly from the CDN:

import 'https://ka-f.webawesome.com/webawesome@0.14.0/components/switch/switch.js';

After installing Web Awesome via npm, import this component:

import '@awesome.me/webawesome/dist/components/switch/switch.js';

If you're self-hosting Web Awesome, import this component from your server:

import './webawesome/dist/components/switch/switch.js';

To import this component for React 18 or below, use the following code:

import WaSwitch from '@awesome.me/webawesome/dist/react/switch/index.js';

Slots

Learn more about using slots.

Attributes & Properties

Learn more about attributes and properties.

Methods

Learn more about methods.

Events

Learn more about events.

CSS custom properties

Learn more about CSS custom properties.

CSS parts

Learn more about CSS parts.

Need a hand? Report a bug Ask for help
    No results