Components
Tooltip
A control's name, for icon buttons: a White fill with Black caption text and the small corner. It waits 300ms under the pointer, shows at once on keyboard focus, and the next one in a row shows at once.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add tooltip<script type="module" src="/spyglass-ds/elements/tooltip.js"></script>Usage
Naming icon buttons in a row of tools: save, copy link, download. The button still carries its aria-label.
Never
- Anything someone needs to read or act on: put it on the page
- A tooltip on a button that already shows its label
- Showing on touch: a tap presses the button
Adds with it: popover
Props
The web component takes the same as attributes or properties (<spyglass-tooltip>); title is heading there.
<Tooltip>
| Prop | Type | Default |
|---|---|---|
text | string The control's name. | |
side | Side The side it shows on (it flips when there is no room). | 'bottom' |
align | Align | 'center' |
children | ReactNode One focusable trigger. |
Examples
The same example in React and as web components; both render identically.
import { Bookmark, Download, Link, EyeOff } from 'lucide-react'
import { Button } from '@/react/components/ui/Button'
import { Tooltip } from '@/react/components/ui/Tooltip'
// A creative's tools as icon buttons, each named by its tooltip. Point at one and wait, then move along: the next shows
// at once. Tab to one and its name shows without a wait.
const tools = [
{ label: 'Save to board', icon: Bookmark },
{ label: 'Copy link', icon: Link },
{ label: 'Download', icon: Download },
{ label: 'Hide', icon: EyeOff },
]
export default function Tools() {
return (
<div className="flex min-h-24 items-start justify-center gap-1">
{tools.map((t) => (
<Tooltip key={t.label} text={t.label}>
<Button variant="ghost" icon aria-label={t.label}><t.icon size={16} strokeWidth={1.5} /></Button>
</Tooltip>
))}
</div>
)
}<!-- A creative's tools as icon buttons, each named by its tooltip. Point at one and wait, then move along: the next
shows at once. Tab to one and its name shows without a wait. -->
<div class="flex min-h-24 items-start justify-center gap-1">
<spyglass-tooltip text="Save to board"><spyglass-button variant="ghost" icon aria-label="Save to board"><i data-lucide="bookmark" data-size="16" data-stroke-width="1.5"></i></spyglass-button></spyglass-tooltip>
<spyglass-tooltip text="Copy link"><spyglass-button variant="ghost" icon aria-label="Copy link"><i data-lucide="link" data-size="16" data-stroke-width="1.5"></i></spyglass-button></spyglass-tooltip>
<spyglass-tooltip text="Download"><spyglass-button variant="ghost" icon aria-label="Download"><i data-lucide="download" data-size="16" data-stroke-width="1.5"></i></spyglass-button></spyglass-tooltip>
<spyglass-tooltip text="Hide"><spyglass-button variant="ghost" icon aria-label="Hide"><i data-lucide="eye-off" data-size="16" data-stroke-width="1.5"></i></spyglass-button></spyglass-tooltip>
</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'
// When the last tooltip closed: one opened right after it shows at once, so moving along a toolbar reads fast.
let lastClosed = 0
// A control's name, for icon buttons: a White fill with Black DM Sans caption text and the small corner, 4px from its
// trigger. Under the pointer it waits 300ms, then fades and moves 2px in over 160ms; on keyboard focus it shows at
// once, without motion; a press, Escape or leaving hides it. Right after another tooltip it shows at once, so moving
// along a toolbar reads fast. It never shows on touch (a tap presses the button). Wrap one focusable trigger; it gets
// aria-describedby.
export interface TooltipProps {
/** The control's name. */
text: string
/** The side it shows on (it flips when there is no room). */
side?: Side
align?: Align
/** One focusable trigger. */
children?: ReactNode
}
const WAIT = 300
export function Tooltip({ text, side = 'bottom', align = 'center', children }: TooltipProps) {
const pop = usePopover({ side, align, gap: 4, sticky: true })
const id = `tip-${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
const was = useRef(false)
useEffect(() => {
if (was.current && !pop.open) lastClosed = Date.now()
was.current = pop.open
}, [pop.open])
useEffect(() => () => clearTimeout(timer.current), [])
function enter(event: PointerEvent) {
if (event.pointerType !== 'mouse' || pop.open) return
clearTimeout(timer.current)
if (Date.now() - lastClosed < WAIT) return void pop.show({ keyboard: true })
// ds-latency: the wait is the point; a pass of the pointer shows nothing.
timer.current = setTimeout(() => void pop.show(), WAIT)
}
function leave() {
clearTimeout(timer.current)
// Closed now, not after the next render: the pointer entering the next button lands in the same moment.
if (pop.open) lastClosed = Date.now()
pop.hide()
}
function focus(event: FocusEvent) {
if ((event.target as HTMLElement).matches(':focus-visible')) void pop.show({ keyboard: true })
}
return (
<>
<span data-ds="tooltip" ref={root} className="inline-flex" onPointerEnter={enter} onPointerLeave={leave} onPointerDown={leave} onFocus={focus} onBlur={leave}>
{children}
</span>
<PopoverPanel data-ds="tooltip" id={id} pop={pop} tip role="tooltip">{text}</PopoverPanel>
</>
)
}
export default Tooltipimport { html, nothing, type PropertyValues } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import { boxOf, PopoverController, popoverStyles, type Align, type Side } from '@/elements/lib/popover'
import styles from 'virtual:tw'
// Tooltip.tsx as <spyglass-tooltip>: a control's name, for icon buttons: a White fill with Black caption text and the
// small corner, 4px from its trigger. Under the pointer it waits 300ms, then fades and moves 2px in over 160ms; on
// keyboard focus it shows at once, without motion; a press, Escape or leaving hides it. Right after another tooltip it
// shows at once. It never shows on touch (a tap presses the button). Wrap one focusable trigger; it gets
// aria-description (an id cannot reach into the element's shadow root, as React's aria-describedby does).
// When the last tooltip closed: one opened right after it shows at once, so moving along a toolbar reads fast.
let lastClosed = 0
const WAIT = 300
/**
* @summary A control's name, shown under it after a short wait.
* @slot - One focusable trigger.
*/
export class Tooltip extends DsElement {
static styles = [DsElement.styles, popoverStyles, styles]
static properties = { text: {}, side: {}, align: {} }
/** The control's name. */
text = ''
side: Side = 'bottom'
align: Align = 'center'
private timer?: ReturnType<typeof setTimeout>
private wasOpen = false
private pop = new PopoverController(this, {
gap: 4,
sticky: true,
anchor: () => boxOf(this.firstElementChild),
panel: () => this.renderRoot.querySelector<HTMLElement>('.ds-pop'),
})
private enter = (event: PointerEvent) => {
if (event.pointerType !== 'mouse' || this.pop.open) return
clearTimeout(this.timer)
if (Date.now() - lastClosed < WAIT) return void this.pop.show({ keyboard: true })
// ds-latency: the wait is the point; a pass of the pointer shows nothing.
this.timer = setTimeout(() => void this.pop.show(), WAIT)
}
private leave = () => {
clearTimeout(this.timer)
this.pop.hide()
}
private onFocusIn = (event: FocusEvent) => {
if ((event.composedPath()[0] as Element).matches(':focus-visible')) void this.pop.show({ keyboard: true })
}
disconnectedCallback() {
super.disconnectedCallback()
clearTimeout(this.timer)
}
protected willUpdate() {
this.pop.configure({ side: this.side, align: this.align })
}
protected updated(changed: PropertyValues<this>) {
super.updated(changed)
if (this.wasOpen && !this.pop.open) lastClosed = Date.now()
this.wasOpen = this.pop.open
this.firstElementChild?.setAttribute('aria-description', this.text)
}
render() {
const panel = this.pop.rendered
? html`<div data-ds="tooltip" popover="manual" class="ds-pop" data-tip role="tooltip" data-side=${this.pop.pos.side} ?data-instant=${this.pop.instant} ?data-placed=${this.pop.pos.placed} style=${this.pop.style()}>${this.text}</div>`
: nothing
return html`<span data-ds="tooltip" class="inline-flex" @pointerenter=${this.enter} @pointerleave=${this.leave} @pointerdown=${this.leave} @focusin=${this.onFocusIn} @focusout=${this.leave}><slot></slot></span>${panel}`
}
}
define('tooltip', Tooltip)Reference: Derived from the brand guidelines (no tooltip in the brand assets): White on Black as the brand's type, DM Sans caption size, the small corner.