Preview Card
A link that shows a destination preview without interrupting keyboard or screen reader navigation.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{PreviewCard, PreviewCardArrow, PreviewCardPopup, PreviewCardTrigger};
#[Component]
function PreviewCardHeroDemo(): Node
{
return (
<PreviewCard>
<p className="text-base text-neutral-950 text-balance dark:text-white">The principles of good <PreviewCardTrigger href="https://en.wikipedia.org/wiki/Typography">typography</PreviewCardTrigger> remain in the digital age.</p>
<PreviewCardPopup>
<PreviewCardArrow />
<PreviewCardTypography long />
</PreviewCardPopup>
</PreviewCard>
);
}
#[Component]
function PreviewCardTypography(bool $long = false): Node
{
return (
<div className="flex w-min flex-col gap-2 p-2">
<img width="224" height="150" className="block max-w-none" src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300" alt="Station Hofplein signage in Rotterdam, Netherlands" />
{% if long %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type to make written language clear, visually appealing, and effective in communication.</p>
{% else %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type.</p>
{% endif %}
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{PreviewCard, PreviewCardArrow, PreviewCardPopup, PreviewCardTrigger};
#[Component]
function PreviewCardDetachedDemo(): Node
{
return (
<div>
<p className="text-base text-neutral-950 text-balance dark:text-white">The principles of good <PreviewCardTrigger handle="detached-card" href="https://en.wikipedia.org/wiki/Typography">typography</PreviewCardTrigger> remain in the digital age.</p>
<PreviewCard handle="detached-card">
<PreviewCardPopup>
<PreviewCardArrow />
<PreviewCardTypography long />
</PreviewCardPopup>
</PreviewCard>
</div>
);
}
#[Component]
function PreviewCardTypography(bool $long = false): Node
{
return (
<div className="flex w-min flex-col gap-2 p-2">
<img width="224" height="150" className="block max-w-none" src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300" alt="Station Hofplein signage in Rotterdam, Netherlands" />
{% if long %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type to make written language clear, visually appealing, and effective in communication.</p>
{% else %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type.</p>
{% endif %}
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{PreviewCard, PreviewCardArrow, PreviewCardPayload, PreviewCardPopup, PreviewCardTrigger, PreviewCardViewport};
#[Component]
function PreviewCardMultipleDemo(): Node
{
return (
<div>
<p className="text-base text-neutral-950 dark:text-white">Open <PreviewCardTrigger handle="multiple-card" payload="trigger-1" href="#">Trigger 1</PreviewCardTrigger> or <PreviewCardTrigger handle="multiple-card" payload="trigger-2" href="#">Trigger 2</PreviewCardTrigger>.</p>
<PreviewCard handle="multiple-card">
<PreviewCardPopup>
<PreviewCardArrow />
<PreviewCardViewport className="p-2 text-sm">
<PreviewCardPayload name="trigger-1">
<span>Preview card opened by Trigger 1</span>
</PreviewCardPayload>
<PreviewCardPayload name="trigger-2">
<span>Preview card opened by Trigger 2</span>
</PreviewCardPayload>
</PreviewCardViewport>
</PreviewCardPopup>
</PreviewCard>
</div>
);
}
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.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{PreviewCard, PreviewCardArrow, PreviewCardPayload, PreviewCardPopup, PreviewCardTrigger, PreviewCardViewport};
#[Component]
function PreviewCardControlledDemo(): Node
{
return (
<PreviewCard>
<div className="flex flex-wrap items-baseline justify-center gap-2">
<p className="text-base text-neutral-950 text-balance dark:text-white">Discover <PreviewCardTrigger id="card-trigger-1" payload="typography" href="https://en.wikipedia.org/wiki/Typography">typography</PreviewCardTrigger>, <PreviewCardTrigger id="card-trigger-2" payload="design" href="https://en.wikipedia.org/wiki/Industrial_design">design</PreviewCardTrigger>, or <PreviewCardTrigger id="card-trigger-3" payload="art" href="https://en.wikipedia.org/wiki/Art">art</PreviewCardTrigger>.</p>
<button type="button" data-action="phem-popover#open" data-phem-popover-trigger-id-param="card-trigger-2" className="flex h-control items-center justify-center gap-2 border border-neutral-950 bg-white px-3 font-[inherit] text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 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>
<PreviewCardPopup className="max-w-[var(--available-width)]">
<PreviewCardArrow />
<PreviewCardViewport>
<PreviewCardPayloads designWidth={{ 241 }} designHeight={{ 240 }} artWidth={{ 206 }} artHeight={{ 240 }} />
</PreviewCardViewport>
</PreviewCardPopup>
</PreviewCard>
);
}
#[Component]
function PreviewCardTypography(bool $long = false): Node
{
return (
<div className="flex w-min flex-col gap-2 p-2">
<img width="224" height="150" className="block max-w-none" src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300" alt="Station Hofplein signage in Rotterdam, Netherlands" />
{% if long %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type to make written language clear, visually appealing, and effective in communication.</p>
{% else %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type.</p>
{% endif %}
</div>
);
}
#[Component]
function PreviewCardPayloads(
int $designWidth,
int $designHeight,
int $artWidth,
int $artHeight,
): Node
{
return (
<>
<PreviewCardPayload name="typography">
<PreviewCardTypography />
</PreviewCardPayload>
<PreviewCardPayload name="design">
<div className="flex w-min flex-col gap-2 p-2">
<img width={{ designWidth }} height={{ designHeight }} className="block max-w-none" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Braun_ABW30_%28schwarz%29.jpg/250px-Braun_ABW30_%28schwarz%29.jpg" alt="Braun ABW30" />
<p className="text-sm">A <strong>design</strong> is the concept or proposal for an object, process, or system.</p>
</div>
</PreviewCardPayload>
<PreviewCardPayload name="art">
<div className="flex w-min flex-col gap-2 p-2">
<img width={{ artWidth }} height={{ artHeight }} className="block max-w-none" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/MonaLisa_sfumato.jpeg/250px-MonaLisa_sfumato.jpeg" alt="Mona Lisa" />
<p className="text-sm"><strong>Art</strong> is a diverse range of cultural activity centered around works utilizing creative or imaginative talents, which are expected to evoke a worthwhile experience, generally through an expression of emotional power, conceptual ideas, technical proficiency, or beauty.</p>
</div>
</PreviewCardPayload>
</>
);
}
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).
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{PreviewCard, PreviewCardArrow, PreviewCardPayload, PreviewCardPopup, PreviewCardTrigger, PreviewCardViewport};
#[Component]
function PreviewCardAnimatedDemo(): Node
{
return (
<div>
<p className="text-base text-neutral-950 text-balance dark:text-white">Discover <PreviewCardTrigger handle="animated-card" payload="typography" href="https://en.wikipedia.org/wiki/Typography">typography</PreviewCardTrigger>, <PreviewCardTrigger handle="animated-card" payload="design" href="https://en.wikipedia.org/wiki/Design">design</PreviewCardTrigger>, or <PreviewCardTrigger handle="animated-card" payload="art" href="https://en.wikipedia.org/wiki/Art">art</PreviewCardTrigger>.</p>
<PreviewCard handle="animated-card">
<PreviewCardPopup className="max-w-[var(--available-width)] transition-[top,left,width,height,opacity,transform] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)]">
<PreviewCardArrow className="transition-[left] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)]" />
<PreviewCardViewport className="relative overflow-clip w-full h-full [&_[data-previous]]:w-[var(--popup-width)] [&_[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-current]]:w-[var(--popup-width)] [&_[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-[30%] data-[activation-direction~='left']:[&_[data-current][data-starting-style]]:opacity-0 data-[activation-direction~='right']:[&_[data-current][data-starting-style]]:translate-x-[30%] data-[activation-direction~='right']:[&_[data-current][data-starting-style]]:opacity-0 data-[activation-direction~='left']:[&_[data-previous][data-ending-style]]:translate-x-[30%] data-[activation-direction~='left']:[&_[data-previous][data-ending-style]]:opacity-0 data-[activation-direction~='right']:[&_[data-previous][data-ending-style]]:-translate-x-[30%] data-[activation-direction~='right']:[&_[data-previous][data-ending-style]]:opacity-0">
<PreviewCardPayloads designWidth={{ 250 }} designHeight={{ 249 }} artWidth={{ 250 }} artHeight={{ 290 }} />
</PreviewCardViewport>
</PreviewCardPopup>
</PreviewCard>
</div>
);
}
#[Component]
function PreviewCardTypography(bool $long = false): Node
{
return (
<div className="flex w-min flex-col gap-2 p-2">
<img width="224" height="150" className="block max-w-none" src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300" alt="Station Hofplein signage in Rotterdam, Netherlands" />
{% if long %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type to make written language clear, visually appealing, and effective in communication.</p>
{% else %}
<p className="text-sm"><strong>Typography</strong> is the art and science of arranging type.</p>
{% endif %}
</div>
);
}
#[Component]
function PreviewCardPayloads(
int $designWidth,
int $designHeight,
int $artWidth,
int $artHeight,
): Node
{
return (
<>
<PreviewCardPayload name="typography">
<PreviewCardTypography />
</PreviewCardPayload>
<PreviewCardPayload name="design">
<div className="flex w-min flex-col gap-2 p-2">
<img width={{ designWidth }} height={{ designHeight }} className="block max-w-none" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Braun_ABW30_%28schwarz%29.jpg/250px-Braun_ABW30_%28schwarz%29.jpg" alt="Braun ABW30" />
<p className="text-sm">A <strong>design</strong> is the concept or proposal for an object, process, or system.</p>
</div>
</PreviewCardPayload>
<PreviewCardPayload name="art">
<div className="flex w-min flex-col gap-2 p-2">
<img width={{ artWidth }} height={{ artHeight }} className="block max-w-none" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/MonaLisa_sfumato.jpeg/250px-MonaLisa_sfumato.jpeg" alt="Mona Lisa" />
<p className="text-sm"><strong>Art</strong> is a diverse range of cultural activity centered around works utilizing creative or imaginative talents, which are expected to evoke a worthwhile experience, generally through an expression of emotional power, conceptual ideas, technical proficiency, or beauty.</p>
</div>
</PreviewCardPayload>
</>
);
}
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.
| Prop | Type | Default |
|---|---|---|
handle | ?string | null |
defaultOpen | bool | false |
triggerId | ?string | null |
delay | int | 600 |
closeDelay | int | 300 |
side | 'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end' | Side::Bottom |
align | 'start' | 'center' | 'end' | Align::Center |
sideOffset | int | 8 |
children | ?Html | null |
...rest | mixed | — |
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.
| Prop | Type | Default |
|---|---|---|
href (required) | string | — |
handle | ?string | null |
payload | ?string | null |
children | ?Html | null |
...rest | mixed | — |
PreviewCardPopup#
The box of the preview card, with the preview of the link. The controller places it next to the link.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
PreviewCardArrow#
The arrow of the popup, at the side of the link.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
PreviewCardViewport#
The content of a card with several links: the PreviewCardPayload of the active link.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
PreviewCardPayload#
The content that the card shows for the links with this payload.
| Prop | Type | Default |
|---|---|---|
name (required) | string | — |
children | ?Html | null |
The source of the examples: examples/ui/PreviewCard.phem.