Tooltip
Short delay once, then instant between targets.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/, installsradix-ui@base-ui/reactand the colour tokens go into your CSS. This is the Radix build. Drop --radix for Base UI.This is the Base UI build, the default. Want Radix? Add --radix.$ pnpm dlx asta-ui@latest add tooltip --radixOr install the package
Don’t want the file? Import from
asta-uiasta-ui/baseand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-ui radix-ui @base-ui/reactimport { Tooltip } from "asta-ui";import { Tooltip } from "asta-ui/base";Or copy it by hand
Install
radix-ui@base-ui/react, then copy this file. Keep the paths, or fix the@/imports. Then add the tokens fromglobals.css."use client"; import { Tooltip as Tip } from "radix-ui"; import { isValidElement, useEffect, useRef, useState, type ReactNode } from "react"; // Spread rather than a literal `asChild` attribute: the shadcn CLI rewrites `asChild` to `render` in Base UI // projects (base-* styles), which Radix doesn't understand. const SLOT = { asChild: true } as const; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); export type TooltipSide = "top" | "right" | "bottom" | "left"; // Shared by every tooltip on the page: once one has been shown, the next opens instantly // (as long as the pointer moves over within the grace period). let warmUntil = 0; const GRACE = 400; const KEYFRAMES = `@keyframes kit-tip-in { from { opacity: 0; transform: var(--kit-tip-from) scale(.95) } } @keyframes kit-tip-out { to { opacity: 0; transform: scale(.97) } }`; export interface TooltipProps { content: ReactNode; side?: TooltipSide; /** First-open delay in ms. Later tooltips open instantly while "warm". */ delay?: number; /** Optional shortcut shown at the end, e.g. "⌘S". */ shortcut?: string; /** A focusable element (a button, a link). Anything else gets wrapped in a span. */ children: ReactNode; /** Extra classes for the tooltip bubble. */ className?: string; } /** * A tooltip on Radix Tooltip: shows on hover or keyboard focus, hides on leave, blur, press or Escape, and * flips to the other side when it would leave the screen. After one has been shown, the next opens instantly. */ export function Tooltip({ content, side = "top", delay = 500, shortcut, children, className }: TooltipProps) { const [open, setOpen] = useState(false); const timer = useRef<ReturnType<typeof setTimeout>>(undefined); useEffect(() => () => clearTimeout(timer.current), []); const cancel = () => clearTimeout(timer.current); const onOpenChange = (next: boolean) => { clearTimeout(timer.current); if (!next) { if (open) warmUntil = Date.now() + GRACE; setOpen(false); } else if (Date.now() < warmUntil) setOpen(true); else timer.current = setTimeout(() => setOpen(true), delay); }; return ( <Tip.Provider delayDuration={0} skipDelayDuration={0}> <Tip.Root open={open} onOpenChange={onOpenChange}> {/* Outside the content: Radix copies the content into a hidden role="tooltip" node for screen readers. React 19 dedupes it by href, so a page of tooltips adds it once. */} <style href="asta-tooltip" precedence="asta"> {KEYFRAMES} </style> {/* Radix doesn't report a close for a tooltip that never opened, so drop a pending open here. */} <Tip.Trigger {...SLOT} onPointerLeave={cancel} onBlur={cancel} onPointerDown={cancel}>{isValidElement(children) ? children : <span className="inline-flex">{children}</span>}</Tip.Trigger> <Tip.Portal> <Tip.Content side={side} sideOffset={8} collisionPadding={8} className={cx( "z-50 flex w-max max-w-[240px] select-none items-center gap-2 rounded-lg bg-ink px-2.5 py-1.5 text-[12px] font-medium leading-snug text-paper", "shadow-[0_6px_20px_-6px_rgb(0_0_0/0.35)] origin-[var(--radix-tooltip-content-transform-origin)]", "data-[side=top]:[--kit-tip-from:translateY(4px)] data-[side=bottom]:[--kit-tip-from:translateY(-4px)] data-[side=left]:[--kit-tip-from:translateX(4px)] data-[side=right]:[--kit-tip-from:translateX(-4px)]", "animate-[kit-tip-in_.2s_cubic-bezier(.16,1,.3,1)_both] data-[state=closed]:animate-[kit-tip-out_.1s_cubic-bezier(.16,1,.3,1)_both] motion-reduce:!animate-none", className, )} > {content} {shortcut && <kbd className="rounded bg-paper/15 px-1 font-sans text-[11px] text-paper/80">{shortcut}</kbd>} <Tip.Arrow width={10} height={5} className="fill-ink" /> </Tip.Content> </Tip.Portal> </Tip.Root> </Tip.Provider> ); } export default Tooltip;"use client"; import { Tooltip as Tip } from "@base-ui/react/tooltip"; import { isValidElement, useEffect, useRef, useState, type ReactElement, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); export type TooltipSide = "top" | "right" | "bottom" | "left"; // Shared by every tooltip on the page: once one has been shown, the next opens instantly // (as long as the pointer moves over within the grace period). let warmUntil = 0; const GRACE = 400; // Base UI can report the next trigger's hover before the previous tooltip's close, so an open tooltip also counts as warm. let shown = 0; export interface TooltipProps { content: ReactNode; side?: TooltipSide; /** First-open delay in ms. Later tooltips open instantly while "warm". */ delay?: number; /** Optional shortcut shown at the end, e.g. "⌘S". */ shortcut?: string; /** A focusable element (a button, a link). Anything else gets wrapped in a span. */ children: ReactNode; /** Extra classes for the tooltip bubble. */ className?: string; } /** * A tooltip on Base UI Tooltip: shows on hover or keyboard focus, hides on leave, blur, press or Escape, and * flips to the other side when it would leave the screen. After one has been shown, the next opens instantly. */ export function Tooltip({ content, side = "top", delay = 500, shortcut, children, className }: TooltipProps) { const [open, setOpen] = useState(false); const timer = useRef<ReturnType<typeof setTimeout>>(undefined); const isOpen = useRef(false); useEffect( () => () => { clearTimeout(timer.current); if (isOpen.current) shown--; }, [], ); const set = (next: boolean) => { if (next === isOpen.current) return; isOpen.current = next; shown += next ? 1 : -1; setOpen(next); }; // Base UI reports intent immediately (delay 0); the first-open delay and the page-wide warm window live here. const onOpenChange = (next: boolean) => { clearTimeout(timer.current); if (!next) { if (open) warmUntil = Date.now() + GRACE; set(false); } else if (shown > 0 || Date.now() < warmUntil) set(true); else timer.current = setTimeout(() => set(true), delay); }; const trigger = isValidElement(children) ? (children as ReactElement) : <span className="inline-flex">{children}</span>; return ( <Tip.Provider delay={0}> <Tip.Root open={open} onOpenChange={onOpenChange}> <Tip.Trigger delay={0} closeDelay={0} render={trigger} /> <Tip.Portal> <Tip.Positioner side={side} sideOffset={8} collisionPadding={8} className="z-50"> <Tip.Popup className={cx( "flex w-max max-w-[240px] select-none items-center gap-2 rounded-lg bg-ink px-2.5 py-1.5 text-[12px] font-medium leading-snug text-paper", "shadow-[0_6px_20px_-6px_rgb(0_0_0/0.35)] origin-[var(--transform-origin)]", // Enter: rise from the trigger side with the studio ease. Exit: a quicker fade. "transition-[opacity,transform] duration-200 ease-[cubic-bezier(.16,1,.3,1)] data-[ending-style]:duration-100 data-[ending-style]:ease-[cubic-bezier(.16,1,.3,1)]", "data-[starting-style]:opacity-0 data-[ending-style]:opacity-0 data-[ending-style]:scale-[.97]", "data-[side=top]:data-[starting-style]:[transform:translateY(4px)_scale(.95)] data-[side=bottom]:data-[starting-style]:[transform:translateY(-4px)_scale(.95)]", "data-[side=left]:data-[starting-style]:[transform:translateX(4px)_scale(.95)] data-[side=right]:data-[starting-style]:[transform:translateX(-4px)_scale(.95)]", "motion-reduce:!transition-none", className, )} > {content} {shortcut && <kbd className="rounded bg-paper/15 px-1 font-sans text-[11px] text-paper/80">{shortcut}</kbd>} <Tip.Arrow className="flex data-[side=bottom]:top-[-5px] data-[side=left]:right-[-7.5px] data-[side=left]:-rotate-90 data-[side=right]:left-[-7.5px] data-[side=right]:rotate-90 data-[side=top]:bottom-[-5px] data-[side=bottom]:rotate-180"> <svg width="10" height="5" viewBox="0 0 10 5" className="fill-ink" aria-hidden> <path d="M0 0h10L5 5z" /> </svg> </Tip.Arrow> </Tip.Popup> </Tip.Positioner> </Tip.Portal> </Tip.Root> </Tip.Provider> ); } export default Tooltip;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Tooltip component to my project. Short delay once, then instant between targets. It includes: top, right, bottom, left, delay group. Install it (Radix build): ```sh pnpm dlx asta-ui@latest add tooltip --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/tooltip.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { Button } from "@/components/astabyte/button"; import { Tooltip } from "@/components/astabyte/tooltip"; ``` Usage example and props: https://ui.astabyte.com/tooltip Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour (interactive pieces are built on Radix primitives from the radix-ui package). - Stay on Radix: install other Astabyte UI components with `--radix` so the project uses one primitive library. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Add the Astabyte UI Tooltip component to my project. Short delay once, then instant between targets. It includes: top, right, bottom, left, delay group. Install it (Base UI build): ```sh pnpm dlx asta-ui@latest add tooltip ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/base/tooltip.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { Button } from "@/components/astabyte/button"; import { Tooltip } from "@/components/astabyte/tooltip"; ``` Usage example and props: https://ui.astabyte.com/tooltip Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour (interactive pieces are built on Base UI primitives from the @base-ui/react package). - Stay on Base UI: install other Astabyte UI components without `--radix` so the project uses one primitive library. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<div style="display:flex;flex-wrap:wrap;gap:12px;padding:40px 0"> <span class="asta-tooltip"><button class="asta-btn asta-btn-outline" aria-describedby="t1"><span class="asta-btn-label">Top</span></button><span class="asta-tooltip-bubble" id="t1" role="tooltip">Copy link<kbd class="asta-tooltip-kbd">⌘C</kbd><span class="asta-tooltip-arrow" aria-hidden="true"></span></span></span> <span class="asta-tooltip asta-tooltip-bottom asta-tooltip-instant"><button class="asta-btn asta-btn-outline" aria-describedby="t2"><span class="asta-btn-label">Bottom</span></button><span class="asta-tooltip-bubble" id="t2" role="tooltip">Below<span class="asta-tooltip-arrow" aria-hidden="true"></span></span></span> <span class="asta-tooltip asta-tooltip-right asta-tooltip-instant" id="tipright"><button class="asta-btn asta-btn-outline" aria-describedby="t3"><span class="asta-btn-label">Right</span></button><span class="asta-tooltip-bubble" id="t3" role="tooltip">On the right<span class="asta-tooltip-arrow" aria-hidden="true"></span></span></span> </div>
Usage
The demo above, as code.
"use client";
import { Button } from "@/components/astabyte/button";
import { Tooltip } from "@/components/astabyte/tooltip";
const icon = (d: string) => (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d={d} />
</svg>
);
const TOOLS = [
{ label: "Bold", shortcut: "⌘B", d: "M7 4.5h5.5a3.75 3.75 0 0 1 0 7.5H7ZM7 12h6.5a3.75 3.75 0 0 1 0 7.5H7Z" },
{ label: "Italic", shortcut: "⌘I", d: "M10 4.5h8M6 19.5h8M14 4.5l-4 15" },
{ label: "Link", shortcut: "⌘K", d: "M10 14a4 4 0 0 0 5.66 0l3-3a4 4 0 0 0-5.66-5.66l-1 1M14 10a4 4 0 0 0-5.66 0l-3 3a4 4 0 0 0 5.66 5.66l1-1" },
{ label: "Quote", shortcut: "⌘⇧9", d: "M9.5 11.5h-4a1 1 0 0 1-1-1v-3a1 1 0 0 1 1-1h3a1 1 0 0 1 1 1v4c0 3.5-1.5 5.5-5 6.5M19 11.5h-4a1 1 0 0 1-1-1v-3a1 1 0 0 1 1-1h3a1 1 0 0 1 1 1v4c0 3.5-1.5 5.5-5 6.5" },
{ label: "Code", shortcut: "⌘E", d: "M8.5 7l-5 5 5 5M15.5 7l5 5-5 5" },
];
export default function TooltipDemo() {
return (
<div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
<div className="flex flex-col items-center gap-14">
<div className="flex flex-col items-center gap-3">
<div className="flex gap-0.5 rounded-[10px] bg-paper p-1 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent),0_8px_24px_-12px_rgb(0_0_0/0.2)]">
{TOOLS.map((t) => (
<Tooltip key={t.label} content={t.label} shortcut={t.shortcut}>
<Button variant="ghost" size="sm" iconOnly aria-label={t.label}>
{icon(t.d)}
</Button>
</Tooltip>
))}
</div>
<p className="text-[12px] text-mute">Wait once, then slide along: the rest open instantly.</p>
</div>
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
{(["top", "right", "bottom", "left"] as const).map((side) => (
<Tooltip key={side} side={side} content={`Opens on the ${side}`} delay={200}>
<Button variant="outline" size="sm" className="w-24 capitalize">
{side}
</Button>
</Tooltip>
))}
</div>
</div>
</div>
);
}