Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Toggle for Vue UI SDK

A button that stays pressed until it's pressed again, for Vue on UI SDK: bold or bookmark in a toolbar, in two variants and three sizes.

On this page9 sections

A toggle is a button that stays pressed after a click and comes back up on the next one, like bold in a text editor or the bookmark on an article. The button's own look is the state, so it suits a mode that applies to whatever is in front of you.

For a setting that turns a feature on or off, use a Switch. For a row of toggles that work together, such as text alignment, use a Toggle Group.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Toggle anatomy: root, indicator

Usage

<script setup lang="ts">
import { BookmarkIcon } from "lucide-vue-next";
import { Toggle } from "@/components/ui/toggle";
</script>
<template>
  <Toggle size="sm" variant="outline">
    <BookmarkIcon
      aria-hidden="true"
      class="group-aria-pressed/toggle:fill-foreground"
    />
    Bookmark
  </Toggle>
</template>

The icon's class fills it while the toggle is pressed: group-aria-pressed/toggle reads the aria-pressed attribute that every version of the button carries.

Accessibility

Toggle renders a <button> with aria-pressed, so a screen reader announces a toggle button that is pressed or not pressed, and Space or Enter flips it. The name is yours to get right. A toggle with visible text is named by that text, and an aria-label replaces it for screen readers, so leave the label off or start it with the visible words. An icon-only toggle needs an aria-label that names the action, such as Bold, and keeps it whether pressed or not.

Space
Toggle the pressed state.
Enter
Toggle the pressed state.

Demos

Outline

Italic and Bold, each an icon with a word, with variant set to outline so the resting button has a border. They are separate toggles, so both can be pressed at once, the way italic and bold combine on one word. Use the outline on a plain surface where a borderless button would read as text.

Preview width

Text

An Italic toggle with its icon and the word, on the default variant, which has no border and takes a muted background once pressed. A word beside the icon helps when the glyph alone could be misread, and it names the button for screen readers too, so the toggle needs no aria-label.

Preview width

Icon only

A bold toggle with nothing but its icon, on the default variant, the button most editor toolbars are built from. With no visible word, aria-label set to Bold names the action, and it stays the same whether the toggle is pressed or not. Use it for actions people know by their glyph, and add the word when they might not.

Preview width

Sizes

Small, Default and Large outline toggles, set with the size values sm, md and lg. md is what you get when you leave the prop out, and default is accepted as another name for it, as in shadcn/ui. Use the small one in a dense editor toolbar and the large one where it's a main touch target.

Preview width

Disabled

Two toggles that can't be pressed, one on the default variant and one outlined, both with disabled. They fade to half opacity and ignore clicks, and the outlined one keeps its border. Disable a toggle when its action doesn't apply yet, such as bold with nothing selected, and keep it in place so the toolbar doesn't shift.

Preview width

RTL

The bookmark toggle with an Arabic label and dir set to rtl. The icon sits on the right, before the word in reading order, and still fills when the toggle is pressed. Use it to check that an icon and its label come in the right order in a right-to-left toolbar.

Preview width

FAQ

What is the difference between a Toggle and a Switch?

A toggle is a button that stays pressed, like bold in an editor toolbar, and a screen reader announces it as a pressed button. A Switch turns a setting on or off, like Wi-Fi, and is announced as on or off.

Pick by what the control changes. A toggle acts on the content in front of you, and a switch changes a preference that outlives the page. A Toggle Group is a row of related toggles, and a Checkbox belongs in a form.

How do I control a Toggle in Vue?

Bind v-model:pressed to a ref, or pass pressed and update it in @pressed-change, which receives the new boolean first and the change's details second. Calling details.cancel() there keeps the current state. Use defaultPressed instead when the toggle can keep its own state.

Control it when the state belongs to something else, like a bold button that should show whether the selected text is already bold.

How do I make an outline Toggle?

