# Button

> Button or link styled as a button, in seven variants and three text sizes, plus a square icon size and press and loading states.

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

## Use when
- Any action or navigation that should look like a button
- Pass href to render a real link; omit it for a <button>

## Avoid when
- Navigation inside running text; use a plain underlined link

## Alternatives
- [badge](https://hazeui.com/ui/badge.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| href | `string` |  | no | Renders an <a> when set, otherwise a <button>. |
| variant | `"primary" \| "secondary" \| "outline" \| "ghost" \| "soft" \| "link" \| "destructive"` | "primary" | no | Visual style. soft is a muted fill for a secondary call to action. |
| size | `"sm" \| "md" \| "lg" \| "icon"` | "md" | no | Height 32, 36 or 44 px; icon is a 36 px square for one icon plus an aria-label. |
| type | `"button" \| "submit" \| "reset"` | "button" | no | Button type when no href is set. |
| loading | `boolean` | false | no | Replaces the label with a spinner, disables the button and sets aria-busy. The button keeps its width. Applies to <button> only, not to links. |
| class | `string` |  | no | Extra classes, merged with tailwind-merge. |
| ...attributes | `HTML button or anchor attributes` |  | no | Anything else is passed to the rendered <button> or <a>: aria-label, disabled, form, command, commandfor, popovertarget and so on. The dialog, drawer, popover and dropdown menu rely on this. |

## Accessibility
- Chooses <a> or <button> from href, so the semantics match the behaviour.
- A loading button is disabled, so it takes no clicks or focus. Its label stays in the DOM (hidden from sight) so the accessible name does not change, and aria-busy="true" tells assistive technology it is working.
- Pressing a button scales it to 97% for 75 ms. The motion is switched off under prefers-reduced-motion.
- The visible focus ring uses --ring at 3:1 or better; the smallest size is still 32 px tall.
- Key Enter / Space: Activate (Space only for <button>)

## Agent prompts

### Install and use

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

### Customize with real content

Adapt the Haze button ui for our product. Replace the demo values of these props with our real content: href, variant, size, type, loading, class, ...attributes. Keep the layout, the heading level rules and these accessibility details: Chooses <a> or <button> from href, so the semantics match the behaviour. A loading button is disabled, so it takes no clicks or focus. Its label stays in the DOM (hidden from sight) so the accessible name does not change, and aria-busy="true" tells assistive technology it is working. Pressing a button scales it to 97% for 75 ms. The motion is switched off under prefers-reduced-motion. The visible focus ring uses --ring at 3:1 or better; the smallest size is still 32 px tall. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using button, check it fits: use it when any action or navigation that should look like a button; pass href to render a real link; omit it for a <button>. Avoid it when navigation inside running text; use a plain underlined link. Alternatives: @haze/badge.

## Example requests that should lead here
- Add a primary and an outline button side by side
- Make this link look like a small ghost button
- Show a loading state on the Save button while the form submits

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Props {
  href?: string;
  variant?: "primary" | "secondary" | "outline" | "ghost" | "soft" | "link" | "destructive";
  size?: "sm" | "md" | "lg" | "icon";
  type?: "button" | "submit" | "reset";
  /** Replaces the label with a spinner and disables the button. The button keeps its width. */
  loading?: boolean;
  class?: string;
  [key: string]: unknown;
}

const { href, variant = "primary", size = "md", type = "button", loading = false, class: className, ...rest } = Astro.props;

const variants = {
  primary: "border border-primary bg-primary text-primary-foreground hover:opacity-91 hover:shadow-key active:opacity-84",
  secondary: "border border-frame bg-field text-foreground hover:bg-accent hover:shadow-key active:bg-accent disabled:opacity-100 disabled:bg-muted disabled:text-muted-foreground",
  outline: "border border-border bg-transparent text-foreground hover:border-border-strong hover:bg-accent active:bg-accent",
  ghost: "border border-transparent bg-transparent text-muted-foreground hover:bg-accent hover:text-foreground active:bg-accent active:text-foreground disabled:opacity-100",
  soft: "border border-transparent bg-muted text-foreground hover:bg-accent hover:shadow-key",
  link: "h-auto min-h-6 rounded-sm bg-transparent px-0 text-foreground underline decoration-muted-foreground underline-offset-4 hover:decoration-foreground active:scale-100",
  destructive: "border border-frame bg-field text-destructive hover:border-destructive hover:bg-accent active:border-destructive active:bg-accent",
};
const sizes = {
  sm: "h-8 gap-1.5 rounded-lg px-3 text-[0.8125rem]/5",
  md: "h-10 gap-2 rounded-lg px-4 text-[0.8125rem]/5",
  lg: "h-11 gap-2 rounded-lg px-4.5 text-[0.8125rem]/5",
  icon: "size-10 rounded-lg p-0",
};
const classes = cn(
  "relative inline-flex shrink-0 cursor-pointer items-center justify-center whitespace-nowrap font-medium select-none",
  "transition-[background-color,border-color,color,box-shadow,opacity,scale,text-decoration-color] duration-150 ease-snappy active:scale-[0.97] active:duration-100",
  "focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring",
  "disabled:pointer-events-none disabled:opacity-52 aria-disabled:pointer-events-none aria-disabled:opacity-52 [&_svg]:size-4 [&_svg]:shrink-0",
  sizes[size],
  variants[variant],
  loading && "disabled:opacity-100",
  className,
);
const spinner = "size-4 animate-spin rounded-full border-2 border-current border-r-transparent";
---

{
  href ? (
    <a href={href} class={classes} {...rest}><slot /></a>
  ) : loading ? (
    <button type={type} class={classes} disabled aria-busy="true" {...rest}>
      <span class="inline-flex items-center gap-[inherit] opacity-0"><slot /></span>
      <span class="absolute inset-0 grid place-items-center"><span aria-hidden="true" class={spinner}></span></span>
    </button>
  ) : (
    <button type={type} class={classes} {...rest}><slot /></button>
  )
}
```
