Star button
A secondary-style Star button with a compact count segment, whose accessible name carries the exact number of stars.
pnpm dlx shadcn@latest add @haze/star-button---
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>
)
}When to use
- 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.
When not to use
- 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.
Installation
Add the Haze registry to components.json once, then install Star button with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/star-buttonnpx shadcn@latest add @haze/star-buttonyarn dlx shadcn@latest add @haze/star-buttonbunx --bun shadcn@latest add @haze/star-buttonFirst 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 |
|---|---|---|---|
| count | number | 12431 | Number of stars. The segment shows it compactly (12.4K); the accessible name carries the exact figure. |
| href | string | – | 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" | Visible action text. |
| starredLabel | string | "Starred" | Visible text when starred is true. |
| countLabel | string | "stars" | Word after the exact count in the accessible name. |
| countLabelOne | string | "star" | Singular of countLabel for a count of 1. |
| starred | boolean | false | Filled star in the warning color plus the starred label. Never color alone. |
| locale | string | "en-US" | BCP 47 locale for compact and exact number formatting. |
| size | "sm" | "md" | "lg" | "md" | Height 32, 40 or 44 px, matching button. |
| class | string | – | Extra classes, merged with tailwind-merge. |
| ...attributes | HTML anchor or button attributes | – | Passed to the element: target, rel, form, data-* hooks for your own script. |
Keyboard interactions
- Enter
- Follow the link, or press the button
- Space
- Press the button (not when rendered as a link)
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.
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 "Star button" with `npx shadcn@latest add @haze/star-button`. 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 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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.