Components

Preview Card

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

The principles of good typography remain in the digital age.

Usage guidelines#

  • Protect screen reader users' current context: Exposing each preview to screen readers would force users through its contents before they could continue through the page, repeatedly disrupting their current context. Keep the link as the only accessible interface and include all previewed information at its destination.
  • Keep popup content supplementary: Avoid placing unique or essential information in the popup unless it is also available on the linked page. For the reason above, preview card content is not touch, keyboard or screen reader navigable. It acts as a visual progressive enhancement for sighted mouse and keyboard users only.

Anatomy#

Import the component and assemble its parts:

Anatomytsx
import { PreviewCard } from '@egoist/solid-base/preview-card';

<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#

Detached triggers#

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

However, if defining the preview card's content next to its trigger is not practical, you can use a detached trigger. This involves placing the <PreviewCard.Trigger> outside of <PreviewCard.Root> and linking them with a handle created by the PreviewCard.createHandle() function.

The imperative methods on the handle, such as open() and close(), require a <PreviewCard.Root> using the same handle to be mounted. Calls made while no root is attached to the handle — before one mounts, or after it unmounts — are ignored. Each time a root mounts, it starts from fresh state: a call made while no root was attached is not replayed, and no open state carries over from a previous mount.

Detached triggerstsx
const demoPreviewCard = PreviewCard.createHandle();

<PreviewCard.Trigger handle={demoPreviewCard} href="#">
  Link
</PreviewCard.Trigger>

<PreviewCard.Root handle={demoPreviewCard}>
  ...
</PreviewCard.Root>

The principles of good typography remain in the digital age.

Multiple triggers#

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

Multiple triggers within the Root parttsx
<PreviewCard.Root>
  <PreviewCard.Trigger href="#">Trigger 1</PreviewCard.Trigger>
  <PreviewCard.Trigger href="#">Trigger 2</PreviewCard.Trigger>
  ...
</PreviewCard.Root>
Multiple detached triggerstsx
const demoPreviewCard = PreviewCard.createHandle();

<PreviewCard.Trigger handle={demoPreviewCard} href="#">
  Trigger 1
</PreviewCard.Trigger>

<PreviewCard.Trigger handle={demoPreviewCard} href="#">
  Trigger 2
</PreviewCard.Trigger>

<PreviewCard.Root handle={demoPreviewCard}>
  ...
</PreviewCard.Root>

The preview card can render different content depending on which trigger opened it. This is achieved by passing a payload to the <PreviewCard.Trigger> and using a function as the child of <PreviewCard.Root>.

The payload can be strongly typed by providing a type argument to the createHandle() function:

Detached triggers with payloadtsx
const demoPreviewCard = PreviewCard.createHandle<{ title: string }>();

<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 handle={demoPreviewCard}>
  {(state) => (
    <PreviewCard.Portal>
      <PreviewCard.Positioner sideOffset={8}>
        <PreviewCard.Popup>
          <Show when={state.payload}>
            {(payload) => <span>Preview card opened by {payload().title}</span>}
          </Show>
        </PreviewCard.Popup>
      </PreviewCard.Positioner>
    </PreviewCard.Portal>
  )}
</PreviewCard.Root>

Controlled mode with multiple triggers#

You can control the preview card's open state externally using the open and onOpenChange props on <PreviewCard.Root>. This allows you to manage the preview card's visibility based on your application's state. When using multiple triggers, you have to manage which trigger is active with the triggerId prop on <PreviewCard.Root> and the id prop on each <PreviewCard.Trigger>.

Note that there is no separate onTriggerIdChange prop. Instead, the onOpenChange callback receives an additional argument, eventDetails, which contains the trigger element that initiated the state change.

Discover typography, design, or art.

Animating the Preview Card#

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

Position and size

To animate the preview card's position, apply CSS transitions to the left, right, top, and bottom properties of the Positioner part. To animate its size, transition the width and height of the Popup part.

Content

The preview card also supports content transitions. This is useful when different triggers display different content within the same preview card.

To enable content animations, wrap the content in the <PreviewCard.Viewport> part. This part provides features to create direction-aware animations. It renders a div with a data-activation-direction attribute that indicates the new trigger's position relative to the previous one. The value is a space-separated set of up to two tokens (one per axis) — left or right for the horizontal axis and up or down for the vertical axis (for example, right down). Match a single token with the ~= attribute selector, such as [data-activation-direction~='right'].

Inside the <PreviewCard.Viewport>, the content is further wrapped in divs with data attributes to help with styling:

  • data-current: The currently visible content when no transitions are present or the incoming content.
  • data-previous: The outgoing content during a transition.

