Switch - Usage
The Switch component allows users to toggle between two states.
AndroidreleasedElementsreleasediOSreleasedReactreleasedVuereleased
Elements
Usage Notes
<w-switch> is a form-associated toggle component.
- It reflects its
name,value,checked, anddisabledproperties to attributes. - It dispatches a bubbling
changeCustomEventwhen toggled (when not disabled). - When used in a form, the control submits
valueonly whencheckedistrue(otherwise it submits nothing).
Example
html
<w-switch id="demo-switch" name="marketing" value="yes"></w-switch>
<script>
const el = document.querySelector('#demo-switch');
el.addEventListener('change', (event) => {
// event.detail = { checked: boolean, value: string | null }
console.log('changed', event.detail);
});
</script>Props
Optional Props
| Name | Type | Default | Description |
|---|---|---|---|
| name | string | Name used when submitting an HTML form. | |
| value | string | Value submitted when the switch is checked. | |
| checked | boolean | false | Whether the switch is on (checked). |
| disabled | boolean | false | Whether the switch is disabled. |
Events
| Name | Description |
|---|---|
| change | (event: CustomEvent) => void |
Name (name)
Name used when submitting an HTML form.
Default: ''
html
<form>
<w-switch name="notifications" value="on"></w-switch>
<button type="submit">Submit</button>
</form>Value (value)
Value submitted when the switch is checked.
Default: ''
Notes:
- The component reports
nullas the value in thechangeevent whenvalueis an empty string.
html
<w-switch value="enabled"></w-switch>Checked (checked)
Whether the switch is on (checked).
Default: false
html
<w-switch checked></w-switch>Disabled (disabled)
Whether the switch is disabled.
Default: false
html
<w-switch disabled></w-switch>
<w-switch checked disabled></w-switch>Events
Change (change)
Dispatched when the switch toggles (only when not disabled).
- Type:
CustomEvent - Bubbles:
true - Composed:
true - Detail:
{ checked: boolean; value: string | null }
html
<w-switch id="switch-events" value="yes"></w-switch>
<script>
const el = document.querySelector('#switch-events');
el.addEventListener('change', (e) => {
console.log('checked:', e.detail.checked);
console.log('value:', e.detail.value);
});
</script>Accessibility
The internal control is rendered as a native button with role="switch" and aria-checked / aria-disabled.