Tooltip
An animated React tooltip built on shadcn/ui and Radix: the drop-in Tooltip opens from its trigger with a short fade and scale on an elevated dark surface — plus Motion blocks for a spring tooltip with a one-line API and an overlapping avatar group whose name cards tilt toward the cursor.
- Drop-in shadcn API
- Radix Tooltip
- Spring animation
- Motion values
- prefers-reduced-motion
import { AvatarTooltipGroup } from "@/components/dusk/tooltip/avatar-tooltip-group";
const team = [
{ name: "Mia Chen", role: "Design engineer" },
{ name: "Leo Okafor", role: "Frontend" },
{ name: "Ava Rossi", role: "Product design" },
{ name: "Kai Nakamura", role: "Platform" },
{ name: "Noah Patel", role: "Developer relations" },
{ name: "Zoe Martin", role: "Support" },
{ name: "Ivan Petrov", role: "Infrastructure" },
];
export default function AvatarTooltipGroupDemo() {
return <AvatarTooltipGroup aria-label="Project members" items={team} max={5} size={44} />;
}Installation
$ npx shadcn@latest add https://duskui.com/r/tooltip.json1. Install the dependencies.
npm install @radix-ui/react-tooltip2. Copy the source into
components/ui/tooltip.tsx.tooltip.tsx "use client" import * as React from "react" import * as TooltipPrimitive from "@radix-ui/react-tooltip" import { cn } from "@/lib/utils" /** * DuskUI Tooltip — a drop-in upgrade of the shadcn/ui Tooltip. * * Same parts, props and data-slot attributes as shadcn (Tooltip, * TooltipTrigger, TooltipContent, TooltipProvider) plus: * - An elevated dark surface with a hairline border and a matching arrow. * - Opens from the trigger: scales slightly from the Radix transform origin and * shifts a few pixels from the side it opens on (150ms ease-out). * Closes faster than it opens. * - `showArrow={false}` to hide the arrow. * - Under `prefers-reduced-motion` it only fades. * CSS only (tw-animate-css), powered by Radix for hover/focus intent and aria. */ function TooltipProvider({ delayDuration = 0, ...props }: React.ComponentProps<typeof TooltipPrimitive.Provider>) { return ( <TooltipPrimitive.Provider data-slot="tooltip-provider" delayDuration={delayDuration} {...props} /> ) } /** Root. Wraps itself in a TooltipProvider, like shadcn, so it works anywhere. */ function Tooltip({ ...props }: React.ComponentProps<typeof TooltipPrimitive.Root>) { return ( <TooltipProvider> <TooltipPrimitive.Root data-slot="tooltip" {...props} /> </TooltipProvider> ) } /** The element that shows the tooltip on hover and keyboard focus. Use `asChild` for your own button. */ function TooltipTrigger({ ...props }: React.ComponentProps<typeof TooltipPrimitive.Trigger>) { return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} /> } /** The floating label, portalled to `document.body`. */ function TooltipContent({ className, sideOffset = 0, showArrow = true, children, ...props }: React.ComponentProps<typeof TooltipPrimitive.Content> & { /** Render the small arrow pointing at the trigger. */ showArrow?: boolean }) { return ( <TooltipPrimitive.Portal> <TooltipPrimitive.Content data-slot="tooltip-content" sideOffset={sideOffset} className={cn( "z-50 w-fit max-w-xs origin-(--radix-tooltip-content-transform-origin) rounded-md border border-white/10 bg-popover px-2.5 py-1.5 text-xs text-balance text-popover-foreground", "shadow-md", "animate-in fade-in-0 zoom-in-95 duration-150 ease-out", "data-[side=bottom]:slide-in-from-top-1 data-[side=left]:slide-in-from-right-1 data-[side=right]:slide-in-from-left-1 data-[side=top]:slide-in-from-bottom-1", "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:duration-100 data-[state=closed]:ease-in", "motion-reduce:[--tw-enter-scale:1]! motion-reduce:[--tw-enter-translate-x:0]! motion-reduce:[--tw-enter-translate-y:0]! motion-reduce:[--tw-exit-scale:1]!", className )} {...props} > {children} {showArrow && ( <TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%_-_1px)] rotate-45 rounded-[2px] border-r border-b border-white/10 bg-popover fill-popover" /> )} </TooltipPrimitive.Content> </TooltipPrimitive.Portal> ) } export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
Replaces components/ui/tooltip.tsx with the same parts and props, so the shadcn Sidebar and any existing tooltips keep working. CSS only (tw-animate-css).
Usage
import { Copy } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"
export function CopyButton() {
return (
<Tooltip>
<TooltipTrigger asChild>
<Button variant="outline" size="icon" aria-label="Copy link">
<Copy />
</Button>
</TooltipTrigger>
<TooltipContent>Copy link</TooltipContent>
</Tooltip>
)
}Examples
Default
Hover or Tab to an icon button. The tooltip grows out of the trigger and closes faster than it opens.
import { Copy, Plus, Trash2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
const actions = [
{ label: "Add item", icon: Plus },
{ label: "Copy link", icon: Copy },
{ label: "Delete", icon: Trash2 },
];
export default function TooltipBasicDemo() {
return (
<>
{actions.map(({ label, icon: Icon }) => (
<Tooltip key={label}>
<TooltipTrigger asChild>
<Button variant="outline" size="icon" aria-label={label}>
<Icon />
</Button>
</TooltipTrigger>
<TooltipContent>{label}</TooltipContent>
</Tooltip>
))}
</>
);
}Sides
side on TooltipContent picks where it opens; it slides in from that side and flips automatically near the viewport edge.
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
const sides = ["top", "right", "bottom", "left"] as const;
export default function TooltipSidesDemo() {
return (
<>
{sides.map((side) => (
<Tooltip key={side}>
<TooltipTrigger asChild>
<Button variant="outline" className="capitalize">
{side}
</Button>
</TooltipTrigger>
<TooltipContent side={side}>Opens on the {side}</TooltipContent>
</Tooltip>
))}
</>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Animated Tooltip
A spring tooltip with a one-line API: wrap any trigger and pass content. It scales up from the Radix transform origin, travels a few pixels away from the side it opens on, then fades back on exit thanks to AnimatePresence and forceMount.
import { Bold, Italic, Link2, Underline } from "lucide-react";
import { Button } from "@/components/ui/button";
import { AnimatedTooltip } from "@/components/dusk/tooltip/animated-tooltip";
const tools = [
{ label: "Bold", keys: "⌘B", icon: Bold },
{ label: "Italic", keys: "⌘I", icon: Italic },
{ label: "Underline", keys: "⌘U", icon: Underline },
{ label: "Insert link", keys: "⌘K", icon: Link2 },
];
export default function AnimatedTooltipDemo() {
return (
<div role="toolbar" aria-label="Formatting" className="flex gap-1 rounded-xl border bg-card p-1.5">
{tools.map(({ label, keys, icon: Icon }) => (
<AnimatedTooltip
key={label}
content={
<span className="flex items-center gap-2">
{label}
<kbd className="rounded border border-white/10 bg-white/5 px-1 font-mono text-[0.625rem] text-muted-foreground">
{keys}
</kbd>
</span>
}
>
<Button variant="ghost" size="icon" aria-label={label}>
<Icon />
</Button>
</AnimatedTooltip>
))}
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-tooltip.json1. Install the dependencies.
npm install motion @radix-ui/react-tooltip2. Copy the source into
components/dusk/tooltip/animated-tooltip.tsx.animated-tooltip.tsx "use client"; import * as React from "react"; import * as TooltipPrimitive from "@radix-ui/react-tooltip"; import { AnimatePresence, motion, useReducedMotion, type Transition } from "motion/react"; import { Tooltip, TooltipTrigger } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; type Side = "top" | "right" | "bottom" | "left"; export interface AnimatedTooltipProps extends Omit< React.ComponentProps<typeof TooltipPrimitive.Content>, "content" | "children" | "asChild" | "forceMount" | "side" > { /** What the tooltip says. */ content: React.ReactNode; /** The trigger. Must accept a ref and props (a button, link or DuskUI Button). */ children: React.ReactElement; side?: Side; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; /** Hover delay in ms before opening. */ delayDuration?: number; showArrow?: boolean; } const spring: Transition = { type: "spring", bounce: 0.1, duration: 0.2 }; // Where the tooltip starts, relative to its final spot: pulled back toward the trigger. const fromSide: Record<Side, { x?: number; y?: number }> = { top: { y: 4 }, bottom: { y: -4 }, left: { x: 4 }, right: { x: -4 }, }; /** * A tooltip that scales out of its trigger: it scales up slightly from the Radix * transform origin and travels a few pixels away from the side it opens on, * then fades back on exit. One-line API: wrap any trigger. * Built on the DuskUI Tooltip (Radix), so it opens on hover and keyboard focus, * closes on Escape and is linked to the trigger with aria-describedby. */ export function AnimatedTooltip({ content, children, side = "top", sideOffset, open: openProp, defaultOpen = false, onOpenChange, delayDuration = 150, showArrow = true, className, ...props }: AnimatedTooltipProps) { const reduceMotion = useReducedMotion(); const [uncontrolled, setUncontrolled] = React.useState(defaultOpen); const open = openProp ?? uncontrolled; const offset = reduceMotion ? {} : fromSide[side]; const setOpen = (next: boolean) => { if (openProp === undefined) setUncontrolled(next); onOpenChange?.(next); }; return ( <Tooltip open={open} onOpenChange={setOpen} delayDuration={delayDuration}> <TooltipTrigger asChild>{children}</TooltipTrigger> <AnimatePresence> {open && ( // forceMount lets AnimatePresence keep the content around for the exit animation. <TooltipPrimitive.Portal forceMount key="tooltip"> <TooltipPrimitive.Content forceMount asChild side={side} sideOffset={sideOffset ?? (showArrow ? 2 : 8)} {...props}> <motion.div data-slot="tooltip-content" initial={{ opacity: 0, scale: 0.96, ...offset }} animate={{ opacity: 1, scale: 1, x: 0, y: 0 }} exit={{ opacity: 0, scale: 0.96, ...offset, transition: { duration: 0.1, ease: "easeIn" }, }} transition={spring} style={{ transformOrigin: "var(--radix-tooltip-content-transform-origin)" }} className={cn( "z-50 w-fit max-w-xs rounded-md border border-white/10 bg-popover px-3 py-1.5 text-xs text-balance text-popover-foreground", "shadow-md", className )} > {content} {showArrow && ( <TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%_-_1px)] rotate-45 rounded-[2px] border-r border-b border-white/10 bg-popover fill-popover" /> )} </motion.div> </TooltipPrimitive.Content> </TooltipPrimitive.Portal> )} </AnimatePresence> </Tooltip> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| content | ReactNode | — | What the tooltip says. Required. |
| children | ReactElement | — | The trigger; must accept a ref (button, link, Button). |
| side | "top" | "right" | "bottom" | "left" | "top" | Preferred side; flips on collision. |
| sideOffset | number | 2 (8 without arrow) | Distance from the trigger in px. |
| delayDuration | number | 150 | Hover delay before opening, in ms. |
| showArrow | boolean | true | Render the arrow. |
| open / defaultOpen / onOpenChange | boolean / (open: boolean) => void | — | Controlled or initial state. |
| align / collisionPadding / className… | Radix TooltipContent props | — | Forwarded to the content. |
Avatar Tooltip Group
Overlapping avatars for teams and collaborators. Hovering one lifts it above its neighbours and opens a name card that tilts slightly toward the cursor as you move across the face — driven by motion values, so it never re-renders React. Extra members collapse into a “+N” chip whose tooltip lists them. Uses plain <img> or generated initials.
import { AvatarTooltipGroup } from "@/components/dusk/tooltip/avatar-tooltip-group";
const team = [
{ name: "Mia Chen", role: "Design engineer" },
{ name: "Leo Okafor", role: "Frontend" },
{ name: "Ava Rossi", role: "Product design" },
{ name: "Kai Nakamura", role: "Platform" },
{ name: "Noah Patel", role: "Developer relations" },
{ name: "Zoe Martin", role: "Support" },
{ name: "Ivan Petrov", role: "Infrastructure" },
];
export default function AvatarTooltipGroupDemo() {
return <AvatarTooltipGroup aria-label="Project members" items={team} max={5} size={44} />;
}$ npx shadcn@latest add https://duskui.com/r/avatar-tooltip-group.json1. Install the dependencies.
npm install motion @radix-ui/react-tooltip2. Copy the source into
components/dusk/tooltip/avatar-tooltip-group.tsx.avatar-tooltip-group.tsx "use client"; import * as React from "react"; import * as TooltipPrimitive from "@radix-ui/react-tooltip"; import { AnimatePresence, motion, useMotionValue, useReducedMotion, useSpring, useTransform, } from "motion/react"; import { Tooltip, TooltipTrigger } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; export interface AvatarTooltipItem { name: string; /** Second line in the tooltip, e.g. a role or status. */ role?: string; /** Image URL. Falls back to initials on a color derived from the name. */ src?: string; /** Override the generated initials. */ initials?: string; } export interface AvatarTooltipGroupProps extends React.ComponentProps<"div"> { items: AvatarTooltipItem[]; /** Show at most this many avatars; the rest collapse into a "+N" chip with its own tooltip. */ max?: number; /** Avatar diameter in px. */ size?: number; /** Called when an avatar is clicked. */ onAvatarClick?: (item: AvatarTooltipItem) => void; } const tiltSpring = { stiffness: 220, damping: 26, mass: 0.5 }; function initialsOf(name: string) { return name .split(/\s+/) .filter(Boolean) .slice(0, 2) .map((part) => part.charAt(0).toUpperCase()) .join(""); } /** Deterministic hue from the name, so SSR and client agree and colors are stable. */ function hueOf(name: string) { let hash = 0; for (let i = 0; i < name.length; i++) hash = (hash * 31 + name.charCodeAt(i)) % 360; return hash; } function GroupMember({ label, tooltip, size, onClick, children, className, style, }: { label: string; tooltip: React.ReactNode; size: number; onClick?: () => void; children: React.ReactNode; className?: string; style?: React.CSSProperties; }) { const reduceMotion = useReducedMotion(); const [open, setOpen] = React.useState(false); // Pointer position across the avatar: -1 (left edge) … 1 (right edge). const pointer = useMotionValue(0); const rotate = useSpring(useTransform(pointer, [-1, 1], [-6, 6]), tiltSpring); const x = useSpring(useTransform(pointer, [-1, 1], [-4, 4]), tiltSpring); const handleMove = (event: React.PointerEvent<HTMLButtonElement>) => { if (reduceMotion || event.pointerType !== "mouse") return; const rect = event.currentTarget.getBoundingClientRect(); pointer.set(((event.clientX - rect.left) / rect.width) * 2 - 1); }; return ( <Tooltip open={open} onOpenChange={setOpen} delayDuration={0}> <TooltipTrigger asChild> <button type="button" aria-label={label} onClick={onClick} onPointerMove={handleMove} onPointerLeave={() => pointer.set(0)} className={cn( "relative -ms-3 grid shrink-0 place-items-center overflow-hidden rounded-full font-semibold text-white outline-none first:ms-0", "ring-2 ring-background transition-[translate,box-shadow] duration-150 ease-out", "hover:z-10 hover:-translate-y-0.5 focus-visible:z-10 focus-visible:-translate-y-0.5", "focus-visible:ring-ring motion-reduce:transition-none motion-reduce:hover:translate-y-0", "motion-reduce:focus-visible:translate-y-0", className )} style={{ width: size, height: size, fontSize: size * 0.36, ...style }} > {children} </button> </TooltipTrigger> <AnimatePresence> {open && ( <TooltipPrimitive.Portal forceMount key="tooltip"> <TooltipPrimitive.Content forceMount asChild side="top" sideOffset={10} // Follow the avatar while it lifts on hover. updatePositionStrategy="always" > <motion.div data-slot="tooltip-content" initial={{ opacity: 0, y: 4, scale: 0.96 }} animate={{ opacity: 1, y: 0, scale: 1 }} exit={{ opacity: 0, y: 4, scale: 0.96, transition: { duration: 0.1 } }} transition={{ type: "spring", bounce: 0.1, duration: 0.25 }} style={{ rotate, x, transformOrigin: "var(--radix-tooltip-content-transform-origin)" }} className={cn( "z-50 rounded-md border border-white/10 bg-popover px-3 py-1.5 text-center text-popover-foreground", "shadow-md" )} > {tooltip} </motion.div> </TooltipPrimitive.Content> </TooltipPrimitive.Portal> )} </AnimatePresence> </Tooltip> ); } /** * Overlapping avatars for teams, collaborators or "who's here". Hovering one * lifts it above its neighbours and opens a name card that tilts slightly * toward the cursor as you move across the face. Extra members collapse * into a "+N" chip whose tooltip lists them. Every avatar is a real button, * so tooltips also open on keyboard focus. Tilt uses motion values (no React * re-renders), ignores touch and turns off under reduced motion. */ export function AvatarTooltipGroup({ items, max = 5, size = 40, onAvatarClick, className, ...props }: AvatarTooltipGroupProps) { const visible = items.slice(0, max); const hidden = items.slice(max); return ( <div data-slot="avatar-tooltip-group" role="group" className={cn("flex items-center", className)} {...props}> {visible.map((item) => { const hue = hueOf(item.name); return ( <GroupMember key={item.name} label={item.name} size={size} onClick={onAvatarClick ? () => onAvatarClick(item) : undefined} style={ item.src ? undefined : { background: `oklch(0.42 0.06 ${hue})`, } } tooltip={ <> <p className="text-sm font-semibold leading-5">{item.name}</p> {item.role && <p className="text-xs text-muted-foreground">{item.role}</p>} </> } > {item.src ? ( // Plain <img> so the block works outside Next.js; the button carries the name. // eslint-disable-next-line @next/next/no-img-element <img src={item.src} alt="" className="size-full object-cover" draggable={false} /> ) : ( <span aria-hidden> {item.initials ?? initialsOf(item.name)} </span> )} </GroupMember> ); })} {hidden.length > 0 && ( <GroupMember label={`${hidden.length} more: ${hidden.map((item) => item.name).join(", ")}`} size={size} className="border border-white/10 bg-muted text-muted-foreground" tooltip={ <ul className="space-y-0.5 text-left text-xs"> {hidden.map((item) => ( <li key={item.name}> <span className="font-medium text-foreground">{item.name}</span> {item.role && <span className="text-muted-foreground"> · {item.role}</span>} </li> ))} </ul> } > <span aria-hidden>+{hidden.length}</span> </GroupMember> )} </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| items | { name, role?, src?, initials? }[] | — | The people. Without src, initials sit on a muted color derived from the name. |
| max | number | 5 | Avatars shown before the +N chip. |
| size | number | 40 | Avatar diameter in px. |
| onAvatarClick | (item) => void | — | Called when an avatar is clicked. |
| aria-label | string | — | Names the group, e.g. “Project members”. |
API Reference
Same parts as shadcn/ui: TooltipProvider, Tooltip, TooltipTrigger and TooltipContent, each forwarding its Radix Tooltip props. Like shadcn, Tooltip wraps itself in a provider so it works anywhere.
| Prop | Type | Default | Description |
|---|---|---|---|
| side / align | "top" | "right" | "bottom" | "left" / "start" | "center" | "end" | "top" / "center" | On TooltipContent. |
| sideOffset | number | 0 | On TooltipContent: distance from the trigger (the arrow adds its own height). |
| showArrow | boolean | true | On TooltipContent: render the arrow. |
| delayDuration | number | 0 | On TooltipProvider or Tooltip: hover delay in ms. |
| open / defaultOpen / onOpenChange | boolean / (open: boolean) => void | — | On Tooltip: controlled or initial state. |
Accessibility
- Built on Radix Tooltip: opens on hover and keyboard focus, closes on Escape and pointer-down, and links the trigger with aria-describedby.
- Tooltips describe, they don't name: icon-only triggers still need an aria-label.
- Every avatar in the group is a focusable button named after the person, and the +N chip's name lists the hidden members.
- Under prefers-reduced-motion tooltips only fade, and the avatar lift and tooltip tilt are turned off.