# Hover Card

> A preview of what a link leads to, on hover or focus, such as the person behind a mention.

Page: https://uicontext.com/docs/components/hover-card

Group: Overlays.

## When to use

A preview of what a link leads to, on hover or focus, such as the person behind a mention. Use [Tooltip](https://uicontext.com/docs/components/tooltip.md) instead when the hint is one short line.

## Usage

```ts
import { HoverCard, HoverCardState } from "./ui/uicontext/components/HoverCard.ts"

const Message = UiSchema.TaggedEnum({ ChangedPreview: { preview: HoverCardState } })

export const Mention = (h: Html.H<typeof Message.Type>, preview: HoverCardState) =>
	HoverCard(h, {
		state: preview,
		onState: (next) => Message.ChangedPreview({ preview: next }),
		card: () => h.div.children("Ada Lovelace · Staff engineer"),
		view: (link) => h.p.children("Thanks ", link(h.a.attr("href", "/people/ada"), "ada").children("@Ada"), "!"),
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### People in a comment

The example the page opens with: Hover Card in “People in a comment”.

This example also uses Avatar. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/avatar
```

```ts title="src/demo.ts" source="www/src/examples/hover-card/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Avatar } from "./ui/uicontext/components/Avatar.ts"
import { HoverCard, hoverCardClosed, HoverCardState } from "./ui/uicontext/components/HoverCard.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"
import { ada, ben, priya } from "./people.ts"
import { Schema } from "effect"

const styles = stylex.create({
	comment: { margin: 0, maxInlineSize: "34rem", fontSize: font.base, lineHeight: 1.6, color: color.fg },
	card: { display: "grid", gridTemplateColumns: "auto minmax(0, 1fr)", columnGap: space.md, rowGap: space.xs, alignItems: "center" },
	name: { fontWeight: 600, overflowWrap: "anywhere" },
	role: { fontSize: font.sm, color: color.muted },
	details: { gridColumnStart: "1", gridColumnEnd: "-1", display: "grid", gap: 2, marginBlockStart: space.sm, fontSize: font.sm, color: color.muted },
})

const people = {
	ada: { ...ada, role: "Staff engineer · Payments", time: "9:42 in London", status: "Reviewing the refunds RFC" },
	ben: { ...ben, role: "Product designer · Checkout", time: "4:42 in Lagos", status: "Away until Monday" },
	priya: { ...priya, role: "Engineering manager · Payments", time: "14:12 in Bengaluru", status: "In a meeting until 15:00" },
} as const

const Message = UiSchema.TaggedEnum({ ChangedPreview: { preview: HoverCardState } })

// a comment's @mentions: rest the pointer on a name, or Tab to it, to see who it is
export const MentionPreview = Html.component({
	name: "MentionPreview",
	model: Schema.Struct({ preview: HoverCardState }),
	message: Message,
}).make({
	init: () => ({ preview: hoverCardClosed }),
	update: { ChangedPreview: ($, { preview }) => $.evolve({ preview }) },
	view: (h, model) =>
		HoverCard(h, {
			state: model.preview,
			onState: (preview) => Message.ChangedPreview({ preview }),
			card: (id) => {
				const person = people[id as keyof typeof people]
				return h.div.attrs(sx(styles.card)).children(
					Avatar(h, { ...person, size: "lg", decorative: true }),
					h.div.children(h.div.attrs(sx(styles.name)).children(person.name), h.div.attrs(sx(styles.role)).children(person.role)),
					h.div.attrs(sx(styles.details)).children(h.span.children(person.status), h.span.children(person.time)),
				)
			},
			view: (link) =>
				h.p.attrs(sx(styles.comment)).children(
					"Refunds over $500 still need a second approver. ",
					link(h.a.attr("href", "#people/ada"), "ada").children("@Ada Lovelace"),
					" can you check the ledger side, and ",
					link(h.a.attr("href", "#people/ben"), "ben").children("@Ben Okafor"),
					" the confirmation screen? ",
					link(h.a.attr("href", "#people/priya"), "priya").children("@Priya Raman"),
					" for the rollout date.",
				),
		}),
})
```

### Issues in release notes

A shorter `delay` suits a list of links people scan. Moving to the next link moves the open card at once.

This example also uses Badge. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/badge
```

```ts title="src/issues.ts" source="www/src/examples/hover-card/issues.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Badge } from "./ui/uicontext/components/Badge.ts"
import { HoverCard, hoverCardClosed, HoverCardState } from "./ui/uicontext/components/HoverCard.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	list: { margin: 0, paddingInlineStart: space.xl, display: "grid", gap: space.sm, maxInlineSize: "34rem", fontSize: font.base, lineHeight: 1.5 },
	card: { display: "grid", gap: space.sm },
	top: { display: "flex", alignItems: "center", gap: space.sm, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },
	title: { fontWeight: 600, overflowWrap: "anywhere" },
	meta: { fontSize: font.sm, color: color.muted },
})

