Toggle
Neobrutalist-styled toggle button built on reka-ui's Toggle primitive, with pressed state support.
Demo
默认变体
轮廓变体
尺寸
禁用
加载中
Installation
pnpm dlx brutx-vue@latest add toggleUsage
Basic Usage
<script setup>
import { ref } from 'vue'
import { Toggle } from 'brutx-ui-vue'
import { Bold, Italic, Underline } from '@lucide/vue'
const bold = ref(false)
const italic = ref(false)
const underline = ref(false)
</script>
<template>
<div class="flex items-center gap-2">
<Toggle v-model="bold" variant="default" size="default">
<Bold class="h-4 w-4" />
</Toggle>
<Toggle v-model="italic" variant="default" size="default">
<Italic class="h-4 w-4" />
</Toggle>
<Toggle v-model="underline" variant="default" size="default">
<Underline class="h-4 w-4" />
</Toggle>
</div>
</template>Native Form
When name is provided, a pressed Toggle submits the value true; an unpressed Toggle omits the field. required requires the Toggle to be pressed. Disabled and loading Toggles are excluded from submission and required validation. Uncontrolled mode starts unpressed (aria-pressed="false"). Use defaultValue to set the initial state.
<script setup>
import { Toggle } from 'brutx-ui-vue'
</script>
<template>
<form>
<Toggle name="bold" required ariaLabel="Bold">Bold</Toggle>
</form>
</template>Loading State
Display a loading indicator (Loader2 spinning icon) via the loading prop. The button is automatically disabled and sets aria-busy="true", suitable for async operation scenarios. During loading, the original slot content is replaced with the spinning icon.
<script setup>
import { Toggle } from 'brutx-ui-vue'
import { Bold } from '@lucide/vue'
</script>
<template>
<Toggle loading aria-label="Bold">
<Bold class="h-4 w-4" />
</Toggle>
</template>Accessibility Label
Toggle does not automatically generate a default aria-label. When slot content is icon-only, it is recommended to provide readable text via the ariaLabel prop so screen readers can correctly announce the button's purpose.
<script setup>
import { ref } from 'vue'
import { Toggle } from 'brutx-ui-vue'
import { Bold } from '@lucide/vue'
const bold = ref(false)
</script>
<template>
<Toggle v-model="bold" aria-label="Bold">
<Bold class="h-4 w-4" />
</Toggle>
</template>Variants
| Variant | Description |
|---|---|
default | With background and small shadow, primary color background when pressed |
outline | Transparent with border, secondary color background when pressed |
Sizes
| Size | Height | Min Width | Font Size |
|---|---|---|---|
sm | h-8 | min-w-8 | text-xs |
default | h-10 | min-w-10 | text-sm |
lg | h-12 | min-w-12 | text-sm |
required participates in native validation even without name; only enabled fields with a name are included in native form submission.
API Reference
| Name | Type | Default | Description |
|---|---|---|---|
modelValue | boolean | undefined | undefined | The current pressed state, controlled with v-model. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
defaultValue | boolean | undefined | false | The initial pressed state in uncontrolled mode. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
variant | "default" | "outline" | undefined | "default" | Sets the visual variant of the Toggle. |
DetailsDeclared typeNonNullable<"default" | "outline" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
size | "default" | "lg" | "sm" | undefined | "default" | Sets the size of the Toggle button. |
DetailsDeclared typeNonNullable<"default" | "lg" | "sm" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
disabled | boolean | undefined | false | Disables button interaction. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
loading | boolean | undefined | false | Shows a loading indicator and disables interaction; the loading state replaces default-slot content. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
class | string | undefined | undefined | CSS classes added to the Toggle root button. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
ariaLabel | string | undefined | undefined | Sets the Toggle’s accessible name; provide it for icon-only buttons. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
name | string | undefined | undefined | Native form field name; submits the value true while pressed and enabled. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
required | boolean | undefined | false | Requires the Toggle to be pressed; disabled or loading Toggles are excluded from form validation. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/toggle/Toggle.vue | |||
| Name | Signature or type | Description |
|---|---|---|
@update:modelValue | [value: boolean] | Emits the new boolean when the user changes the pressed state. |
DetailsDeclared type[value: boolean] Literal values
Sourcepackages/ui/src/components/toggle/Toggle.vue | ||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | Text, an icon, or other button content; replaced by the loading indicator while loading is true. |
DetailsDeclared type{}Sourcepackages/ui/src/components/toggle/Toggle.vue | ||
Accessibility
- Keyboard: Supports
Space/Enterto trigger toggle - ARIA attributes: Automatically manages
aria-pressedstate; setsaria-busy="true"duringloading - Accessibility label: Toggle does not automatically generate a default
aria-label; when slot content is icon-only, provide readable text via theariaLabelprop