Hover Card

Previews the content behind a link, such as a profile, for sighted mouse and keyboard users. Compose it from HoverCard, HoverCardTrigger, and HoverCardContent.

Built with @sveltejs and theme tokens.

+page.svelte
<script lang="ts">
	import { HoverCard, HoverCardTrigger, HoverCardContent } from "#lib/components/ui/hover-card/index.js";
</script>

<HoverCard>
	<HoverCardTrigger href="https://svelte.dev">@sveltejs</HoverCardTrigger>
	<HoverCardContent>Cybernetically enhanced web apps.</HoverCardContent>
</HoverCard>

Installation

pnx remote0 add https://plainsv.com/r/hover-card.json

Behavior

The card opens after the pointer rests on the trigger for openDelay, 700 milliseconds by default, or when the trigger gets keyboard focus. It stays open while the pointer is over the card, so links inside it can be clicked, and closes closeDelay after the pointer leaves, 300 milliseconds by default. Escape closes it. Touch screens skip it, so a tap follows the link. Use bind:open to read or set whether it is open, or onOpenChange to run code when it changes.

@sveltejs

Open: no

+page.svelte
<script lang="ts">
	let open = $state(false);
</script>

<HoverCard bind:open openDelay={100} closeDelay={1000}>
	<HoverCardTrigger href="https://svelte.dev">@sveltejs</HoverCardTrigger>
	<HoverCardContent>...</HoverCardContent>
</HoverCard>

The card does not take focus and is not announced, so keep anything important in the page itself. Use Popover for content that must be reachable by every user.

Trigger

HoverCardTrigger renders a link with href and its children. To use another element, pass a trigger snippet and spread its props onto it.

+page.svelte
<HoverCard openDelay={200}>
	<HoverCardTrigger>
		{#snippet trigger({ props })}
			<Button variant="link" {...props}>Hover me</Button>
		{/snippet}
	</HoverCardTrigger>
	<HoverCardContent>...</HoverCardContent>
</HoverCard>

Side

Set side to top, right, bottom, or left, and align to start, center, or end. The card flips to the opposite side when there is not enough room, and shifts to stay inside the viewport.

+page.svelte
<HoverCard side="right" align="start">...</HoverCard>

Sizes

Set size on HoverCard to xs, sm, md, or lg. The default is md, and the padding, gap, text size, and corner radius follow the size. The card is 16rem wide; set style="width: ..." on the content to change it.

+page.svelte
<HoverCard size="sm">...</HoverCard>

Rounded

Set rounded on HoverCard to xs through 4xl or pill to override the radius that follows size. pill is a true pill and can clip tall content, so use 3xl or 4xl for round corners that stay safe.

+page.svelte
<HoverCard rounded="lg">...</HoverCard>

Overrides

HoverCardContent takes size and rounded, which win over the hover card's for the card only.

+page.svelte
<HoverCardContent size="lg" rounded="lg">...</HoverCardContent>

API

HoverCard renders no element of its own. HoverCardContent extends the native div attributes.

index.ts
type Props = CompPropsWithChildSimple<{
	/** Whether the card is shown; supports `bind:open` */
	open?: boolean;
	/** Called when the card opens or closes */
	onOpenChange?: (open: boolean) => void;
	/** Milliseconds the pointer rests on the trigger before the card opens; defaults to `700` */
	openDelay?: number;
	/** Milliseconds the card stays open after the pointer leaves, so it can reach the card; defaults to `300` */
	closeDelay?: number;
	/** Preferred side; flips to the opposite side when there is not enough room; defaults to `bottom` */
	side?: "top" | "right" | "bottom" | "left";
	/** Alignment along the trigger's edge; defaults to `center` */
	align?: "start" | "center" | "end";
	/** Gap in pixels between the trigger and the card; defaults to `4` */
	sideOffset?: number;
	/** Padding and text size on the shared scale; defaults to `md` */
	size?: UiSize;
	/** Corner radius of the card; follows `size` when omitted. `pill` is a true pill that can clip the content, so use `3xl` or `4xl` to stay round */
	rounded?: UiRounded;
}>;

interface TriggerProps {
	/** Link text for the default trigger */
	children?: Snippet;
	/** Destination of the default link trigger */
	href?: string;
	/** Custom trigger; spread `props` onto the element that opens the card */
	trigger?: Snippet<[{ props: TriggerAttrs }]>;
}

interface ContentProps extends CompPropsWithChild<"div"> {
	/** Padding and text of this card; overrides the hover card's `size` */
	size?: UiSize;
	/** Corner radius of this card; overrides the hover card's `rounded`. `pill` is a true pill that can clip the content, so use `3xl` or `4xl` to stay round */
	rounded?: UiRounded;
}