Skip to Content
Getting startedFoundationsComponentsPackagesTools

Popover

A floating panel anchored to a trigger.

Examples

Playground

Placement

Which side of the trigger the panel opens on. `top` is for a trigger near the bottom of the viewport.

bottom
top

Align

Which edge of the trigger the panel lines up with. `end` suits a trigger at the right of a toolbar; `stretch` matches the trigger's width.

start
end
stretch

Role

What the panel is. `menu` holds actions; `dialog` holds anything else, such as a small form. The role is announced, so a panel that claims `menu` and holds a form misleads a screen reader.

menu
dialog

Width

`auto` fits the content and suits a menu; `trigger` matches the trigger; the four fixed widths are for panels that hold a form.

auto
trigger
sm
md

Install

npx shadcn add @hipuku/popover

Usage

import { Popover } from '@/components/haus/components/Popover'

const trigger = useRef<HTMLButtonElement>(null)

<div style={{ position: 'relative' }}>
  <Button ref={trigger} aria-expanded={open} onClick={() => setOpen(o => !o)}>
    Share
  </Button>
  <Popover open={open} onClose={() => setOpen(false)} triggerRef={trigger} aria-label="Share">
    {shareForm}
  </Popover>
</div>

The element wrapping the trigger and the Popover needs position: relative. The panel is placed against it.

API

Popover also takes its element's own attributes and forwards its ref. className lands on the panel.

PropTypeDescription
aria-labelstringDefines a string value that labels the current element.
openrequiredboolean
onCloserequired() => void
triggerRefrequiredReact.RefObject<HTMLElement | null>The element the panel belongs to. Focus returns here when the panel closes, and a click on it is not treated as a click outside. Required, and that is the point of the component. vault's version leaves this to the caller and none of its six call sites does it, so dismissing a menu with Escape drops focus onto <body>: the keyboard user is back at the top of the document with no way to know it moved.
align"start" | "end" | "stretch"
placement"bottom" | "top"
width"auto" | "trigger" | "sm" | "md" | "lg" | "xl"
portalbooleanRender the panel out of the caller's positioned ancestor and position it from the trigger's rect instead. The host is document.body, except inside a Modal, where it is the dialog. The dialog is what the panel needs to escape *into*: the thing clipping it is the dialog's scrolling body, not the dialog, and a panel in document.body is outside the focus trap that keeps aria-modal honest, so every control in it lost focus the instant it was given and the panel could not be used at all. **Off by default, and it stays a caller's decision.** Today's behaviour needs no measuring and no dependency, and is right whenever nothing between the trigger and the page clips. It is wrong the moment something does: Modal's body is overflow-y: auto, so a panel inside a dialog is cut off at its edge, and placement cannot answer that. Flipping to top clips at the other edge instead. haus#68. The consumer knows whether it is inside a scrolling container; the panel cannot. That is the same division placement already makes.
role"dialog" | "menu" | "listbox" | "group"

Placement and width

  • placement puts the panel bottom or top of the trigger.
  • align lines it up with the trigger's start or end edge, or stretches it across.
  • width is auto to fit its content, trigger to be at least as wide as the trigger, or a fixed step for a panel holding a form. A panel grows to fit long content either way.

Inside something that scrolls

Set portal when the trigger is inside something that clips or scrolls. The panel is then placed over the page from the trigger's position, so it is not cut off.

Accessibility

  • triggerRef is required: Escape closes the panel and returns focus to the trigger.
  • A name is required, as aria-label or aria-labelledby. role is dialog, menu, listbox or group, and only menu promises arrow-key navigation: use Menu for that.
  • Not a focus trap. Tabbing out of the panel closes it, and a click outside closes it.

On this page