Menu

Components

Tooltip

A short label that shows when the pointer rests on an element or when it is focused with the keyboard.

Introduction

A tooltip names or explains an element without taking space on the page: the label of an icon button, the shortcut of a command, the meaning of a term. It appears when the user rests the pointer on the element or reaches it with the keyboard, and goes away when they leave.

  • It shows on hover, after a short delay, and on keyboard focus. Escape closes it. Once one is showing, the next one appears at once as the pointer moves along a row of buttons.
  • The element it belongs to is described by it for screen readers, and it opens on the side you choose, flipping when there is no room.
  • An optional arrow, a dark and a light look, a place for a keyboard shortcut, and a description form with a title and a longer text (isDescription).

A tooltip is only a hint. It never shows on touch screens, so it must not hold anything the user needs, or anything they can press. For that, use Popover.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx

# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx

# npm
npm install react-aria-components tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/tooltip-styles.tsShow code
import { tv } from "tailwind-variants";

export const tooltipStyles = tv({
  slots: {
    // `popover` is the class that gives it a small entrance animation (see
    // globals.css).
    tooltip:
      "popover z-50 max-w-xs rounded-md px-2 py-1 text-xs leading-snug font-medium shadow-md",
    // The little triangle that points at the trigger. It is drawn pointing
    // down (for a tooltip above its trigger) and turned for the other sides.
    arrow: [
      "block",
      "group-data-[placement=bottom]:rotate-180",
      "group-data-[placement=left]:-rotate-90",
      "group-data-[placement=right]:rotate-90",
    ],
    // For a tooltip that explains (`isDescription`): a bold line, then a
    // quieter one under it.
    title: "text-xs leading-snug font-semibold",
    description: "text-xs leading-snug font-normal opacity-80",
    shortcut:
      "ml-2 inline-flex items-center gap-0.5 rounded-[4px] bg-white/15 px-1 font-mono text-[10px] leading-4",
  },
  variants: {
    // The default is the inverse of the page (dark on light, light on dark), so
    // it stands out from any background.
    variant: {
      default: { tooltip: "bg-fg text-bg", arrow: "fill-fg" },
      light: {
        tooltip: "border bg-card text-card-fg",
        arrow: "fill-card stroke-border",
        shortcut: "bg-muted text-muted-fg",
      },
    },
    // A short label, or a small explanation: wider, left-aligned, roomier, and
    // its text may run over several lines.
    isDescription: {
      true: {
        tooltip:
          "flex w-max max-w-64 flex-col gap-0.5 px-3 py-2 text-left font-normal",
      },
      false: {},
    },
  },
  defaultVariants: { variant: "default", isDescription: false },
});

2. Add the Tooltip file

src/components/ui/tooltip/tooltip.tsxShow code
"use client";

import type { ReactNode } from "react";
import {
  Tooltip as AriaTooltip,
  type TooltipProps as AriaTooltipProps,
  Focusable,
  OverlayArrow,
  TooltipTrigger,
  type TooltipTriggerComponentProps,
} from "react-aria-components";
import { cn } from "@/lib/cn";
import { tooltipStyles } from "@/types/tooltip-styles";

export interface TooltipProps
  extends Omit<TooltipTriggerComponentProps, "children"> {
  children: ReactNode;
}

/**
 * A short label that shows when the pointer rests on an element, or when the
 * element is focused with the keyboard. Put the trigger and a `TooltipContent`
 * inside it. Built on react-aria's TooltipTrigger: the trigger is described by
 * the tooltip for screen readers, Escape closes it, and a tooltip shown for one
 * element makes the next one appear at once.
 *
 * Set `isDescription` on the content for a tooltip that explains: a title and
 * a description, in a wider panel.
 *
 * A tooltip only adds a hint. It must not hold anything the user needs, or
 * anything they can press, because touch screens never show it. Give an icon
 * button its own `aria-label`; the tooltip is the same label made visible.
 *
 * @example
 * <Tooltip>
 *   <Button isIcon aria-label="Save"><SaveIcon /></Button>
 *   <TooltipContent>Save</TooltipContent>
 * </Tooltip>
 */
export function Tooltip({
  delay = 300,
  closeDelay = 0,
  ...props
}: TooltipProps) {
  return <TooltipTrigger delay={delay} closeDelay={closeDelay} {...props} />;
}

export interface TooltipContentProps
  extends Omit<AriaTooltipProps, "children" | "className" | "style"> {
  /** Point a small arrow at the trigger. */
  showArrow?: boolean;
  /** The inverse of the page (default), or a light card with a border. */
  variant?: "default" | "light";
  /**
   * Turns the tooltip from a short label into a description: wider, roomier,
   * left-aligned, with room for a `TooltipTitle` and a `TooltipDescription`.
   */
  isDescription?: boolean;
  className?: string;
  children?: ReactNode;
}

/**
 * The label of a `Tooltip`. It opens on the side of the trigger given by
 * `placement`, and flips to the other side when there is not enough room.
 */
export function TooltipContent({
  showArrow = false,
  variant,
  isDescription = false,
  placement = "top",
  offset,
  className,
  children,
  ...props
}: TooltipContentProps) {
  const styles = tooltipStyles({ variant, isDescription });

  return (
    <AriaTooltip
      {...props}
      placement={placement}
      // Leave room for the arrow.
      offset={offset ?? (showArrow ? 10 : 6)}
      className={cn(styles.tooltip(), className)}
    >
      {showArrow ? (
        <OverlayArrow className="group">
          <svg
            aria-hidden="true"
            width={10}
            height={10}
            viewBox="0 0 12 12"
            className={styles.arrow()}
          >
            <path d="M0 0 L6 6 L12 0" />
          </svg>
        </OverlayArrow>
      ) : null}
      {children}
    </AriaTooltip>
  );
}

