Switch provides "on" and "off" choices and can be useful for opt-in and additive actions.

Overview

Switches should be used for boolean and "on" and "off" choices. They should not be for binary choices such as "blue or red", "sky or ground", "miles or kilometers", "large or small", etc.

For "yes" and "no" interactions, multiple selections, and interactions where a choice is optional, please consider Checkbox.

For single choice interactions, please consider Radio Button or Segmented Control.

Sample

Usage

  • Dark mode switch
  • Layer visibility

Component comparison

Switches, Checkboxes, and Segmented Controls are similar, but each component is designed for a different purpose.

SwitchThis component
Best for toggling a setting "on" or "off".
Allows only one option to be selected.
CheckboxAlternative
Best for "yes" or "no" decisions.
Allows a choice to be selected or left unselected.
Segmented ControlAlternative
Best for choosing one option from a group of two or more related options.
Allows only one option to be selected.

Best practices

Below are important guidelines on using the Switch component.

Correct Switch decision
Do use Switches to indicate a boolean or reciprocal relationship.
Avoid Switch decision
Avoid using Switches to indicate a choice between similar options. Consider using Segmented Control instead.

Accessibility

Switch should include a label property value, or be accompanied by a Label, which provides context of the intent to support assistive technologies.

Keyboard navigation

KeyFunction
TabMoves focus in or out of component.
Tab and ShiftMove focus in or out of component.
EnterToggles the component.
SpaceToggles the component.

Writing and copy

Keep labels for Switches succinct. Do not use contractions such as "you're", "aren't", "can't", and "haven't" for Switch labels in order to reduce confusion. Avoid writing Switch text as a question.

A label for a Switch should be supporting "On" as additive, not subtractive. For example:

  • Show layer
  • Enable editing
  • Allow credit usage
  • Display controls
  • Update automatically

API reference

Properties

PropertyAttributeDescriptionTypeDefault
checkedchecked

When true, the component is checked.

booleanfalse
disableddisabled

When true, interaction is prevented and the component is displayed with lower opacity.

booleanfalse
formform

Specifies the id of the component's associated form.

When not set, the component is associated with its ancestor form element, if one exists.

string
labellabel

Specifies an accessible label for the component.

string
labelTextEndlabel-text-end

Specifies the component's end label text.

string
labelTextStartlabel-text-start

Specifies the component's start label text.

string
namename

Specifies the name of the component. Required to pass the component's value on form submission.

string
scalescale

Specifies the component's size.

Scale"m"
valuevalue

The component's value.

any

Styles

NameDescription
--calcite-switch-background-color

Specifies the component's background color.

--calcite-switch-background-color-hover

Specifies the component's background color when hovered or pressed.

--calcite-switch-border-color
deprecated in v3.2.0, removal target v6.0.0 - No longer necessary. Specifies the component's border color.
--calcite-switch-corner-radius

Specifies the component's corner radius.

--calcite-switch-handle-background-color

Specifies the handle's background color.

--calcite-switch-handle-border-color
deprecated in v3.2.0, removal target v6.0.0 - No longer necessary. Specifies the handle's border color.
--calcite-switch-handle-shadow
deprecated in v3.2.0, removal target v6.0.0 - No longer necessary. Specifies the handle's shadow.

Events

NameDescriptionBehavior
calciteSwitchChange

Fires when the checked value has changed.

bubblescomposed

Methods

NameDescriptionSignature
componentOnReady

Creates a promise that resolves once the component is fully loaded.

componentOnReady(): Promise<this>
setFocus

Sets focus on the component.

setFocus(options?: FocusOptions): Promise<void>

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.