Preview Card
A link that shows a preview of its destination and does not stop keyboard or screen reader navigation.
The principles of good typography remain in the digital age.
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
import styles from '../../index.module.css';
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<PreviewCard.Root>
<p :class="styles.Paragraph">
The principles of good
<PreviewCard.Trigger :class="styles.Link" href="https://en.wikipedia.org/wiki/Typography"
>typography</PreviewCard.Trigger
>
remain in the digital age.
</p>
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass" appear>
<PreviewCard.Popup :class="styles.Popup">
<PreviewCard.Arrow :class="styles.Arrow" />
<div :class="styles.PopupContent">
<img
width="224"
height="150"
:class="styles.Image"
src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300"
alt="Station Hofplein signage in Rotterdam, Netherlands"
/>
<p :class="styles.Summary">
<strong>Typography</strong> is the art and science of arranging type to make written
language clear, visually appealing, and effective in communication.
</p>
</div>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template>.Positioner {
height: var(--positioner-height);
width: var(--positioner-width);
max-width: var(--available-width);
}
.Popup {
box-sizing: border-box;
position: relative;
width: var(--popup-width, auto);
height: var(--popup-height, auto);
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.Arrow {
display: block;
position: relative;
width: 12px;
height: 6px;
overflow: clip;
&[data-side='top'] {
bottom: -6px;
rotate: 180deg;
}
&[data-side='bottom'] {
top: -6px;
rotate: 0deg;
}
&[data-side='left'] {
right: -9px;
rotate: 90deg;
}
&[data-side='right'] {
left: -9px;
rotate: -90deg;
}
&::before {
content: '';
position: absolute;
bottom: 0;
left: 50%;
box-sizing: border-box;
width: calc(6px * sqrt(2));
height: calc(6px * sqrt(2));
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
transform: translate(-50%, 50%) rotate(45deg);
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
}
}
.PopupContent {
width: min-content;
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 0.5rem;
box-sizing: border-box;
}
.Image {
display: block;
max-width: none;
}
.Summary {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
}
.Container {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
justify-content: center;
align-items: baseline;
}
.Paragraph {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
color: oklch(14.5% 0 0deg);
text-wrap: balance;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Link {
outline: 0;
color: oklch(14.5% 0 0deg);
text-decoration-line: underline;
text-decoration-thickness: 1px;
text-decoration-color: color-mix(in oklab, oklch(14.5% 0 0deg), transparent 40%);
text-underline-offset: 2px;
@media (prefers-color-scheme: dark) {
color: white;
text-decoration-color: color-mix(in oklab, white, transparent 40%);
}
@media (hover: hover) {
&:hover {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
}
&[data-popup-open] {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
text-decoration-line: none;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.LinkGroup {
display: flex;
flex-wrap: wrap;
gap: 0.25rem;
align-items: baseline;
}
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
const popupHiddenClass = '[transform:scale(0.98)] opacity-0';
</script>
<template>
<PreviewCard.Root>
<p class="text-base text-neutral-950 text-balance dark:text-white">
The principles of good
<PreviewCard.Trigger
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
href="https://en.wikipedia.org/wiki/Typography"
>typography</PreviewCard.Trigger
>
remain in the digital age.
</p>
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass" appear>
<PreviewCard.Popup
class="relative h-[var(--popup-height,auto)] w-[var(--popup-width,auto)] origin-[var(--transform-origin)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,opacity] duration-100 ease-out dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<PreviewCard.Arrow
class="relative block h-1.5 w-3 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:absolute before:bottom-0 before:left-1/2 before:h-[calc(6px*sqrt(2))] before:w-[calc(6px*sqrt(2))] before:border before:border-neutral-950 before:bg-white before:content-[''] before:[transform:translate(-50%,50%)_rotate(45deg)] dark:before:border-white dark:before:bg-neutral-950"
/>
<div class="flex w-min flex-col gap-2 p-2">
<img
width="224"
height="150"
class="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"
/>
<p class="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>
</div>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template>Usage guidelines
- Keep the context of screen reader users: A screen reader that reads each preview makes the user go through its content before the rest of the page. Keep the link as the only accessible interface, and put all the previewed information at its destination.
- Keep the content of the popup supplementary: Do not put unique or essential information in the popup, unless the linked page also has it. Touch, keyboard and screen reader users cannot move into the content of a preview card. It is a visual progressive enhancement for sighted users with a mouse or a keyboard.
Anatomy
Import the component and assemble its parts:
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
</script>
<template>
<PreviewCard.Root>
<PreviewCard.Trigger />
<PreviewCard.Portal>
<PreviewCard.Backdrop />
<PreviewCard.Positioner>
<PreviewCard.Popup>
<PreviewCard.Arrow />
<PreviewCard.Viewport />
</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template>Examples
Detached triggers
A trigger can be inside or outside PreviewCard.Root. For a simple preview card, put PreviewCard.Trigger inside PreviewCard.Root, as the demo at the top of this page does.
When the content of the preview card cannot be next to its trigger, use a detached trigger. Put PreviewCard.Trigger outside PreviewCard.Root, and connect the two with a handle from PreviewCard.createHandle().
The methods open() and close() of the handle need a mounted PreviewCard.Root with the same handle. The handle ignores a call while no root uses it: before a root mounts, and after it unmounts. Each root starts with a new state at its mount. It does not replay an earlier call, and it does not keep the open state of an earlier root.
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
const demoPreviewCard = PreviewCard.createHandle();
</script>
<template>
<PreviewCard.Trigger :handle="demoPreviewCard" href="#">Link</PreviewCard.Trigger>
<PreviewCard.Root :handle="demoPreviewCard">
<PreviewCard.Portal>
<PreviewCard.Positioner>
<PreviewCard.Popup>Preview</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template>The principles of good typography remain in the digital age.
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
import styles from '../../index.module.css';
const demoPreviewCard = PreviewCard.createHandle();
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<div>
<p :class="styles.Paragraph">
The principles of good
<PreviewCard.Trigger
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Typography"
>typography</PreviewCard.Trigger
>
remain in the digital age.
</p>
<PreviewCard.Root :handle="demoPreviewCard">
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8">
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
appear
>
<PreviewCard.Popup :class="styles.Popup">
<PreviewCard.Arrow :class="styles.Arrow" />
<div :class="styles.PopupContent">
<img
width="224"
height="150"
:class="styles.Image"
src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300"
alt="Station Hofplein signage in Rotterdam, Netherlands"
/>
<p :class="styles.Summary">
<strong>Typography</strong> is the art and science of arranging type to make
written language clear, visually appealing, and effective in communication.
</p>
</div>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</div>
</template>.Positioner {
height: var(--positioner-height);
width: var(--positioner-width);
max-width: var(--available-width);
}
.Popup {
box-sizing: border-box;
position: relative;
width: var(--popup-width, auto);
height: var(--popup-height, auto);
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.Arrow {
display: block;
position: relative;
width: 12px;
height: 6px;
overflow: clip;
&[data-side='top'] {
bottom: -6px;
rotate: 180deg;
}
&[data-side='bottom'] {
top: -6px;
rotate: 0deg;
}
&[data-side='left'] {
right: -9px;
rotate: 90deg;
}
&[data-side='right'] {
left: -9px;
rotate: -90deg;
}
&::before {
content: '';
position: absolute;
bottom: 0;
left: 50%;
box-sizing: border-box;
width: calc(6px * sqrt(2));
height: calc(6px * sqrt(2));
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
transform: translate(-50%, 50%) rotate(45deg);
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
}
}
.PopupContent {
width: min-content;
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 0.5rem;
box-sizing: border-box;
}
.Image {
display: block;
max-width: none;
}
.Summary {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
}
.Container {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
justify-content: center;
align-items: baseline;
}
.Paragraph {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
color: oklch(14.5% 0 0deg);
text-wrap: balance;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Link {
outline: 0;
color: oklch(14.5% 0 0deg);
text-decoration-line: underline;
text-decoration-thickness: 1px;
text-decoration-color: color-mix(in oklab, oklch(14.5% 0 0deg), transparent 40%);
text-underline-offset: 2px;
@media (prefers-color-scheme: dark) {
color: white;
text-decoration-color: color-mix(in oklab, white, transparent 40%);
}
@media (hover: hover) {
&:hover {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
}
&[data-popup-open] {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
text-decoration-line: none;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.LinkGroup {
display: flex;
flex-wrap: wrap;
gap: 0.25rem;
align-items: baseline;
}
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
const demoPreviewCard = PreviewCard.createHandle();
const popupHiddenClass = '[transform:scale(0.98)] opacity-0';
</script>
<template>
<div>
<p class="text-base text-neutral-950 text-balance dark:text-white">
The principles of good
<PreviewCard.Trigger
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Typography"
>typography</PreviewCard.Trigger
>
remain in the digital age.
</p>
<PreviewCard.Root :handle="demoPreviewCard">
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8">
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
appear
>
<PreviewCard.Popup
class="relative h-[var(--popup-height,auto)] w-[var(--popup-width,auto)] origin-[var(--transform-origin)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,opacity] duration-100 ease-out dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<PreviewCard.Arrow
class="relative block h-1.5 w-3 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:absolute before:bottom-0 before:left-1/2 before:h-[calc(6px*sqrt(2))] before:w-[calc(6px*sqrt(2))] before:border before:border-neutral-950 before:bg-white before:content-[''] before:[transform:translate(-50%,50%)_rotate(45deg)] dark:before:border-white dark:before:bg-neutral-950"
/>
<div class="flex w-min flex-col gap-2 p-2">
<img
width="224"
height="150"
class="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"
/>
<p class="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>
</div>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</div>
</template>Multiple triggers
One preview card can have more than one trigger. Give the same handle to more than one detached trigger, or put more than one PreviewCard.Trigger in one PreviewCard.Root.
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
</script>
<template>
<PreviewCard.Root>
<PreviewCard.Trigger href="#">Trigger 1</PreviewCard.Trigger>
<PreviewCard.Trigger href="#">Trigger 2</PreviewCard.Trigger>
<PreviewCard.Portal>
<PreviewCard.Positioner>
<PreviewCard.Popup>Preview</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template><script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
const demoPreviewCard = PreviewCard.createHandle();
</script>
<template>
<PreviewCard.Trigger :handle="demoPreviewCard" href="#">Trigger 1</PreviewCard.Trigger>
<PreviewCard.Trigger :handle="demoPreviewCard" href="#">Trigger 2</PreviewCard.Trigger>
<PreviewCard.Root :handle="demoPreviewCard">
<PreviewCard.Portal>
<PreviewCard.Positioner>
<PreviewCard.Popup>Preview</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template>The preview card can show different content for each trigger. Give a payload to each PreviewCard.Trigger. The default slot of PreviewCard.Root gets the payload of the active trigger. The type argument of createHandle() gives the payload its type:
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
const demoPreviewCard = PreviewCard.createHandle<{ title: string }>();
</script>
<template>
<PreviewCard.Trigger :handle="demoPreviewCard" :payload="{ title: 'Trigger 1' }" href="#">
Trigger 1
</PreviewCard.Trigger>
<PreviewCard.Trigger :handle="demoPreviewCard" :payload="{ title: 'Trigger 2' }" href="#">
Trigger 2
</PreviewCard.Trigger>
<PreviewCard.Root v-slot="{ payload }" :handle="demoPreviewCard">
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8">
<PreviewCard.Popup>
<span v-if="payload !== undefined">Preview card opened by {{ payload.title }}</span>
</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>
</template>Controlled mode with multiple triggers
To control the open state, use v-model:open on PreviewCard.Root, or the open prop and the openChange event. With more than one trigger, control the active trigger with v-model:trigger-id on PreviewCard.Root and the id prop of each PreviewCard.Trigger.
The second argument of openChange has trigger, the element of the trigger that started the change.
Discover typography, design, or art.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { PreviewCard } from '@vase-ui/vue/preview-card';
import styles from '../../index.module.css';
type CardContent = 'typography' | 'design' | 'art';
const demoPreviewCard = PreviewCard.createHandle<CardContent>();
const open = shallowRef(false);
const triggerId = shallowRef<string | null>(null);
function handleOpenChange(isOpen: boolean, eventDetails: PreviewCard.Root.ChangeEventDetails) {
open.value = isOpen;
triggerId.value = eventDetails.trigger?.id ?? null;
}
function openProgrammatically() {
triggerId.value = 'trigger-2';
open.value = true;
}
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<div>
<div :class="styles.Container">
<p :class="styles.Paragraph">
Discover
<PreviewCard.Trigger
id="trigger-1"
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Typography"
payload="typography"
>typography</PreviewCard.Trigger
>,
<PreviewCard.Trigger
id="trigger-2"
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Industrial_design"
payload="design"
>design</PreviewCard.Trigger
>, or
<PreviewCard.Trigger
id="trigger-3"
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Art"
payload="art"
>art</PreviewCard.Trigger
>.
</p>
<button type="button" :class="styles.Button" @click="openProgrammatically">
Open programmatically
</button>
</div>
<PreviewCard.Root
:handle="demoPreviewCard"
:open="open"
:trigger-id="triggerId"
@open-change="handleOpenChange"
>
<template #default="{ payload }">
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8" :class="styles.Positioner">
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
appear
>
<PreviewCard.Popup :class="styles.Popup">
<PreviewCard.Arrow :class="styles.Arrow" />
<div v-if="payload === 'typography'" :class="styles.PopupContent">
<img
width="224"
height="150"
:class="styles.Image"
src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300"
alt="Station Hofplein signage in Rotterdam, Netherlands"
/>
<p :class="styles.Summary">
<strong>Typography</strong> is the art and science of arranging type.
</p>
</div>
<div v-else-if="payload === 'design'" :class="styles.PopupContent">
<img
width="241"
height="240"
:class="styles.Image"
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 :class="styles.Summary">
A <strong>design</strong> is the concept or proposal for an object, process, or
system.
</p>
</div>
<div v-else-if="payload === 'art'" :class="styles.PopupContent">
<img
width="206"
height="240"
:class="styles.Image"
src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/MonaLisa_sfumato.jpeg/250px-MonaLisa_sfumato.jpeg"
alt="Mona Lisa"
/>
<p :class="styles.Summary">
<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>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</template>
</PreviewCard.Root>
</div>
</template>.Positioner {
height: var(--positioner-height);
width: var(--positioner-width);
max-width: var(--available-width);
}
.Popup {
box-sizing: border-box;
position: relative;
width: var(--popup-width, auto);
height: var(--popup-height, auto);
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.Arrow {
display: block;
position: relative;
width: 12px;
height: 6px;
overflow: clip;
&[data-side='top'] {
bottom: -6px;
rotate: 180deg;
}
&[data-side='bottom'] {
top: -6px;
rotate: 0deg;
}
&[data-side='left'] {
right: -9px;
rotate: 90deg;
}
&[data-side='right'] {
left: -9px;
rotate: -90deg;
}
&::before {
content: '';
position: absolute;
bottom: 0;
left: 50%;
box-sizing: border-box;
width: calc(6px * sqrt(2));
height: calc(6px * sqrt(2));
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
transform: translate(-50%, 50%) rotate(45deg);
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
}
}
.PopupContent {
width: min-content;
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 0.5rem;
box-sizing: border-box;
}
.Image {
display: block;
max-width: none;
}
.Summary {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
}
.Container {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
justify-content: center;
align-items: baseline;
}
.Paragraph {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
color: oklch(14.5% 0 0deg);
text-wrap: balance;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Link {
outline: 0;
color: oklch(14.5% 0 0deg);
text-decoration-line: underline;
text-decoration-thickness: 1px;
text-decoration-color: color-mix(in oklab, oklch(14.5% 0 0deg), transparent 40%);
text-underline-offset: 2px;
@media (prefers-color-scheme: dark) {
color: white;
text-decoration-color: color-mix(in oklab, white, transparent 40%);
}
@media (hover: hover) {
&:hover {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
}
&[data-popup-open] {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
text-decoration-line: none;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.LinkGroup {
display: flex;
flex-wrap: wrap;
gap: 0.25rem;
align-items: baseline;
}
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { shallowRef } from 'vue';
import { PreviewCard } from '@vase-ui/vue/preview-card';
type CardContent = 'typography' | 'design' | 'art';
const demoPreviewCard = PreviewCard.createHandle<CardContent>();
const open = shallowRef(false);
const triggerId = shallowRef<string | null>(null);
function handleOpenChange(isOpen: boolean, eventDetails: PreviewCard.Root.ChangeEventDetails) {
open.value = isOpen;
triggerId.value = eventDetails.trigger?.id ?? null;
}
function openProgrammatically() {
triggerId.value = 'trigger-2';
open.value = true;
}
const popupHiddenClass = '[transform:scale(0.98)] opacity-0';
</script>
<template>
<div>
<div class="flex flex-wrap items-baseline justify-center gap-2">
<p class="text-base text-neutral-950 text-balance dark:text-white">
Discover
<PreviewCard.Trigger
id="trigger-1"
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Typography"
payload="typography"
>typography</PreviewCard.Trigger
>,
<PreviewCard.Trigger
id="trigger-2"
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Industrial_design"
payload="design"
>design</PreviewCard.Trigger
>, or
<PreviewCard.Trigger
id="trigger-3"
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Art"
payload="art"
>art</PreviewCard.Trigger
>.
</p>
<button
type="button"
class="flex h-8 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 disabled:border-neutral-500 disabled:text-neutral-500 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
@click="openProgrammatically"
>
Open programmatically
</button>
</div>
<PreviewCard.Root
:handle="demoPreviewCard"
:open="open"
:trigger-id="triggerId"
@open-change="handleOpenChange"
>
<template #default="{ payload }">
<PreviewCard.Portal>
<PreviewCard.Positioner
:side-offset="8"
class="h-[var(--positioner-height)] w-[var(--positioner-width)] max-w-[var(--available-width)]"
>
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
appear
>
<PreviewCard.Popup
class="relative h-[var(--popup-height,auto)] w-[var(--popup-width,auto)] origin-[var(--transform-origin)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,opacity] duration-100 ease-out dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<PreviewCard.Arrow
class="relative block h-1.5 w-3 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:absolute before:bottom-0 before:left-1/2 before:h-[calc(6px*sqrt(2))] before:w-[calc(6px*sqrt(2))] before:border before:border-neutral-950 before:bg-white before:content-[''] before:[transform:translate(-50%,50%)_rotate(45deg)] dark:before:border-white dark:before:bg-neutral-950"
/>
<div v-if="payload === 'typography'" class="flex w-min flex-col gap-2 p-2">
<img
width="224"
height="150"
class="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"
/>
<p class="text-sm">
<strong>Typography</strong> is the art and science of arranging type.
</p>
</div>
<div v-else-if="payload === 'design'" class="flex w-min flex-col gap-2 p-2">
<img
width="241"
height="240"
class="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 class="text-sm">
A <strong>design</strong> is the concept or proposal for an object, process, or
system.
</p>
</div>
<div v-else-if="payload === 'art'" class="flex w-min flex-col gap-2 p-2">
<img
width="206"
height="240"
class="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 class="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>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</template>
</PreviewCard.Root>
</div>
</template>Animating the Preview Card
A preview card can animate its position, its size and its content when it moves from one trigger to another.
Position and size
To animate the position, give PreviewCard.Positioner a CSS transition of left, right, top and bottom. To animate the size, give PreviewCard.Popup a transition of width and height.
Content
To animate the content, put it in PreviewCard.Viewport. The default slot of the viewport gets triggerId, the id of the active trigger. Put the content in a <Transition>, with triggerId as its key. At a move, the old content leaves while the new content enters. Select the old content with the leave classes, and the viewport while it has old content with :has(> .x-leave-active).
The data-activation-direction attribute of the viewport gives the place of the new trigger from the old trigger. Its value has one or two tokens, one for each axis: left or right, and up or down, such as right down. Match one token with the ~= selector, such as [data-activation-direction~='right'].
Use a viewport only when one popup has more than one trigger, and the content changes with an animation at a move to another trigger. With a viewport, give PreviewCard.Positioner the styles width: var(--positioner-width) and height: var(--positioner-height). The positioner then keeps the measured size during the transition. Without these styles, the size of the content can change the size of the popup in each frame, or move the popup to another side.
Discover typography, design, or art.
<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
import styles from './index.module.css';
type CardContent = 'typography' | 'design' | 'art';
const demoPreviewCard = PreviewCard.createHandle<CardContent>();
const popupHiddenClass = styles.PopupHidden;
const contentEnterFromClass = styles.ContentEnterFrom;
const contentLeavingClass = styles.ContentLeaving;
const contentLeaveToClass = styles.ContentLeaveTo;
</script>
<template>
<div>
<p :class="styles.Paragraph">
Discover
<PreviewCard.Trigger
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Typography"
payload="typography"
>typography</PreviewCard.Trigger
>,
<PreviewCard.Trigger
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Design"
payload="design"
>design</PreviewCard.Trigger
>, or
<PreviewCard.Trigger
:class="styles.Link"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Art"
payload="art"
>art</PreviewCard.Trigger
>.
</p>
<PreviewCard.Root :handle="demoPreviewCard">
<template #default="{ payload }">
<PreviewCard.Portal>
<PreviewCard.Positioner :side-offset="8" :class="styles.Positioner">
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
appear
>
<PreviewCard.Popup :class="styles.Popup">
<PreviewCard.Arrow :class="styles.Arrow" />
<PreviewCard.Viewport v-slot="{ triggerId }" :class="styles.Viewport">
<Transition
:enter-from-class="contentEnterFromClass"
:leave-active-class="contentLeavingClass"
:leave-to-class="contentLeaveToClass"
>
<div :key="triggerId ?? ''" :class="styles.Content">
<div v-if="payload === 'typography'" :class="styles.PopupContent">
<img
width="224"
height="150"
:class="styles.Image"
src="https://images.unsplash.com/photo-1619615391095-dfa29e1672ef?q=80&w=448&h=300"
alt="Station Hofplein signage in Rotterdam, Netherlands"
/>
<p :class="styles.Summary">
<strong>Typography</strong> is the art and science of arranging type.
</p>
</div>
<div v-else-if="payload === 'design'" :class="styles.PopupContent">
<img
width="250"
height="249"
:class="styles.Image"
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 :class="styles.Summary">
A <strong>design</strong> is the concept or proposal for an object,
process, or system.
</p>
</div>
<div v-else-if="payload === 'art'" :class="styles.PopupContent">
<img
width="250"
height="290"
:class="styles.Image"
src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/MonaLisa_sfumato.jpeg/250px-MonaLisa_sfumato.jpeg"
alt="Mona Lisa"
/>
<p :class="styles.Summary">
<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>
</div>
</Transition>
</PreviewCard.Viewport>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</template>
</PreviewCard.Root>
</div>
</template>.Positioner {
--easing: cubic-bezier(0.22, 1, 0.36, 1);
--animation-duration: 0.35s;
height: var(--positioner-height);
width: var(--positioner-width);
max-width: var(--available-width);
transition-property: top, left, right, bottom, transform;
transition-timing-function: var(--easing);
transition-duration: var(--animation-duration);
}
.Popup {
position: relative;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
/* These are required to make the size animations work */
width: var(--popup-width, auto);
height: var(--popup-height, auto);
/* width and height are essential for the resize animation; opacity and transform handle the enter/exit animation */
transition-property: width, height, opacity, transform;
transition-timing-function: var(--easing);
transition-duration: var(--animation-duration);
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.Arrow {
display: block;
position: relative;
width: 12px;
height: 6px;
overflow: clip;
&[data-side='top'] {
bottom: -6px;
rotate: 180deg;
}
&[data-side='bottom'] {
top: -6px;
rotate: 0deg;
}
&[data-side='left'] {
right: -9px;
rotate: 90deg;
}
&[data-side='right'] {
left: -9px;
rotate: -90deg;
}
&::before {
content: '';
position: absolute;
bottom: 0;
left: 50%;
box-sizing: border-box;
width: calc(6px * sqrt(2));
height: calc(6px * sqrt(2));
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
transform: translate(-50%, 50%) rotate(45deg);
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
}
}
.Viewport {
/* Required to clip the overflowing content during the slide in/out animations */
position: relative;
overflow: clip;
width: 100%;
height: 100%;
/* The content of a trigger. A `<Transition>` with the trigger id as the key keeps the old content
for its leave while the new content enters. */
.Content {
/* This freezes the width of the content while transitioning. */
width: var(--popup-width);
translate: 0;
opacity: 1;
transition:
translate var(--animation-duration) var(--easing),
opacity calc(var(--animation-duration) / 2) var(--easing);
}
.ContentLeaving {
position: absolute;
top: 0;
left: 0;
}
&[data-activation-direction~='left'] .ContentEnterFrom {
translate: -30% 0;
opacity: 0;
}
&[data-activation-direction~='right'] .ContentEnterFrom {
translate: 30% 0;
opacity: 0;
}
&[data-activation-direction~='left'] .ContentLeaveTo {
translate: 30% 0;
opacity: 0;
}
&[data-activation-direction~='right'] .ContentLeaveTo {
translate: -30% 0;
opacity: 0;
}
}
.PopupContent {
width: min-content;
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 0.5rem;
box-sizing: border-box;
}
.Image {
display: block;
max-width: none;
}
.Summary {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
}
.Paragraph {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
color: oklch(14.5% 0 0deg);
text-wrap: balance;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Link {
outline: 0;
color: oklch(14.5% 0 0deg);
text-decoration-line: underline;
text-decoration-thickness: 1px;
text-decoration-color: color-mix(in oklab, oklch(14.5% 0 0deg), transparent 40%);
text-underline-offset: 2px;
@media (prefers-color-scheme: dark) {
color: white;
text-decoration-color: color-mix(in oklab, white, transparent 40%);
}
@media (hover: hover) {
&:hover {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
}
&[data-popup-open] {
text-decoration-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
text-decoration-color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
text-decoration-line: none;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { PreviewCard } from '@vase-ui/vue/preview-card';
type CardContent = 'typography' | 'design' | 'art';
const demoPreviewCard = PreviewCard.createHandle<CardContent>();
const popupHiddenClass = '[transform:scale(0.98)] opacity-0';
const contentEnterFromClass =
'opacity-0 group-data-[activation-direction~=left]:-translate-x-[30%] group-data-[activation-direction~=right]:translate-x-[30%]';
const contentLeavingClass = 'absolute top-0 left-0';
const contentLeaveToClass =
'opacity-0 group-data-[activation-direction~=left]:translate-x-[30%] group-data-[activation-direction~=right]:-translate-x-[30%]';
</script>
<template>
<div>
<p class="text-base text-neutral-950 text-balance dark:text-white">
Discover
<PreviewCard.Trigger
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Typography"
payload="typography"
>typography</PreviewCard.Trigger
>,
<PreviewCard.Trigger
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Design"
payload="design"
>design</PreviewCard.Trigger
>, or
<PreviewCard.Trigger
class="text-neutral-950 underline decoration-neutral-950/60 decoration-1 underline-offset-2 outline-0 hover:decoration-neutral-950 data-popup-open:decoration-neutral-950 focus-visible:no-underline focus-visible:outline-2 focus-visible:outline-neutral-950 dark:text-white dark:decoration-white/60 dark:hover:decoration-white dark:data-popup-open:decoration-white dark:focus-visible:outline-white"
:handle="demoPreviewCard"
href="https://en.wikipedia.org/wiki/Art"
payload="art"
>art</PreviewCard.Trigger
>.
</p>
<PreviewCard.Root :handle="demoPreviewCard">
<template #default="{ payload }">
<PreviewCard.Portal>
<PreviewCard.Positioner
:side-offset="8"
class="h-[var(--positioner-height)] w-[var(--positioner-width)] max-w-[var(--available-width)] transition-[top,left,right,bottom,transform] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)]"
>
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
appear
>
<PreviewCard.Popup
class="relative h-[var(--popup-height,auto)] w-[var(--popup-width,auto)] origin-[var(--transform-origin)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[width,height,opacity,transform] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<PreviewCard.Arrow
class="relative block h-1.5 w-3 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:absolute before:bottom-0 before:left-1/2 before:h-[calc(6px*sqrt(2))] before:w-[calc(6px*sqrt(2))] before:border before:border-neutral-950 before:bg-white before:content-[''] before:[transform:translate(-50%,50%)_rotate(45deg)] dark:before:border-white dark:before:bg-neutral-950"
/>
<PreviewCard.Viewport
v-slot="{ triggerId }"
class="group relative h-full w-full overflow-clip"
>
<Transition
:enter-from-class="contentEnterFromClass"
:leave-active-class="contentLeavingClass"
:leave-to-class="contentLeaveToClass"
>
<div
:key="triggerId ?? ''"
class="w-[var(--popup-width)] transition-[translate,opacity] duration-[350ms,175ms] ease-[cubic-bezier(0.22,1,0.36,1)]"
>
<div v-if="payload === 'typography'" class="flex w-min flex-col gap-2 p-2">
<img
width="224"
height="150"
class="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"
/>
<p class="text-sm">
<strong>Typography</strong> is the art and science of arranging type.
</p>
</div>
<div v-else-if="payload === 'design'" class="flex w-min flex-col gap-2 p-2">
<img
width="250"
height="249"
class="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 class="text-sm">
A <strong>design</strong> is the concept or proposal for an object,
process, or system.
</p>
</div>
<div v-else-if="payload === 'art'" class="flex w-min flex-col gap-2 p-2">
<img
width="250"
height="290"
class="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 class="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>
</div>
</Transition>
</PreviewCard.Viewport>
</PreviewCard.Popup>
</Transition>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</template>
</PreviewCard.Root>
</div>
</template>API reference
PreviewCard.Root
Groups the parts of the preview card. Renders no element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultOpen | boolean | undefined | false | The preview card is open at the mount of an uncontrolled root. |
defaultTriggerId | string | null | undefined | null | The id of the active trigger at the mount, with defaultOpen. |
handle | PreviewCardHandle<Payload> | undefined | Connects the root to triggers outside it and to code. | |
open | boolean | undefined | The preview card is open. v-model:open binds it. | |
triggerId | string | null | undefined | The id of the active trigger. v-model:trigger-id binds it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | payload: Payload | undefined | The content of the preview card. payload is the payload of the active trigger. |
Events
| Event | Arguments | Description |
|---|---|---|
openChange | [open: boolean, details: PreviewCardRootChangeEventDetails] | The preview card opens or closes. details.cancel() keeps it as it was. |
update:open | [open: boolean] | Comes after openChange, unless a listener cancels the change. v-model:open uses it. |
update:triggerId | [id: string | null] | The active trigger changes. v-model:trigger-id uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
close | () => void | Closes the preview card with the reason 'imperative-action'. |
PreviewCard.Trigger
A link that opens the preview card when a mouse rests on it or a key moves the focus to it. A
touch opens nothing. The card takes no focus, so Tab moves from the link to the next element of
the page. When the link is on more than one line, the card opens at the line under the pointer.
Renders an <a> element, with no ARIA attribute, no role and no tabindex.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeDelay | number | undefined | 300 | The time in milliseconds before the close, after the pointer left the link. |
delay | number | undefined | 600 | The time in milliseconds that the mouse rests on the link, or that the link has the focus, before the open. |
handle | PreviewCardHandle<Payload> | undefined | Connects the trigger to a root outside it. | |
id | string | undefined | The id of the element, with a generated id as the default. The triggerId of the root and
open() of the handle name the trigger by it. | |
payload | Payload | undefined | The payload that the preview card gets when this trigger opens it. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: PreviewCardTriggerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: PreviewCardTriggerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-popup-open] | The preview card is open for this link. |
PreviewCard.Portal
Moves its content to another place of the DOM, <body> by default.
Renders no element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defer | boolean | undefined | false | Moves the content after the other content of the same update mounted. |
disabled | boolean | undefined | false | Renders the content in place, also on the server. |
keepMounted | boolean | undefined | false | The closed popup stays in the DOM, with hidden and inert. |
to | PortalTarget | undefined | The target of the content. With no value, the target of the nearest portal above, else
body. While it is null, the portal renders nothing. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | The content of the part. |
PreviewCard.Backdrop
An overlay under the popup.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: PreviewCardBackdropState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: PreviewCardBackdropState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[hidden] | The preview card is closed, and keepMounted keeps the backdrop in the DOM. |
PreviewCard.Positioner
Places the popup at its anchor.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
align | Align | undefined | 'center' | The alignment of the popup on the side. |
alignOffset | number | OffsetFunction | undefined | 0 | The offset on the align axis in pixels, or a function that gives it. |
anchor | Anchor | (() => Anchor) | The element to place the popup against: an element, a virtual element, a template ref of a part, or a function that gives one. With no value, the active trigger. | |
arrowPadding | number | undefined | 5 | The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels. |
collisionAvoidance | CollisionAvoidance | undefined | How the popup avoids the edge of the boundary. | |
collisionBoundary | Boundary | undefined | 'clipping-ancestors' | The area that holds the popup: an element, elements, or a rectangle. |
collisionPadding | Padding | undefined | 5 | The space between the popup and the edge of the boundary, in pixels. |
disableAnchorTracking | boolean | undefined | false | The popup does not follow a move of its anchor. |
positionMethod | "absolute" | "fixed" | undefined | 'absolute' | The CSS position of the positioner. |
side | Side | undefined | 'bottom' | The side of the anchor for the popup. It can change to avoid a collision. inline-start and
inline-end follow the CSS direction of the positioner: give the positioner dir when the
target of its portal has another direction. |
sideOffset | number | OffsetFunction | undefined | 0 | The distance from the anchor in pixels, or a function that gives it. |
sticky | boolean | undefined | false | The popup stays in the view after its anchor scrolls out of it. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: PreviewCardPositionerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: PreviewCardPositionerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-side] | The side of the anchor that the popup is on: top, bottom, left or right, or inline-start or inline-end for a logical side prop. |
[data-align] | The alignment of the popup on its side: start, center or end. |
[data-anchor-hidden] | The anchor is out of view. |
CSS variables
| Variable | Description |
|---|---|
--anchor-width | The width of the anchor, in pixels. |
--anchor-height | The height of the anchor, in pixels. |
--available-width | The width that the popup can have before it goes over the edge of the collision boundary. |
--available-height | The height that the popup can have before it goes over the edge of the collision boundary. |
--transform-origin | The point that the popup grows from, for the transform-origin of the popup. It is at the arrow, on the edge of the anchor. With no arrow, it is at the aligned edge of the popup or at the center of the anchor. |
--positioner-width | The width of the popup, in pixels, with a PreviewCard.Viewport in the popup. During a move to another trigger, the width of the new content. |
--positioner-height | The height of the popup, in pixels, with a PreviewCard.Viewport in the popup. During a move to another trigger, the height of the new content. |
PreviewCard.Popup
The container of the content of the preview card. A press in it moves the focus to it, so the
card stays open.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: PreviewCardPopupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: PreviewCardPopupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-side] | The side of the anchor that the popup is on, as on the positioner. |
[data-align] | The alignment of the popup on its side, as on the positioner. |
[data-instant] | An animation must not run. The value says why: focus (the focus opened the card) or dismiss (Escape closed it). |
[hidden] | The preview card is closed, and keepMounted keeps the popup in the DOM. |
CSS variables
| Variable | Description |
|---|---|
--popup-width | The width of the popup, in pixels, during a move to another trigger with a PreviewCard.Viewport: first the old width, then the new width. |
--popup-height | The height of the popup, in pixels, during a move to another trigger with a PreviewCard.Viewport: first the old height, then the new height. |
PreviewCard.Viewport
The container of content that changes when the preview card moves to another trigger.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: PreviewCardViewportStatetriggerId: string | null | Give the content the triggerId as its key in a <Transition>, so that it leaves and enters at a move. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: PreviewCardViewportState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-activation-direction] | The place of the new trigger from the old trigger, after a move: at most two tokens, left or right and up or down, such as right down. |
[data-instant] | An animation must not run, with the values of the popup. |
PreviewCard.Arrow
An element that points to the center of the anchor.
Renders a <div> element.
Its place is in the padding box of its nearest positioned ancestor: the popup when the popup has a position, else the positioner.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: PreviewCardArrowState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: PreviewCardArrowState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-side] | The side of the anchor that the popup is on, as on the positioner. |
[data-align] | The alignment of the popup on its side, as on the positioner. |
[data-uncentered] | The arrow cannot point at the center of the anchor. |
createHandle
Makes a PreviewCard.Handle with the type argument as the type of its payload.
Return value
PreviewCardHandle<Payload>
Handle
A handle opens and closes the preview card from code, and connects the triggers outside the root to it.
Properties
| Property | Type | Modifiers | Description |
|---|---|---|---|
isOpen | boolean | readonly | The popup is open. It is false while no root has the handle. |
Methods
| Method | Returns | Description |
|---|---|---|
open(triggerId: string) | void | Opens the popup for the trigger with this id. With no mounted root, it does nothing, and a
development build warns. Throws an error when the root has no trigger with this id. |
close() | void | Closes the popup. With no mounted root, it does nothing. |
Differences from Base UI
Vase UI ports Preview Card from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
PreviewCard.Root | @after-enter and @after-leave of a <Transition> around the popup run after its animations, so the root has no onOpenChangeComplete. A template ref of the root has close(). With :css="false", the leave hook of a <Transition> keeps the popup until it calls done, so the root has no unmount action. A press on the link goes to its href and changes no card, so no change has the reason trigger-press. |
PreviewCard.Portal prop render | The portal is a <Teleport> with no element of its own, so it has no as and no element slot. Put an element in the portal when you need one. |
PreviewCard.Backdrop | <Transition> around the backdrop animates it, so the state has no transitionStatus, and the backdrop has no data-starting-style and no data-ending-style. CSS selects a closed backdrop that stays in the DOM with [hidden], so the backdrop has no data-open and no data-closed. |
PreviewCard.Popup | <Transition> around the popup animates it, so the state has no transitionStatus, and the popup has no data-starting-style and no data-ending-style. CSS selects a closed popup that stays in the DOM with [hidden], so the popup has no data-open and no data-closed. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
PreviewCard.Portal | disabled prop | The portal is a <Teleport>, and disabled renders its content in place, also on the server. |
PreviewCard.Portal | defer prop | The portal is a <Teleport>, and it takes the props of <Teleport>: defer waits for the other content of the update. |
PreviewCard.Root | update:triggerId event | v-model:trigger-id binds the active trigger: Vue binds a second state of a component with a named v-model. |
Return types
| Function | Vase UI returns | Why |
|---|---|---|
PreviewCard.createHandle | PreviewCardHandle<Payload> | PreviewCard.Handle is the class PreviewCardHandle. The declaration of the function names the class, and Base UI documents the same class by its name in the namespace. |
Behavior
| Vase UI | Evidence |
|---|---|
A touch outside an open popup closes it only at a tap. A tap ends with pointerup in less than 1 s and moves 8 px or less. Chromium on Android sends click for a move of 8 px and not for 8.1 px, the smallest limit of the measured browsers. A touch that scrolls the page ends with pointercancel and closes nothing, as a native popover stays open in Chromium and WebKit. A drag of more than 8 px on an element that does not scroll also closes nothing. A tap on a backdrop, or on the element that a modal popup puts over the page, closes the popup and gives the page under it no click. In Base UI, a touch that scrolls the page closes a Popover, a Menu or a Select. | |
An arrow points at the center of its anchor. Its place is in the padding box of its nearest positioned ancestor, so the borders of a popup with a position do not move it. arrowPadding keeps the same space from each inner edge of that box. In the Popover demo of Base UI, the popup has position: relative and a 1 px border, and the arrow is 1.5 px off the center of its trigger. | |
--transform-origin is in the pixels of the popup, from its border box: the arrow or the aligned edge or the center of the anchor, on the edge of the anchor. A margin of the popup, a padding of the positioner, a margin that collapses through the popup and a zoom of the popup do not move it. A Select popup over the trigger grows from the middle of the value, also after it grows while the user scrolls the list. In the Popover demo of Base UI, a 16 px margin on the popup moves the origin 16 px off the edge of the trigger. | |
An arrow in a popup with a CSS zoom points at the center of its anchor. Its place and arrowPadding are in the pixels of its nearest positioned ancestor. A Select popup over the trigger with a zoom puts the text of the selected item over the text of the value. In the Popover demo of Base UI, a zoom: 2 on the popup moves the arrow 225 px off the center of the trigger. | |
close() of a handle with no mounted root does nothing and gives no warning: the popup is closed, which is what the call asks for. When two mounted roots have one handle, the root that mounted last has the handle, and the other root gets it back when that root unmounts; the library gives no warning. Base UI warns in a development build in both cases. |
|
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.