Toggle

new

A two-state button with Button's variants, colours and sizes, and its own look when on.

Demo

Open Props in the corner of the example to try variant, activeVariant, color, size and disabled. variant and color style the toggle while it is off, activeVariant and activeColor while it is on, from Button's variants and colours; by default a toggle is transparent and neutral, and fills softly when on. size takes Button's sizes, the icon-* ones for a lone icon.

Installation

Usage

<script setup lang="ts">
import { Bold } from '@lucide/vue'

import { Toggle } from '@/components/ui/toggle'

const bold = ref(false)
</script>

<template>
  <Toggle v-model="bold" size="icon-md" aria-label="Bold">
    <Bold />
  </Toggle>
</template>

Toggle is Reka UI's toggle styled like a Button. It is a button with aria-pressed, and v-model binds whether it is on. For a set of toggles, use Toggle Group.

Examples

Active look

A toggle can change its look when it's on. Any pair works: an outline toggle that turns solid primary, or soft warning for a favourite.

Using a custom colour as activeColor? Add a second selector to its rule so it also applies when the toggle is on:

@layer base {
  [data-slot][data-color='pro'],
  [data-slot][aria-pressed='true'][data-active-color='pro'] {
    --tone: var(--pro);
    /* … */
  }
}

With text

With text

API Reference

class is merged over the toggle's classes with tailwind-merge.

Data attributes

data-slotAttributesDescription
toggledata-state, data-variant, data-color, data-active-color, data-size, data-disabledThe button. data-state is on or off; while it is on, data-active-color takes over the colour.

