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.
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.
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.
Width
`auto` fits the content and suits a menu; `trigger` matches the trigger; the four fixed widths are for panels that hold a form.
Install
npx shadcn add @hipuku/popoverUsage
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.
| Prop | Type | Description |
|---|---|---|
aria-label | string | Defines a string value that labels the current element. |
openrequired | boolean | |
onCloserequired | () => void | |
triggerRefrequired | React.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" | |
portal | boolean | Render 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
placementputs the panelbottomortopof the trigger.alignlines it up with the trigger'sstartorendedge, orstretches it across.widthisautoto fit its content,triggerto 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
triggerRefis required: Escape closes the panel and returns focus to the trigger.- A name is required, as
aria-labeloraria-labelledby.roleisdialog,menu,listboxorgroup, and onlymenupromises arrow-key navigation: use Menu for that. - Not a focus trap. Tabbing out of the panel closes it, and a click outside closes it.