<cs-switch>
StableFormsSince 0.1

Switches toggle a single setting on or off and apply the change immediately, without requiring a form submission.

Enable notifications

This component works with standard <form> elements. See form controls for form submission and client-side validation.

Examples

Label

Add label text as the switch’s default content. For labels that contain HTML, slot the markup in directly.

Subscribe to the newsletter

Hint

Add descriptive hint to a switch with the hint attribute. For hints that contain HTML, use the hint slot instead.

Email me about new releases

Initial Value

Use the checked attribute to activate the switch.

Remember this device

checked sets the initial value, not the current state.
Consistent with native checkboxes, it doesn’t reflect later changes. To toggle the checked state with JavaScript, use the checked property instead. To target checked switches with CSS, use the :state(checked) selector.

Disabled

Use the disabled attribute to disable the switch.

Sync over cellular

Size

Use the size attribute to change a switch’s size.

Extra Small Small Medium Large Extra Large

Custom Properties

Use the available custom properties to change how the switch is styled.

Really big

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/switch/switch.js';
import './cornerstone/components/switch/switch.js';
import CsSwitch from '@cruglobal/cornerstone-components/react/switch/index.js';

Slots

Learn more about using slots.

NameDescription
(default)The switch's label.
hintText that describes how to use the switch. Alternatively, you can use the hint attribute.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
checkedDraws the checkbox in a checked state.
defaultCheckedcheckedThe default value of the form control. Primarily used for resetting the form control.booleanYes
disableddisabledDisables the switch.booleanfalse
formBy default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work.HTMLFormElement | null
hinthintThe switch's hint. If you need to display HTML, use the hint slot instead.string''
namenameThe name of the switch, submitted as a name/value pair with form data.string | nullnullYes
requiredrequiredMakes the switch a required field.booleanfalseYes
sizesizeThe switch's size.'xs' | 's' | 'm' | 'l' | 'xl''m'Yes
ssrHintssr-hintOnly required for SSR. Set to true if you're slotting in a hint element so the server-rendered markup includes the hint before the component hydrates on the client.booleanfalse
validationTargetOverride this to change where constraint validation popups are anchored.undefined | HTMLElement
validatorsValidators are static because they have observedAttributes, essentially attributes to "watch" for changes. Whenever these attributes change, we want to be notified and update the validator.Validator[][]
valuevalueThe value of the switch, submitted as a name/value pair with form data.string | nullYes

Methods

Learn more about methods.

NameDescriptionArguments
blur()Removes focus from the switch.
click()Simulates a click on the switch.
focus()Sets focus on the switch.options: FocusOptions
formStateRestoreCallback()Called when the browser is trying to restore element’s state to state in which case reason is "restore", or when the browser is trying to fulfill autofill on behalf of user in which case reason is "autocomplete". In the case of "restore", state is a string, File, or FormData object previously set as the second argument to setFormValue.state: string | File | FormData | null, reason: 'autocomplete' | 'restore'
resetValidity()Reset validity is a way of removing manual custom errors and native validation.
setCustomValidity()Do not use this when creating a "Validator". This is intended for end users of components. We track manually defined custom errors so we don't clear them on accident in our validators.message: string

Events

Learn more about events.

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

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--heightThe height of the switch.
--thumb-sizeThe size of the thumb.
--widthThe width of the switch.

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
controlThe control that houses the switch's thumb.::part(control)
hintThe hint's wrapper.::part(hint)
labelThe switch's label.::part(label)
switchThe component's outer wrapper.::part(switch)
thumbThe switch's thumb.::part(thumb)