# Hover Card

A preview that opens when its trigger is hovered or focused.

A preview surface that opens when its trigger is hovered or focused and stays open while the pointer is inside it. Built for previews: an optional mono header with a title and +added −removed counts, and tinted lines for a file diff; anything else goes in the body. Behaviour comes from the Zag hover-card machine, through Ark UI in React, Vue and Svelte.

Kind: component · Category: Overlays · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `hover-card` machine, through Ark UI

## Add

```bash
npx fndn add hover-card
```

It writes the folder `hover-card/` into the project's ui folder (`index.ts`, `hover-card.tsx`, `HoverCard.vue`, `HoverCard.svelte`, `hover-card.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { HoverCard } from "@/components/ui/hover-card";
```

```vue
import { HoverCard } from "@/components/ui/hover-card";
```

```svelte
import { HoverCard } from "$lib/components/ui/hover-card";
```

## Parts

In the DOM the root carries `data-slot="hover-card"` and each part `data-slot="hover-card-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `hover-card` |  | Layout-neutral hook around the trigger (display: contents) |
| `trigger` | `button` | `hover-card-trigger` |  | data-state=open\|closed. The default trigger is a small mono chip. A label that is a path or a file name is cut in the middle while it does not fit ("packages/…/Billing.tsx": the file name stays), with the full text in title and aria-label while cut; any other label keeps its end ellipsis. |
| `positioner` | `div` | `hover-card-positioner` |  | Placed by the engine; rendered in a portal. Exposes --transform-origin and --available-height |
| `content` | `div` | `hover-card-content` |  | The surface: the shared popup surface (`lib/popup.ts`: shadow.raised, motion, keyboard-instant; stacking from base.css); data-state=open\|closed. Keeps its 288px width, no padding of its own (the sections pad themselves) and a 420px limit (--popup-max-height). |
| `header` | `div` | `hover-card-header` | optional |  |
| `title` | `span` | `hover-card-title` | optional |  |
| `meta` | `span` | `hover-card-meta` | optional |  |
| `add` | `span` | `hover-card-add` | optional |  |
| `del` | `span` | `hover-card-del` | optional |  |
| `lines` | `div` | `hover-card-lines` | optional |  |
| `line` | `div` | `hover-card-line` | optional, repeats | data-tone=add\|del\|ctx |
| `sign` | `span` | `hover-card-sign` | optional |  |
| `text` | `span` | `hover-card-text` | optional |  |
| `body` | `div` | `hover-card-body` | optional |  |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | `false` | Bindable (React: `defaultOpen` and `onOpenChange`; Vue: `v-model:open`; Svelte: `bind:open`).  |
| `title` | `string` |  | Header text, e.g. a file name. |
| `add` | `number` |  | Lines added, shown in the header. |
| `del` | `number` |  | Lines removed, shown in the header. |
| `lines` | `{ text: string; tone?: 'add' \| 'del' \| 'ctx' }[]` |  | Preformatted lines; add and del lines are tinted and signed. |
| `tone` | `"ctx" \| "add" \| "del"` | `"ctx"` | Per line. |
| `placement` | `"top" \| "top-start" \| "top-end" \| "bottom" \| "bottom-start" \| "bottom-end" \| "left" \| "right"` | `"bottom-start"` | Preferred side; flips when there is no room. |
| `openDelay` | `number` | `500` | Delay before opening, in ms. |
| `closeDelay` | `number` | `150` | Delay before closing, in ms. |
| `triggerLabel` | `string` |  | Renders the default chip trigger with this label. Otherwise pass your own trigger. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `openChange` | (open: boolean) |

## Slots

| Slot | Content |
| --- | --- |
| `default` | Body content |
| `trigger` | Your own trigger element (React `trigger` prop, Vue `trigger` slot, Svelte `trigger` snippet that spreads the props it receives) |

## States

`open`, `hover`, `press`, `focus-visible`

## Keyboard and accessibility

Keyboard:

- Focus on the trigger opens the card; blur closes it
- Escape closes it
- The card is a preview: anything essential in it must also be reachable another way