You can use these attributes to style the enter and exit animations.

Discover typography, design, or art.

API reference#

<PreviewCard.Root>

Groups all parts of the preview card. Doesn't render its own HTML element.

PropTypeDefault
defaultOpenbooleanfalse

Whether the preview card is initially open.

To render a controlled preview card, use the open prop instead.

openboolean—

Whether the preview card is currently open.

onOpenChange| ((open: boolean, eventDetails: PreviewCardRoot.ChangeEventDetails) => void)—

Event handler called when the preview card is opened or closed.

onOpenChangeComplete((open: boolean) => void)—

Event handler called after any animations complete when the preview card is opened or closed.

actionsRefActionsRef<PreviewCardRoot.Actions>—

A ref to imperative actions: a ref object ({ current: null }) or a callback receiving them (and null on unmount). - unmount: Ends the closing phase of the preview card after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the preview card completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the preview card imperatively when called.

handlePreviewCardHandle<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.

triggerIdstring | 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).

defaultTriggerIdstring | 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.

<PreviewCard.Trigger>

A link that opens the preview card. Renders an <a> element.

PropTypeDefault
handlePreviewCardHandle<Payload>—

A handle to associate the trigger with a preview card.

payloadPayload—

A payload to pass to the preview card when it is opened.

delaynumber600

How long to wait before the preview card opens. Specified in milliseconds.

closeDelaynumber300

How long to wait before closing the preview card. Specified in milliseconds.

idstring—

ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the preview card in controlled mode (with the PreviewCardRoot triggerId prop).

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<PreviewCard.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.

PropTypeDefault
keepMountedbooleanfalse

Whether to keep the portal mounted in the DOM while the popup is hidden.

containerPortalContainer—

A parent element to render the portal element into.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<PreviewCard.Positioner>

Positions the popup against the trigger. Renders a <div> element.

PropTypeDefault
anchor| Element | null | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)—

An element to position the popup against. By default, the popup will be positioned against the trigger.

Accepts an element, a virtual element, a ref object, or a function (such as a signal accessor) that returns an element or a virtual element.

positionMethod'absolute' | 'fixed''absolute'

Determines which CSS position property to use.

sideSide'bottom'

Which side of the anchor element to align the popup against. May automatically change to avoid collisions.

sideOffsetnumber | OffsetFunction0

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.

alignAlign'center'

How to align the popup relative to the specified side.

alignOffsetnumber | OffsetFunction0

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.

collisionBoundaryBoundary'clipping-ancestors'

An element or a rectangle that delimits the area that the popup is confined to.

collisionPaddingPadding5

Additional space to maintain from the edge of the collision boundary.

stickybooleanfalse

Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.

arrowPaddingnumber5

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.

disableAnchorTrackingbooleanfalse

Whether to disable the popup from tracking any layout shift of its positioning anchor.

collisionAvoidanceCollisionAvoidance—

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'.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

CSS variableDescription
--available-width

The available width between the trigger and the edge of the viewport.

--available-height

The available height between the trigger and the edge of the viewport.

--anchor-width

The anchor's width.

--anchor-height

The anchor's height.

--transform-origin

The coordinates that this element is anchored to. Used for animations and transitions.

--positioner-width

The width of the preview card's positioner. It is important to set width to this value when using CSS to animate size changes.

--positioner-height

The height of the preview card's positioner. It is important to set height to this value when using CSS to animate size changes.

<PreviewCard.Popup>

A container for the preview card contents. Renders a <div> element.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<PreviewCard.Arrow>

Displays an element positioned against the preview card anchor. Renders a <div> element.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-uncentered

Present when the preview card arrow is uncentered.

<PreviewCard.Backdrop>

An overlay displayed beneath the preview card. Renders a <div> element.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<PreviewCard.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.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-current

Applied to the direct child of the viewport when no transitions are present or the new content when it's entering.

data-previous

Applied to the direct child of the viewport that contains the exiting content when transitions are present.

data-activation-direction

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-transitioning

Indicates that the viewport is currently transitioning between old and new content.

data-instant

Present if animations should be instant.

CSS variableDescription
--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.

--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.

The Viewport is optional — reach for it only when a single popup is opened by multiple triggers, its content differs per trigger, and the switch between them is animated. When used, set width: var(--positioner-width) and height: var(--positioner-height) on the Positioner so its box is frozen to the measured size during the transition; otherwise content-driven resizing can make the popup thrash or flip to another side.

createHandle#

PreviewCard.createHandle<Payload>() creates a PreviewCard.Handle that connects detached triggers to a root. The handle exposes open(triggerId), close() and a reactive isOpen getter.