# Star button

> A secondary-style Star button with a compact count segment, whose accessible name carries the exact number of stars.

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

## Use when
- A project, template or repository page links to where people can star it and shows how many already did.
- A count must sit inside the control, such as stars, likes or followers, without any brand mark.
- A docs or open-source landing page needs social proof next to the install command.

## Avoid when
- A row of joined actions with no count is needed; use button-group.
- A plain action or link is enough; use button.
- The user rates something from 1 to 5; use rating.

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

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| count | `number` | 12431 | no | Number of stars. The segment shows it compactly (12.4K); the accessible name carries the exact figure. |
| href | `string` |  | no | A non-empty string renders an <a> to the project page. Leave it out to render a <button type="button"> with aria-pressed; that button is inert until your own script toggles it. |
| label | `string` | "Star" | no | Visible action text. |
| starredLabel | `string` | "Starred" | no | Visible text when starred is true. |
| countLabel | `string` | "stars" | no | Word after the exact count in the accessible name. |
| countLabelOne | `string` | "star" | no | Singular of countLabel for a count of 1. |
| starred | `boolean` | false | no | Filled star in the warning color plus the starred label. Never color alone. |
| locale | `string` | "en-US" | no | BCP 47 locale for compact and exact number formatting. |
| size | `"sm" \| "md" \| "lg"` | "md" | no | Height 32, 40 or 44 px, matching button. |
| class | `string` |  | no | Extra classes, merged with tailwind-merge. |
| ...attributes | `HTML anchor or button attributes` |  | no | Passed to the element: target, rel, form, data-* hooks for your own script. |

## Accessibility
- The accessible name is the visible label followed by the exact count in screen-reader text, such as "Star, 12,431 stars" or "Starred, 12,431 stars". The compact "12.4K" segment is hidden from assistive technology, and no aria-label overrides the visible label.
- The button variant also sets aria-pressed to match starred; the link variant has no pressed state, so its name carries the state.
- The button variant is inert without your script: it toggles nothing on its own, so wire click handling (and update starred, aria-pressed and the label) in your page or form, since this component ships no script.
- The star glyph is decorative. It is filled and the text changes when starred, so state is never color alone.
- Key Enter: Follow the link, or press the button
- Key Space: Press the button (not when rendered as a link)

## Agent prompts

### Install and use

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

### Customize with real content

Adapt the Haze star-button ui for our product. Replace the demo values of these props with our real content: count, href, label, starredLabel, countLabel, countLabelOne, starred, locale, size, class, ...attributes. Keep the layout, the heading level rules and these accessibility details: The accessible name is the visible label followed by the exact count in screen-reader text, such as "Star, 12,431 stars" or "Starred, 12,431 stars". The compact "12.4K" segment is hidden from assistive technology, and no aria-label overrides the visible label. The button variant also sets aria-pressed to match starred; the link variant has no pressed state, so its name carries the state. The button variant is inert without your script: it toggles nothing on its own, so wire click handling (and update starred, aria-pressed and the label) in your page or form, since this component ships no script. The star glyph is decorative. It is filled and the text changes when starred, so state is never color alone. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using star-button, check it fits: use it when a project, template or repository page links to where people can star it and shows how many already did.; a count must sit inside the control, such as stars, likes or followers, without any brand mark.; a docs or open-source landing page needs social proof next to the install command.. Avoid it when a row of joined actions with no count is needed; use button-group.; a plain action or link is enough; use button.; the user rates something from 1 to 5; use rating.. Alternatives: @haze/button-group, @haze/button, @haze/copy-button.

## Example requests that should lead here
- Add a Star button with 12.4K stars next to the install command.
- Show a starred state with a filled star and 'Starred' text.
- Render the star count as a toggle button with aria-pressed instead of a link, by leaving href out.

## Source

```astro
---
import { Star } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  /** Number of stars. Shown compactly (12.4K) while the exact figure goes into the accessible name. */
  count?: number;
  /** Repository or project page. A non-empty string renders an <a>; leave it out to render a <button type="button"> with aria-pressed (inert without your own script). */
  href?: string;
  /** Visible action text. */
  label?: string;
  /** Visible text once starred. */
  starredLabel?: string;
  /** Word after the exact count in the accessible name ("12,431 stars"). */
  countLabel?: string;
  /** Singular of countLabel, used when count is 1. */
  countLabelOne?: string;
  /** Filled star and the starred label. */
  starred?: boolean;
  /** BCP 47 locale used to format the count. */
  locale?: string;
  size?: "sm" | "md" | "lg";
  class?: string;
  /** Any other attribute, for example target, rel, form or data-* hooks for the caller's own script. */
  [key: string]: unknown;
}

const {
  count = 12431,
  href,
  label = "Star",
  starredLabel = "Starred",
  countLabel = "stars",
  countLabelOne = "star",
  starred = false,
  locale = "en-US",
  size = "md",
  class: className,
  ...rest
} = Astro.props;

const safeCount = Number.isFinite(count) ? Math.max(0, Math.round(count)) : 0;
const compact = new Intl.NumberFormat(locale, { notation: "compact", maximumFractionDigits: 1 }).format(safeCount);
const exact = new Intl.NumberFormat(locale).format(safeCount);
const text = starred ? starredLabel : label;
const isLink = typeof href === "string" && href !== "";
// The name is the visible label plus the exact count in screen-reader text; the compact count is hidden from it.
const countText = `, ${exact} ${safeCount === 1 ? countLabelOne : countLabel}`;

const sizes = {
  sm: "h-8 text-[0.8125rem]/5",
  md: "h-10 text-[0.8125rem]/5",
  lg: "h-11 text-[0.8125rem]/5",
};
const classes = cn(
  "relative inline-flex max-w-full shrink-0 cursor-pointer items-stretch overflow-hidden rounded-lg border border-frame bg-field font-medium text-foreground select-none",
  "transition-[background-color,border-color,box-shadow,scale] duration-150 ease-snappy hover:bg-accent hover:shadow-key active:bg-accent active:scale-[0.97] active:duration-75 motion-reduce:transition-none motion-reduce:active:scale-100",
  "focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring",
  sizes[size],
  className,
);
---

{
  isLink ? (
    <a href={href} class={classes} {...rest}>
      <span class="inline-flex min-w-0 items-center gap-2 px-3">
        <Star aria-hidden="true" class={cn("size-4 shrink-0", starred && "fill-current text-foreground")} />
        <span class="whitespace-nowrap">{text}</span>
      </span>
      <span aria-hidden="true" class="inline-flex min-w-8 items-center justify-center border-s border-border px-3 text-muted-foreground tabular-nums">{compact}</span>
      <span class="sr-only">{countText}</span>
    </a>
  ) : (
    <button type="button" aria-pressed={starred ? "true" : "false"} class={classes} {...rest}>
      <span class="inline-flex min-w-0 items-center gap-2 px-3">
        <Star aria-hidden="true" class={cn("size-4 shrink-0", starred && "fill-current text-foreground")} />
        <span class="whitespace-nowrap">{text}</span>
      </span>
      <span aria-hidden="true" class="inline-flex min-w-8 items-center justify-center border-s border-border px-3 text-muted-foreground tabular-nums">{compact}</span>
      <span class="sr-only">{countText}</span>
    </button>
  )
}
```