Notes:

- Not shown on touch.
- Opening from keyboard focus is instant.

## Motion

- surface: the shared popup recipe (`popup`): opacity + scale from motion.popup-scale out of the trigger, duration.popup, easing.out-strong; closes at duration.fast; instant from the keyboard
- default trigger: colour over duration.fast, press scale over duration.press
- mounted open (defaultOpen, or open at mount): the surface is drawn at once, with no entrance (data-mounted-open until it first closes); its close, and every later open, animate as above

## Tokens

`color.surface`, `color.ink`, `color.ink-2`, `color.hover`, `color.line`, `color.green`, `color.green-tint`, `color.red`, `color.red-tint`, `shadow.btn`, `radius.chip`, `font.mono`, `text.2xs`, `duration.fast`, `duration.press`, `easing.out-strong`, `motion.press-scale`

## Examples

### Pull request link in an activity line

React · `pull-request.tsx`

```tsx
import { HoverCard } from "@/components/ui/hover-card";
import { StatusPill } from "@/components/ui/status-pill";

export default function PullRequest() {
  return (
    <div className="flex w-80 flex-wrap items-center gap-x-1.5 gap-y-1 text-sm text-ink-2">
      <span>Daniel Okafor opened</span>
      <HoverCard triggerLabel="#482" title="feat/webhook-retry → main" add={128} del={14}>
        <div className="flex flex-col gap-2">
          <span className="font-medium text-ink">Add retry to webhook delivery</span>
          <span className="text-ink-2">Retries failed deliveries 5 times with backoff, then marks the endpoint unhealthy.</span>
          <StatusPill tone="green" className="self-start">Checks passing</StatusPill>
        </div>
      </HoverCard>
      <span>2 hours ago</span>
    </div>
  );
}
```

Vue · `pull-request.vue`

```vue
<script setup lang="ts">
import { HoverCard } from "@/components/ui/hover-card";
import { StatusPill } from "@/components/ui/status-pill";
</script>

<template>
  <div class="flex w-80 flex-wrap items-center gap-x-1.5 gap-y-1 text-sm text-ink-2">
    <span>Daniel Okafor opened</span>
    <HoverCard triggerLabel="#482" title="feat/webhook-retry → main" :add="128" :del="14">
      <div class="flex flex-col gap-2">
        <span class="font-medium text-ink">Add retry to webhook delivery</span>
        <span class="text-ink-2">Retries failed deliveries 5 times with backoff, then marks the endpoint unhealthy.</span>
        <StatusPill tone="green" class="self-start">Checks passing</StatusPill>
      </div>
    </HoverCard>
    <span>2 hours ago</span>
  </div>
</template>
```

Svelte · `pull-request.svelte`

```svelte
<script lang="ts">
  import { HoverCard } from "$lib/components/ui/hover-card";
  import { StatusPill } from "$lib/components/ui/status-pill";
</script>

<div class="flex w-80 flex-wrap items-center gap-x-1.5 gap-y-1 text-sm text-ink-2">
  <span>Daniel Okafor opened</span>
  <HoverCard triggerLabel="#482" title="feat/webhook-retry → main" add={128} del={14}>
    <div class="flex flex-col gap-2">
      <span class="font-medium text-ink">Add retry to webhook delivery</span>
      <span class="text-ink-2">Retries failed deliveries 5 times with backoff, then marks the endpoint unhealthy.</span>
      <StatusPill tone="green" class="self-start">Checks passing</StatusPill>
    </div>
  </HoverCard>
  <span>2 hours ago</span>
</div>
```

### Changed file with its diff

React · `changed-file.tsx`

```tsx
import { HoverCard, type HoverCardLine } from "@/components/ui/hover-card";

const lines: HoverCardLine[] = [
  { text: "for (let attempt = 1; ; attempt++) {", tone: "ctx" },
  { text: "  await send(endpoint, event);", tone: "del" },
  { text: "  const res = await send(endpoint, event);", tone: "add" },
  { text: "  if (res.ok || attempt === MAX_ATTEMPTS) break;", tone: "add" },
  { text: "  await sleep(backoff(attempt));", tone: "add" },
];

export default function ChangedFile() {
  return (
    <HoverCard
      triggerLabel="src/webhooks/deliver.ts"
      title="src/webhooks/deliver.ts"
      add={24}
      del={6}
      lines={lines}
    />
  );
}
```

