Components
Hint
A tooltip for an icon control in no JavaScript: a span inside the control and two CSS rules.
Usage
Icon button with a hint
Uses.aka-hint-host.aka-hint.aka-hint-end
<button type="button" aria-label="Deck view" className="aka-hint-host aka-card grid h-7 w-7 place-items-center rounded-md text-muted-foreground transition-colors hover:text-foreground"> <Layers aria-hidden className="h-[15px] w-[15px]" /> <span aria-hidden className="aka-hint aka-card px-2 py-1 text-11 font-light">Deck view</span> </button>
Hover or focus the button. The Radix tooltip this replaced was thirty-odd kilobytes of JavaScript to explain a button, on a page built to ship none.
Examples
Pinned open
<span aria-hidden className="aka-hint aka-card px-2 py-1 text-11 font-light" style={{ visibility: 'visible', opacity: 1, position: 'static', transform: 'none' }}>
Deck view
</span>A hint only shows on hover, which a page cannot print, so this copy is the same span pinned open with inline overrides. It exists only to make a hover state printable.
At the page edge
<span aria-hidden className="aka-hint aka-hint-end aka-card px-2 py-1 text-11 font-light">Grid view</span>
Anchored to the control's trailing edge instead of centred, for the last button in a right-aligned group, where a centred hint would hang past the viewport.
Best practices
When to use
- An icon control whose picture needs a word for a mouse user.
- Never as the only label, and never for text a reader must see.
Behaviour
- It arrives at full strength and slides its last four pixels, motion moving space per law 04, rather than fading up.
- Shown on hover and on keyboard focus; no transition under reduced motion.
- Its ink is set in the class, so a host with an inverted fill cannot turn it invisible.
Accessibility
- The button's aria-label is the accessible name. The hint is aria-hidden: a hint for a mouse, not a label.
- Icons inside the button are aria-hidden too.
CSS
The rules, as globals.css has them, with the reasoning the stylesheet keeps beside them.
@layer components {
/*
* A hint on an icon control, in no JavaScript.
*
* The Radix tooltip this replaced is a fine component and was thirty-odd
* kilobytes of it, imported nowhere else on the site, for two buttons on one
* page whose entire design goal was to not ship JavaScript. A tooltip is a
* positioned span that appears on hover and on focus, and that is two rules.
*
* The button's own aria-label is the accessible name, so this is never the
* only way to know what the control does and is aria-hidden. It is a hint for
* a mouse, not a label.
*
* Only the transform is transitioned. It arrives at full strength and slides
* the last four pixels — motion moving space, per law 04 — rather than fading
* up, which would be an animated brightness.
*/
.aka-hint-host {
position: relative;
}
.aka-hint {
/*
* The colour is set here rather than left to a utility on the element.
*
* The hint lives inside the button it describes, and the selected button
* is an inverted fill: `text-background`. So an unset hint inherits
* near-white ink and lands on a near-white card, which is how "Current
* view" arrived invisible in light mode.
*
* Set here rather than as `text-foreground/88` on the element, so the
* hint's ink is part of its material and no host button can override it.
*/
color: color-mix(in srgb, var(--foreground) 88%, transparent);
position: absolute;
top: calc(100% + 7px);
left: 50%;
z-index: 50;
white-space: nowrap;
pointer-events: none;
visibility: hidden;
opacity: 0;
transform: translate(-50%, -4px);
transition: transform var(--dur-hint) var(--ease-arrive);
}
.aka-hint-host:hover > .aka-hint,
.aka-hint-host:focus-visible > .aka-hint {
visibility: visible;
opacity: 1;
transform: translate(-50%, 0);
}
/*
* Anchored to the control's trailing edge instead of centred on it, for a
* control that sits at the edge of the page. Centred, the hint on the last
* button in a right-aligned group hangs three pixels past the viewport and
* opens a horizontal scrollbar on the whole document.
*/
.aka-hint-end {
left: auto;
right: 0;
transform: translate(0, -4px);
}
.aka-hint-host:hover > .aka-hint-end,
.aka-hint-host:focus-visible > .aka-hint-end {
transform: translate(0, 0);
}
@media (prefers-reduced-motion: reduce) {
.aka-hint {
transition: none;
}
}
}