Skip to content

Tooltip

Names or briefly explains a control when the user hovers over it or focuses it.

Installation

pnpm dlx shadcn@latest add @silkware/tooltip

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/tooltip.tsx:

components/ui/tooltip.tsx
"use client"
import * as React from "react"import { cn } from "cn"import {  Focusable,  OverlayArrow,  Tooltip as TooltipPrimitive,  TooltipTrigger as TooltipTriggerPrimitive,} from "@animate-ui/react/primitives/react-aria"
function TooltipTrigger({  delay = 0,  children,  ...props}: React.ComponentProps<typeof TooltipTriggerPrimitive>) {  const [trigger, tooltip] = React.Children.toArray(children)
  return (    <TooltipTriggerPrimitive      data-slot="tooltip-trigger"      delay={delay}      {...props}    >      <Focusable>        {trigger as React.ComponentProps<typeof Focusable>["children"]}      </Focusable>      {tooltip}    </TooltipTriggerPrimitive>  )}
function Tooltip({  className,  placement = "top",  offset = 4,  crossOffset = 0,  children,  ...props}: Omit<  React.ComponentProps<typeof TooltipPrimitive>,  "children" | "className"> & {  className?: string  children?: React.ReactNode}) {  return (    <TooltipPrimitive      data-slot="tooltip-content"      placement={placement}      offset={offset}      crossOffset={crossOffset}      className={cn(        "z-50 inline-flex w-fit max-w-xs origin-(--trigger-anchor-point) items-center gap-1.5 rounded-md bg-foreground px-3 py-1.5 text-xs text-background has-data-[slot=kbd]:pr-1.5 **:data-[slot=kbd]:relative **:data-[slot=kbd]:isolate **:data-[slot=kbd]:z-50 **:data-[slot=kbd]:rounded-sm",        className      )}      {...props}    >      {children}      <OverlayArrow        className="z-50 size-2.5 translate-y-[calc(-50%-2px)] rotate-45 rounded-[2px] bg-foreground fill-foreground"        style={({ placement, defaultStyle }) => ({          ...defaultStyle,          rotate: "0deg",          translate: "0 0",          transform:            placement === "bottom"              ? "translate(-50%, calc(50% + 2px)) rotate(45deg)"              : placement === "top"                ? "translate(-50%, calc(-50% - 2px)) rotate(45deg)"                : placement === "left"                  ? "translate(calc(-50% - 2px), -50%) rotate(45deg)"                  : "translate(calc(50% + 2px), -50%) rotate(45deg)",        })}      />    </TooltipPrimitive>  )}
export { Tooltip, TooltipTrigger }

Usage

import { Button } from "@/components/ui/button"import { Tooltip, TooltipTrigger } from "@/components/ui/tooltip"
<TooltipTrigger>  <Button variant="outline" size="icon" aria-label="Save order">    <SaveIcon />  </Button>  <Tooltip>Save order</Tooltip></TooltipTrigger>

TooltipTrigger reads its first child as the trigger and its second as the tooltip, so pass exactly those two. It wraps the trigger in a Focusable, which makes a trigger without its own focus handling, such as a span, work if it has a role and can take focus. The tooltip does not open on touch screens, and a disabled button cannot take focus, so neither can show it.

Examples

Placement

placement on Tooltip sets the side: top, right, bottom or left. The default is top, and the tooltip flips if it would leave the screen.

Delay

This component opens the tooltip at once, delay of 0. Set delay on TooltipTrigger to wait before opening, and closeDelay to wait before closing. Once one tooltip is open, the next in a row opens without the delay.

With a keyboard shortcut

Put a Kbd inside the tooltip to show the shortcut after the name. The tooltip restyles it for its dark surface.

Accessibility

KeyAction
TabFocuses the trigger, which shows the tooltip at once.
EscHides the tooltip.

React Aria provides:

  • A tooltip that opens on hover and on keyboard focus, and closes when the pointer or focus leaves.
  • The tooltip role, and an aria-describedby on the trigger that points to it, so screen readers read the tooltip as the description of the control.
  • A shared warm-up and cool-down timer, so that moving across a row of buttons does not restart the delay each time.
  • Positioning that flips the tooltip when there is no room.

You provide:

  • A name for the control that does not depend on the tooltip. An icon button needs an aria-label: the tooltip is a description, not the label.
  • Tooltip text that is short and static. It cannot hold links or buttons, because focus stays on the trigger and keyboard users could never reach them.
  • A way to reach every piece of information without hovering. Touch screens never show tooltips.

Guidelines

When to use

  • To name an icon button, and to show its shortcut.
  • To add a few words of description to a control whose label is clear but short.

When not to use

  • For information the user needs to complete a task, such as a rule for a field. Put it on the page, under the field.
  • For a preview of an item or a person. Use a Hover Card.
  • For content with links, buttons or fields. Use a Popover.
  • To explain why a control is disabled. A disabled button takes no focus and shows nothing on touch. Say it in text next to the control.
  • On a control that has a clear text label. A tooltip that repeats the label adds noise.

Content

  • Use a noun or a verb phrase in sentence case with no full stop: "Download price list", "Save order".
  • One short line, at most. The tooltip is max-w-xs wide and wraps beyond that.
  • Name the action the same way as the control's aria-label.

Essential or extra

Do

Name an icon button with a short phrase, and give the button the same aria-label.

Don’t

Don’t hide a warning in a tooltip. Anyone on a touch screen never sees it. Confirm in an alert dialog and say it in the dialog.

API reference

TooltipTrigger

Wraps the trigger and the tooltip through the React Aria TooltipTrigger. It accepts every TooltipTrigger prop.

PropTypeDefaultDescription
childrenReactNodeNoneThe trigger, then the Tooltip.
delay?number0Milliseconds before the tooltip opens. React Aria's own default is 1500; this component sets 0.
closeDelay?number500Milliseconds before the tooltip closes.
trigger?"hover" | "focus""hover"Opens the tooltip on hover and focus, or on focus only.
isDisabled?booleanNoneStops the tooltip from opening, independent of the trigger.
isOpen?booleanNoneWhether the tooltip is open. Makes it controlled.
defaultOpen?booleanNoneWhether the tooltip is open on first render, when uncontrolled.
onOpenChange?(isOpen: boolean) => voidNoneCalled when the tooltip opens or closes.

Tooltip

Renders the React Aria Tooltip with an arrow. It accepts every Tooltip prop and the props below.

PropTypeDefaultDescription
placement?Placement"top"Where the tooltip opens relative to the trigger.
offset?number4The distance in pixels from the trigger along the main axis.
crossOffset?number0The shift in pixels along the cross axis.
className?stringNoneClasses merged onto the tooltip.
children?ReactNodeNoneThe tooltip text. It may hold a Kbd.