A preview of what a link leads to, on hover or focus, such as the person behind a mention.
- 1.1 kB JS 2.7 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
Refunds over $500 still need a second approver. @Ada Lovelace can you check the ledger side, and @Ben Okafor the confirmation screen? @Priya Raman for the rollout date.
This example also uses Avatar. Install it too:
npx shadcn@latest add checkfu/uicontext/avatarimport { 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 constconst Message = UiSchema.TaggedEnum({ ChangedPreview: { preview: HoverCardState } })// a comment's @mentions: rest the pointer on a name, or Tab to it, to see who it isexport 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.", ), }),})When to use
A preview of what a link leads to, on hover or focus, such as the person behind a mention. Use Tooltip instead when the hint is one short line.
Usage
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
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:
npx shadcn@latest add checkfu/uicontext/badgeimport { 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 constconst Message = UiSchema.TaggedEnum({ ChangedPreview: { preview: HoverCardState } })// release notes that name issues: the card shows each one's title and status without leaving the pageexport 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"), ")"), ), }),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the card among others in the same view; needed for more than one. |
staterequired | HoverCardState | — | — |
onStaterequired | (state: HoverCardState) => Message | — | — |
cardrequired | (target: string) => View.Content | — | What the card shows for the link with this id. |
viewrequired | (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
pnpm dlx shadcn@latest add checkfu/uicontext/hover-card