# Expanding button group

> A compact icon action tray that slides open a label on hover or keyboard focus.

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

## Use when
- Space is limited but the available actions still need names.
- The label should reveal itself beside its icon on focus.

## Avoid when
- Use a regular Button Group when all labels should stay visible.

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

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| label | `string` |  | no | Accessible group name. |
| actions | `Action[]` |  | no | Label, icon and optional link destination, submit name and value, or disabled state. |
| class | `string` |  | no | Classes for the outer tray. |

## Accessibility
- Every action has an accessible name in its compact state.
- Only motion-safe contexts animate the label width.
- Key Keyboard: Tab focuses each action and reveals its label. Enter activates the focused link or button.

## Agent prompts

### 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": "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 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: https://hazeui.com/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.

## Example requests that should lead here
- Show a compact release toolbar whose labels expand as each action receives focus.

## Source

```astro
---
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>
```