const issues = {
	"ENG-142": { title: "Checkout fails when the coupon field has a trailing space", status: "In review", tone: "info", meta: "Ada Lovelace · Payments · 3 comments" },
	"ENG-139": { title: "Export invoices as CSV", status: "Done", tone: "success", meta: "Ben Okafor · Billing · shipped in 4.12" },
	"ENG-128": { title: "Tax rates for Québec are a cent off on orders over $10,000", status: "Blocked", tone: "danger", meta: "Mira Patel · Billing · waiting on finance" },
} as const

const Message = UiSchema.TaggedEnum({ ChangedPreview: { preview: HoverCardState } })

// release notes that name issues: the card shows each one's title and status without leaving the page
export const ReleaseNotes = Html.component({
	name: "ReleaseNotes",
	model: Schema.Struct({ preview: HoverCardState }),
	message: Message,
}).make({
	init: () => ({ preview: hoverCardClosed }),
	update: { ChangedPreview: ($, { preview }) => $.evolve({ preview }) },
	view: (h, model) =>
		HoverCard(h, {
			state: model.preview,
			onState: (preview) => Message.ChangedPreview({ preview }),
			delay: 300,
			card: (id) => {
				const issue = issues[id as keyof typeof issues]
				return h.div.attrs(sx(styles.card)).children(
					h.div.attrs(sx(styles.top)).children(id, Badge(h, { tone: issue.tone, size: "sm", dot: true }).children(issue.status)),
					h.div.attrs(sx(styles.title)).children(issue.title),
					h.div.attrs(sx(styles.meta)).children(issue.meta),
				)
			},
			view: (link) =>
				h.ul.attrs(sx(styles.list)).children(
					h.li.children("Coupons with spaces apply again (", link(h.a.attr("href", "#issues/ENG-142"), "ENG-142").children("ENG-142"), ")"),
					h.li.children("Invoices download as CSV from Billing (", link(h.a.attr("href", "#issues/ENG-139"), "ENG-139").children("ENG-139"), ")"),
					h.li.children("Known issue: Québec tax on large orders (", link(h.a.attr("href", "#issues/ENG-128"), "ENG-128").children("ENG-128"), ")"),
				),
		}),
})
```

### Sample data

The examples above import these files.

```ts title="src/people.ts" source="www/src/examples/_shared/people.ts"
import type { AvatarPerson } from "./ui/uicontext/components/Avatar.ts"

// People for avatars, pickers and mentions. Pictures are drawn inline, a head and shoulders on a
// soft backdrop, so the examples need no image files.

const portrait = (backdrop: string, skin: string, shirt: string, hair: string) =>
	`data:image/svg+xml,${encodeURIComponent(
		`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="${backdrop}"/><path d="M10 64c2-13 11-19 22-19s20 6 22 19z" fill="${shirt}"/><circle cx="32" cy="27" r="12" fill="${skin}"/><path d="M19 26c0-9 6-14 13-14s13 5 13 14c-3-5-8-7-13-7s-10 2-13 7z" fill="${hair}"/></svg>`,
	)}`

