Action swap
A real checkbox styled as one action that exchanges its label and icon when the state changes.
pnpm dlx shadcn@latest add @haze/action-swap---
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>When to use
- A follow or subscribe action has two lasting states
- A compact action needs to show the checked result in place
When not to use
- For a single asynchronous save with pending and error states, use stateful-button
- For several independent choices, use toggle-group
Installation
Add the Haze registry to components.json once, then install Action swap with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/action-swapnpx shadcn@latest add @haze/action-swapyarn dlx shadcn@latest add @haze/action-swapbunx --bun shadcn@latest add @haze/action-swapFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique caller-supplied prefix for this component and its related elements. |
| name | string | id | Form submission name. |
| label | string | "Follow project" | Visible label before selection. |
| activeLabel | string | "Following" | Visible label while selected. |
| checked | boolean | false | Initial checkbox state. |
| disabled | boolean | – | Disables the native control and its enhancement. |
| class | string | – | Additional classes for the component root. |
| ...attributes | native control attributes | – | Other attributes are forwarded to the real form control. |
Keyboard interactions
- Tab, Space
- Focus the checkbox and exchange its lasting state.
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.