Switch
The Switch component is utilized to toggle the state of a single setting between on and 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. To set the switch to the on state by default, add the checked
attribute.
Disabled State
The disabled
attribute can be used to disable the switch.
Icons
The unchecked-icon
and checked-icon
attributes can be used to set the Material Icons for the unchecked and checked states, respectively. Alternatively, the unchecked-icon
and checked-icon
slots can be used 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. This can only be set via a JavaScript property. | ||||
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 when submitting the form. | ||||
form | form | string | - | |
Associates the switch with a This attribute allows switch elements to be associated with | ||||
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 | - | ||
The element's validation message. This is empty if the element meets its constraints. | ||||
autofocus | autofocus | boolean | false | |
Determines if the element should be focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
Specifies the order in which the element receives focus when navigating with the Tab key. |
Methods
Name | Parameters | Returns |
---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it's invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it's invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the text is non-empty, it indicates that the field is invalid. | ||
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional parameter can be an object with 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 |
Icon container. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
CSS Custom Properties
Name |
---|
--shape-corner |
The corner size of the track. You can use a specific pixel value, but it's recommended to reference design tokens. |
--shape-corner-thumb |
The corner size of the icon container. You can use a specific pixel value, but it's recommended to reference design tokens. |