Vue · `changed-file.vue`

```vue
<script setup lang="ts">
import { HoverCard, type HoverCardLine } from "@/components/ui/hover-card";

const lines: HoverCardLine[] = [
  { text: "for (let attempt = 1; ; attempt++) {", tone: "ctx" },
  { text: "  await send(endpoint, event);", tone: "del" },
  { text: "  const res = await send(endpoint, event);", tone: "add" },
  { text: "  if (res.ok || attempt === MAX_ATTEMPTS) break;", tone: "add" },
  { text: "  await sleep(backoff(attempt));", tone: "add" },
];
</script>

<template>
  <HoverCard
    triggerLabel="src/webhooks/deliver.ts"
    title="src/webhooks/deliver.ts"
    :add="24"
    :del="6"
    :lines="lines"
  />
</template>
```

Svelte · `changed-file.svelte`

```svelte
<script lang="ts">
  import { HoverCard, type HoverCardLine } from "$lib/components/ui/hover-card";

  const lines: HoverCardLine[] = [
    { text: "for (let attempt = 1; ; attempt++) {", tone: "ctx" },
    { text: "  await send(endpoint, event);", tone: "del" },
    { text: "  const res = await send(endpoint, event);", tone: "add" },
    { text: "  if (res.ok || attempt === MAX_ATTEMPTS) break;", tone: "add" },
    { text: "  await sleep(backoff(attempt));", tone: "add" },
  ];
</script>

<HoverCard
  triggerLabel="src/webhooks/deliver.ts"
  title="src/webhooks/deliver.ts"
  add={24}
  del={6}
  {lines}
/>
```

### Teammate profile

React · `teammate.tsx`

```tsx
import { Avatar } from "@/components/ui/avatar";
import { HoverCard } from "@/components/ui/hover-card";

const person = {
  name: "Priya Raman",
  role: "Staff engineer, Platform",
  email: "priya@northwind.example",
  localTime: "16:42 in Berlin",
};

export default function Teammate() {
  return (
    <div className="flex flex-wrap items-center gap-x-1 text-sm text-ink-2">
      <span>Approved by</span>
      <HoverCard
        placement="top-start"
        trigger={<button type="button" className="font-medium text-ink underline decoration-line-strong underline-offset-2">{person.name}</button>}
      >
        <div className="flex items-start gap-3">
          <Avatar name={person.name} size={20} />
          <div className="flex flex-col gap-0.5">
            <span className="font-medium text-ink">{person.name}</span>
            <span className="text-ink-2">{person.role}</span>
            <span className="text-ink-2">{person.email} · {person.localTime}</span>
          </div>
        </div>
      </HoverCard>
      <span>· 2 min ago</span>
    </div>
  );
}
```

Vue · `teammate.vue`

```vue
<script setup lang="ts">
import { Avatar } from "@/components/ui/avatar";
import { HoverCard } from "@/components/ui/hover-card";

const person = {
  name: "Priya Raman",
  role: "Staff engineer, Platform",
  email: "priya@northwind.example",
  localTime: "16:42 in Berlin",
};
</script>

<template>
  <div class="flex flex-wrap items-center gap-x-1 text-sm text-ink-2">
    <span>Approved by</span>
    <HoverCard placement="top-start">
      <template #trigger>
        <button type="button" class="font-medium text-ink underline decoration-line-strong underline-offset-2">{{ person.name }}</button>
      </template>
      <div class="flex items-start gap-3">
        <Avatar :name="person.name" :size="20" />
        <div class="flex flex-col gap-0.5">
          <span class="font-medium text-ink">{{ person.name }}</span>
          <span class="text-ink-2">{{ person.role }}</span>
          <span class="text-ink-2">{{ person.email }} · {{ person.localTime }}</span>
        </div>
      </div>
    </HoverCard>
    <span>· 2 min ago</span>
  </div>
</template>
```

