Switch

Instantly toggle a setting on or off.

Live preview
Interact with the component to see results here.
View code
HTML
<sy-switch
  label="Receive notifications"
  size="medium"
  checked
  name="notification"
  id="sample"
></sy-switch>

Usage guide

#

Use for on/off settings that take effect immediately. Checkbox is better for consent confirmed on form submission.

  1. 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.

  2. 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.

  3. 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 loading to indicate processing and prevent repeated interaction.

Feature examples

#

Choose a feature from 12 topics. Explore property settings, method calls, event results, and slot layouts through live examples and code.

Usage examples · Overview

Try the selected example. Topics with multiple configurations let you switch between detailed examples.

View code
HTML
Loading the example.

Property · Checked

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Property · Disabled

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Property · Label

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Property · Loading

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Property · Readonly

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Property · Required

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Property · Size

See how settings change appearance and behavior.

View code
HTML
Loading the example.

Methods · Check Validity

Use the example's buttons to call methods and inspect their results.

View code
HTML
Loading the example.

Methods · Report Validity

Use the example's buttons to call methods and inspect their results.

View code
HTML
Loading the example.

Methods · Set Custom Error

Use the example's buttons to call methods and inspect their results.

View code
HTML
Loading the example.

Events · Changed

Interact with the component to see event names and payloads in the live preview below.

View code
HTML
Loading the example.

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 / PropertyTypeDefaultDescription
booleanfalseSelected or on state.
booleanfalseBlock user interaction and keyboard focus.
string''Label describing the item.
booleanfalseIndicate that processing is in progress.
booleanfalseDisplay the current value while preventing edits.
'small' | 'medium''medium'Component size.
string''Field name used in FormData.
string'on'Current value or item identifier.
booleanfalseFail form validation when no value is provided.

Events

EventDetailDescription
CustomEvent<boolean>Emitted when the value or selection state changes.

Methods

MethodReturnsDescription
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

SlotDescription
Validation error message.