# Toggle

> A labelled toolbar button backed by a checkbox with a visible check mark in the active state.

- Kind: ui · Category: Actions · Interactivity: native
- Install: `npx shadcn@latest add @haze/toggle`
- File: `src/components/haze/ui/toggle.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/toggle

## Use when
- A filter, pin, or view option can stay on or off
- A compact toggle should submit as part of a form

## Avoid when
- A group allows exactly one choice. Use toggle-group.
- The control represents an immediate settings change. Consider switch.

## Alternatives
- [toggle-group](https://hazeui.com/ui/toggle-group.md)
- [switch](https://hazeui.com/ui/switch.md)
- [checkbox](https://hazeui.com/ui/checkbox.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique control ID. Use it to associate a label with the input. |
| name | `string` | id | no | Form field name. |
| label | `string` | "Pin view" | no | Visible and accessible label. |
| checked | `boolean` |  | no | Initial checked state. |
| disabled | `boolean` |  | no | Disables the toggle. |
| icon | `slot` |  | no | Optional decorative icon before the label. |
| ...attributes | `HTML input attributes` |  | no | Additional attributes for the native input. |
| class | `string` |  | no | Additional classes on the label. |

## Accessibility
- The visual button exposes its state as a native checked checkbox.
- Keep the label stable across states and mark optional icons decorative.
- Key Tab then Space: Focus the checkbox and change the checked state.

## Agent prompts

### Install and use

Install the Haze UI component "Toggle" with `npx shadcn@latest add @haze/toggle`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Toggle from "@/components/haze/ui/toggle.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/toggle.md

### Customize with real content

Adapt the Haze toggle ui for our product. Replace the demo values of these props with our real content: id, name, label, checked, disabled, icon, ...attributes, class. Keep the layout, the heading level rules and these accessibility details: The visual button exposes its state as a native checked checkbox. Keep the label stable across states and mark optional icons decorative. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using toggle, check it fits: use it when a filter, pin, or view option can stay on or off; a compact toggle should submit as part of a form. Avoid it when a group allows exactly one choice. use toggle-group.; the control represents an immediate settings change. consider switch.. Alternatives: @haze/toggle-group, @haze/switch, @haze/checkbox.

## Example requests that should lead here
- Add a persistent Pin filter toggle to a toolbar and include its value in the settings form.

## Source

```astro
---
import { Check } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  id: string;
  name?: string;
  label?: string;
  checked?: boolean;
  disabled?: boolean;
  class?: string;
  [key: string]: unknown;
}

const { id, name = id, label = "Pin view", checked, disabled, class: className, ...rest } = Astro.props;
---

<label for={id} class={cn("relative inline-flex min-w-0 items-center gap-2", className)}>
  <input {...rest} id={id} name={name} type="checkbox" checked={checked} disabled={disabled} aria-label={label}
    class="peer sr-only forced-colors:not-sr-only" />
  <span class={cn(
    "inline-flex min-h-10 min-w-10 cursor-pointer items-center justify-center gap-2 rounded-lg border border-frame bg-field px-3 py-1.5 text-[0.8125rem]/5 font-medium text-muted-foreground select-none",
    "transition-[background-color,border-color,color,box-shadow,scale] duration-150 ease-snappy hover:bg-accent hover:text-foreground hover:shadow-key active:scale-[0.97] active:duration-100 motion-reduce:active:scale-100",
    "peer-checked:border-border-strong peer-checked:bg-accent peer-checked:text-foreground peer-checked:[&>[data-indicator]]:visible",
    "peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-ring peer-disabled:cursor-not-allowed peer-disabled:opacity-50 peer-disabled:hover:bg-card peer-disabled:hover:shadow-none [&_svg]:size-4 [&_svg]:shrink-0",
  )}>
    <slot name="icon" />
    {label}
    <Check data-indicator aria-hidden="true" class="invisible text-foreground" />
  </span>
</label>
```
