# Action swap

> A real checkbox styled as one action that exchanges its label and icon when the state changes.

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

## Use when
- A follow or subscribe action has two lasting states
- A compact action needs to show the checked result in place

## Avoid when
- For a single asynchronous save with pending and error states, use stateful-button
- For several independent choices, use toggle-group

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

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique caller-supplied prefix for this component and its related elements. |
| name | `string` | id | no | Form submission name. |
| label | `string` | "Follow project" | no | Visible label before selection. |
| activeLabel | `string` | "Following" | no | Visible label while selected. |
| checked | `boolean` | false | no | Initial checkbox state. |
| disabled | `boolean` |  | no | Disables the native control and its enhancement. |
| class | `string` |  | no | Additional classes for the component root. |
| ...attributes | `native control attributes` |  | no | Other attributes are forwarded to the real form control. |

## Accessibility
- The visible label remains associated with the real checkbox as it changes.
- The checked value submits through native forms, including with scripts disabled; haze:action-swap reports enhanced changes.
- Key Tab, Space: Focus the checkbox and exchange its lasting state.

## Agent prompts

### Install and use

Install the Haze UI component "Action swap" with `npx shadcn@latest add @haze/action-swap`. 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 ActionSwap from "@/components/haze/ui/action-swap.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/action-swap.md

### Customize with real content

Adapt the Haze action-swap ui for our product. Replace the demo values of these props with our real content: id, name, label, activeLabel, checked, disabled, class, ...attributes. Keep the layout, the heading level rules and these accessibility details: The visible label remains associated with the real checkbox as it changes. The checked value submits through native forms, including with scripts disabled; haze:action-swap reports enhanced changes. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using action-swap, check it fits: use it when a follow or subscribe action has two lasting states; a compact action needs to show the checked result in place. Avoid it when for a single asynchronous save with pending and error states, use stateful-button; for several independent choices, use toggle-group. Alternatives: @haze/toggle, @haze/stateful-button, @haze/toggle-group.

## Example requests that should lead here
- Add a follow-project control that changes to Following and retains a native checked state.

## Source

```astro
---
import { Check, Plus } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  id: string;
  name?: string;
  label?: string;
  activeLabel?: string;
  checked?: boolean;
  disabled?: boolean;
  class?: string;
  [key: string]: unknown;
}

const { id, name = id, label = "Follow project", activeLabel = "Following", checked = false, disabled, class: className, ...rest } = Astro.props;
---

<div data-haze-action-swap data-label={label} data-active-label={activeLabel} class={cn("relative inline-grid max-w-full", className)}>
  <input {...rest} id={id} name={name} type="checkbox" checked={checked} disabled={disabled} class="peer sr-only" />
  <label for={id} class="group inline-flex min-h-10 max-w-full cursor-pointer items-center justify-center gap-2 rounded-lg border border-frame bg-field px-4 py-2 text-[0.8125rem]/5 font-medium text-foreground transition-[background-color,box-shadow,scale] duration-150 ease-snappy hover:bg-accent hover:shadow-key active:scale-[0.97] active:duration-75 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-ring peer-disabled:cursor-not-allowed peer-disabled:bg-muted peer-disabled:text-muted-foreground peer-checked:[&_[data-off]]:hidden peer-checked:[&_[data-on]]:inline-flex">
    <span data-off class="inline-flex min-w-0 items-center gap-2"><Plus aria-hidden="true" class="size-4 shrink-0" /><span class="wrap-anywhere">{label}</span></span>
    <span data-on class="hidden min-w-0 items-center gap-2"><Check aria-hidden="true" class="size-4 shrink-0 motion-safe:animate-pop" /><span class="wrap-anywhere">{activeLabel}</span></span>
  </label>
  <span data-swap-status role="status" class="sr-only"></span>
</div>

<script>
  function initActionSwaps() {
    document.querySelectorAll<HTMLElement>("[data-haze-action-swap]").forEach(root => {
      if (root.hasAttribute("data-enhanced")) return;
      const input = root.querySelector<HTMLInputElement>("input");
      const label = root.querySelector("label");
      const status = root.querySelector<HTMLElement>("[data-swap-status]");
      if (!input || !label || !status) return;
      input.addEventListener("change", () => {
        status.textContent = input.checked ? root.dataset.activeLabel ?? "" : root.dataset.label ?? "";
        if (!matchMedia("(prefers-reduced-motion: reduce)").matches) label.animate([{ opacity: .6, translate: "0 2px" }, { opacity: 1, translate: "0 0" }], { duration: 200, easing: "cubic-bezier(.2,.8,.2,1)" });
        root.dispatchEvent(new CustomEvent("haze:action-swap", { bubbles: true, detail: { checked: input.checked } }));
      });
      root.setAttribute("data-enhanced", "");
    });
  }
  initActionSwaps();
  if (!document.documentElement.hasAttribute("data-haze-action-swap-listener")) {
    document.documentElement.setAttribute("data-haze-action-swap-listener", "");
    document.addEventListener("astro:page-load", initActionSwaps);
  }
</script>
```
