Switch
The Switch component toggles a single setting on or off.
Usage
Import the component:
import 'mdui/components/switch.js';
Import the TypeScript type:
import type { Switch } from 'mdui/components/switch.js';
Example:
<mdui-switch></mdui-switch>Examples
Checked State
The checked attribute indicates whether the switch is on or off. Add the checked attribute to turn it on by default.
Disabled State
Use the disabled attribute to disable the switch.
Icons
Use the unchecked-icon and checked-icon attributes to set the Material Icons for the unchecked and checked states, respectively. Alternatively, use the unchecked-icon and checked-icon slots to set the icons for the unchecked and checked states.
API
Properties
| Attribute | Property | Reflect | Type | Default |
|---|---|---|---|---|
disabled | disabled | boolean | false | |
Disables the switch. | ||||
checked | checked | boolean | false | |
Sets the switch to the checked state. | ||||
defaultChecked | boolean | false | ||
The default checked state. The switch resets to this state when the form is reset. JavaScript only. | ||||
unchecked-icon | uncheckedIcon | string | - | |
The Material Icons name for the unchecked state. Alternatively, use | ||||
checked-icon | checkedIcon | string | - | |
The Material Icons name for the checked state. Alternatively, use | ||||
required | required | boolean | false | |
The switch must be checked before the form is submitted. | ||||
form | form | string | - | |
Associates the switch with a This lets the switch work with any form in the document, not just the one it is nested in. | ||||
name | name | string | '' | |
The name of the switch, which is submitted with form data. | ||||
value | value | string | 'on' | |
The value of the switch, which is submitted with form data. | ||||
validity | ValidityState | - | ||
A | ||||
validationMessage | string | - | ||
Validation message. Empty string when valid. | ||||
autofocus | autofocus | boolean | false | |
Whether the element is focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
The element's tab order when navigating with the Tab key. | ||||
Methods
| Name | Parameters | Returns |
|---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the message is non-empty, the field is considered invalid. | ||
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional object parameter may include a | ||
blur | void | |
Removes focus from the element. | ||
Slots
| Name |
|---|
unchecked-icon |
Element for the unchecked state. |
checked-icon |
Element for the checked state. |
CSS Parts
| Name |
|---|
track |
Track. |
thumb |
Thumb. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
CSS Custom Properties
| Name |
|---|
--shape-corner |
The corner radius of the track. You can use a specific pixel value, but it is recommended to reference design tokens. |
--shape-corner-thumb |
The corner radius of the icon container. You can use a specific pixel value, but it is recommended to reference design tokens. |