Popover
An accessible popup anchored to a button.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{Popover, PopoverArrow, PopoverDescription, PopoverPopup, PopoverTitle, PopoverTrigger};
#[Component]
function PopoverHeroDemo(): Node
{
return (
<Popover sideOffset={{ 8 }}>
<PopoverTrigger>Notifications</PopoverTrigger>
<PopoverPopup>
<PopoverArrow />
<PopoverTitle>Notifications</PopoverTitle>
<PopoverDescription>You are all caught up. Good job!</PopoverDescription>
</PopoverPopup>
</Popover>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{Popover, PopoverArrow, PopoverDescription, PopoverPopup, PopoverTitle, PopoverTrigger};
#[Component]
function PopoverHoverDemo(): Node
{
return (
<Popover openOnHover sideOffset={{ 8 }}>
<PopoverTrigger>Notifications</PopoverTrigger>
<PopoverPopup>
<PopoverArrow />
<PopoverTitle>Notifications</PopoverTitle>
<PopoverDescription>You are all caught up. Good job!</PopoverDescription>
</PopoverPopup>
</Popover>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{Popover, PopoverArrow, PopoverDescription, PopoverPopup, PopoverTitle, PopoverTrigger};
#[Component]
function PopoverDetachedDemo(): Node
{
return (
<div>
<PopoverTrigger handle="detached-popover">Notifications</PopoverTrigger>
<Popover handle="detached-popover" sideOffset={{ 8 }}>
<PopoverPopup>
<PopoverArrow />
<PopoverTitle>Notifications</PopoverTitle>
<PopoverDescription>You are all caught up. Good job!</PopoverDescription>
</PopoverPopup>
</Popover>
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{Popover, PopoverArrow, PopoverDescription, PopoverPayload, PopoverPopup, PopoverTitle, PopoverTrigger, PopoverViewport};
#[Component]
function PopoverMultipleDemo(): Node
{
return (
<div className="flex gap-2">
<PopoverTrigger handle="multiple-popover" payload="trigger-1">Trigger 1</PopoverTrigger>
<PopoverTrigger handle="multiple-popover" payload="trigger-2">Trigger 2</PopoverTrigger>
<Popover handle="multiple-popover" sideOffset={{ 8 }}>
<PopoverPopup>
<PopoverArrow />
<PopoverViewport>
{% for name in ['Trigger 1', 'Trigger 2'] %}
<PopoverPayload key={{ loop.index }} name={{ 'trigger-' ~ loop.index }}>
<div className="flex flex-col gap-1">
<PopoverTitle>Popover</PopoverTitle>
<PopoverDescription>This has been opened by {{ name }}</PopoverDescription>
</div>
</PopoverPayload>
{% endfor %}
</PopoverViewport>
</PopoverPopup>
</Popover>
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{Popover, PopoverArrow, PopoverDescription, PopoverPopup, PopoverTitle, PopoverTrigger};
#[Component]
function PopoverControlledDemo(): Node
{
return (
<Popover sideOffset={{ 8 }}>
<div className="flex flex-wrap justify-center gap-2">
<PopoverTrigger id="popover-trigger-1">Trigger 1</PopoverTrigger>
<PopoverTrigger id="popover-trigger-2">Trigger 2</PopoverTrigger>
<PopoverTrigger id="popover-trigger-3">Trigger 3</PopoverTrigger>
<button type="button" data-action="phem-popover#open" data-phem-popover-trigger-id-param="popover-trigger-2" className="flex h-control items-center justify-center border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm font-normal whitespace-nowrap text-neutral-950 dark:text-white select-none hover:bg-neutral-100 dark:hover:bg-neutral-800 active:bg-neutral-200 dark:active:bg-neutral-700 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">Open programmatically</button>
</div>
<PopoverPopup className="max-w-[var(--available-width)]">
<PopoverArrow />
<PopoverTitle>Notifications</PopoverTitle>
<PopoverDescription>You are all caught up. Good job!</PopoverDescription>
</PopoverPopup>
</Popover>
);
}
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).
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{Avatar, AvatarImage, Popover, PopoverArrow, PopoverDescription, PopoverPayload, PopoverPopup, PopoverTitle, PopoverTrigger, PopoverViewport};
#[Component]
function PopoverAnimatedDemo(): Node
{
return (
<div className="flex gap-2">
<PopoverTrigger handle="animated-popover" payload="notifications">Notifications</PopoverTrigger>
<PopoverTrigger handle="animated-popover" payload="activity">Activity</PopoverTrigger>
<PopoverTrigger handle="animated-popover" payload="profile">Profile</PopoverTrigger>
<Popover handle="animated-popover" sideOffset={{ 8 }}>
<PopoverPopup className="max-w-[min(31.25rem,var(--available-width))] gap-0 p-0 transition-[top,left,width,height,opacity,scale] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-ending-style:scale-90 data-starting-style:scale-90 data-instant:transition-none">
<PopoverArrow className="transition-[left] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-instant:transition-none" />
<PopoverViewport className="relative h-full w-full overflow-clip p-2 [&_[data-current]]:w-[calc(var(--popup-width)-1rem)] [&_[data-current]]:translate-x-0 [&_[data-current]]:opacity-100 [&_[data-current]]:transition-[translate,opacity] [&_[data-current]]:duration-[350ms,175ms] [&_[data-current]]:ease-[cubic-bezier(0.22,1,0.36,1)] data-[activation-direction~='left']:[&_[data-current][data-starting-style]]:-translate-x-1/2 data-[activation-direction~='left']:[&_[data-current][data-starting-style]]:opacity-0 data-[activation-direction~='right']:[&_[data-current][data-starting-style]]:translate-x-1/2 data-[activation-direction~='right']:[&_[data-current][data-starting-style]]:opacity-0 [&_[data-previous]]:w-[calc(var(--popup-width)-1rem)] [&_[data-previous]]:translate-x-0 [&_[data-previous]]:opacity-100 [&_[data-previous]]:transition-[translate,opacity] [&_[data-previous]]:duration-[350ms,175ms] [&_[data-previous]]:ease-[cubic-bezier(0.22,1,0.36,1)] data-[activation-direction~='left']:[&_[data-previous][data-ending-style]]:translate-x-1/2 data-[activation-direction~='left']:[&_[data-previous][data-ending-style]]:opacity-0 data-[activation-direction~='right']:[&_[data-previous][data-ending-style]]:-translate-x-1/2 data-[activation-direction~='right']:[&_[data-previous][data-ending-style]]:opacity-0">
<PopoverPayload name="notifications">
<div className="flex flex-col gap-1">
<PopoverTitle>Notifications</PopoverTitle>
<PopoverDescription>You are all caught up. Good job!</PopoverDescription>
</div>
</PopoverPayload>
<PopoverPayload name="activity">
<div className="flex flex-col gap-1">
<PopoverTitle>Activity</PopoverTitle>
<PopoverDescription>Nothing interesting happened recently.</PopoverDescription>
</div>
</PopoverPayload>
<PopoverPayload name="profile">
<div className="grid w-max grid-cols-[auto_auto] gap-x-2">
<PopoverTitle className="col-start-2 col-end-3 row-start-1 row-end-2">Jason Eventon</PopoverTitle>
<Avatar className="col-start-1 col-end-2 row-start-1 row-end-3 size-12 rounded-none font-bold">
<AvatarImage src="https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1?w=128&h=128&dpr=2&q=80" width="48" height="48" />
</Avatar>
<span className="col-start-2 col-end-3 row-start-2 row-end-3 text-sm text-neutral-600 dark:text-neutral-400">Pro plan</span>
<div className="col-start-1 col-end-3 row-start-3 row-end-4 flex flex-col gap-2 pt-2 text-sm">
<a href="#" className="text-neutral-950 dark:text-white underline underline-offset-[0.16em] decoration-[1px] hover:no-underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">Profile settings</a>
<a href="#" className="text-neutral-950 dark:text-white underline underline-offset-[0.16em] decoration-[1px] hover:no-underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">Log out</a>
</div>
</div>
</PopoverPayload>
</PopoverViewport>
</PopoverPopup>
</Popover>
</div>
);
}
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.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
defaultOpen | bool | false |
triggerId | ?string | null |
openOnHover | bool | false |
delay | int | 300 |
closeDelay | int | 0 |
side | 'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end' | Side::Bottom |
align | 'start' | 'center' | 'end' | Align::Center |
sideOffset | int | 0 |
alignOffset | int | 0 |
collisionPadding | int | 5 |
children | ?Html | null |
...rest | mixed | — |
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.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
payload | ?string | null |
disabled | bool | false |
children | ?Html | null |
...rest | mixed | — |
PopoverPopup#
The box of the popover, with its content. The controller places it next to the trigger.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
PopoverArrow#
The arrow of the popup, at the side of the trigger.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
PopoverTitle#
The heading of the popover. It names the popup (aria-labelledby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
PopoverDescription#
The text that describes the popover (aria-describedby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
PopoverClose#
A button that closes the popover.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
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.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
PopoverPayload#
The content that the PopoverViewport shows for the triggers with this payload.
| Prop | Type | Default |
|---|---|---|
name (required) | string | — |
children | ?Html | null |
The source of the examples: examples/ui/Popover.phem.