Preview card
A link that shows a destination preview without interrupting keyboard or screen reader navigation
import { PreviewCard } from "@nyte-ai/ui/preview-card";A card that previews where a link goes, shown on hover. Reach for it when a sighted reader benefits from seeing the destination before clicking.
No Nyte styling here.
Usage
- The trigger renders an
<a>. Give it a realhref, since the link is the only part users can reach by keyboard, touch, or screen reader. - Put everything the card shows on the linked page as well. Nothing unique lives in the popup.
- The card opens after
delay, which is 600ms, and closes aftercloseDelay, which is 300ms. Both live on the trigger. PreviewCard.Positionerplaces the popup. It defaults toside="bottom"andalign="center". PasssideOffsetfor the gap.PreviewCard.Arrowonly works inside the positioner.PreviewCard.Viewportis only needed when one popup serves several triggers and you animate the content change.- To open from a trigger outside the root, pass one handle from
PreviewCard.createHandle()to both the trigger and the root.
Anatomy
<PreviewCard.Root>
<PreviewCard.Trigger />
<PreviewCard.Portal>
<PreviewCard.Backdrop />
<PreviewCard.Positioner>
<PreviewCard.Popup>
<PreviewCard.Arrow />
<PreviewCard.Viewport />
</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>Examples
A link that previews its destination.
<PreviewCard.Root>
<PreviewCard.Trigger href="/cloud/components/button">Button</PreviewCard.Trigger>
<PreviewCard.Portal>
<PreviewCard.Positioner sideOffset={8}>
<PreviewCard.Popup>
<span>Button</span>
<p>Variants, sizes, and an unstyled mode.</p>
</PreviewCard.Popup>
</PreviewCard.Positioner>
</PreviewCard.Portal>
</PreviewCard.Root>Open sooner and keep the card up longer.
<PreviewCard.Trigger href="/docs/sdk" delay={200} closeDelay={600}>
SDK
</PreviewCard.Trigger>Animate the popup's position with CSS transitions on left, right, top, and bottom of the
positioner, and its size with width and height on the popup.
Props
The part tables below come from Base UI, MIT, © Material-UI SAS.
Root
Groups all parts of the preview card. Doesn't render its own HTML element.
Root Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultOpen | boolean | false | Whether the preview card is initially open. To render a controlled preview card, use the open prop instead. |
| open | boolean | - | Whether the preview card is currently open. |
| onOpenChange | ((open: boolean, eventDetails: PreviewCard.Root.ChangeEventDetails) => void) | - | Event handler called when the preview card is opened or closed. |
| actionsRef | React.RefObject<PreviewCard.Root.Actions | null> | - | A ref to imperative actions. unmount: Unmounts the preview card popup.close: Closes the preview card imperatively when called. |
| defaultTriggerId | string | null | - | ID of the trigger that the preview card is associated with.
This is useful in conjunction with the defaultOpen prop to create an initially open preview card. |
| handle | PreviewCard.Handle<Payload> | - | A handle to associate the preview card with a trigger. If specified, allows external triggers to control the card's open state. Can be created with the PreviewCard.createHandle() method. |
| onOpenChangeComplete | ((open: boolean) => void) | - | Event handler called after any animations complete when the preview card is opened or closed. |
| triggerId | string | null | - | ID of the trigger that the preview card is associated with.
This is useful in conjunction with the open prop to create a controlled preview card.
There's no need to specify this prop when the preview card is uncontrolled (that is, when the open prop is not set). |
| children | React.ReactNode | PayloadChildRenderFunction<Payload> | - | The content of the preview card.
This can be a regular React node or a render function that receives the payload of the active trigger. |
Trigger
A link that opens the preview card.
Renders an <a> element.
Trigger Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| handle | PreviewCard.Handle<Payload> | - | A handle to associate the trigger with a preview card. |
| payload | Payload | - | A payload to pass to the preview card when it is opened. |
| delay | number | 600 | How long to wait before the preview card opens. Specified in milliseconds. |
| closeDelay | number | 300 | How long to wait before closing the preview card. Specified in milliseconds. |
| className | string | ((state: PreviewCard.Trigger.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Trigger.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, state: PreviewCard.Trigger.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Trigger Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-popup-open | - | Present when the corresponding preview card is open. |
Portal
A portal element that moves the popup to a different part of the DOM.
By default, the portal element is appended to <body>.
Renders a <div> element.
Portal Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| container | HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null | - | A parent element to render the portal element into. |
| className | string | ((state: PreviewCard.Portal.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Portal.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| keepMounted | boolean | false | Whether to keep the portal mounted in the DOM while the popup is hidden. |
| render | ReactElement | ((props: HTMLProps, state: PreviewCard.Portal.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Backdrop
A presentational overlay displayed beneath the popup.
Renders a <div> element.
Backdrop Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: PreviewCard.Backdrop.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Backdrop.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: PreviewCard.Backdrop.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Backdrop Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the preview card is open. |
| data-closed | - | Present when the preview card is closed. |
| data-starting-style | - | Present when the preview card begins animating in. |
| data-ending-style | - | Present when the preview card is animating out. |
Positioner
Positions the popup against the trigger.
Renders a <div> element.
Positioner Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| disableAnchorTracking | boolean | false | Whether to disable the popup from tracking any layout shift of its positioning anchor. |
| align | Align | 'center' | How to align the popup relative to the specified side. |
| alignOffset | number | OffsetFunction | 0 | Additional offset along the alignment axis in pixels.
Also accepts a function that returns the offset to read the dimensions of the anchor
and positioner elements, along with its side and alignment. The function takes a data object parameter with the following properties: data.anchor: the dimensions of the anchor element with properties width and height.data.positioner: the dimensions of the positioner element with properties width and height.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side. |
| side | Side | 'bottom' | Which side of the anchor element to align the popup against. May automatically change to avoid collisions. |
| sideOffset | number | OffsetFunction | 0 | Distance between the anchor and the popup in pixels.
Also accepts a function that returns the distance to read the dimensions of the anchor
and positioner elements, along with its side and alignment. The function takes a data object parameter with the following properties: data.anchor: the dimensions of the anchor element with properties width and height.data.positioner: the dimensions of the positioner element with properties width and height.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side. |
| arrowPadding | number | 5 | Minimum distance to maintain between the arrow and the edges of the popup. Use it to prevent the arrow element from hanging out of the rounded corners of a popup. |
| anchor | Element | VirtualElement | React.RefObject<Element | null> | (() => Element | VirtualElement | null) | null | - | An element to position the popup against. By default, the popup will be positioned against the trigger. |
| collisionAvoidance | CollisionAvoidance | - | Determines how to handle collisions when positioning the popup. side controls overflow on the preferred placement axis (top/bottom or left/right): 'flip': keep the requested side when it fits; otherwise try the opposite side
(top and bottom, or left and right).'shift': never change side; keep the requested side and move the popup within
the clipping boundary so it stays visible.'none': do not correct side-axis overflow. align controls overflow on the alignment axis (start/center/end): 'flip': keep side, but swap start and end when the requested alignment overflows.'shift': keep side and requested alignment, then nudge the popup along the
alignment axis to fit.'none': do not correct alignment-axis overflow. fallbackAxisSide controls fallback behavior on the perpendicular axis when the
preferred axis cannot fit: 'start': allow perpendicular fallback and try the logical start side first
(top before bottom, or left before right in LTR).'end': allow perpendicular fallback and try the logical end side first
(bottom before top, or right before left in LTR).'none': do not fallback to the perpendicular axis. When side is 'shift', explicitly setting align only supports 'shift' or 'none'.
If align is omitted, it defaults to 'flip'. |
| collisionBoundary | Boundary | 'clipping-ancestors' | An element or a rectangle that delimits the area that the popup is confined to. |
| collisionPadding | Padding | 5 | Additional space to maintain from the edge of the collision boundary. |
| sticky | boolean | false | Whether to maintain the popup in the viewport after the anchor element was scrolled out of view. |
| positionMethod | 'absolute' | 'fixed' | 'absolute' | Determines which CSS position property to use. |
| className | string | ((state: PreviewCard.Positioner.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Positioner.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: PreviewCard.Positioner.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
alignOffset Prop Example:
<Positioner
alignOffset={({ side, align, anchor, positioner }) => {
return side === 'top' || side === 'bottom' ? anchor.width : anchor.height;
}}
/>sideOffset Prop Example:
<Positioner
sideOffset={({ side, align, anchor, positioner }) => {
return side === 'top' || side === 'bottom' ? anchor.height : anchor.width;
}}
/>collisionAvoidance Prop Example:
<Positioner
collisionAvoidance={{
side: 'shift',
align: 'shift',
fallbackAxisSide: 'none',
}}
/>Positioner Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the preview card is open. |
| data-closed | - | Present when the preview card is closed. |
| data-anchor-hidden | - | Present when the anchor is hidden. |
| data-align | 'start' | 'center' | 'end' | Indicates how the popup is aligned relative to specified side. |
| data-side | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | Indicates which side the popup is positioned relative to the trigger. |
Positioner CSS Variables:
| Variable | Type | Description |
|---|---|---|
--anchor-height | number | The anchor's height. |
--anchor-width | number | The anchor's width. |
--available-height | number | The available height between the trigger and the edge of the viewport. |
--available-width | number | The available width between the trigger and the edge of the viewport. |
--positioner-height | number | The height of the preview card's positioner.
It is important to set height to this value when using CSS to animate size changes. |
--positioner-width | number | The width of the preview card's positioner.
It is important to set width to this value when using CSS to animate size changes. |
--transform-origin | string | The coordinates that this element is anchored to. Used for animations and transitions. |
Popup
A container for the preview card contents.
Renders a <div> element.
Popup Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: PreviewCard.Popup.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Popup.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: PreviewCard.Popup.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Popup Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the preview card is open. |
| data-closed | - | Present when the preview card is closed. |
| data-align | 'start' | 'center' | 'end' | Indicates how the popup is aligned relative to specified side. |
| data-side | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | Indicates which side the popup is positioned relative to the trigger. |
| data-starting-style | - | Present when the preview card begins animating in. |
| data-ending-style | - | Present when the preview card is animating out. |
Arrow
Displays an element positioned against the preview card anchor.
Renders a <div> element.
Arrow Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: PreviewCard.Arrow.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Arrow.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: PreviewCard.Arrow.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Arrow Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the preview card is open. |
| data-closed | - | Present when the preview card is closed. |
| data-uncentered | - | Present when the preview card arrow is uncentered. |
| data-align | 'start' | 'center' | 'end' | Indicates how the popup is aligned relative to specified side. |
| data-side | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | Indicates which side the popup is positioned relative to the trigger. |
Viewport
A viewport for displaying content transitions.
This component is only required if one popup can be opened by multiple triggers, its content
changes based on the trigger, and switching between them is animated.
Renders a <div> element.
Viewport Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| children | React.ReactNode | - | The content to render inside the transition container. |
| className | string | ((state: PreviewCard.Viewport.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: PreviewCard.Viewport.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: PreviewCard.Viewport.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Viewport Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-activation-direction | `${'left' | 'right' | ''} ${'down' | 'up' | ''}` | Indicates the direction from which the popup was activated. This can be used to create directional animations based on how the popup was triggered. Contains space-separated values for both horizontal and vertical axes. |
| data-current | - | Applied to the direct child of the viewport when no transitions are present or the new content when it's entering. |
| data-instant | 'dismiss' | 'focus' | Present if animations should be instant. |
| data-previous | - | Applied to the direct child of the viewport that contains the exiting content when transitions are present. |
| data-transitioning | - | Indicates that the viewport is currently transitioning between old and new content. |
Viewport CSS Variables:
| Variable | Type | Description |
|---|---|---|
--popup-height | `` | The height of the parent popup. This variable is placed on the 'previous' container and stores the height of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content. |
--popup-width | `` | The width of the parent popup. This variable is placed on the 'previous' container and stores the width of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content. |
Accessibility
- The popup is not exposed to screen readers, and nothing inside it can be reached by touch or keyboard. It is a visual enhancement for sighted mouse and keyboard users.
- The link is the accessible interface. Screen reader users read the link text and continue, without being walked through the preview.
- The trigger carries
data-popup-openwhile the card is open. - Escape closes the card. Outside press closes it.