Skip to Content
CodeUI

HoverCard

@codeui/kit v0.1.5

A card of extra detail beside a link, opened by hover, focus or click.

When to use

Use a hover card to preview what sits behind a link: a file, a person, a thread. Use it for a description or for more than one fact. A single name belongs in a Tooltip, and the title attribute is never the answer. It opens when the pointer rests on the trigger, when the trigger gets keyboard focus, and on click, so it works on touch screens. It closes when the pointer leaves both, on blur, on Escape and on a press outside.

A hover card is supplementary. Never put the only path to an action or to information inside it. The card never takes focus, so a keyboard user cannot reach a button inside it. Keep the same details reachable from the page the link opens.

When to choose another component

  • Tooltip names a control in a few words. A hover card holds a few lines of rich content.
  • Popover opens on click, takes focus, and can hold buttons and fields. Use it when the user has to act on the content.

Live demo

Ada shared fetch-retry.ts with the team.

hover-card/basic.tsx
import { Badge, HoverCard, HoverCardContent, HoverCardTrigger, Link, Text } from "@codeui/kit";

export default function HoverCardBasic() {
	return (
		<Text as="p">
			Ada shared{" "}
			<HoverCard placement="bottom-start">
				<HoverCardTrigger as={Link} href="#fetch-retry">
					fetch-retry.ts
				</HoverCardTrigger>
				<HoverCardContent>
					<div style={{ display: "flex", "align-items": "center", gap: "var(--cui-space-2)" }}>
						<Text weight="semibold">fetch-retry.ts</Text>
						<Badge tone="info">Shared</Badge>
					</div>
					<Text tone="muted">TypeScript · Vitesse Dark · JetBrains Mono</Text>
					<Text>Retries a failed request three times with exponential backoff.</Text>
					<Text size="xs" tone="hint">
						Edited by Ada Lovelace 2 hours ago
					</Text>
				</HoverCardContent>
			</HoverCard>{" "}
			with the team.
		</Text>
	);
}

Put HoverCardTrigger and HoverCardContent in HoverCard. Hover the link, or move focus to it with Tab.

Anatomy

fetch-retry.ts
fetch-retry.tsTypeScript, edited 2 hours ago
  • #142Retry failed requests with backoff
  • #143Cap the delay at 8 seconds

Tap a part to outline it in the demo.

  1. 01HoverCardTriggerhovercard-trigger
  2. 02HoverCardContenthovercard-contentclasses.content
  3. 03HoverCardListhovercard-list
  4. 04HoverCardItemhovercard-itemclasses.item
ExportRendersclasses keydata-cuiRole
HoverCardNothing. Holds the open state and the timersnonenonenone
HoverCardTriggera, or the element passed to asnonehovercard-triggerLink
HoverCardContentdiv in a portal on mount, or document.bodycontenthovercard-contentnone
HoverCardListul, optionalnonehovercard-listList
HoverCardItemli, with an icon slot, an identifier and a titleitem, itemIcon, itemId, itemTitlehovercard-item, hovercard-item-icon, hovercard-item-id, hovercard-item-titleList item

Variants

Placement

hover-card/placements.tsx
import { For } from "solid-js";
import { HoverCard, HoverCardContent, HoverCardTrigger, Link } from "@codeui/kit";

const placements = ["top-start", "right-start", "bottom-start", "left-start"] as const;

export default function HoverCardPlacements() {
	return (
		<For each={placements}>
			{placement => (
				<HoverCard placement={placement}>
					<HoverCardTrigger as={Link} href={`#${placement}`}>
						{placement}
					</HoverCardTrigger>
					<HoverCardContent>The card opens on this side of the link.</HoverCardContent>
				</HoverCard>
			)}
		</For>
	);
}

placement sets the side and the alignment. The default is right-start: the card sits beside its trigger, top edges aligned, so it does not cover the lines below. When there is no room on the right, Kobalte flips it to the left. The card wraps its text at 20rem (320px). gutter sets the distance from the trigger. The kit sets it to 6px.

Delays

hover-card/delays.tsx
import { HoverCard, HoverCardContent, HoverCardTrigger, Link } from "@codeui/kit";

export default function HoverCardDelays() {
	return (
		<>
			<HoverCard>
				<HoverCardTrigger as={Link} href="#default-delay">
					Default delay
				</HoverCardTrigger>
				<HoverCardContent>Opens after 500ms and closes 150ms after you leave.</HoverCardContent>
			</HoverCard>
			<HoverCard openDelay={200}>
				<HoverCardTrigger as={Link} href="#short-delay">
					Short delay
				</HoverCardTrigger>
				<HoverCardContent>Opens after 200ms.</HoverCardContent>
			</HoverCard>
			<HoverCard closeDelay={600}>
				<HoverCardTrigger as={Link} href="#long-close">
					Long close delay
				</HoverCardTrigger>
				<HoverCardContent>Stays 600ms after you leave the link and the card.</HoverCardContent>
			</HoverCard>
		</>
	);
}

The card opens 500ms after the pointer enters or the trigger gets focus, and closes 150ms after the pointer leaves the trigger and the card. Change this with openDelay and closeDelay. On its way from the trigger to the card, the pointer may cross the gap between them without closing the card. ignoreSafeArea turns that off.

List of entries

Codex is working on 5 tasks.

hover-card/list.tsx
import {
	HoverCard,
	HoverCardContent,
	HoverCardItem,
	HoverCardList,
	HoverCardTrigger,
	Link,
	Text,
} from "@codeui/kit";
import { For } from "solid-js";

