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

Skip to content

Preview Card

A link that shows a destination preview without interrupting keyboard or screen reader navigation.

Examples#

Detached triggers#

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

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

Multiple triggers#

A single preview card can be opened by multiple trigger elements. You can achieve this by using the same handle for several detached triggers, or by placing multiple PreviewCardTrigger components inside a single PreviewCard.

The preview card can render different content depending on which trigger opened it. Give each trigger a payload, and put a PreviewCardPayload with the same name in the PreviewCardViewport: the viewport shows a copy of the content of the active trigger.

Controlled mode#

You can control the preview card's open state with the defaultOpen and triggerId props of PreviewCard: a render with new values opens the card next to the link 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 card without a request with the phem-popover#open action and a triggerId param. The focus stays on the button.

Animating the Preview Card#

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

To animate the preview card's position, apply CSS transitions to the top and left properties of the PreviewCardPopup. 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 PreviewCardViewport 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\{PreviewCard, PreviewCardTrigger, PreviewCardPopup, PreviewCardArrow, PreviewCardViewport, PreviewCardPayload};

PreviewCard#

A popup with a preview of the page of a link, on hover: Base UI PreviewCard.Root, on the phem-popover controller with openOnHover. The link is the only accessible part: keep the content of the popup a supplement.

A handle links the root to detached links: a PreviewCardTrigger with the same handle, anywhere in the page, shows this card. One card can have several links, and the PreviewCardViewport shows the PreviewCardPayload of the link under the pointer or with the focus. triggerId is the id of the link of the open card.

PropTypeDefault
handle?stringnull
defaultOpenboolfalse
triggerId?stringnull
delayint600
closeDelayint300
side'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'Side::Bottom
align'start' | 'center' | 'end'Align::Center
sideOffsetint8
children?Htmlnull
...restmixed—

PreviewCardTrigger#

The link that shows the card. No whitespace goes around its text: the underline would include it.

With a handle, it is a detached link of the PreviewCard with the same handle. The payload is the name of the PreviewCardPayload that the card shows for this link.

PropTypeDefault
href (required)string—
handle?stringnull
payload?stringnull
children?Htmlnull
...restmixed—

PreviewCardPopup#

The box of the preview card, with the preview of the link. The controller places it next to the link.

PropTypeDefault
children?Htmlnull
...restmixed—

PreviewCardArrow#

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

PropTypeDefault
...restmixed—

PreviewCardViewport#

The content of a card with several links: the PreviewCardPayload of the active link.

PropTypeDefault
children?Htmlnull
...restmixed—

PreviewCardPayload#

The content that the card shows for the links with this payload.

PropTypeDefault
name (required)string—
children?Htmlnull

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

Phem
Esc
↑↓ to navigate↵ to openEsc to close