Expanding button group
A compact icon action tray that slides open a label on hover or keyboard focus.
pnpm dlx shadcn@latest add @haze/expanding-button-group---
import { Download, Link, Share2 } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
type Action = {
label: string;
icon: (props: Record<string, unknown>) => unknown;
href?: string;
name?: string;
value?: string;
disabled?: boolean;
};
interface Props {
label?: string;
actions?: Action[];
class?: string;
}
const defaultActions: Action[] = [
{ label: "Share", icon: Share2, href: "#share" },
{ label: "Copy link", icon: Link, href: "#link" },
{ label: "Export", icon: Download, href: "#export" },
];
const { label = "Project actions", actions = defaultActions, class: className } = Astro.props;
const actionClass = "group/action inline-grid h-8 min-w-8 max-w-full grid-cols-[1.125rem_0fr] items-center rounded-sm border border-transparent px-2 text-[0.8125rem]/5 font-medium text-muted-foreground select-none motion-safe:transition-[grid-template-columns,background-color,color] motion-safe:duration-200 motion-safe:ease-snappy focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring data-[expanded]:grid-cols-[1.125rem_1fr] data-[expanded]:border-frame data-[expanded]:bg-background data-[expanded]:shadow-key data-[expanded]:text-foreground group-data-[enhanced=false]/rail:hover:grid-cols-[1.125rem_1fr] group-data-[enhanced=false]/rail:focus-visible:grid-cols-[1.125rem_1fr] aria-disabled:opacity-50 disabled:opacity-50";
---
<div role="group" aria-label={label} data-haze-expanding-button-group data-enhanced="false"
class={cn("group/rail inline-flex w-fit max-w-full flex-wrap items-center gap-1 rounded-lg border border-frame bg-muted p-1", className)}>
{actions.map(({ label: text, icon: Icon, href, name, value, disabled }) => {
const Action = href && !disabled ? "a" : "button";
return (
<Action href={href && !disabled ? href : undefined} type={href && !disabled ? undefined : name ? "submit" : "button"}
name={name} value={value} disabled={disabled} aria-label={text} data-expanding-action class={actionClass}>
<Icon aria-hidden="true" class="size-4 transition-[scale] duration-75 ease-snappy group-active/action:scale-90" />
<span aria-hidden="true" class="min-w-0 overflow-hidden">
<span class="block whitespace-nowrap ps-1.5 opacity-0 motion-safe:transition-opacity motion-safe:duration-150 motion-safe:ease-snappy group-data-[expanded]/action:opacity-100 group-data-[enhanced=false]/rail:group-hover/action:opacity-100 group-data-[enhanced=false]/rail:group-focus-visible/action:opacity-100">{text}</span>
</span>
</Action>
);
})}
</div>
<script>
function enhance(group: HTMLElement) {
if (group.dataset.enhanced === "true") return;
let expanded: HTMLElement | null = null;
let pointerX = NaN;
let pointerY = NaN;
const show = (next: HTMLElement | null) => {
if (next === expanded) return;
expanded?.removeAttribute("data-expanded");
next?.setAttribute("data-expanded", "");
expanded = next;
};
const focused = () => group.querySelector<HTMLElement>("[data-expanding-action]:focus-visible");
group.addEventListener("pointermove", event => {
if (event.pointerType === "touch" || (event.clientX === pointerX && event.clientY === pointerY)) return;
pointerX = event.clientX;
pointerY = event.clientY;
const next = (event.target as Element).closest<HTMLElement>("[data-expanding-action]:not(:disabled)");
if (!next || next === expanded) return;
const box = next.getBoundingClientRect();
if (!expanded || (event.clientX >= box.left + 4 && event.clientX <= box.right - 4)) show(next);
});
group.addEventListener("pointerleave", () => { pointerX = pointerY = NaN; show(focused()); });
group.addEventListener("focusin", () => { if (focused()) show(focused()); });
group.addEventListener("focusout", event => { if (!group.contains(event.relatedTarget as Node | null)) show(null); });
group.dataset.enhanced = "true";
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-expanding-button-group]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-expanding-button-group-listener")) {
document.documentElement.setAttribute("data-haze-expanding-button-group-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- Space is limited but the available actions still need names.
- The label should reveal itself beside its icon on focus.
When not to use
- Use a regular Button Group when all labels should stay visible.
Installation
Add the Haze registry to components.json once, then install Expanding button group with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/expanding-button-groupnpx shadcn@latest add @haze/expanding-button-groupyarn dlx shadcn@latest add @haze/expanding-button-groupbunx --bun shadcn@latest add @haze/expanding-button-groupFirst 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 |
|---|---|---|---|
| label | string | – | Accessible group name. |
| actions | Action[] | – | Label, icon and optional link destination, submit name and value, or disabled state. |
| class | string | – | Classes for the outer tray. |
Keyboard interactions
- Keyboard
- Tab focuses each action and reveals its label. Enter activates the focused link or button.
Accessibility
- Every action has an accessible name in its compact state.
- Only motion-safe contexts animate the label width.
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 "Expanding button group" with `npx shadcn@latest add @haze/expanding-button-group`. 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 ExpandingButtonGroup from "@/components/haze/ui/expanding-button-group.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/expanding-button-group.md
Customize with real content
Adapt the Haze expanding-button-group ui for our product. Replace the demo values of these props with our real content: label, actions, class. Keep the layout, the heading level rules and these accessibility details: Every action has an accessible name in its compact state. Only motion-safe contexts animate the label width. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using expanding-button-group, check it fits: use it when space is limited but the available actions still need names.; the label should reveal itself beside its icon on focus.. Avoid it when use a regular button group when all labels should stay visible.. Alternatives: @haze/button-group, @haze/floating-button-group.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.