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/toggleComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/toggleCopy the files
src/components/ui/toggle/index.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
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
---
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
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
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.