Toggle

ToggleGitHub
A button that can be pressed on or off.

Usage

Use the v-model directive to control the pressed state of the Toggle.

<script setup lang="ts">
const value = ref(true)
</script>

<template>
  <PToggle v-model="value" icon="i-lucide-bold" aria-label="Toggle bold" />
</template>
<script setup lang="ts">
import { ref } from 'vue'

const value = ref(true)
</script>

<template>
  <PToggle v-model="value" icon="i-lucide-bold" aria-label="Toggle bold" />
</template>

Use the default-value prop to set the initial value when you do not need to control its state.

<template>
  <PToggle default-value icon="i-lucide-bold" aria-label="Toggle bold" />
</template>
The Toggle component extends the Button component, so you can pass its properties such as label, icon, size, etc., except the link ones. It sets the active prop of the Button from its pressed state.

Color

Use the color and active-color props to set the color of the Toggle when not pressed and when pressed. Defaults to neutral and primary.

<template>
  <PToggle color="neutral" active-color="neutral" default-value label="Bold" />
</template>

Variant

Use the variant and active-variant props to set the variant of the Toggle when not pressed and when pressed. Defaults to ghost and soft.

<template>
  <PToggle variant="outline" active-variant="subtle" default-value label="Bold" />
</template>

Disabled

Use the disabled prop to disable the Toggle.

<template>
  <PToggle disabled label="Bold" />
</template>

Examples

Style the pressed state

The Toggle renders data-state="on" when pressed, so you can style it with the data-[state=on] variant.

<template>
  <PToggle label="Bold" class="data-[state=on]:ring data-[state=on]:ring-primary" />
</template>

API

Props

Prop Default Type
as
'button'
any

The element or component this component should render as when not a link.

id
string
color
'neutral'
"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"
variant
'ghost'
"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"
activeColor
'primary'
"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"

The color of the toggle when pressed.

activeVariant
'soft'
"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

The variant of the toggle when pressed.

autofocus
false | true | "true" | "false"
disabled
boolean
name
string
label
string
size
'md'
"xs" | "sm" | "md" | "lg" | "xl"
square
boolean

Render the button with equal padding on all sides.

block
boolean

Render the button full width.

icon
any

Display an icon based on the leading and trailing props.

avatar
AvatarProps

Display an avatar on the left side.

leading
boolean

When true, the icon will be displayed on the left side.

leadingIcon
any

Display an icon on the left side.

trailing
boolean

When true, the icon will be displayed on the right side.

trailingIcon
any

Display an icon on the right side.

loading
boolean

When true, the loading icon will be displayed.

loadingIcon
appConfig.ui.icons.loading
any

The icon when the loading prop is true.

defaultValue
boolean

The pressed state of the toggle when it is initially rendered. Use when you do not need to control its open state.

modelValue
boolean | null

The controlled pressed state of the toggle. Can be bind as v-model.

required
boolean

When true, indicates that the user must set the value before the owning form can be submitted.

ui
{ base?: SlotClass; label?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; trailingIcon?: SlotClass; }

Slots

Slot Type
leading
{ ui: object; }
default
{ ui: object; }
trailing
{ ui: object; }

Emits

Event Type
change
[event: Event]
update:modelValue
[value: boolean]

Changelog

No recent changes