Switch

Switches allow the user to toggle an option on or off.

Switches allow the user to toggle an option on or off.

Switch

Use the checked attribute to activate the switch.

Switch

Use the disabled attribute to disable the switch.

Switch

Add descriptive help text to a switch with the help-text attribute. For help texts that contain HTML, use the *help-text slot instead.

Label

You can listen for the ods-change event to determine when a switch is updated. You can see other switch events here . You can also see how a switch is used in forms by checking out the form section .

Switch
import '@ods/components/web.switch';

Note: You only need to import a web component once in your code, as it registers itself globally when defined, allowing you to use it anywhere within your application without needing to re-import it each time you want to use the component.

Slots

NameDescription
(default)The switch's label.
help-textText that describes how to use the switch. Alternatively, you can use the `help-text` attribute.

Properties

Please note, both DOM properties and HTML attributes are shown in the table below. Unless otherwise specified, the property and attribute names are identical.

Property/AttributeReflectsTypeDefault
title
string''
name
string''
value
string
disabled
booleanfalse
checked
booleanfalse
defaultChecked
booleanfalse
form
string''
required
booleanfalse
helpText
help-text
string''
validity
validationMessage

Events

React EventDescriptionEvent Detail
ods-blurEmitted when the control loses focus.--
ods-changeEmitted when the control's checked state changes.--
ods-inputEmitted when the control receives input.--
ods-focusEmitted when the control gains focus.--
ods-invalidEmitted when the form control has been checked for validity and its constraints aren't satisfied.--

Methods

NameDescriptionArguments
click()Simulates a click on the switch.--
focus()Sets focus on the switch.--
blur()Removes focus from the switch.--
checkValidity()Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid.--
getForm()Gets the associated form, if one exists.--
reportValidity()Checks for validity and shows the browser's validation message if the control is invalid.--
setCustomValidity()Sets a custom validation message. Pass an empty string to restore validity.--

Parts

NameDescription
baseThe component's base wrapper.
controlThe control that houses the switch's thumb.
thumbThe switch's thumb.
labelThe switch's label.
form-control-help-textThe help text's wrapper.
form-control-error-textThe error text's wrapper.

Search