---
title: "Hover Card · UI SDK — StyleX Blocks"
description: "Rich preview panels revealed from a trigger, in four padding sizes."
url: "https://stylexblocks.com/components/ui-sdk/hover-card/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Hover Card

Rich preview panels revealed from a trigger, in four padding sizes.

- Behavior engine: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/hover-card.md), [Base UI](https://stylexblocks.com/components/base-ui/hover-card.md)
- Category: Display
- Sizes: xs, sm, md, lg
- Used in blocks: [Project Status Dashboard](https://stylexblocks.com/block/ui-sdk/project-status-dashboard.md)
- HTML page: https://stylexblocks.com/components/ui-sdk/hover-card/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add hover-card
```

Equivalent: `npx shadcn add @stylexblocks/hover-card`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/hover-card.json

## Usage

```tsx
import { HoverCard } from "#components/ui/ui-sdk/hover-card.tsx";

// UI SDK's hover card: the trigger is a link, and Content opens in the
// browser's top layer, so there is no Portal or Positioner.
export function Example() {
  return (
    <HoverCard.Root size="md" delay={200} closeDelay={300}>
      <HoverCard.Trigger href="https://x.com/stylexblocks">
        @stylexblocks
      </HoverCard.Trigger>
      <HoverCard.Content sideOffset={8}>
        <strong>StyleX Blocks</strong>
        <p>Production-ready blocks and themes, compiled with StyleX.</p>
      </HoverCard.Content>
    </HoverCard.Root>
  );
}
```

## Source

`components/ui/ui-sdk/hover-card.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import {
  HoverCard as UiSdkHoverCard,
  HoverCardArrow,
  HoverCardContent,
  HoverCardTrigger,
} from "@ui-sdk/react/hover-card";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import { defineSlotRecipe, resolveSlotStyles } from "#lib/recipe-props.ts";
import { semanticShadows } from "@stylexblocks/ui/semantic-tokens/shadows.stylex.ts";
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'

const hoverCardContent = stylex.create({
  base: {
    position: 'relative',
    display: 'flex',
    flexDirection: 'column',
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    '--hovercard-bg': semanticColors.bgPanel,
    backgroundColor: 'var(--hovercard-bg)',
    maxWidth: sizes._80,
    '--hover-card-z-index': zIndices.layer2,
    zIndex: 'calc(var(--hover-card-z-index) + var(--layer-index, 0))',
    transformOrigin: 'var(--transform-origin)',
    outline: 'none',
    '--hover-card-radius': semanticRadii.panel,
    '--hover-card-concentric-radius':
      'max(0px, calc(var(--hover-card-radius) - var(--hover-card-padding)))',
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'scale-in, fade-in',
      ':is([data-closed], [data-state="closed"])': 'scale-out, fade-out',
    },
    animationDuration: {
      default: null,
      ':is([data-open], [data-state="open"])': durations.fast,
      ':is([data-closed], [data-state="closed"])': durations.faster,
    },
  },
})

const hoverCardContentSizes = stylex.create({
  xs: {
    '--hover-card-padding': spacing._3,
    padding: 'var(--hover-card-padding)',
  },
  sm: {
    '--hover-card-padding': spacing._4,
    padding: 'var(--hover-card-padding)',
  },
  md: {
    '--hover-card-padding': spacing._5,
    padding: 'var(--hover-card-padding)',
  },
  lg: {
    '--hover-card-padding': spacing._6,
    padding: 'var(--hover-card-padding)',
  },
})

const hoverCardArrow = stylex.create({
  base: {
    '--arrow-size': sizes._3,
    '--arrow-background': 'var(--hovercard-bg)',
  },
})

const hoverCardArrowTip = stylex.create({
  base: {
    borderTopWidth: '0.5px',
    borderInlineStartWidth: '0.5px',
  },
})

export type HoverCardSize = keyof typeof hoverCardContentSizes

// defineSlotRecipe validates the shape at compile time: default values must
// exist in the axis maps, and slot keys must be `styles` or a declared axis
const hoverCardRecipe = defineSlotRecipe({
  slots: {
    content: { styles: hoverCardContent, sizes: hoverCardContentSizes },
    arrow: { styles: hoverCardArrow },
    arrowTip: { styles: hoverCardArrowTip },
  },
  defaultVariants: { size: "md" },
});

// Local chrome the recipe leaves to the composition layer: the panel surface
// and a z-index that clears sticky page chrome — the generated `layer2` (10)
// would sit beneath a `z-index: 50` site header. UI SDK's content is a
// native popover in the top layer, so the surface also clears the browser's
// popover margin, border and overflow, and only takes the recipe's
// `display: flex` while open (the recipe sizes set the padding).
const chrome = stylex.create({
  content: {
    "--hover-card-z-index": zIndices.popover,
    display: {
      default: null,
      ":popover-open": "flex",
    },
    margin: 0,
    borderWidth: 0,
    overflow: "visible",
    color: "inherit",
    borderRadius: "var(--hover-card-radius)",
    boxShadow: semanticShadows.overlay,
  },
  // UI SDK places the arrow along the edge (`left`/`top`); the cross axis,
  // the diamond, and its color are ours.
  arrow: {
    width: "var(--arrow-size)",
    height: "var(--arrow-size)",
    backgroundColor: "var(--arrow-background)",
    transform: "rotate(45deg)",
    top: {
      default: null,
      ':is([data-side="bottom"])': "calc(var(--arrow-size) / -2)",
    },
    bottom: {
      default: null,
      ':is([data-side="top"])': "calc(var(--arrow-size) / -2)",
    },
    left: {
      default: null,
      ':is([data-side="right"])': "calc(var(--arrow-size) / -2)",
    },
    right: {
      default: null,
      ':is([data-side="left"])': "calc(var(--arrow-size) / -2)",
    },
  },
});

const { withRootProvider, withContext } = createSlotRecipeContext(hoverCardRecipe);

const StyledContent = withContext(
  HoverCardContent,
  (v) => [...resolveSlotStyles(hoverCardRecipe.slots.content, v), chrome.content],
  "HoverCardContent",
);

/** Content defaults to an 8px gap from the trigger, like Ark's positioner. */
function Content(allProps: React.ComponentProps<typeof StyledContent>) {
  const { sideOffset = 8, ...rest } = allProps;
  return <StyledContent sideOffset={sideOffset} {...rest} />;
}
Content.displayName = "HoverCardContent";

/**
 * Hover Card — UI SDK's hover card wearing the hover-card slot recipe. The
 * trigger is a link (give it an `href`); the content is a native popover in
 * the browser's top layer, so there is no Portal or Positioner: placement
 * props go on `Content`. Hover intent with `delay` / `closeDelay` (on Root)
 * comes from the engine. `<HoverCard.Root size>` resolves the recipe once;
 * parts inherit it through context and compose via `render`.
 */
export const HoverCard = {
  Root: withRootProvider(UiSdkHoverCard, "HoverCardRoot"),
  Trigger: HoverCardTrigger,
  Content,
  Arrow: withContext(
    HoverCardArrow,
    (v) => [...resolveSlotStyles(hoverCardRecipe.slots.arrow, v), chrome.arrow],
    "HoverCardArrow",
  ),
};
```
