Skip to content

Switch ​

A Neo-Brutalism sliding switch built on reka-ui's Switch primitive, featuring a bold border, a fixed hard shadow, and a solid thumb, with v-model support.

Demo ​

Preview

滑动开关

状态标签与切换音效

自定义状态标签

尺寸矩阵 (sm / default / lg)

SM
DEFAULT
LG

原生表单

尚未读取表单

禁用状态

Installation ​

pnpm dlx brutx-vue@latest add switch

Usage ​

vue
<script setup>
import { ref } from 'vue'
import { Switch, Label } from 'brutx-ui-vue'

const enabled = ref(false)
</script>

<template>
    <div class="flex items-center gap-3">
        <Switch id="airplane-mode" v-model="enabled" />
        <Label for="airplane-mode">Airplane mode</Label>
    </div>
</template>

Use defaultValue to set the initial uncontrolled state at mount:

vue
<Switch :default-value="true" aria-label="Receive notifications" />

When upgrading, rename defaultChecked to defaultValue and remove shape. For a custom switch, use switchRootVariants({ variant, size }) for the track and call switchThumbVariants() for the thumb. The thumb inherits its dimensions from the track. Both functions are exported from brutx-ui-vue/switch.

Status Labels ​

Enable showLabels to display the current state inside the thumb. The built-in labels are I for on and O for off. Customize the text or symbols with checkedLabel and uncheckedLabel; omitted values use the locale's switch.checkedLabel and switch.uncheckedLabel text.

The thumb has limited space, so single characters such as “✓ / ×” are recommended. Place longer descriptions in an external Label. The label color is paired with the thumb background. Decorative text is hidden with aria-hidden; aria-checked conveys the accessible state, and ariaLabel sets the control's accessible name.

vue
<script setup>
import { ref } from 'vue'
import { Switch, Label } from 'brutx-ui-vue'

const power = ref(true)
const sync = ref(false)
</script>

<template>
    <div class="flex items-center gap-3">
        <Switch id="device-power" v-model="power" show-labels />
        <Label for="device-power">Main Equipment Power</Label>
    </div>
    <div class="flex items-center gap-3">
        <Switch id="auto-sync" v-model="sync" show-labels checked-label="✓" unchecked-label="×" />
        <Label for="auto-sync">Auto sync</Label>
    </div>
</template>

Switch Motion ​

The outer frame and hard shadow remain fixed during hover, press, and state changes. The thumb moves horizontally over 180ms with ease-brutal-snap, while the track changes color over 120ms. Keyboard focus indicators appear immediately.

When the system enables reduced motion (prefers-reduced-motion: reduce), the thumb position and track color change immediately.

Disabled State ​

vue
<template>
    <Switch disabled />
</template>

Custom Accessibility Label ​

Associate a visible name using id and a Label with for, or wrap the switch in a Label. Clicking the associated label toggles the switch. Use ariaLabelledby to reference another naming element with the highest priority, or ariaLabel to provide an explicit name. The locale fallback is used only without an explicit name or associated label.

vue
<script setup>
import { ref } from 'vue'
import { Switch, Label } from 'brutx-ui-vue'

const sync = ref(false)
</script>

<template>
    <div class="flex items-center gap-3">
        <Switch v-model="sync" aria-label="Auto-sync data" />
        <Label>Auto sync</Label>
    </div>
</template>

Native Forms ​

Set name to submit the enabled, checked switch as name/value with its owning native <form>. The default value is on. Off or disabled fields are omitted. Combine required with name to require the switch to be on for native validation.

vue
<script setup>
import { Button, Switch, Label } from 'brutx-ui-vue'
</script>

<template>
    <form>
        <Switch id="notifications" name="notifications" value="enabled" :default-value="true" required />
        <Label for="notifications">Receive notifications</Label>
        <Button type="submit">Submit</Button>
        <Button type="reset">Reset</Button>
    </form>
</template>

A native form reset restores an uncontrolled switch to its defaultValue at mount without playing the toggle sound. Controlled state remains determined by v-model; update the binding in the form's reset handler when needed. Cancelling the reset event preserves the current state.

API Reference ​

Switch — Props
NameTypeDefaultDescription
class
string | undefined
undefined
CSS classes added to the Switch primitive root element.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:14

packages/ui/src/components/switch/Switch.vue

modelValue
Nullable
boolean | null | undefined
undefined
The controlled switch state, bound with v-model; null is treated as off.
Details

Declared type

boolean | null | undefined

Literal values

  • false
  • null
  • true

Default value details

