Switch
Instantly toggle a setting on or off.
Usage guide
#Use for on/off settings that take effect immediately. Checkbox is better for consent confirmed on form submission.
Write the error message in the error slot.
Inside the component, add <span slot="error">Error message</span>. You can include links and other guidance as well as text. For inputs supporting noNativeValidity, enable it to show this guidance instead of a browser popup. Switch always displays errors below the component.
Report business rule failures with setCustomError().
For example, if the server rejects a duplicate value, update the error slot and call setCustomError(). This method does not take a message argument. A custom error also blocks form submission.
Clear the error after resolving it and check whether submission is allowed.
Use clearCustomError() to clear a business rule error. Required-field and format validation remain active. await checkValidity() returns true or false; reportValidity() validates and displays errors. Read valid named values through FormData on submission.
- Current state
- Reflect the actual setting in
checked. - Saving state
- Use
loadingto indicate processing and prevent repeated interaction.
Examples
#Feature examples
#Choose a feature from 12 topics. Explore property settings, method calls, event results, and slot layouts through live examples and code.
No matching features.
API reference
#Hover over a name or focus it with the keyboard to see the code. On touch screens, tap the name.
<sy-switch>
Properties
| Attribute / Property | Type | Default | Description |
|---|---|---|---|
boolean | false | Selected or on state. | |
boolean | false | Block user interaction and keyboard focus. | |
string | '' | Label describing the item. | |
boolean | false | Indicate that processing is in progress. | |
boolean | false | Display the current value while preventing edits. | |
'small' | 'medium' | 'medium' | Component size. | |
string | '' | Field name used in FormData. | |
string | 'on' | Current value or item identifier. | |
boolean | false | Fail form validation when no value is provided. |
Events
| Event | Detail | Description |
|---|---|---|
CustomEvent<boolean> | Emitted when the value or selection state changes. |
Methods
| Method | Returns | Description |
|---|---|---|
Promise<boolean> | Return true if the current constraints are satisfied, otherwise false. | |
Promise<boolean> | Validate the current value and display errors to the user. | |
Promise<string> | Return the current validation state. | |
Promise<void> | Set a custom error state. | |
Promise<void> | Clear the custom error state. |
Slots
| Slot | Description |
|---|---|
| Validation error message. |