--- templateEngineOverride: njk layout: layouts/page.njk title: Switch description: A control that allows the user to toggle between checked and not checked. toc: - label: Usage id: usage - label: Checked state id: checked-state - label: Invalid state id: invalid-state - label: Examples id: examples children: - label: Basic id: example-basic - label: Description id: example-description - label: Choice Card id: example-choice-card - label: Disabled id: example-disabled - label: Invalid id: example-invalid - label: Size id: example-size - label: RTL id: example-rtl --- {% from "macros/code_preview.njk" import code_preview %} {% from "macros/code_block.njk" import code_block %} {% set code %}
Add role="switch" and the input class to a native <input type="checkbox">. Use a native <label> or aria-label for accessible naming.
Use native HTML state. Add checked for an initially enabled switch, or read and update the element's checked property in JavaScript.
Set aria-invalid="true" on the switch and, when using field, set data-invalid="true" on the field wrapper.
Choose whether marketing emails are enabled.
Receive emails about new products, features, and more.
Wrap a .field in a native <label> when the whole card should toggle the switch.
Choose whether marketing emails are enabled.
Switch field layout uses document direction. Set dir="rtl" on a parent to render right-to-left.