Preview Card
A popup that appears on hovering over a link, showing a preview of the linked content. The trigger renders a native `<a>` — designed exclusively for hover, not click.
Hover a repo to preview it:
Popular open source projects include React, Tailwind CSS, and Base UI.
import { PreviewCard } from "gnome-ui/preview-card"; export function PreviewCardBasic() { return ( <p> The principles of good{" "} <PreviewCard.Root> <PreviewCard.Trigger href="https://en.wikipedia.org/wiki/Typography" target="_blank" > typography </PreviewCard.Trigger> <PreviewCard.Portal> <PreviewCard.Positioner sideOffset={8}> <PreviewCard.Popup> <img src="..." alt="..." /> <div> <p>Typography</p> <Icons.ExternalLink /> <p> The art and science of arranging type to make written language legible, readable, and appealing. </p> </div> </PreviewCard.Popup> </PreviewCard.Positioner> </PreviewCard.Portal> </PreviewCard.Root>{" "} remain essential in the digital age. </p> ); }
API reference
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 | 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 conjuntion with the open prop to create a controlled preview card.
There's no need to specify this prop when the preview card is uncontrolled (i.e. when the open prop is not set). |
| children | 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 | CSSProperties | ((state: PreviewCard.Trigger.State) => CSSProperties | undefined) | - | - |
| render | ReactElement | ((props: HTMLProps, 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 | 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 | CSSProperties | ((state: PreviewCard.Portal.State) => CSSProperties | undefined) | - | - |
| 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
An 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 | CSSProperties | ((state: PreviewCard.Backdrop.State) => CSSProperties | undefined) | - | - |
| 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 is 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 propertieswidthandheight.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 adataobject parameter with the following properties:*data.anchor: the dimensions of the anchor element with propertieswidthandheight.data.positioner: the dimensions of the positioner element with propertieswidthandheight.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 \| 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. | | 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 CSSpositionproperty 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 |CSSProperties \| ((state: PreviewCard.Positioner.State) => CSSProperties \| undefined)| - | - | | 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 aReactElementor a function that returns the element to render. |
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 | Default | 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. |
| --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 | CSSProperties | ((state: PreviewCard.Popup.State) => CSSProperties | undefined) | - | - |
| 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 is animating in. |
| data-ending-style | - | Present when the preview card is animating out. |
Viewport
A viewport for displaying content transitions.
This component is only required if one popup can be opened by multiple triggers, its content change based on the trigger
and switching between them is animated.
Renders a <div> element.
Viewport Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| children | 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 | CSSProperties | ((state: PreviewCard.Viewport.State) => CSSProperties | undefined) | - | - |
| 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'} {'top' | 'bottom'}` | 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 | 'delay' | '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 | Default | 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. |
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 | CSSProperties | ((state: PreviewCard.Arrow.State) => CSSProperties | undefined) | - | - |
| 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. |