export const ada: AvatarPerson = { id: "ada", name: "Ada Lovelace", image: portrait("#dbe7f7", "#f1c7a5", "#3b5b8c", "#4a2f22"), presence: "active" }
export const ben: AvatarPerson = { id: "ben", name: "Ben Okafor", image: portrait("#f3e3d3", "#8d5a3b", "#2f6b4f", "#1d1410"), presence: "away" }
export const mira: AvatarPerson = { id: "mira", name: "Mira Patel", presence: "dnd" }
export const sam: AvatarPerson = { id: "sam", name: "Sam Rivera", image: portrait("#e6e1f5", "#d9a57e", "#7a3e8c", "#2b1c14"), presence: "offline" }
export const kenji: AvatarPerson = { id: "kenji", name: "Kenji Watanabe" }
export const lena: AvatarPerson = { id: "lena", name: "Lena Fischer", image: portrait("#e3f1e8", "#f4d2b8", "#b0413e", "#c8a165") }
export const omar: AvatarPerson = { id: "omar", name: "Omar Haddad" }
export const priya: AvatarPerson = { id: "priya", name: "Priya Raman", presence: "active" }
export const scout: AvatarPerson = { id: "scout", name: "Scout", agent: true }

export const reviewers: ReadonlyArray<AvatarPerson> = [ada, ben, mira, sam, kenji, lena, omar]

/** Names that break layouts, and a picture that never loads. */
export const worstPeople: ReadonlyArray<AvatarPerson> = [
	{ id: "aleksandra", name: "Aleksandra Wiśniewska-Kowalczyk", presence: "active" },
	{ id: "christopher", name: "Christopher Alexander Montgomery III" },
	{ id: "j", name: "J" },
	{ id: "fox", name: "🦊 Fox" },
	{ id: "xiuying", name: "王秀英", presence: "away" },
	{ id: "ngoc-han", name: "Đặng Thị Ngọc Hân" },
	{ id: "broken", name: "Dana Whitfield", image: "/missing/dana.jpg" },
	{ id: "relay", name: "Relay Customer Escalations Assistant", agent: true },
]

/** A big workspace: 1,284 members. */
export const workspace: ReadonlyArray<AvatarPerson> = Array.from({ length: 1284 }, (_, i) => {
	const first = ["Ada", "Ben", "Mira", "Sam", "Kenji", "Lena", "Omar", "Priya", "Jo", "Rafael", "Ines", "Tomas"][i % 12]!
	const last = ["Lovelace", "Okafor", "Patel", "Rivera", "Watanabe", "Fischer", "Haddad", "Raman", "Kim", "Silva", "Novak", "Berg"][Math.floor(i / 12) % 12]!
	return { id: `m${i}`, name: `${first} ${last}` }
})
```

## API reference

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `key` | `string` | — | Names the card among others in the same view; needed for more than one. |
| `state` (required) | `HoverCardState` | — | — |
| `onState` (required) | `(state: HoverCardState) => Message` | — | — |
| `card` (required) | `(target: string) => View.Content` | — | What the card shows for the link with this id. |
| `view` (required) | `(link: HoverCardLink) => View.Content` | — | Draws the page; `link(h.a.attr("href", …), id)` marks a link that opens the card. |
| `side` | `"top" \| "bottom"` | — | Where the card sits; below the link by default. |
| `delay` | `number` | `600` | Milliseconds the pointer rests before it opens; 600 by default. |
| `style` | `Style` | — | StyleX styles for the card, after its own: a wider one for a pull request. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/hover-card
```

With npm: `npx shadcn@latest add checkfu/uicontext/hover-card`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/hover-card`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/hover-card`.

It writes these files:

- `src/ui/uicontext/components/HoverCard.ts`
- `src/ui/uicontext/components/Motion.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/HoverCard.ts](https://github.com/checkfu/uicontext/blob/main/src/HoverCard.ts)

## Related

- [Dialog](https://uicontext.com/docs/components/dialog.md) (`dialog`): A modal window with a title, a description and actions.
- [Menu](https://uicontext.com/docs/components/menu.md) (`menu`): A dropdown menu of actions, checkbox and radio items, shortcuts and submenus.
- [Context Menu](https://uicontext.com/docs/components/context-menu.md) (`context-menu`): The menu a right click or long press opens over rows, files and cards.
- [Sheet](https://uicontext.com/docs/components/sheet.md) (`sheet`): A panel that slides over the page from a side or the bottom.
- [Toast](https://uicontext.com/docs/components/toast.md) (`toast`): Short notifications with an optional action like Undo.
- [Popover](https://uicontext.com/docs/components/popover.md) (`popover`): Content that opens beside its trigger and closes when you click away.
