Skip to content

Toggle

Neobrutalist-styled toggle button built on reka-ui's Toggle primitive, with pressed state support.

Demo

Preview

默认变体

轮廓变体

尺寸

禁用

加载中

Installation

pnpm dlx brutx-vue@latest add toggle

Usage

Basic Usage

vue
<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>

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.

vue
<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.

vue
<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

VariantDescription
defaultWith background and small shadow, primary color background when pressed
outlineTransparent with border, secondary color background when pressed

Sizes

SizeHeightMin WidthFont Size
smh-8min-w-8text-xs
defaulth-10min-w-10text-sm
lgh-12min-w-12text-sm

Props

PropTypeDefaultDescription
modelValuebooleanBinding value, indicates whether pressed
variant'default' | 'outline''default'Color variant
size'sm' | 'default' | 'lg''default'Size
disabledbooleanfalseWhether disabled
loadingbooleanfalseWhether to show loading state
ariaLabelstringAccessibility label text
classstringCustom style class

Events

EventPayloadDescription
update:modelValuebooleanTriggered when toggle state changes

Accessibility

  • Keyboard: Supports Space / Enter to trigger toggle
  • ARIA attributes: Automatically manages aria-pressed state; sets aria-busy="true" during loading
  • Accessibility label: Toggle does not automatically generate a default aria-label; when slot content is icon-only, provide readable text via the ariaLabel prop

Brute force builds.