Tooltip
Small text bubble that appears above or below a control on hover and on keyboard focus.
pnpm dlx shadcn@latest add @haze/tooltip---
import { cn } from "../../../lib/haze/utils";
interface Props {
/** Unique on the page. Put `aria-describedby="<id>"` on the control in the slot. */
id: string;
/** Short plain text. Never put links or buttons in a tooltip. */
text: string;
side?: "top" | "bottom";
/** Which edge of the control the bubble lines up with. Use start or end near a screen edge. */
align?: "start" | "center" | "end";
class?: string;
}
const { id, text, side = "top", align = "center", class: className } = Astro.props;
// Padding (not margin) bridges the gap, so the pointer can move onto the bubble without closing it.
const sides = { top: "bottom-full translate-y-1 pb-1.5", bottom: "top-full -translate-y-1 pt-1.5" };
const aligns = { start: "left-0", center: "left-1/2 -translate-x-1/2", end: "right-0" };
---
<span data-haze-tooltip class={cn("group/tooltip relative inline-flex", className)}>
<slot />
<span
id={id}
role="tooltip"
class={cn(
"pointer-events-none invisible absolute z-50 w-max max-w-[min(15rem,calc(100vw-2rem))] opacity-0 transition-[opacity,visibility,translate] duration-200 ease-snappy motion-reduce:transition-none",
"group-hover/tooltip:pointer-events-auto group-hover/tooltip:visible group-hover/tooltip:translate-y-0 group-hover/tooltip:opacity-100 group-hover/tooltip:delay-150",
"group-focus-within/tooltip:visible group-focus-within/tooltip:translate-y-0 group-focus-within/tooltip:opacity-100",
"group-data-[dismissed]/tooltip:invisible! group-data-[dismissed]/tooltip:opacity-0!",
// Instant group: while data-tooltip-instant is on <html>, a tooltip that opens skips the delay and the enter transition.
"[[data-tooltip-instant]_[data-haze-tooltip]:is(:hover,:focus-within)>&]:delay-0! [[data-tooltip-instant]_[data-haze-tooltip]:is(:hover,:focus-within)>&]:duration-0!",
sides[side],
aligns[align],
)}
>
<span class="block rounded-lg border border-border bg-popover px-2.5 py-1.5 text-xs/4 text-balance text-popover-foreground shadow-float">
{text}
</span>
</span>
</span>
<script>
// Enhancement: Escape hides an open tooltip (WCAG 1.4.13), a control that was not given
// aria-describedby gets it, and tooltips form an instant group. Hover and focus already work without this script.
// Instant group: html[data-tooltip-instant] is set while a tooltip is shown and for 300 ms after it hides
// (its value is the time it was last set). While it is on, the next tooltip opens with no delay and no transition.
const SHOW_DELAY = 150, GRACE = 300;
let current: HTMLElement | null = null;
let graceTimer = 0;
function show(wrapper: HTMLElement) {
clearTimeout(graceTimer);
const previous = current;
if (previous && previous !== wrapper) {
// The tooltip being left hides at once too, so two bubbles never cross-fade.
previous.querySelector(":scope > [role=tooltip]")?.getAnimations().forEach((animation) => animation.finish());
}
current = wrapper;
document.documentElement.setAttribute("data-tooltip-instant", String(Math.round(performance.now())));
}
function hide(wrapper: HTMLElement) {
// current stays set to the wrapper that just hid, so the next tooltip can end its exit at once.
if (current !== wrapper) return;
clearTimeout(graceTimer);
document.documentElement.setAttribute("data-tooltip-instant", String(Math.round(performance.now())));
graceTimer = window.setTimeout(() => document.documentElement.removeAttribute("data-tooltip-instant"), GRACE);
}
function enhance(wrapper: HTMLElement) {
if (wrapper.hasAttribute("data-enhanced")) return;
wrapper.setAttribute("data-enhanced", "");
const bubble = wrapper.querySelector<HTMLElement>(":scope > [role=tooltip]");
const control = wrapper.querySelector<HTMLElement>("a[href], button, input, select, textarea, [tabindex]");
if (bubble && control && !control.hasAttribute("aria-describedby")) control.setAttribute("aria-describedby", bubble.id);
let showTimer = 0;
wrapper.addEventListener("mouseenter", () => {
clearTimeout(showTimer);
if (document.documentElement.hasAttribute("data-tooltip-instant")) show(wrapper);
else showTimer = window.setTimeout(() => show(wrapper), SHOW_DELAY);
});
wrapper.addEventListener("focusin", () => show(wrapper));
// A dismissed tooltip stays hidden until the control is neither hovered nor focused any more.
wrapper.addEventListener("mouseleave", () => {
clearTimeout(showTimer);
if (!wrapper.matches(":focus-within")) { wrapper.removeAttribute("data-dismissed"); hide(wrapper); }
});
wrapper.addEventListener("focusout", () => {
if (!wrapper.matches(":hover")) { wrapper.removeAttribute("data-dismissed"); hide(wrapper); }
});
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-tooltip]").forEach(enhance);
init();
document.addEventListener("astro:page-load", init);
if (!document.documentElement.hasAttribute("data-haze-tooltips")) {
document.documentElement.setAttribute("data-haze-tooltips", "");
document.addEventListener("keydown", (event) => {
if (event.key !== "Escape") return;
document.querySelectorAll<HTMLElement>("[data-haze-tooltip]:is(:hover, :focus-within)").forEach((wrapper) => wrapper.setAttribute("data-dismissed", ""));
});
}
</script>When to use
- Explaining what an icon-only button does
- A short consequence or shortcut hint for a labelled control
When not to use
- Information people need to complete the task; show it on the page
- Anything with links or buttons inside; use popover
- Touch-first screens, where hover does not exist
Installation
Add the Haze registry to components.json once, then install Tooltip with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/tooltipnpx shadcn@latest add @haze/tooltipyarn dlx shadcn@latest add @haze/tooltipbunx --bun shadcn@latest add @haze/tooltipFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique on the page. Reference it from the control with aria-describedby. |
| text* | string | – | Plain text, ideally under 80 characters. |
| side | "top" | "bottom" | "top" | Where the bubble appears. |
| align | "start" | "center" | "end" | "center" | Bubble alignment; use start or end near a screen edge. |
| class | string | – | Extra classes on the wrapper. |
| default slot* | slot | – | One focusable control, such as a button or link. |
Keyboard interactions
- Tab
- Focus the control and show the tooltip
- Escape
- Hide the tooltip without moving focus
Accessibility
- The bubble fades in after a 150 ms hover delay, so passing over a control does not flash it.
- The bubble has role="tooltip" and describes the control; it never replaces the control's own name, so icon buttons still need aria-label.
- Meets WCAG 1.4.13: it shows on focus as well as hover, stays while the pointer is over it, and Escape hides it (the Escape part needs the script).
- Tooltips form an instant group: once one has shown, moving to another within 300 ms shows it at once with no delay and no fade; after 300 ms idle the 150 ms delay returns. The group needs the script; without it every tooltip keeps the delay.
- If the control has no aria-describedby, the script adds it; set it in the markup so it also works with scripts off.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Tooltip" with `npx shadcn@latest add @haze/tooltip`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Tooltip from "@/components/haze/ui/tooltip.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/tooltip.md
Customize with real content
Adapt the Haze tooltip ui for our product. Replace the demo values of these props with our real content: id, text, side, align, class, default slot. Keep the layout, the heading level rules and these accessibility details: The bubble fades in after a 150 ms hover delay, so passing over a control does not flash it. The bubble has role="tooltip" and describes the control; it never replaces the control's own name, so icon buttons still need aria-label. Meets WCAG 1.4.13: it shows on focus as well as hover, stays while the pointer is over it, and Escape hides it (the Escape part needs the script). Tooltips form an instant group: once one has shown, moving to another within 300 ms shows it at once with no delay and no fade; after 300 ms idle the 150 ms delay returns. The group needs the script; without it every tooltip keeps the delay. If the control has no aria-describedby, the script adds it; set it in the markup so it also works with scripts off. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using tooltip, check it fits: use it when explaining what an icon-only button does; a short consequence or shortcut hint for a labelled control. Avoid it when information people need to complete the task; show it on the page; anything with links or buttons inside; use popover; touch-first screens, where hover does not exist. Alternatives: @haze/popover.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.