Tooltip
A short text hint for the control it belongs to: what an icon button does, why an option is off. It shows on hover and on keyboard focus and holds text only. For anything a reader has to press or read at length use a popover; for a preview of a link use a hover card.
Install
npx shadcn@latest add https://thrumui.dev/r/tooltip.jsonAdds components/ui/tooltip.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { TooltipProvider } from "@/components/ui/tooltip";Props
| Prop | Type | Default | Description |
|---|---|---|---|
TooltipProvider: delay | number | Default 400 | How long the pointer rests on a trigger before its hint shows, in ms. |
TooltipProvider: closeDelay | number | Default 0 | How long a hint stays after the pointer leaves, in ms. |
TooltipProvider: timeout | number | Default 400 | After a hint closes, the next one in the group shows at once for this long, in ms. |
Tooltip: open | boolean | – | Whether the hint shows, when the parent holds the state. Use it with onOpenChange. |
Tooltip: defaultOpen | boolean | Default false | Whether the hint shows from the start. |
Tooltip: onOpenChange | (open: boolean) => void | – | Called when the hint shows or hides. |
Tooltip: disabled | boolean | Default false | Never show the hint. |
Tooltip: disableHoverablePopup | boolean | Default false | Hide the hint as soon as the pointer leaves the trigger, even when it moves onto the hint. |
TooltipTrigger: render | ReactElement | – | The control the hint belongs to. Without it the trigger is a plain button. |
TooltipTrigger: delay | number | – | The wait for this trigger alone, in ms. It overrides the provider. |
TooltipTrigger: closeOnClick | boolean | Default true | Hide the hint when the trigger is pressed. |
TooltipContent: side | "top" | "right" | "bottom" | "left" | Default "top" | The side of the trigger the hint shows on. It flips to the opposite side when there is no room. |
TooltipContent: align | "start" | "center" | "end" | Default "center" | How the hint lines up with the trigger along that side. |
TooltipContent: sideOffset | number | Default 6 | The gap between the trigger and the hint, in pixels. With an arrow the default is 10. |
TooltipContent: alignOffset | number | Default 0 | Moves the hint along the trigger's side, in pixels. |
TooltipContent: collisionPadding | number | Default 8 | The space kept between the hint and the edge of the screen, in pixels. |
TooltipContent: arrow | boolean | Default false | Draw a small arrow that points at the trigger. |
TooltipContent: className | string | – | Extra classes for the hint. The default is at most 240 pixels wide; longer text wraps. |
Motion and sound
Motion
- The hint fades in and grows from 96% to full size over 150 ms, from the edge that faces the trigger.
- Hiding plays the same change backwards.
- Inside a provider, moving from one trigger to the next within 400 ms swaps the hints with no fade.
- Reduced motion: the hint appears and leaves at once.
Sound
Silent. This component makes no sound.
Accessibility
- It follows the WAI-ARIA tooltip pattern: the hint has role tooltip, and while it shows the trigger points at it with aria-describedby.
- The hint is a description, not a name. An icon button still needs its own aria-label, as in the example.
- It shows on hover and on keyboard focus, and Escape hides it. The pointer can move onto the hint without losing it.
- A hint holds text only. A link or a button inside it cannot be reached by keyboard: use a popover.
- Touch screens have no hover, so nothing a reader needs may live only in a tooltip.
- Under a glass material the hint is translucent. Its tint keeps the text at 4.5 to 1 over any page.
| Keys | Action |
|---|---|
| Tab | Focus the trigger. The hint shows at once, with no delay. |
| Esc | Hide the hint. Focus stays on the trigger. |
| Enter / Space | Press the trigger. The hint hides. |
Source
"use client";
import { createContext, useContext, useId, useState } from "react";
import { Tooltip as TooltipPrimitive } from "@base-ui/react/tooltip";
import { cn } from "@/lib/utils";
export type TooltipSide = "top" | "right" | "bottom" | "left";
export type TooltipAlign = "start" | "center" | "end";
/** How long the pointer rests on a trigger before its hint shows, in ms. */
const DELAY = 400;
const Grouped = createContext(false);
/** The hint's id while it is open, so the trigger can point at it as its description. */
const OpenHint = createContext<{ id: string; open: boolean } | null>(null);
export type TooltipProviderProps = TooltipPrimitive.Provider.Props;
/**
* Groups the tooltips inside it: once one has shown, the next one shows at once instead of after the delay,
* so a reader can sweep along a toolbar. Put one around the app, or around each toolbar.
*/
export function TooltipProvider({ delay = DELAY, ...props }: TooltipProviderProps) {
return (
<Grouped value={true}>
<TooltipPrimitive.Provider delay={delay} {...props} />
</Grouped>
);
}
export type TooltipProps = TooltipPrimitive.Root.Props;
/**
* A short text hint for the control it belongs to, shown on hover and on keyboard focus.
* It never holds a link or a button: the pointer cannot be relied on to reach it. Use a popover for that.
*/
export function Tooltip({ onOpenChange, ...props }: TooltipProps) {
const grouped = useContext(Grouped);
const id = useId();
// A copy of the open state, only to tell the trigger when its description exists.
const [shown, setShown] = useState(props.defaultOpen ?? false);
const root = (
<OpenHint value={{ id, open: props.open ?? shown }}>
<TooltipPrimitive.Root
onOpenChange={(open, details) => {
onOpenChange?.(open, details);
if (!details.isCanceled) setShown(open);
}}
{...props}
/>
</OpenHint>
);
// A tooltip outside any provider still waits the same 400 ms.
return grouped ? root : <TooltipPrimitive.Provider delay={DELAY}>{root}</TooltipPrimitive.Provider>;
}
/**
* The control the hint describes. It renders a button; pass your own control through `render`.
* The control keeps its own name: the hint is added as its description while it shows.
*/
export function TooltipTrigger(props: TooltipPrimitive.Trigger.Props) {
const hint = useContext(OpenHint);
return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" aria-describedby={hint?.open ? hint.id : undefined} {...props} />;
}
export type TooltipContentProps = TooltipPrimitive.Popup.Props &
Pick<TooltipPrimitive.Positioner.Props, "alignOffset" | "collisionPadding"> & {
/** The side of the trigger the hint shows on. It flips to the opposite side when there is no room. */
side?: TooltipSide;
/** How the hint lines up with the trigger along that side. */
align?: TooltipAlign;
/** The gap between the trigger and the hint, in pixels. The default is 6, or 10 with an arrow. */
sideOffset?: number;
/** Draw a small arrow that points at the trigger. */
arrow?: boolean;
};
/**
* The hint: plain text, 12 pixels, at most 240 pixels wide.
* It is a floating surface, so it turns to glass when the page's material asks for it.
*/
export function TooltipContent({ side = "top", align = "center", sideOffset, alignOffset, collisionPadding = 8, arrow = false, className, children, ...props }: TooltipContentProps) {
const hint = useContext(OpenHint);
return (
<TooltipPrimitive.Portal>
<TooltipPrimitive.Positioner side={side} align={align} sideOffset={sideOffset ?? (arrow ? 10 : 6)} alignOffset={alignOffset} collisionPadding={collisionPadding} className="z-50">
<TooltipPrimitive.Popup
id={hint?.id}
role="tooltip"
data-slot="tooltip"
data-surface="floating"
className={cn(
"w-max max-w-[min(15rem,var(--available-width))] origin-(--transform-origin) rounded-md border bg-popover px-2.5 py-2 text-xs leading-4 text-pretty text-popover-foreground shadow-md",
"transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 data-instant:transition-none motion-reduce:transition-none",
className,
)}
{...props}
>
{arrow && (
// A square turned on its corner and cut along the hint's edge, so only the half outside shows. It takes the hint's own colour.
<TooltipPrimitive.Arrow
data-slot="tooltip-arrow"
className={cn(
"size-2.5 border bg-inherit [clip-path:polygon(0_0,100%_0,0_100%)]",
"data-[side=bottom]:-top-1.5 data-[side=bottom]:rotate-45 data-[side=top]:-bottom-1.5 data-[side=top]:rotate-225",
"data-[side=right]:-left-1.5 data-[side=right]:-rotate-45 data-[side=left]:-right-1.5 data-[side=left]:rotate-135",
)}
/>
)}
{children}
</TooltipPrimitive.Popup>
</TooltipPrimitive.Positioner>
</TooltipPrimitive.Portal>
);
}Files and dependencies
- Adds
components/ui/tooltip.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.