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
滑动开关
状态标签与切换音效
自定义状态标签
尺寸矩阵 (sm / default / lg)
原生表单
禁用状态
Installation
pnpm dlx brutx-vue@latest add switchUsage
<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:
<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.
<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
<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.
<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.
<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
| Name | Type | Default | Description |
|---|---|---|---|
class | string | undefined | undefined | CSS classes added to the Switch primitive root element. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
modelValueNullable | boolean | null | undefined | undefined | The controlled switch state, bound with v-model; null is treated as off. |
DetailsDeclared typeboolean | null | undefined Literal values
Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
defaultValue | boolean | undefined | undefined | The initial uncontrolled state at mount, restored when the owning native form resets. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
disabled | boolean | undefined | false | Disables switch interaction. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
variant | "accent" | "danger" | "default" | "primary" | "secondary" | undefined | "default" | Sets the switch color and state style variant. |
DetailsDeclared typeNonNullable<"default" | "primary" | "secondary" | "danger" | "accent" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
size | "default" | "lg" | "sm" | undefined | "default" | Sets the size of the switch track and thumb. |
DetailsDeclared typeNonNullable<"default" | "lg" | "sm" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/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. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/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. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/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. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
id | string | undefined | undefined | Control ID, linked to a visible name through a Label’s for attribute. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
name | string | undefined | undefined | Form field name, submitted with the owning native form when on and enabled. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
value | string | undefined | undefined | String value submitted when on; the primitive defaults to on when omitted. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
required | boolean | undefined | false | Requires the switch to be on for native form validation; also set name. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
ariaLabelledby | string | undefined | undefined | ID of the naming element, used as the preferred accessible naming source. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/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. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
sound | boolean | undefined | false | Explicitly enables the relay sound when the state changes; sound is off by default. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/switch/Switch.vue | |||
| Name | Signature or type | Description |
|---|---|---|
@update:modelValue | [value: boolean] | Emits the new boolean after the user toggles the switch to synchronize v-model. |
DetailsDeclared type[value: boolean] Literal values
Sourcepackages/ui/src/components/switch/Switch.vue | ||
Accessibility
- Keyboard: Supports
Space/Enterto toggle switch state - ARIA attributes: Automatically manages
role="switch",aria-checked, andaria-required; supports a visible associated Label,ariaLabelledby, and an explicitariaLabel - Focus management: Focusable via Tab key, uses
--brutal-ringtoken for visible focus ring - Reduced motion: Respects
prefers-reduced-motionby disabling thumb and track transitions - Label isolation:
showLabelsstatus labels carryaria-hidden="true";aria-checkedconveys the switch state