Changelog

  1. registry 0.22.0

    4712efa — A Kbd inside a Button wrapped in TooltipTrigger as-child (or any other as-child trigger) keeps the button's colours, and an InputGroupButton wrapped the same way in a block addon keeps the addon's inset. The trigger's data-slot replaces the child's own, as in shadcn, so Kbd now keys on the <button> element (any button tints it, Toggle and Toolbar items included) and the block addon sets the inset on its children.

    ac9afad — An aria-disabled Toggle, ToggleGroupItem or ToolbarToggleItem ignores click, Enter and Space like an aria-disabled Button, so a toolbar toggle kept focusable for its tooltip no longer changes the model. swallowWhenDisabled moves to @/ui/button, where the three parts share it.

    ea2717b — In a ToggleGroup (or a ButtonGroup of toggles) whose activeVariant is outline or subtle, pressed neighbours share one edge instead of drawing two side by side: Toggle and ToggleGroupItem carry data-active-variant, and the group overlaps them by that edge as it does for an outline variant.

  2. registry 0.21.0

    5ada64b — A pressed Toggle or ToggleGroup item keeps its on look inside TooltipTrigger as-child (or any wrapper that writes its own data-state). The on styles read aria-pressed, which Reka sets on every toggle, instead of data-state="on", which the tooltip overwrote with closed; core's active-colour rules match both, so a custom [data-state="on"][data-active-color] rule keeps working.

  3. core 0.15.1

    5ada64b — A pressed Toggle or ToggleGroup item keeps its on look inside TooltipTrigger as-child (or any wrapper that writes its own data-state). The on styles read aria-pressed, which Reka sets on every toggle, instead of data-state="on", which the tooltip overwrote with closed; core's active-colour rules match both, so a custom [data-state="on"][data-active-color] rule keeps working.

  4. registry 0.15.0

    d09f26a — Pressable parts ripple on their own: Button, Toggle, ToggleGroupItem, Item as a link or a button, the calendar days, tabs, accordion, menubar and navigation menu triggers, the items of Menu, Menubar, Select, Combobox, Command, DrawerMenu, Listbox and Tree, and the clear buttons of TagsInput and Combobox. Each imports the directive from @/lib/ripple, so add brings the ripple item along and nothing needs registering. On menu, listbox and tree rows the wave replaces the pressed layer, as state-layer already did. Bind v-ripple="false" on a Button to turn one off, or set --kappa-ripple: none on :root to turn them all off.

  5. registry 0.13.0

    93e57a0 — Every size on the control scale is named xs to xl; default and the bare icon are gone. To move over:

    - Button: size="default" becomes size="md", or leave size out; size="icon" becomes size="icon-md". The full set is xs to xl and icon-xs to icon-xl. Toggle, ToggleGroup, ToggleGroupItem, the Toolbar parts, AlertDialogAction, AlertDialogCancel and ToastAction take the same names; Button, Toggle and ToggleGroup default to md, the Toolbar parts keep sm and ToastAction keeps xs. InputGroupButton keeps its own xs, sm, icon-xs and icon-sm, 24 and 32px to fit inside the group, xs by default. Pagination hands its md to Button as it is. Button, Toggle and ToggleGroupItem set data-variant, data-color and data-size even when you leave them at their defaults. - AlertDialogContent: size="default" becomes size="md", still the default; sm is unchanged. The size option of useConfirm's confirm, alert and prompt takes the same names. - Table and DataTable: density becomes size, data-density becomes data-size, and the TableDensity type is now TableSize. sm, md and lg keep their 36, 44 and 52px rows; xs (28px) and xl (60px) are new, and DataTable's virtual row estimates cover them. The cells' padding above and below the text is now --table-cell-py on the table: 8px as before, 3px at xs. At xs, DataTable's sort buttons and expand and group toggles shrink to fit the row. The table-density example is now table-sizes. - DrawerMenu: size takes xs to xl like Menu and reads the control scale, plus room for a thumb. sm, md and lg keep their 40, 48 and 56px rows, padding and icons; xs is 36px and xl 64px. Overriding a --control-* token on :root now resizes DrawerMenu rows as well. - Select and Combobox: the list opens at the trigger's or anchor's size instead of always at md, so <SelectTrigger size="xl"> opens an xl list. size on SelectContent or ComboboxList picks another. Behind an as-child button, set size on ComboboxAnchor to match the button. The Select list now sets its menu variables on SelectContent instead of its viewport, so a class such as [--menu-item-height:2.5rem] on SelectContent reaches the items. - Input, Textarea, InputFloating, SelectTrigger, ComboboxAnchor and Separator set data-size with the default filled in, and the anchor sets data-variant too. With as-child, the anchor leaves the button's own data-variant and data-size alone.

  6. registry 0.12.0

    c79028b — The control scale is now a set of tokens: --control-height-*, --control-padding-*, --control-icon-* and --control-gap-* for xs to xl, added to :root by the tokens item. Button, Input, InputFloating, Select, Combobox, InputNumber, TagsInput, PinInput, InputTime, InputGroup, Menu, Tabs, ColorPicker and Pagination read them, so overriding one on :root resizes every control of that size.

    Some sizes that strayed from the scale now follow it. Button icons are 20px at lg and xl instead of 16px, and so are the icons of Toggle, ToggleGroup, Toolbar and Pagination at those sizes. The gap between an icon and its label is 6, 8, 8, 10 and 12px from xs to xl, about half the icon, everywhere that has one: Button and the components built on it (where it was 4, 6, 8, 8 and 8px), Menu items and the Select, Combobox and Command lists (8, 10, 12, 12 and 12px), and Tabs triggers (6, 8, 8, 8 and 10px). Inset and checkbox or radio items move their labels with it, so they stay in line with items that have an icon. Everything else keeps its size.

  7. registry 0.6.0

    715f6fc — Components are coloured by tones. A colour is three inputs, --tone, --tone-foreground and --tone-text, set on [data-slot][data-color="<name>"]; core derives --tone-soft, --tone-soft-foreground, --tone-border and --tone-border-subtle from them and registers all seven as tone-* colours, so bg-tone, text-tone-text and the rest work as classes. The --c-* variables are gone: a rule that set --c, --c-fg, --c-soft, --c-soft-fg or --c-edge now sets --tone, --tone-foreground and --tone-text, and drops the rest. Button, Badge, Alert, Toggle, the choice controls and Toast read tones; Toast no longer exports toastAccents.

  8. core 0.5.0

    715f6fc — Components are coloured by tones. A colour is three inputs, --tone, --tone-foreground and --tone-text, set on [data-slot][data-color="<name>"]; core derives --tone-soft, --tone-soft-foreground, --tone-border and --tone-border-subtle from them and registers all seven as tone-* colours, so bg-tone, text-tone-text and the rest work as classes. The --c-* variables are gone: a rule that set --c, --c-fg, --c-soft, --c-soft-fg or --c-edge now sets --tone, --tone-foreground and --tone-text, and drops the rest. Button, Badge, Alert, Toggle, the choice controls and Toast read tones; Toast no longer exports toastAccents.

  9. registry 0.5.0

    d290041 — Add Toggle and ToggleGroup. Toggle is a two-state button with Button's variants, colours and sizes, styled when on by activeVariant and activeColor; ToggleGroup joins toggles like a ButtonGroup, with single or multiple selection and arrow-key navigation. The core colour rules also match [data-state="on"][data-active-color], so an on toggle reads its active colour.

  10. core 0.4.0

    d290041 — Add Toggle and ToggleGroup. Toggle is a two-state button with Button's variants, colours and sizes, styled when on by activeVariant and activeColor; ToggleGroup joins toggles like a ButtonGroup, with single or multiple selection and arrow-key navigation. The core colour rules also match [data-state="on"][data-active-color], so an on toggle reads its active colour.