/** The bold first line of a tooltip that has `isDescription`. */
export function TooltipTitle({
  className,
  children,
}: {
  className?: string;
  children?: ReactNode;
}) {
  return (
    <span className={cn(tooltipStyles().title(), className)}>{children}</span>
  );
}

/** The quieter text under the title of a tooltip that has `isDescription`. */
export function TooltipDescription({
  className,
  children,
}: {
  className?: string;
  children?: ReactNode;
}) {
  return (
    <span className={cn(tooltipStyles().description(), className)}>
      {children}
    </span>
  );
}

/** A key or a key combination, for a tooltip such as "Save ⌘S". */
export function TooltipShortcut({
  variant,
  children,
}: {
  variant?: "default" | "light";
  children?: ReactNode;
}) {
  return (
    <kbd className={tooltipStyles({ variant }).shortcut()}>{children}</kbd>
  );
}

/**
 * Makes an element that is not a button, such as text or an icon, able to have
 * a tooltip. The element inside must be able to take focus (give it a
 * `tabIndex`), so that keyboard users can reach the tooltip too.
 *
 * @example
 * <TooltipTarget><span tabIndex={0}>Beta</span></TooltipTarget>
 */
export const TooltipTarget = Focusable;

3. Add the entrance animation

In src/app/globals.css copy the popover-in keyframes and the .popover[data-entering] rule (with its prefers-reduced-motion override). Without them the tooltip still works, it just appears without the small fade.

4. Use it

import {
  Tooltip,
  TooltipContent,
  TooltipDescription,
  TooltipShortcut,
  TooltipTarget,
  TooltipTitle,
} from "@/components/ui/tooltip/tooltip";

Anatomy

A trigger and its label, held together by Tooltip.

<Tooltip>
  <Button isIcon aria-label="Save"><SaveIcon /></Button>

  <TooltipContent placement="top" showArrow>
    Save <TooltipShortcut>⌘S</TooltipShortcut>
  </TooltipContent>
</Tooltip>
PartRendered asNotes
Tooltipno elementConnects the trigger to the label and decides when it shows (delay, closeDelay).
TriggerButtonAny Button, or any element wrapped in TooltipTarget. It is described by the tooltip for screen readers.
TooltipContentdiv, role=tooltipThe small label. It sits on a side of the trigger and flips when there is no room.
ArrowsvgAn optional triangle that points at the trigger.
TooltipTitle / TooltipDescriptionspanThe two lines of a tooltip that has isDescription: a bold one, and a quieter one under it.
TooltipShortcutkbdA key or a key combination inside the label.

The trigger must be a Button (or another react-aria component). For anything else, wrap it in TooltipTarget, and make sure the element can take focus.

Examples

Rest the pointer on a button, or Tab to it, to see its tooltip.

Usage

Placement

Which side of the trigger it opens on. If there is no room, it flips to the opposite side.

Alignment

Add start or end to line the tooltip up with an edge of the trigger instead of its middle.

Arrow

showArrow adds a small triangle that points at the trigger, on whichever side the tooltip opens.

Variants

The default is the inverse of the page, dark on a light page and light on a dark one, so it stands out from any background. The light variant is a card with a border.

Description

Set isDescription to turn a short label into an explanation: the tooltip gets wider (up to 16rem), roomier and left-aligned, and its text can run over several lines. Fill it with a TooltipTitle and a TooltipDescription. It works with both variants and with the arrow.

Icon buttons

A button with only an icon needs a name for everyone, so give it an aria-label. The tooltip shows that same label to people who can see it, and the button already has it for screen readers.

Keyboard shortcut

TooltipShortcut sets a key or a combination apart inside the label.

Other elements

Wrap text, an icon or a badge in TooltipTarget to give it a tooltip. The element needs a tabIndex, so a keyboard user can focus it and see the tooltip too.

An operation is idempotent if repeating it changes nothing.

Delay

How long the pointer must rest before the tooltip shows. The default is 300 ms. Use 0 for a tooltip that should answer at once, and more for a busy interface. closeDelay does the same for hiding.

Disabled button

A disabled button gets no pointer or focus events, so it cannot open a tooltip. Wrap it in a focusable element, and explain why it is disabled.

Controlled

Keep whether it is shown in your own state with isOpen and onOpenChange, for example to show a hint after an action.

isOpen: false

Component API

Tooltip and TooltipContent take the props below. TooltipContent also accepts the rest of what the react-aria Tooltip does.

Tooltip

PropTypeDefault
delaynumber (ms)300
closeDelaynumber (ms)0
isOpenboolean-
defaultOpenbooleanfalse
onOpenChange(isOpen: boolean) => void-
isDisabledbooleanfalse
trigger"focus"hover and focus

TooltipContent

PropTypeDefault
placement"top" | "bottom" | "left" | "right" | "top start" | "top end" | …"top"
offsetnumber6 (10 with an arrow)
showArrowbooleanfalse
variant"default" | "light""default"
isDescriptionbooleanfalse
shouldFlipbooleantrue
childrenReactNode-

TooltipTitle and TooltipDescription take only a className and their children. TooltipShortcut takes a variant that matches the tooltip it is in, and its children. TooltipTarget is the react-aria Focusable.