Set variant to outline, as the Outline demo does, for a toggle with a border at rest. The default variant has no border and a transparent background, and ghost is accepted as another name for it.

Both variants show the pressed state the same way, with a muted background, so the outline is only about how the toggle looks before it's pressed.

How do I change the size of a Toggle?

Set size to sm, md or lg, as the Sizes demo does. md is the default, and default is accepted as another name for it, so code copied from shadcn/ui keeps working.

The heights and paddings are classes in components/ui/toggle, under the size key of toggleVariants, so a new size is one more entry under that key.

How do I make an icon-only Toggle accessible?

Give it an aria-label that names the action, such as Bold or Bookmark, as the Icon only demo does, and mark the icon aria-hidden="true" so it isn't read as well. A screen reader then announces the name, the button and whether it's pressed.

Keep the label the same in both states. Switching it to Unbold when pressed makes a screen reader announce Unbold, pressed, which reads as a contradiction.

How do I show a different icon when a Toggle is pressed?

Put both icons in the toggle and switch them with classes keyed on aria-pressed: group-aria-pressed/toggle:hidden on the unpressed one, and hidden group-aria-pressed/toggle:block on the pressed one. ToggleIndicator in this version is a plain <span> and doesn't read the state.

For a single icon that only changes its fill, as the bookmark does in the first demo, one class keyed on aria-pressed is enough.

How do I style the pressed state of a Toggle?

Key the classes on aria-pressed, which every version of the button carries: aria-pressed:bg-accent on the toggle, or group-aria-pressed/toggle: on something inside it. To change every toggle at once, edit toggleVariants in components/ui/toggle.

UI SDK also adds data-pressed while it's pressed.

API Reference

Toggle

<button> data-slot="toggle"

PropType
pressed?boolean

Whether it's pressed. Pass it to control the toggle. Inside a group, the group's value decides. Two-way with v-model or v-model:pressed.

defaultPressed?boolean

Whether it's pressed at first, when uncontrolled.

Default
false
controller?{ isPressed: () → boolean; setPressed: (pressed: boolean, options?: ToggleChangeOptions) → boolean; toggle: (options?: ToggleChangeOptions) → boolean; focus: () → void; }

Presses it from anywhere. Create one with createToggleController().

Type
{ isPressed: () → boolean; setPressed: (pressed: boolean, options?: ToggleChangeOptions) → boolean; toggle: (options?: ToggleChangeOptions) → boolean; focus: () → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. Alone, the toggle is natively disabled, unless focusableWhenDisabled is set. Inside a group or a toolbar, it stays focusable with aria-disabled, so arrow keys still reach it.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled toggle in the tab order with aria-disabled instead of the native attribute. Toggles in a group or a toolbar always are.

Default
false
size?"sm" | "md" | "default" | "lg"

The size.

Type
"sm" | "md" | "default" | "lg"
Default
"md"
value?string

Identifies the toggle inside a group, whose value lists the pressed toggles' values. Defaults to a generated id.

variant?"default" | "ghost" | "outline"

The visual style.

Type
"default" | "ghost" | "outline"
Default
"default"
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@pressed-change(pressed: boolean, details: ChangeDetails<TogglePressedChangeReason>) → void

Runs when a press wants to change it. details.reason says why, and details.cancel() stops it. Inside a group, it runs before the group's onValueChange, and canceling it cancels the group's change too.

Type
(pressed: boolean, details: ChangeDetails<TogglePressedChangeReason>) → void
Parameters

pressed – boolean

details – ChangeDetails<TogglePressedChangeReason>

Returns
void
AttributeType
data-disabled?string

Present when disabled, by its own prop or its group.

data-has-icon?string

Present while the toggle holds an icon part.

data-orientation"horizontal" | "vertical"

Inside a group, the group's orientation.

Type
"horizontal" | "vertical"
data-pressed?string

Present while pressed.

ToggleIndicator

<span> data-slot="toggle-indicator"

PropType
class?string

Merged with the part's classes.