Components
Tooltip
A short label on hover and focus, built on Radix.
Use Tooltip to add a few words of extra help to a control, such as naming an icon button. Never put essential information in one: tooltips don't appear on touch screens, and many people never see them.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/tooltip.json
This adds one dependency, @radix-ui/react-tooltip, which the command installs for you.
Usage
The child must be a focusable element that accepts a ref, such as a button or link.
import { Tooltip } from "@/components/ui/tooltip";
<Tooltip content="Add to favourites">
<Button size="icon" aria-label="Favourite">♥</Button>
</Tooltip>To share one delay across many tooltips, wrap them in TooltipProvider.
Props
Tooltip takes the Radix content props, such as side, align and sideOffset.
| Prop | Type | Default | Description |
|---|---|---|---|
| content | ReactNode | – | The tooltip text. Required. |
| children | ReactNode | – | The trigger element. Required. |
| delayDuration | number | 300 | Milliseconds before it appears. |
| side | "top" | "right" | "bottom" | "left" | "top" | Where it appears. It flips if there is no room. |
Accessibility
- It appears on keyboard focus as well as hover, and Escape dismisses it.
- It is announced as a description of the trigger (
aria-describedby). It does not replace the trigger's name, so an icon-only button still needs its ownaria-label. - It stays open while the pointer is over it, so users who magnify the screen can read it (WCAG 1.4.13).
- It does not appear on touch, so don't rely on it there.
Tokens used
--foreground, --background, --duration-fast, --z-tooltip.
Known limitations
- Text only: no interactive content. Use a Popover for that.
- It does not animate out.
Source
"use client";import * as TooltipPrimitive from "@radix-ui/react-tooltip";import type { ComponentProps, ReactNode } from "react";import { cn } from "@/lib/utils";/** Wrap a group of tooltips to share one open delay. A single Tooltip works without it. */export const TooltipProvider = TooltipPrimitive.Provider;export interface TooltipProps extends Omit<ComponentProps<typeof TooltipPrimitive.Content>, "content" | "children"> { /** The tooltip text. Keep it short; it is a label, not a place for essential information. */ content: ReactNode; /** The element that triggers it. It must accept a ref and be focusable (a button or link). */ children: ReactNode; /** Milliseconds before it appears on hover or focus. Defaults to 300. */ delayDuration?: number;}/** * A short label shown on hover and keyboard focus, and dismissed with Escape. It describes the * trigger (via aria-describedby) and never replaces its accessible name: an icon-only button * still needs its own aria-label. Tooltips do not appear on touch, so never put anything * essential in one. */export function Tooltip({ content, children, delayDuration = 300, className, sideOffset = 6, collisionPadding = 8, ...props}: TooltipProps) { return ( <TooltipPrimitive.Provider delayDuration={delayDuration}> <TooltipPrimitive.Root> <TooltipPrimitive.Trigger asChild>{children}</TooltipPrimitive.Trigger> <TooltipPrimitive.Portal> <TooltipPrimitive.Content sideOffset={sideOffset} collisionPadding={collisionPadding} className={cn( "z-[var(--z-tooltip)] max-w-xs rounded-md bg-foreground px-2.5 py-1.5 text-xs font-medium text-background transition-opacity duration-[var(--duration-fast)] starting:opacity-0", className, )} {...props} > {content} </TooltipPrimitive.Content> </TooltipPrimitive.Portal> </TooltipPrimitive.Root> </TooltipPrimitive.Provider> );}