Command palette

@yasinsensoy/web

Web · Components

HoverCard

Preview card that opens when hovering a link or a name: a user profile, product summary, link preview.

4 exports

Live example

Preview

This design was made by @ayse .

Code

import { HoverCard, Profil } from "@yasinsensoy/web";

<HoverCard openDelay={500}
  trigger={<Link href="/u/ayse">@ayse</Link>}
  content={async () => <Profil kullanici={await getir("ayse")} />} />

Events

  1. Events appear here when you interact with the preview.

Import

import { HoverCard } from "@yasinsensoy/web";

HoverCard

componentClient component

Preview card that opens when hovering a link or a name: a user profile, product summary, link preview. Only for extra info; what's in the card must also be reachable another way. For short text use Tooltip, and for a panel that opens on click use Popover.

  • The mouse opens it after a short delay; it stays open when the mouse moves onto the card, so links in the card can be clicked.
  • Opens on keyboard focus, closes with Esc.
  • content can be an (async) function: called on first open, a placeholder shows until the result arrives, and the result is cached; hundreds of triggers on the page make no requests.
  • Mobile: opens on long press and closes on tapping elsewhere; a normal tap opens the link.
  • Opens in the top layer (popover) and is positioned with CSS anchor positioning; the card is not added to the DOM until first needed.
HoverCard({ trigger, content, side, openDelay, closeDelay, touch, open, defaultOpen, onOpenChange, className, effect, }: HoverCardProps): JSX.Element

Examples

<HoverCard
  trigger={<Link href="/u/yasin">@yasin</Link>}
  content={async () => <Profile user={await fetchUser("yasin")} />}
/>

<HoverCard trigger={<a href={url}>{title}</a>} className="w-80"
  content={<LinkPreview url={url} />} side="top" />

Content of the card: an element or a function called once on first open. The function can return a Promise (e.g. fetching user info from the server); a placeholder is shown until the result arrives, and the result is cached.

type HoverCardContent = ReactNode | (() => ReactNode | Promise<ReactNode>)

How the card opens on touch screens.

  • longPress: opens on long press; a normal tap opens the link.
  • none: never opens on touch.
type HoverCardTouch = "longPress" | "none"

Props of the HoverCard component.

NameTypeDefaultDescription
triggerReactElementElement that opens the card; usually a link (user name, product name). The element's own job (following the link) is not broken.
contentHoverCardContentContent. See HoverCardContent.
side?Placement"auto"Position relative to the trigger. See Placement.
openDelay?number500Open delay after the mouse enters (ms).
closeDelay?number250Close delay after the mouse leaves (ms). If the mouse moves onto the card in this time, it stays open.
touch?HoverCardTouch"longPress"How it opens on touch screens. See HoverCardTouch.
open?booleanIs it open when controlled?
defaultOpen?booleanfalseIs it initially open when uncontrolled?
onOpenChange?(open: boolean) => voidCalled with the new state when it opens or closes.
className?stringClasses added to the card (e.g. w-80 for width).
effect?EffectPropEffect or effects of the card (e.g. "beam", "glow"). If omitted, UIProvider appearance.defaults.hoverCard.effect. See BuiltinEffect.