Tooltip
A tooltip on Base UI Tooltip.
A short label for a control that a pointer or the keyboard can reach. On a touch screen it never shows, neither on a tap nor on a long press, so it may only repeat what the control already says.
Default
import { ArchiveIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
export default function TooltipDefault() {
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger render={<Button variant="outline" size="icon" aria-label="Archive the project" />}>
<ArchiveIcon />
</TooltipTrigger>
<TooltipContent>Archive the project</TooltipContent>
</Tooltip>
</TooltipProvider>
)
}
With a shortcut
import { BoldIcon, ItalicIcon, UnderlineIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { Kbd } from "@/components/ui/kbd"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
const tools = [
{ label: "Bold", keys: "⌘B", Icon: BoldIcon },
{ label: "Italic", keys: "⌘I", Icon: ItalicIcon },
{ label: "Underline", keys: "⌘U", Icon: UnderlineIcon },
]
export default function TooltipWithAShortcut() {
return (
<TooltipProvider>
<div className="flex items-center gap-1">
{tools.map(({ label, keys, Icon }) => (
<Tooltip key={label}>
<TooltipTrigger render={<Button variant="ghost" size="icon" aria-label={label} />}>
<Icon />
</TooltipTrigger>
<TooltipContent>
{label}
<Kbd>{keys}</Kbd>
</TooltipContent>
</Tooltip>
))}
</div>
</TooltipProvider>
)
}
Sides
import { Button } from "@/components/ui/button"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
const sides = [
{ side: "top", label: "Top" },
{ side: "right", label: "Right" },
{ side: "bottom", label: "Bottom" },
{ side: "left", label: "Left" },
] as const
export default function TooltipSides() {
return (
<TooltipProvider>
<div className="flex flex-wrap items-center justify-center gap-2">
{sides.map(({ side, label }) => (
<Tooltip key={side}>
<TooltipTrigger render={<Button variant="outline" />}>{label}</TooltipTrigger>
<TooltipContent side={side}>Opens on the {side}</TooltipContent>
</Tooltip>
))}
</div>
</TooltipProvider>
)
}
With a delay
import { DownloadIcon, PrinterIcon, Share2Icon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
const actions = [
{ label: "Download the invoice", Icon: DownloadIcon },
{ label: "Print the invoice", Icon: PrinterIcon },
{ label: "Share the invoice", Icon: Share2Icon },
]
export default function TooltipWithADelay() {
return (
<TooltipProvider delay={600}>
<div className="flex items-center gap-1">
{actions.map(({ label, Icon }) => (
<Tooltip key={label}>
<TooltipTrigger render={<Button variant="ghost" size="icon" aria-label={label} />}>
<Icon />
</TooltipTrigger>
<TooltipContent>{label}</TooltipContent>
</Tooltip>
))}
</div>
</TooltipProvider>
)
}
Installation
npx shadcn@latest add @khaeli/tooltipbunx --bun shadcn@latest add @khaeli/tooltipInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Keyboard
Tab- Focusing the trigger with the keyboard shows the tooltip at once, even when
TooltipProvidersets adelay. Moving focus on hides it. Escape- Hides the tooltip. Focus stays on the trigger.
Accessible name
- The tooltip is not tied to its trigger: it has no
role="tooltip"and the trigger gets noaria-describedby. An icon-only trigger needs its ownaria-label, and the tooltip repeats it for people who see the screen. - A shortcut goes in a @khaeli/kbd inside
TooltipContent. The tooltip shows the keys and binds nothing. TooltipProvidersets the wait before a pointer opens a tooltip: none by default. With adelay, once one tooltip is open its neighbours open without waiting.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/tooltip.json