Skip to content

Switch

A control that indicates whether a setting is on or off.

Usage guidelines

  • Form controls must have an accessible name: It can be created using a <label> element or the Field component. See Labeling a switch and the forms guide.

Anatomy

Import the component and assemble its parts:

vue
<script setup lang="ts">
import { SwitchRoot, SwitchThumb } from 'base-ui-vue'
</script>

<template>
  <SwitchRoot>
    <SwitchThumb />
  </SwitchRoot>
</template>

Examples

Labeling a switch

An enclosing <label> is the simplest labeling pattern:

vue
<label>
  <SwitchRoot />
  Notifications
</label>

Rendering as a native button

By default, SwitchRoot renders a <span> element to support enclosing labels. Prefer rendering the switch as a native button when using sibling labels (for/id).

vue
<div>
  <label for="notifications-switch">Notifications</label>
  <SwitchRoot
    id="notifications-switch"
    as="button"
    :native-button="true"
  >
    <SwitchThumb />
  </SwitchRoot>
</div>

Native buttons with wrapping labels are supported by using renderless mode so the hidden input is placed outside the label:

vue
<script setup lang="ts">
import { Slot, SwitchRoot, SwitchThumb } from 'base-ui-vue'
</script>

<template>
  <SwitchRoot
    v-slot="{ props, ref }"
    :as="Slot"
    :native-button="true"
  >
    <label>
      <button :ref="ref" v-bind="props">
        <SwitchThumb />
      </button>
      Notifications
    </label>
  </SwitchRoot>
</template>

Form integration

Use Field to handle label associations and form integration:

vue
<script setup lang="ts">
import {
  FieldLabel,
  FieldRoot,
  Form,
  SwitchRoot,
  SwitchThumb,
} from 'base-ui-vue'
</script>

<template>
  <Form>
    <FieldRoot name="notifications">
      <FieldLabel>
        <SwitchRoot>
          <SwitchThumb />
        </SwitchRoot>
        Notifications
      </FieldLabel>
    </FieldRoot>
  </Form>
</template>

API reference

Root

PropTypeDefaultDescription
asstring | Component'span'The element or component used for the switch root.
checkedbooleanundefinedWhether the switch is currently active. To render an uncontrolled switch, use the default-checked prop instead.
default-checkedbooleanfalseWhether the switch is initially active. To render a controlled switch, use the checked prop instead.
disabledbooleanfalseWhether the component should ignore user interaction.
read-onlybooleanfalseWhether the user should be unable to activate or deactivate the switch.
requiredbooleanfalseWhether the user must activate the switch before submitting a form.
namestringundefinedIdentifies the field when a form is submitted.
formstringundefinedIdentifies the form that owns the hidden input. Useful when the switch is rendered outside the form.
valuestringundefinedThe value submitted with the form when the switch is on. By default, the switch submits "on", matching native checkbox behavior.
unchecked-valuestringundefinedThe value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior.
input-refanyundefinedA ref or callback to access the hidden <input> element.
native-buttonbooleanfalseWhether the component renders a native <button> element when replacing it via the as prop. Set to true if the rendered element is a native button.
classany | ((state: State) => any)undefinedCSS class applied to the element, or a function that returns a class based on the component's state.
styleStyleValue | ((state: State) => StyleValue)undefinedStyle applied to the element, or a function that returns a style object based on the component's state.
EmitsTypeDescription
checked-change(checked: boolean, details: EventDetails) => voidEmitted when the switch is activated or deactivated.
AttributeDescription
data-checkedPresent when the switch is checked.
data-uncheckedPresent when the switch is unchecked.
data-disabledPresent when the switch is disabled.
data-readonlyPresent when the switch is read-only.
data-requiredPresent when the switch is required.
data-validPresent when the switch is valid.
data-invalidPresent when the switch is invalid.
data-dirtyPresent when the switch's value has changed.
data-touchedPresent when the switch has been touched.
data-filledPresent when the switch is active.
data-focusedPresent when the switch is focused.

Thumb

PropTypeDefaultDescription
asstring | Component'span'The element or component used for the switch thumb.
classany | ((state: State) => any)undefinedCSS class applied to the element, or a function that returns a class based on the component's state.
styleStyleValue | ((state: State) => StyleValue)undefinedStyle applied to the element, or a function that returns a style object based on the component's state.
AttributeDescription
data-checkedPresent when the switch is checked.
data-uncheckedPresent when the switch is unchecked.
data-disabledPresent when the switch is disabled.
data-readonlyPresent when the switch is read-only.
data-requiredPresent when the switch is required.
data-validPresent when the switch is valid.
data-invalidPresent when the switch is invalid.
data-dirtyPresent when the switch's value has changed.
data-touchedPresent when the switch has been touched.
data-filledPresent when the switch is active.
data-focusedPresent when the switch is focused.