---
title: Toggle for Astro
description: "Astro pressed-state buttons for local on/off actions."
seo:
    title: Toggle for Astro | Shadcn & Tailwind CSS
related:
    - /components/toggle-group
    - /components/switch
    - /components/group
sidebar:
    label: "Toggle"
---

```astro
---
import BoldIcon from "@lucide/astro/icons/letter-b"

import { Toggle } from "@/components/ui/toggle"
---

<Toggle aria-label="Toggle bold">
  <BoldIcon />
</Toggle>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/toggle
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/toggle
```

#### src/components/ui/toggle/index.ts

```ts
export { default as Toggle } from './toggle.astro'
export { toggleVariants } from './toggle-variants'
export type { ToggleVariantProps } from './toggle-variants'
export type { ToggleProps } from './types.ts'
```

#### src/components/ui/toggle/toggle-variants.ts

```ts
import { cva, type VariantProps } from 'class-variance-authority'
import { cn } from 'cn'

/** Shared layout, type, and pressed styles for Toggle and Toggle Group items. */
export const toggleBaseClass = "text-foreground hover:bg-accent focus-visible:ring-ring focus-visible:ring-offset-background aria-pressed:bg-input/64 aria-pressed:text-accent-foreground relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 rounded-lg border text-base font-medium whitespace-nowrap transition-shadow outline-none select-none before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] focus-visible:ring-2 focus-visible:ring-offset-1 disabled:pointer-events-none disabled:opacity-64 sm:text-sm pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 [&_svg]:pointer-events-none [&_svg]:-mx-0.5 [&_svg]:shrink-0 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 group/toggle"

/** Height and padding for each Toggle size. */
export const toggleSizeClass = {
    default: "h-9 min-w-9 px-[calc(--spacing(2)-1px)] sm:h-8 sm:min-w-8",
    lg: "h-10 min-w-10 px-[calc(--spacing(2.5)-1px)] sm:h-9 sm:min-w-9",
    sm: "h-8 min-w-8 px-[calc(--spacing(1.5)-1px)] sm:h-7 sm:min-w-7",
} as const

/** Border and surface treatment for each Toggle variant. */
export const toggleVariantClass = {
    default: "border-transparent",
    outline: "border-input bg-background dark:bg-input/32 dark:aria-pressed:bg-input dark:hover:bg-input/64 shadow-xs/5 not-dark:bg-clip-padding not-disabled:not-active:not-aria-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:not-disabled:not-aria-pressed:before:shadow-[0_-1px_--theme(--color-white/2%)] dark:not-disabled:not-active:not-aria-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [:disabled,:active,[aria-pressed=true]]:shadow-none",
} as const

const toggleStyles = cva(toggleBaseClass, {
    defaultVariants: {
        size: 'default',
        variant: 'default',
    },
    variants: {
        size: toggleSizeClass,
        variant: toggleVariantClass,
    },
})

export const toggleVariants = (...args: Parameters<typeof toggleStyles>) =>
    cn(toggleStyles(...args))

export type ToggleVariantProps = VariantProps<typeof toggleVariants>
```

#### src/components/ui/toggle/toggle.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

import { toggleVariants } from "./toggle-variants"
import type { ToggleProps } from "./types.ts"

type Props = HTMLAttributes<"button"> & ToggleProps

const {
  class: className,
  variant = "default",
  size = "default",
  defaultPressed,
  disabled,
  type = "button",
  ...props
} = Astro.props
---

<button
  data-slot="toggle"
  data-size={size}
  data-variant={variant}
  data-default-pressed={defaultPressed}
  data-disabled={disabled}
  type={type}
  disabled={disabled}
  aria-pressed={defaultPressed === true ? "true" : "false"}
  class={cn(toggleVariants({ variant, size }), className)}
  {...props}
>
  <slot />
</button>

<script>
  import { create } from "@data-slot/toggle"
  import type { ToggleController } from "@data-slot/toggle"
  // Destroy bindings before a view transition so the next page-load binds once.
  const controllers: ToggleController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/toggle/types.ts

```ts
import type { ToggleOptions } from '@data-slot/toggle'

import type { ToggleVariantProps } from './toggle-variants.ts'

/**
 * Toggle initial state, visual variant, and size. Button attributes pass through.
 */
export type ToggleProps = Pick<ToggleOptions, 'defaultPressed'> &
    ToggleVariantProps
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```astro
---
import { Toggle } from "@/components/ui/toggle"
---
```

```astro
<Toggle>Toggle</Toggle>
```

## Examples

```astro
---
import ItalicIcon from "@lucide/astro/icons/italic"

import { Toggle } from "@/components/ui/toggle"
---

<Toggle variant="outline" aria-label="Toggle italic">
  <ItalicIcon />
</Toggle>
```

```astro
---
import BoldIcon from "@lucide/astro/icons/letter-b"
import ItalicIcon from "@lucide/astro/icons/italic"
import UnderlineIcon from "@lucide/astro/icons/underline"

import { Toggle } from "@/components/ui/toggle"
---

<div class="flex items-center gap-2">
  <Toggle size="sm" aria-label="Toggle bold">
    <BoldIcon />
  </Toggle>
  <Toggle size="default" aria-label="Toggle italic">
    <ItalicIcon />
  </Toggle>
  <Toggle size="lg" aria-label="Toggle underline">
    <UnderlineIcon />
  </Toggle>
</div>
```

```astro
---
import UnderlineIcon from "@lucide/astro/icons/underline"

import { Toggle } from "@/components/ui/toggle"
---

<Toggle disabled aria-label="Toggle underline">
  <UnderlineIcon />
</Toggle>
```

## Sizes and states

```astro
---
import { Toggle } from "@/components/ui/toggle"
---

<div class="grid gap-4">
  {(["sm", "default", "lg"] as const).map((size) => (
    <div class="flex flex-wrap gap-3">
      <Toggle size={size} aria-label={size + " grid lines"}>
        Grid lines
      </Toggle>
      <Toggle
        size={size}
        variant="outline"
        defaultPressed
        aria-label={size + " guides"}
      >
        Guides
      </Toggle>
      <Toggle size={size} disabled aria-label={size + " locked guides"}>
        Locked
      </Toggle>
    </div>
  ))}
</div>
```

## API Reference

### ToggleProps

<AutoTypeTable path="../../packages/ui/src/components/ui/toggle/types.ts" name="ToggleProps" />

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/toggle/README.md) for more information on interactivity.

### Events

Dispatch these events on the toggle root. The root emits the outbound events.

#### Outbound events

| Event           | Detail                 |
| --------------- | ---------------------- |
| `toggle:change` | `{ pressed: boolean }` |

#### Inbound events

| Event        | Detail               |
| ------------ | -------------------- |
| `toggle:set` | `{ value: boolean }` |

Read the [installed source](/r/toggle.json) for the complete prop types.

## When to use Toggle

Use Toggle for a mode or action that stays pressed, such as a formatting option. Keep the label stable so the pressed state carries the change. Use the existing size and outline variants for density and grouping.

## Keyboard and behavior

The data-slot controller updates `aria-pressed` and handles keyboard activation. A toggle does not save application data automatically. Use Toggle Group when adjacent toggles share a selection.

## Theme and layout

Pressed, hover, focus, and disabled styles use the current theme. Check an icon-only toggle with an accessible name. Color should reinforce the pressed state, not be its only indication.