Resolved value
undefined

Source

Switch.vue:18

packages/ui/src/components/switch/Switch.vue

defaultValue
boolean | undefined
undefined
The initial uncontrolled state at mount, restored when the owning native form resets.
Details

Declared type

boolean | undefined

Literal values

  • false
  • true

Default value details

Resolved value
undefined

Source

Switch.vue:22

packages/ui/src/components/switch/Switch.vue

disabled
boolean | undefined
false
Disables switch interaction.
Details

Declared type

boolean | undefined

Literal values

  • false
  • true

Default value details

Resolved value
false

Source

Switch.vue:23

packages/ui/src/components/switch/Switch.vue

variant
"accent" | "danger" | "default" | "primary" | "secondary" | undefined
"default"
Sets the switch color and state style variant.
Details

Declared type

NonNullable<"default" | "primary" | "secondary" | "danger" | "accent" | null | undefined> | undefined

Literal values

  • "accent"
  • "danger"
  • "default"
  • "primary"
  • "secondary"

Default value details

Resolved value
"default"

Source

Switch.vue:24

packages/ui/src/components/switch/Switch.vue

size
"default" | "lg" | "sm" | undefined
"default"
Sets the size of the switch track and thumb.
Details

Declared type

NonNullable<"default" | "lg" | "sm" | null | undefined> | undefined

Literal values

  • "default"
  • "lg"
  • "sm"

Default value details

Resolved value
"default"

Source

Switch.vue:25

packages/ui/src/components/switch/Switch.vue

showLabels
boolean | undefined
false
Shows the current state label inside the thumb, configured with checkedLabel and uncheckedLabel; labels are decorative and hidden from the accessibility tree.
Details

Declared type

boolean | undefined

Literal values

  • false
  • true

Default value details

Resolved value
false

Source

Switch.vue:27

packages/ui/src/components/switch/Switch.vue

checkedLabel
string | undefined
undefined
Label for the on state, shown only when showLabels is true; falls back to the locale text, I by default. A single character is recommended.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:29

packages/ui/src/components/switch/Switch.vue

uncheckedLabel
string | undefined
undefined
Label for the off state, shown only when showLabels is true; falls back to the locale text, O by default. A single character is recommended.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:31

packages/ui/src/components/switch/Switch.vue

id
string | undefined
undefined
Control ID, linked to a visible name through a Label’s for attribute.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:33

packages/ui/src/components/switch/Switch.vue

name
string | undefined
undefined
Form field name, submitted with the owning native form when on and enabled.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:35

packages/ui/src/components/switch/Switch.vue

value
string | undefined
undefined
String value submitted when on; the primitive defaults to on when omitted.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:37

packages/ui/src/components/switch/Switch.vue

required
boolean | undefined
false
Requires the switch to be on for native form validation; also set name.
Details

Declared type

boolean | undefined

Literal values

  • false
  • true

Default value details

Resolved value
false

Source

Switch.vue:39

packages/ui/src/components/switch/Switch.vue

ariaLabelledby
string | undefined
undefined
ID of the naming element, used as the preferred accessible naming source.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:41

packages/ui/src/components/switch/Switch.vue

ariaLabel
string | undefined
undefined
Explicit accessible name; the locale default is used only without an explicit name or associated Label.
Details

Declared type

string | undefined

Default value details

Resolved value
undefined

Source

Switch.vue:43

packages/ui/src/components/switch/Switch.vue

sound
boolean | undefined
false
Explicitly enables the relay sound when the state changes; sound is off by default.
Details

Declared type

boolean | undefined

Literal values

  • false
  • true

Default value details

Resolved value
false

Source

Switch.vue:45

packages/ui/src/components/switch/Switch.vue

Switch — Events
NameSignature or typeDescription
@update:modelValue
[value: boolean]
Emits the new boolean after the user toggles the switch to synchronize v-model.
Details

Declared type

[value: boolean]

Literal values

  • false
  • true

Source

Switch.vue

packages/ui/src/components/switch/Switch.vue

Accessibility ​

  • Keyboard: Supports Space / Enter to toggle switch state
  • ARIA attributes: Automatically manages role="switch", aria-checked, and aria-required; supports a visible associated Label, ariaLabelledby, and an explicit ariaLabel
  • Focus management: Focusable via Tab key, uses --brutal-ring token for visible focus ring
  • Reduced motion: Respects prefers-reduced-motion by disabling thumb and track transitions
  • Label isolation: showLabels status labels carry aria-hidden="true"; aria-checked conveys the switch state

Brute force builds.