Components
Hover Card
More about a link while the pointer rests on it: the card's #262626 fill, a hairline and the brand corner. The pointer can cross into it; on touch the link just works.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add hover-card<script type="module" src="/spyglass-ds/elements/hover-card.js"></script>Top hooks
This week's top hooks come from Acme Studio, the brand your team saved most.
Usage
A preview of a brand, a creator or a board named in text, with a few numbers. The link must lead to the same thing on its own.
Never
- The only way to reach what it shows: the link must work on its own
- Actions someone has to take: use Popover or Dropdown Menu
- A shadow or a glow: the fill and the hairline set it apart
Adds with it: popover
Props
The web component takes the same as attributes or properties (<spyglass-hover-card>); title is heading there.
<HoverCard>
| Prop | Type | Default |
|---|---|---|
side | Side The side it opens on (it flips when there is no room). | 'bottom' |
align | Align | 'start' |
width | string The card's width (a CSS length). | '288px' |
card | ReactNode What the card shows. | |
children | ReactNode One link. |
Examples
The same example in React and as web components; both render identically.
'use client'
import { HoverCard } from '@/react/components/ui/HoverCard'
// A brand named in a sentence; rest the pointer on it (or tab to it) and its card shows what Spyglass tracks for it.
// The link still works on its own.
export default function Brand() {
return (
<div className="flex min-h-64 flex-col items-center gap-3">
<p className="type-label text-fg-muted">Top hooks</p>
<p className="max-w-90 type-ui text-fg-soft">
This week's top hooks come from
{' '}
<HoverCard
card={(
<>
<p className="type-ui-strong text-fg">Acme Studio</p>
<p className="mt-1 type-caption text-fg-muted">Skincare brand. Tracked on Meta and TikTok since January.</p>
<dl className="mt-4 grid grid-cols-2 gap-x-4 gap-y-1 border-t border-edge pt-4 type-caption">
<dt className="text-fg-muted">Active ads</dt><dd className="text-right text-fg tabular-nums">128</dd>
<dt className="text-fg-muted">New this week</dt><dd className="text-right text-fg tabular-nums">14</dd>
</dl>
</>
)}
>
<a href="#" className="text-fg underline decoration-edge-strong underline-offset-4 transition-[text-decoration-color] duration-160 ease-standard hover:decoration-fg" onClick={(e) => e.preventDefault()}>Acme Studio</a>
</HoverCard>
, the brand your team saved most.
</p>
</div>
)
}<!-- A brand named in a sentence; rest the pointer on it (or tab to it) and its card shows what Spyglass tracks for it.
The link still works on its own. -->
<div class="flex min-h-64 flex-col items-center gap-3">
<p class="type-label text-fg-muted">Top hooks</p>
<p class="max-w-90 type-ui text-fg-soft">
This week's top hooks come from
<spyglass-hover-card><a href="#" class="text-fg underline decoration-edge-strong underline-offset-4 transition-[text-decoration-color] duration-160 ease-standard hover:decoration-fg" onclick="event.preventDefault()">Acme Studio</a><template slot="card"><p class="type-ui-strong text-fg">Acme Studio</p><p class="mt-1 type-caption text-fg-muted">Skincare brand. Tracked on Meta and TikTok since January.</p><dl class="mt-4 grid grid-cols-2 gap-x-4 gap-y-1 border-t border-edge pt-4 type-caption"><dt class="text-fg-muted">Active ads</dt><dd class="text-right text-fg tabular-nums">128</dd><dt class="text-fg-muted">New this week</dt><dd class="text-right text-fg tabular-nums">14</dd></dl></template></spyglass-hover-card>, the brand your team saved most.
</p>
</div>Source
'use client'
import { useEffect, useId, useRef, type FocusEvent, type PointerEvent, type ReactNode } from 'react'
import { PopoverPanel } from '@/react/components/ui/PopoverPanel'
import { usePopover, type Align, type Side } from '@/react/hooks/usePopover'
// More about a link, shown while the pointer rests on it: the card's #262626 fill on Black with a hairline and the
// brand corner, 16px in, opened after the tooltip's 300ms wait and, on keyboard focus, at once. The pointer can cross
// into the card to select or follow what is in it; leaving both closes it after a short grace. It is a preview, never
// the only way to the content: on touch the link just works. Wrap one link; the card is the `card` prop.
export interface HoverCardProps {
/** The side it opens on (it flips when there is no room). */
side?: Side
align?: Align
/** The card's width (a CSS length). */
width?: string
/** What the card shows. */
card?: ReactNode
/** One link. */
children?: ReactNode
}
const WAIT = 300
const GRACE = 120
export function HoverCard({ side = 'bottom', align = 'start', width = '288px', card, children }: HoverCardProps) {
const pop = usePopover({ side, align, gap: 8, sticky: true })
const id = `card-${useId()}`
const root = useRef<HTMLSpanElement | null>(null)
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
useEffect(() => {
const trigger = root.current?.firstElementChild as HTMLElement | null
pop.anchor.current = trigger
trigger?.setAttribute('aria-describedby', id)
}, [id]) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => () => clearTimeout(timer.current), [])
function enter(event: PointerEvent) {
if (event.pointerType !== 'mouse') return
clearTimeout(timer.current)
// ds-latency: the wait keeps a passing pointer from opening it.
if (!pop.open) timer.current = setTimeout(() => void pop.show(), WAIT)
}
function leave(event: PointerEvent) {
if (event.pointerType !== 'mouse') return
clearTimeout(timer.current)
// ds-latency: a grace to cross the gap into the card.
timer.current = setTimeout(() => pop.hide(), GRACE)
}
function focus(event: FocusEvent) {
if ((event.target as HTMLElement).matches(':focus-visible')) void pop.show({ keyboard: true })
}
function blur(event: FocusEvent) {
const next = event.relatedTarget as Node | null
if (!next || !pop.panel.current?.contains(next)) pop.hide()
}
return (
<>
<span data-ds="hover-card" ref={root} className="inline-flex" onPointerEnter={enter} onPointerLeave={leave} onFocus={focus} onBlur={blur}>
{children}
</span>
<PopoverPanel data-ds="hover-card" id={id} pop={pop} className="p-4" style={{ width }} onPointerEnter={enter} onPointerLeave={leave}>
{card}
</PopoverPanel>
</>
)
}
export default HoverCardimport { html, nothing, type PropertyValues } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import { boxOf, PopoverController, popoverStyles, within, type Align, type Side } from '@/elements/lib/popover'
import styles from 'virtual:tw'
// HoverCard.tsx as <spyglass-hover-card>: more about a link, shown while the pointer rests on it: the card's #262626
// fill with a hairline and the brand corner, 16px in, opened after the tooltip's 300ms wait and, on keyboard focus, at
// once. The pointer can cross into the
// card to select or follow what is in it; leaving both closes it after a short grace. It is a preview, never the only
// way to the content: on touch the link just works. Wrap one link; the card is the `card` slot. Inside a paragraph, put
// the card in a <template slot="card"> (a <div> or <p> there would end the paragraph): the element moves its content
// into a <div slot="card"> as it connects.
const WAIT = 300
const GRACE = 120
/**
* @summary More about a link, shown while the pointer rests on it.
* @slot - One link.
* @slot card - What the card shows (an element, or a <template slot="card">).
*/
export class HoverCard extends DsElement {
static styles = [DsElement.styles, popoverStyles, styles]
static properties = { side: {}, align: {}, width: {} }
side: Side = 'bottom'
align: Align = 'start'
width = '288px'
private timer?: ReturnType<typeof setTimeout>
private pop = new PopoverController(this, {
gap: 8,
sticky: true,
anchor: () => boxOf([...this.children].find((c) => !c.hasAttribute('slot'))),
panel: () => this.renderRoot.querySelector<HTMLElement>('.ds-pop'),
})
private enter = (event: PointerEvent) => {
if (event.pointerType !== 'mouse') return
clearTimeout(this.timer)
// ds-latency: the wait keeps a passing pointer from opening it.
if (!this.pop.open) this.timer = setTimeout(() => void this.pop.show(), WAIT)
}
private leave = (event: PointerEvent) => {
if (event.pointerType !== 'mouse') return
clearTimeout(this.timer)
// ds-latency: a grace to cross the gap into the card.
this.timer = setTimeout(() => this.pop.hide(), GRACE)
}
private onFocusIn = (event: FocusEvent) => {
if ((event.composedPath()[0] as Element).matches(':focus-visible')) void this.pop.show({ keyboard: true })
}
private onFocusOut = (event: FocusEvent) => {
const next = event.relatedTarget as Node | null
if (!next || !within(this.renderRoot.querySelector('.ds-pop'), next)) this.pop.hide()
}
connectedCallback() {
super.connectedCallback()
const template = this.querySelector<HTMLTemplateElement>(':scope > template[slot="card"]')
if (template) {
const card = document.createElement('div')
card.slot = 'card'
card.append(template.content.cloneNode(true))
template.replaceWith(card)
}
}
disconnectedCallback() {
super.disconnectedCallback()
clearTimeout(this.timer)
}
protected willUpdate() {
this.pop.configure({ side: this.side, align: this.align })
}
render() {
const panel = this.pop.rendered
? html`<div data-ds="hover-card" popover="manual" class="ds-pop p-4 fade-y" data-side=${this.pop.pos.side} ?data-instant=${this.pop.instant} ?data-placed=${this.pop.pos.placed} style=${this.pop.style(`width: ${this.width}`)} @pointerenter=${this.enter} @pointerleave=${this.leave}><slot name="card"></slot></div>`
: nothing
return html`<span data-ds="hover-card" class="inline-flex" @pointerenter=${this.enter} @pointerleave=${this.leave} @focusin=${this.onFocusIn} @focusout=${this.onFocusOut}><slot></slot></span>${panel}`
}
}
define('hover-card', HoverCard)Reference: Derived from the brand guidelines (no hover card in the brand assets): the plan card's #262626 fill and corner, the neutral steps for the hairline.