# Floating button group

> A wrapped action tray with a shared hover and focus highlight that moves without shifting the buttons.

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

## Use when
- A toolbar should guide focus without moving the actions.
- A set of optional links or submit actions needs a shared surface.

## Avoid when
- Use Expandable Button Group if labels must hide in the resting state.

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

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| label | `string` |  | no | Accessible group name. |
| actions | `Action[]` |  | no | Labels and optional native action properties. A linked action may mark the current page. |
| class | `string` |  | no | Classes for the outer tray. |

## Accessibility
- The highlight is decorative. A native focus outline also marks the current keyboard target.
- Links and submit buttons keep their default behavior.
- Key Keyboard: Tab moves the highlight through the tray. Enter activates the focused link or button.

## Agent prompts

### Install and use

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

### Customize with real content

Adapt the Haze floating-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: The highlight is decorative. A native focus outline also marks the current keyboard target. Links and submit buttons keep their default behavior. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using floating-button-group, check it fits: use it when a toolbar should guide focus without moving the actions.; a set of optional links or submit actions needs a shared surface.. Avoid it when use expandable button group if labels must hide in the resting state.. Alternatives: @haze/button-group, @haze/expanding-button-group.

## Example requests that should lead here
- Make a release toolbar with a single highlight that glides between actions.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Action {
  label: string;
  href?: string;
  name?: string;
  value?: string;
  current?: boolean;
  disabled?: boolean;
}
interface Props {
  label?: string;
  actions?: Action[];
  class?: string;
}
const { label = "Release views", actions = [
  { label: "Overview", href: "#overview", current: true },
  { label: "Activity", href: "#activity" },
  { label: "Settings", href: "#settings" },
], class: className } = Astro.props;
---
<div role="group" aria-label={label} data-haze-floating-button-group
  class={cn("group/float relative isolate inline-flex w-fit max-w-full flex-wrap items-center gap-1 rounded-xl border border-border bg-muted p-1", className)}>
  <span aria-hidden="true" data-floating-highlight
    class="pointer-events-none absolute start-0 top-0 -z-10 h-(--highlight-h) w-(--highlight-w) translate-x-(--highlight-x) translate-y-(--highlight-y) rounded-lg border border-frame bg-card opacity-0 shadow-key transition-opacity duration-150 ease-snappy data-[visible]:opacity-100 motion-safe:data-[travel]:transition-[translate,width,height,opacity,scale] motion-safe:data-[travel]:duration-200 motion-safe:data-[pressing]:scale-[0.965]"></span>
  {actions.map(({ label: text, href, name, value, current, 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-current={current && href ? "page" : undefined}
        data-floating-action class="inline-flex min-h-8 max-w-full items-center rounded-lg px-3 text-[0.8125rem]/5 font-medium wrap-anywhere text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring aria-current:text-foreground disabled:opacity-50 group-not-data-[enhanced]/float:hover:bg-card">
        {text}
      </Action>
    );
  })}
</div>

<script>
  function enhance(group: HTMLElement) {
    if (group.hasAttribute("data-enhanced")) return;
    const highlight = group.querySelector<HTMLElement>("[data-floating-highlight]");
    if (!highlight) return;
    let active: HTMLElement | null = null;
    const resting = () => group.querySelector<HTMLElement>("[data-floating-action]:focus-visible")
      ?? group.querySelector<HTMLElement>("[data-floating-action][aria-current=page]");
    const place = (next: HTMLElement | null, travel = true) => {
      highlight.toggleAttribute("data-travel", Boolean(travel && active));
      active = next;
      highlight.toggleAttribute("data-visible", Boolean(active));
      if (!active) return;
      const rightToLeft = getComputedStyle(group).direction === "rtl";
      const x = rightToLeft ? active.offsetLeft + active.offsetWidth - group.clientWidth : active.offsetLeft;
      highlight.style.setProperty("--highlight-x", `${x}px`);
      highlight.style.setProperty("--highlight-y", `${active.offsetTop}px`);
      highlight.style.setProperty("--highlight-w", `${active.offsetWidth}px`);
      highlight.style.setProperty("--highlight-h", `${active.offsetHeight}px`);
    };
    group.addEventListener("pointerover", event => {
      if (event.pointerType === "touch") return;
      const next = (event.target as Element).closest<HTMLElement>("[data-floating-action]:not(:disabled)");
      if (next && next !== active) place(next);
    });
    group.addEventListener("pointerleave", () => place(resting()));
    group.addEventListener("pointerdown", () => highlight.setAttribute("data-pressing", ""));
    for (const type of ["pointerup", "pointercancel", "pointerleave"]) {
      group.addEventListener(type, () => highlight.removeAttribute("data-pressing"));
    }
    group.addEventListener("focusin", event => {
      const target = event.target as HTMLElement;
      if (target.matches("[data-floating-action]")) place(target);
    });
    group.addEventListener("focusout", event => {
      if (!group.contains(event.relatedTarget as Node | null)) place(group.querySelector<HTMLElement>("[aria-current=page]"));
    });
    const resize = new ResizeObserver(() => place(active, false));
    resize.observe(group);
    group.querySelectorAll("[data-floating-action]").forEach(action => resize.observe(action));
    place(resting(), false);
    group.setAttribute("data-enhanced", "");
  }

  const init = () => document.querySelectorAll<HTMLElement>("[data-haze-floating-button-group]").forEach(enhance);
  init();
  if (!document.documentElement.hasAttribute("data-haze-floating-button-group-listener")) {
    document.documentElement.setAttribute("data-haze-floating-button-group-listener", "");
    document.addEventListener("astro:page-load", init);
  }
</script>
```
