Components

Preview card

More about a trigger, shown on hover or keyboard focus

Import
import { PreviewCard, PreviewCardContent, PreviewCardTrigger } from "@nyte-ai/ui/preview-card";

A card with more about its trigger. For one line, use Tooltip.

Usage

  • Touch never opens the card, so repeat its content elsewhere.

Anatomy

<PreviewCard>
  <PreviewCardTrigger />
  <PreviewCardContent />
</PreviewCard>

Examples

Details for a row

import { Button } from "@nyte-ai/ui/button";
import { Icon } from "@nyte-ai/ui/icon";
import { PreviewCard, PreviewCardContent, PreviewCardTrigger } from "@nyte-ai/ui/preview-card";
import { glyph } from "@nyte-ai/ui/styles/schema.stylex";
import { role, type } from "@nyte-ai/ui/styles/vars.stylex";
import { create, props } from "@stylexjs/stylex";

const styles = create({
  title: { fontWeight: 500 },
  detail: {
    display: "grid",
    gridTemplateColumns: `${glyph.sm} minmax(0, 1fr)`,
    alignItems: "center",
    gap: 6,
    marginBlockStart: 6,
    color: role.contentSecondary,
    fontSize: type.fontSm,
    lineHeight: type.leadingSm,
  },
  detailIcon: { display: "grid", placeItems: "center", color: role.contentTertiary },
});

<PreviewCard>
  <PreviewCardTrigger render={<Button variant="outline">Fix flaky tests</Button>} />
  <PreviewCardContent side="right" align="start" aria-label="Details for Fix flaky tests">
    <span {...props(styles.title)}>Fix flaky tests</span>
    <span {...props(styles.detail)}>
      <span {...props(styles.detailIcon)}>
        <Icon name="git-branch" size={14} />
      </span>
      <span>nyte-ai/nyte</span>
    </span>
    <span {...props(styles.detail)}>
      <span {...props(styles.detailIcon)}>
        <Icon name="folder" size={14} />
      </span>
      <span>~/Developer/nyte</span>
    </span>
  </PreviewCardContent>
</PreviewCard>;

A pasted snippet

const styles = create({
  snippet: {
    maxHeight: 240,
    overflowY: "auto",
    color: role.contentSecondary,
    fontFamily: type.fontMono,
    fontSize: type.fontXs,
    lineHeight: type.leadingXs,
    whiteSpace: "pre-wrap",
  },
});

<PreviewCard>
  <PreviewCardTrigger render={chip} />
  <PreviewCardContent side="top" xstyle={styles.snippet}>
    {snippet}
  </PreviewCardContent>
</PreviewCard>;

One card for a list

import {
  createPreviewCardHandle,
  PreviewCard,
  PreviewCardContent,
  PreviewCardTrigger,
} from "@nyte-ai/ui/preview-card";

const [handle] = useState(() => createPreviewCardHandle<Suggestion>());

<>
  {suggestions.map((suggestion) => (
    <PreviewCardTrigger
      key={suggestion.id}
      handle={handle}
      payload={suggestion}
      delay={0}
      render={<div role="option">{suggestion.label}</div>}
    />
  ))}
  <PreviewCard handle={handle}>
    {({ payload }) =>
      payload === undefined ? null : (
        <PreviewCardContent side="right" align="end" sideOffset={6}>
          {payload.description}
        </PreviewCardContent>
      )
    }
  </PreviewCard>
</>;

Props

PreviewCardContent takes xstyle, className, and style. Its className and style cannot be functions of state. See Theming. PreviewCard, PreviewCardTrigger, and createPreviewCardHandle are Base UI's.

The reference tables below come from Base UI, MIT, © Material-UI SAS.

PreviewCard

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

Root Props:

PropTypeDefaultDescription
defaultOpenbooleanfalseWhether 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: PreviewCard.Root.ChangeEventDetails) => void)-Event handler called when the preview card is opened or closed.
actionsRefReact.RefObject<PreviewCard.Root.Actions | null>-A ref to imperative actions. unmount: Unmounts the preview card popup.close: Closes the preview card imperatively when called.
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.
handlePreviewCard.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.
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).
childrenReact.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.

PreviewCardTrigger

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

Trigger Props:

PropTypeDefaultDescription
handlePreviewCard.Handle<Payload>-A handle to associate the trigger with a preview card.
payloadPayload-A payload to pass to the preview card when it is opened.
delaynumber600How long to wait before the preview card opens. Specified in milliseconds.
closeDelaynumber300How long to wait before closing the preview card. Specified in milliseconds.
classNamestring | ((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.
styleReact.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.
renderReactElement | ((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:

AttributeTypeDescription
data-popup-open-Present when the corresponding preview card is open.

PreviewCardContent

Renders the portal, positioner, and popup. Positioning props go to the positioner and the rest to the popup.

PropTypeDefault
side"top" | "bottom" | "left" | "right" | "inline-start" | "inline-end""bottom"
align"start" | "center" | "end""center"
sideOffsetnumber | OffsetFunction4
alignOffsetnumber | OffsetFunction
collisionAvoidanceCollisionAvoidance
tintTint

Positioner

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

Positioner Props:

PropTypeDefaultDescription
disableAnchorTrackingbooleanfalseWhether to disable the popup from tracking any layout shift of its positioning anchor.
alignAlign'center'How to align the popup relative to the specified side.
alignOffsetnumber | OffsetFunction0Additional 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.
sideSide'bottom'Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
sideOffsetnumber | OffsetFunction0Distance 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.
arrowPaddingnumber5Minimum 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.
anchorElement | 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.
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'.
collisionBoundaryBoundary'clipping-ancestors'An element or a rectangle that delimits the area that the popup is confined to.
collisionPaddingPadding5Additional space to maintain from the edge of the collision boundary.
stickybooleanfalseWhether 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.
classNamestring | ((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.
styleReact.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.
renderReactElement | ((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:

AttributeTypeDescription
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:

VariableTypeDescription
--anchor-heightnumberThe anchor's height.
--anchor-widthnumberThe anchor's width.
--available-heightnumberThe available height between the trigger and the edge of the viewport.
--available-widthnumberThe available width between the trigger and the edge of the viewport.
--positioner-heightnumberThe height of the preview card's positioner. It is important to set height to this value when using CSS to animate size changes.
--positioner-widthnumberThe width of the preview card's positioner. It is important to set width to this value when using CSS to animate size changes.
--transform-originstringThe coordinates that this element is anchored to. Used for animations and transitions.

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

Popup Props:

PropTypeDefaultDescription
classNamestring | ((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.
styleReact.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.
renderReactElement | ((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:

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

createPreviewCardHandle

Creates a new handle to connect a PreviewCard.Root with detached PreviewCard.Trigger components.

Return Value:

type ReturnValue = PreviewCard.Handle<Payload>;

Accessibility

  • Keyboard focus on the trigger opens the card after delay. Escape and an outside press close it.
  • The card appears without motion under prefers-reduced-motion: reduce.