Svelte · `teammate.svelte`

```svelte
<script lang="ts">
  import { Avatar } from "$lib/components/ui/avatar";
  import { HoverCard } from "$lib/components/ui/hover-card";

  const person = {
    name: "Priya Raman",
    role: "Staff engineer, Platform",
    email: "priya@northwind.example",
    localTime: "16:42 in Berlin",
  };
</script>

<div class="flex flex-wrap items-center gap-x-1 text-sm text-ink-2">
  <span>Approved by</span>
  <HoverCard placement="top-start">
    {#snippet trigger(props)}
      <button {...props()} type="button" class="font-medium text-ink underline decoration-line-strong underline-offset-2">{person.name}</button>
    {/snippet}
    <div class="flex items-start gap-3">
      <Avatar name={person.name} size={20} />
      <div class="flex flex-col gap-0.5">
        <span class="font-medium text-ink">{person.name}</span>
        <span class="text-ink-2">{person.role}</span>
        <span class="text-ink-2">{person.email} · {person.localTime}</span>
      </div>
    </div>
  </HoverCard>
  <span>· 2 min ago</span>
</div>
```

## Recipe

`hover-card.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { popup } from "../lib/popup";

/* hover-card — a preview surface that opens when its trigger is hovered or focused and stays
 * open while the pointer is inside. Optional preview parts: a mono header (title + counts) and
 * tinted lines for a file diff; anything else goes in the body.
 * Behaviour comes from the Zag hover-card machine. The surface lives in a portal.
 *
 * The surface and its motion are the shared popup recipe (lib/popup.ts), composed into the content;
 * its stacking comes from base.css; the card keeps its width and a taller limit, and its
 * sections carry their own padding. A line's tone is a variant of the line part. */

export const hoverCard = tv({
  slots: {
    base: "contents",
    positioner: "max-w-[calc(100vw-16px)]",
    /* default trigger: a small mono chip (bring your own trigger for anything else). A block of
     * text, not a flex box, so a long file name can end in an ellipsis. Opened from the keyboard
     * (focus), it takes its open colour at once, like the card. */
    trigger: [
      "inline-block max-w-[min(100%,320px)] h-7 overflow-hidden text-ellipsis align-middle px-2",
      "rounded-chip bg-surface shadow-btn font-mono text-[11.5px] leading-7 whitespace-nowrap text-ink",
      "hover:bg-hover data-[state=open]:bg-hover active:scale-press",
      "motion-colors-fast motion-transform-press kbd-instant",
    ],
    content: [popup(), "[--popup-max-height:420px] w-72 p-0"],
    /* the title wraps, so the counts sit on its first line (same font size and line height) */
    header: "flex items-start justify-between gap-2 px-2.5 py-1.5 border-b border-line font-mono text-2xs leading-normal",
    /* the card is where a trigger's cut path is read in full: the title wraps (a path breaks
     * anywhere), it is never cut a second time */
    title: "min-w-0 wrap-anywhere text-ink-2",
    meta: "shrink-0 tabular-nums whitespace-nowrap",
    add: "text-green",
    /* after the added count */
    del: "text-red not-first:ms-1.5",
    lines: "py-1 font-mono text-2xs leading-[1.8]",
    line: [
      "flex gap-2 px-2.5 whitespace-pre text-ink-2",
    ],
    sign: "shrink-0 w-3 select-none",
    text: "min-w-0 overflow-hidden text-ellipsis",
    body: "px-3 py-2.5 text-dense leading-normal text-ink-2 wrap-anywhere",
  },
  variants: {
    tone: {
      ctx: {},
      add: { line: "bg-green-tint text-green" },
      del: { line: "bg-red-tint text-red" },
    },
  },
  defaultVariants: { tone: "ctx" },
});

export type HoverCardRecipeProps = VariantProps<typeof hoverCard>;
```
