This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Popover

An accessible popup anchored to a button.

Examples#

Opening on hover#

This example shows how you can configure the popover to open on hover using the openOnHover prop.

You can use the delay prop to specify how long to wait (in milliseconds) before the popover opens on hover.

Detached triggers#

A popover can be controlled by a trigger located either inside or outside the Popover component. For simple, one-off interactions, place the PopoverTrigger inside Popover, as shown in the example at the top of this page.

However, if defining the popover's content next to its trigger is not practical, you can use a detached trigger. This involves placing the PopoverTrigger outside of Popover and linking them with the same handle. The root gets the handle as its id, and the trigger gets data-phem-popover-for.

Multiple triggers#

A single popover can be opened by multiple trigger elements. You can achieve this by using the same handle for several detached triggers, or by placing multiple PopoverTrigger components inside a single Popover.

The popover can render different content depending on which trigger opened it. Give each trigger a payload, and put a PopoverPayload with the same name in the PopoverViewport: the server renders each content in a template, and the viewport shows a copy of the content of the active trigger.

Controlled mode#

You can control the popover's open state with the defaultOpen and triggerId props of Popover: a render with new values opens the popover next to the trigger with that id. The controller keeps the values of the browser in data-phem-popover-open-value and data-phem-popover-trigger-id-value.

Here, the button is in the root, and opens the popover without a request with the phem-popover#open action and a triggerId param.

Animating the Popover#

You can animate a popover as it moves between different trigger elements. This includes animating its position, size, and content.

To animate the popover's position, apply CSS transitions to the top and left properties of the PopoverPopup. To animate its size, transition its width and height to var(--popup-width) and var(--popup-height).

To enable content animations, wrap the content in the PopoverViewport part. It gets a data-activation-direction attribute that indicates the new trigger's position relative to the previous one: up to two tokens, left or right and up or down. Match a single token with the ~= attribute selector. Inside the viewport, the content is wrapped in divs with data-current (the incoming content) and data-previous (the outgoing content).

API reference#

Import the components of the family:

use function Phem\Ui\{Popover, PopoverTrigger, PopoverPopup, PopoverArrow, PopoverTitle, PopoverDescription, PopoverClose, PopoverViewport, PopoverPayload};

Popover#

A popup next to a button: Base UI Popover.Root, on the phem-popover controller.

The props of the Base UI Positioner (side, align, sideOffset, alignOffset, collisionPadding) are props of the root. The popup goes in the top layer (popover="manual"), so it needs no Portal.

A handle links the root to detached triggers: a PopoverTrigger with the same handle, anywhere in the page, opens this popover. The root then gets the handle as its id. One popover can have several triggers, and the PopoverViewport shows the payload of the trigger that opened it. defaultOpen and triggerId are the state: a render with new values opens the popover next to the trigger with that id.

PropTypeDefault
handle?stringnull
defaultOpenboolfalse
triggerId?stringnull
openOnHoverboolfalse
delayint300
closeDelayint0
side'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'Side::Bottom
align'start' | 'center' | 'end'Align::Center
sideOffsetint0
alignOffsetint0
collisionPaddingint5
children?Htmlnull
...restmixed—

PopoverTrigger#

The button that opens the popover. With a handle, it is a detached trigger of the Popover with the same handle: it can be anywhere in the page. The payload is the name of a PopoverPayload (or a text) that the PopoverViewport shows while this trigger is active.

PropTypeDefault
handle?stringnull
payload?stringnull
disabledboolfalse
children?Htmlnull
...restmixed—

PopoverPopup#

The box of the popover, with its content. The controller places it next to the trigger.

PropTypeDefault
children?Htmlnull
...restmixed—

PopoverArrow#

The arrow of the popup, at the side of the trigger.

PropTypeDefault
...restmixed—

PopoverTitle#

The heading of the popover. It names the popup (aria-labelledby).

PropTypeDefault
children?Htmlnull
...restmixed—

PopoverDescription#

The text that describes the popover (aria-describedby).

PropTypeDefault
children?Htmlnull
...restmixed—

PopoverClose#

A button that closes the popover.

PropTypeDefault
children?Htmlnull
...restmixed—

PopoverViewport#

The content of a popover with several triggers: the PopoverPayload of the active trigger, or its other children. When the active trigger changes, the old content gets data-previous and the new content data-current, and the viewport gets data-activation-direction, for an animation.

PropTypeDefault
children?Htmlnull
...restmixed—

PopoverPayload#

The content that the PopoverViewport shows for the triggers with this payload.

PropTypeDefault
name (required)string—
children?Htmlnull

The source of the examples: examples/ui/Popover.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close