// Each state changes shape as well as colour. A finished entry shows no icon.
const svg = {
	viewBox: "0 0 16 16",
	fill: "none",
	stroke: "currentColor",
	"stroke-width": "1.5",
};
const states = {
	todo: {
		label: "To do",
		icon: () => (
			<svg {...svg}>
				<circle cx="8" cy="8" r="5.5" />
			</svg>
		),
	},
	running: {
		label: "Running",
		icon: () => (
			<svg {...svg}>
				<circle cx="8" cy="8" r="5.5" />
				<path d="M8 2.5a5.5 5.5 0 0 1 0 11z" fill="currentColor" />
			</svg>
		),
	},
	done: { label: "Done", icon: () => undefined },
};

const tasks = [
	{ id: "T-12", title: "Migrate the kit to Solid 2", state: "running", depth: 0 },
	{
		id: "T-13",
		title: "Port Popover and Tooltip to the new reactivity model",
		state: "done",
		depth: 1,
	},
	{
		id: "T-14",
		title: "Rebuild HoverCard on the Kobalte popover",
		state: "running",
		depth: 1,
	},
	{ id: "T-15", title: "Keep keyboard opens free of motion", state: "todo", depth: 2 },
	{
		id: "T-16",
		title: "Update the docs demos for every overlay",
		state: "todo",
		depth: 1,
	},
] as const;

export default function HoverCardListDemo() {
	return (
		<Text as="p">
			Codex is working on{" "}
			<HoverCard>
				<HoverCardTrigger as={Link} href="#tasks">
					5 tasks
				</HoverCardTrigger>
				<HoverCardContent>
					<Text weight="semibold">Migrate the kit to Solid 2</Text>
					<HoverCardList aria-label="Tasks">
						<For each={tasks}>
							{task => (
								<HoverCardItem
									icon={states[task.state].icon()}
									iconLabel={states[task.state].label}
									identifier={task.id}
									depth={task.depth}
								>
									{task.title}
								</HoverCardItem>
							)}
						</For>
					</HoverCardList>
				</HoverCardContent>
			</HoverCard>
			.
		</Text>
	);
}

Use HoverCardList and HoverCardItem to list tasks, files or members. Each entry is one line: a state icon, a short identifier, then the title, truncated. Show nesting with depth, which indents the entry 16px per level, not with tree lines. Drop a prefix every entry shares. A finished entry shows no icon, and the icon slot stays so the identifiers line up.

The list caps at 24rem (--cui-hovercard-list-max-height) and scrolls on its own, so the card keeps its size. The list takes focus with Tab once the card is open, so a keyboard user can scroll it.

Give each state icon a name with iconLabel, such as "Running", and make each state differ in shape, not only colour.

States

Opened by click or tap

A click opens the card at once, without openDelay. On a touch screen, the first tap on a closed card opens it and does not follow the link; the next tap follows it. A press outside closes the card.

Controlled

Without open, the card manages its own state. To control it, pass open and onOpenChange.

Ready API

Import from @codeui/kit/hover-card or from @codeui/kit.

HoverCard

Holds the open state, the hover and focus timers, and the position.

Prop

Type

Inherited from Kobalte's popover root:

Prop

Type

HoverCardTrigger

Renders an a. Give it an href, or render another focusable element with as, so keyboard users can reach it. An a without href cannot take focus.

HoverCardContent

Portals the card to document.body and places it next to the trigger.

Prop

Type

Prop

Type

HoverCardList

A ul that caps at 24rem and scrolls. Give it an aria-label. Every other prop goes to the ul.

HoverCardItem

One line in a HoverCardList.

Prop

Type

hoverCardStyles

hoverCardStyles.content() returns the card class, cui-hovercard-content. hoverCardStyles.list(), item(), itemIcon(), itemId() and itemTitle() return the list classes. hoverCardParts maps each part to its data-cui value.

Accessibility

Keyboard

KeyAction
TabMoving focus to the trigger opens the card after openDelay. Moving focus away closes it.
EnterFollows the link. A trigger rendered as a button opens the card.
EscapeCloses the card. Focus stays on the trigger.

ARIA

  • A HoverCardList is a list with its aria-label; a named state icon is an image with its state name.
  • The card has no role and is not linked to the trigger. Screen readers announce the link only, which is why the card must not hold anything the user needs.
  • Escape closes the innermost layer only, so a hover card inside a dialog closes before the dialog.

Focus

The card never takes focus. Focus stays on the trigger while it is open.

SSR notes

The trigger renders on the server. The card renders on the client, and only while it is open.

Theming hooks

HoverCardContent reads these variables. Set the --cui-hovercard-content-* ones on the card to change a single card. See Tokens for the defaults.

VariableDefaultUsed for
--cui-hovercard-content-background--cui-surface-dialogFill
--cui-hovercard-content-box-shadow--cui-shadow-dialogShadow, and the 1px ring in dark mode
--cui-hovercard-content-radius--cui-radius-cardRadius (16px)
--cui-hovercard-content-padding--cui-space-4Padding
--cui-hovercard-content-max-widthmin(320px, 100vw - 32px)Width limit
--cui-hovercard-content-font-size, --cui-hovercard-content-line-height--cui-font-size-sm, --cui-font-line-height-snugText (14px / 1.3)
--cui-hovercard-content-color--cui-text-dialogText colour
--cui-hovercard-list-max-height24remHeight of a list before it scrolls
--cui-hovercard-item-indent16pxIndent per depth level
--cui-hovercard-item-depthSet from depthNesting level of one entry
--cui-motion-duration-normal, --cui-motion-duration-fast, --cui-motion-ease-outPop: in from scale(0.96) over 160ms, out to scale(0.98) over 120ms

A card opened by hover or click pops from the trigger side, with transform-origin from --cui-popper-content-transform-origin. A card opened by keyboard focus appears with no animation (data-motion="none"). With reduced motion, the card only fades.

  • Tooltip names a control in a few words.
  • Popover holds interactive content and takes focus.

On this page