Switch
<cs-switch>
Switches toggle a single setting on or off and apply the change immediately, without requiring a form submission.
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.
Hint
Add descriptive hint to a switch with the hint attribute. For hints that contain HTML, use the hint slot instead.
Initial Value
Use the checked attribute to activate the switch.
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.
Size
Use the size attribute to change a switch’s size.
Custom Properties
Use the available custom properties to change how the switch is styled.
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.
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.
| Name | Description |
|---|---|
| (default) | The switch's label. |
hint | Text that describes how to use the switch. Alternatively, you can use the hint attribute. |
Attributes & Properties
Learn more about attributes and properties.
| Property | Attribute | Description | Type | Default | Reflects |
|---|---|---|---|---|---|
checked | — | Draws the checkbox in a checked state. | — | — | — |
defaultChecked | checked | The default value of the form control. Primarily used for resetting the form control. | boolean | — | Yes |
disabled | disabled | Disables the switch. | boolean | false | — |
form | — | By 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 | — | — |
hint | hint | The switch's hint. If you need to display HTML, use the hint slot instead. | string | '' | — |
name | name | The name of the switch, submitted as a name/value pair with form data. | string | null | null | Yes |
required | required | Makes the switch a required field. | boolean | false | Yes |
size | size | The switch's size. | 'xs' | 's' | 'm' | 'l' | 'xl' | 'm' | Yes |
ssrHint | ssr-hint | Only 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. | boolean | false | — |
validationTarget | — | Override this to change where constraint validation popups are anchored. | undefined | HTMLElement | — | — |
validators | — | Validators 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[] | [] | — |
value | value | The value of the switch, submitted as a name/value pair with form data. | string | null | — | Yes |
Methods
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
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.
| Name | Description |
|---|---|
blur | Emitted when the control loses focus. |
change | Emitted when the control's checked state changes. |
cs-invalid | Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
focus | Emitted when the control gains focus. |
input | Emitted when the control receives input. |
CSS Custom Properties
Learn more about CSS custom properties.
| Name | Description | Default |
|---|---|---|
--height | The height of the switch. | — |
--thumb-size | The size of the thumb. | — |
--width | The width of the switch. | — |
CSS Parts
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
control | The control that houses the switch's thumb. | ::part(control) |
hint | The hint's wrapper. | ::part(hint) |
label | The switch's label. | ::part(label) |
switch | The component's outer wrapper. | ::part(switch) |
thumb | The switch's thumb. | ::part(thumb) |

