# Popover

> Button that opens a floating panel next to it, built on the native popover attribute with no script.

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

## Use when
- Extra controls or details that should not take page space until asked for
- Filter and display settings attached to a toolbar button
- Content that may hold form controls, unlike a tooltip

## Avoid when
- A plain list of links or actions; use dropdown-menu
- A task that must block the page until finished; use dialog
- A one-line hint on hover; use tooltip

## Alternatives
- [dropdown-menu](https://hazeui.com/ui/dropdown-menu.md)
- [dialog](https://hazeui.com/ui/dialog.md)
- [tooltip](https://hazeui.com/ui/tooltip.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique on the page. Links the trigger to the panel and names the CSS anchor. |
| label | `string` | "Open" | no | Visible trigger text when the trigger slot is not used. |
| ariaLabel | `string` |  | no | Accessible name for an icon-only trigger. |
| variant | `"primary" \| "secondary" \| "outline" \| "ghost"` | "outline" | no | Trigger button style. |
| size | `"sm" \| "md" \| "lg"` | "md" | no | Trigger button size. |
| align | `"start" \| "end"` | "start" | no | Trigger edge the panel lines up with. |
| side | `"bottom" \| "top"` | "bottom" | no | Preferred side; it flips when there is no room. |
| class | `string` |  | no | Panel classes, for example a width. |
| trigger slot | `slot` |  | no | Custom trigger content, such as an icon and text. |
| default slot | `slot` |  | yes | Panel content. |

## Accessibility
- The panel scales and fades in over 150 ms and out again; reduced-motion users get no animation.
- The browser handles Escape, click-outside closing, the expanded state on the button and returning focus to it.
- Focus stays on the trigger when the panel opens; the next Tab moves into the panel.
- Before Firefox 147 and Safari 26 there is no CSS anchor positioning, so the panel opens centered over a dimmed backdrop instead.
- Key Enter / Space: Open or close the panel from the trigger
- Key Escape: Close the panel and return focus to the trigger
- Key Tab: Move from the trigger into the open panel

## Agent prompts

### Install and use

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

### Customize with real content

Adapt the Haze popover ui for our product. Replace the demo values of these props with our real content: id, label, ariaLabel, variant, size, align, side, class, trigger slot, default slot. Keep the layout, the heading level rules and these accessibility details: The panel scales and fades in over 150 ms and out again; reduced-motion users get no animation. The browser handles Escape, click-outside closing, the expanded state on the button and returning focus to it. Focus stays on the trigger when the panel opens; the next Tab moves into the panel. Before Firefox 147 and Safari 26 there is no CSS anchor positioning, so the panel opens centered over a dimmed backdrop instead. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using popover, check it fits: use it when extra controls or details that should not take page space until asked for; filter and display settings attached to a toolbar button; content that may hold form controls, unlike a tooltip. Avoid it when a plain list of links or actions; use dropdown-menu; a task that must block the page until finished; use dialog; a one-line hint on hover; use tooltip. Alternatives: @haze/dropdown-menu, @haze/dialog, @haze/tooltip.

## Example requests that should lead here
- Add a Display button that opens a popover with column checkboxes
- Put the share settings in a popover aligned to the right edge of its button

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";

interface Props {
  /** Unique on the page. Links the trigger to the panel and names the CSS anchor. */
  id: string;
  /** Visible trigger text. The "trigger" slot replaces it. */
  label?: string;
  /** Accessible name for an icon-only trigger. */
  ariaLabel?: string;
  variant?: "primary" | "secondary" | "outline" | "ghost";
  size?: "sm" | "md" | "lg";
  /** Which trigger edge the panel lines up with. */
  align?: "start" | "end";
  side?: "bottom" | "top";
  /** Extra classes for the trigger button. */
  triggerClass?: string;
  /** Classes for the panel, for example a width. */
  class?: string;
}

const { id, label = "Open", ariaLabel, variant = "outline", size = "md", align = "start", side = "bottom", triggerClass, class: className } = Astro.props;

// With CSS anchor positioning the panel sits next to its trigger and flips at the viewport edge.
// The margin on its far side keeps an 8 px gutter when the panel has to hug that edge; the near side stays flush with the trigger.
// Without it the same panel opens as a centered sheet over a dimmed backdrop.
const placement = {
  "bottom-start": "origin-top-left -translate-y-1 starting:open:-translate-y-1 supports-[position-area:block-end]:me-2 supports-[position-area:block-end]:[position-area:block-end_span-inline-end]",
  "bottom-end": "origin-top-right -translate-y-1 starting:open:-translate-y-1 supports-[position-area:block-end]:ms-2 supports-[position-area:block-end]:[position-area:block-end_span-inline-start]",
  "top-start": "origin-bottom-left translate-y-1 starting:open:translate-y-1 supports-[position-area:block-end]:me-2 supports-[position-area:block-end]:[position-area:block-start_span-inline-end]",
  "top-end": "origin-bottom-right translate-y-1 starting:open:translate-y-1 supports-[position-area:block-end]:ms-2 supports-[position-area:block-end]:[position-area:block-start_span-inline-start]",
};
---

<Button variant={variant} size={size} popovertarget={id} aria-label={ariaLabel} class={triggerClass} style={`anchor-name: --${id}`}>
  <slot name="trigger">{label}</slot>
</Button>
<div
  popover
  id={id}
  style={`position-anchor: --${id}`}
  class={cn(
    "m-auto max-h-[min(24rem,calc(100dvh-2rem))] w-72 max-w-[calc(100vw-1rem)] overflow-y-auto rounded-xl border border-border bg-popover p-4 text-[0.8125rem] leading-[1.4] text-popover-foreground shadow-float backdrop:bg-background/60",
    "scale-[0.97] opacity-0 transition-[opacity,scale,translate,overlay,display] transition-discrete duration-200 ease-snappy motion-reduce:transition-none open:translate-y-0 open:scale-100 open:opacity-100 starting:open:scale-[0.97] starting:open:opacity-0",
    "supports-[position-area:block-end]:mx-0 supports-[position-area:block-end]:my-1 supports-[position-area:block-end]:backdrop:bg-transparent supports-[position-area:block-end]:[position-try-fallbacks:flip-block,flip-inline,flip-block_flip-inline]",
    placement[`${side}-${align}`],
    className,
  )}
>
  <slot />
</div>
```
