Toggle for Astro
Astro pressed-state buttons for local on/off actions.
Installation
bunx --bun shadcn@latest add @gnostic/togglenpx shadcn@latest add @gnostic/togglepnpm dlx shadcn@latest add @gnostic/toggleInstall component packages
bun add @data-slot/togglenpm install @data-slot/togglepnpm add @data-slot/toggleCopy the files
src/components/ui/toggle/index.tssrc/components/ui/toggle/toggle-variants.tssrc/components/ui/toggle/toggle.astrosrc/components/ui/toggle/types.ts
Usage
---
import { Toggle } from "@/components/ui/toggle"
---
<Toggle>Toggle</Toggle>
Examples
Sizes and states
API Reference
ToggleProps
defaultPressed?boolean
Initial pressed state
booleansize?"default" | "lg" | "sm" | null | undefined
"default" | "lg" | "sm" | null | undefinedvariant?"default" | "outline" | null | undefined
"default" | "outline" | null | undefinedSee the data-slot docs 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 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.