Components / Overlays

Tooltip25

A short hint that appears on hover and keyboard focus. Sticker slaps it on like a tilted label, Comic writes it in a yellow caption box, Arcade shows it on a neon screen.

Tooltip · PlaygroundLive
placement
JSX
<TooltipTrigger>
  <Button aria-label="Copy link">…</Button>
  <Tooltip>Copy link</Tooltip>
</TooltipTrigger>

Usage

TSX
import { Button, Tooltip, TooltipTrigger } from "plonk-ui";

export function CopyButton() {
  return (
    <TooltipTrigger>
      <Button aria-label="Copy link">…</Button>
      <Tooltip>Copy link</Tooltip>
    </TooltipTrigger>
  );
}

TooltipTrigger takes the trigger and the tooltip. It opens after 500ms of hovering, or right away on keyboard focus. Once one tooltip is open, moving to the next one doesn't wait again.

Icon buttons

The classic use: an icon button names itself with aria-label, and the tooltip shows the same name to sighted users.

Tooltip · Icon buttonsLive
TSX
import { Button, Tooltip, TooltipTrigger } from "plonk-ui";

const ICONS = {
  Bold: "M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z",
  Italic: "M10 5h8M6 19h8M14 5l-4 14",
  Link: "M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1",
};

export default function Icons() {
  return (
    <div className="flex gap-2">
      {Object.entries(ICONS).map(([label, d]) => (
        <TooltipTrigger key={label}>
          <Button variant="neutral" size="sm" aria-label={label}>
            <svg
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth={2.5}
              strokeLinecap="round"
              strokeLinejoin="round"
              className="size-4"
            >
              <path d={d} />
            </svg>
          </Button>
          <Tooltip>{label}</Tooltip>
        </TooltipTrigger>
      ))}
    </div>
  );
}

Shortcuts

A tooltip can hold a Kbd to teach a keyboard shortcut.

Tooltip · ShortcutLive
TSX
import { Button, Kbd, Tooltip, TooltipTrigger } from "plonk-ui";

export default function Shortcut() {
  return (
    <TooltipTrigger>
      <Button variant="ghost">Search</Button>
      <Tooltip placement="bottom">
        <span className="flex items-center gap-2">
          Open search <Kbd>⌘</Kbd>
          <Kbd>K</Kbd>
        </span>
      </Tooltip>
    </TooltipTrigger>
  );
}

When not to use it

A tooltip disappears as soon as the pointer leaves, and touch screens have no hover. Keep it for hints people can do without, and never put buttons or links in it. For something to click or read at length, use a Popover.

Personalities

Sticker sticks the tooltip on at an angle with a little bounce, Retro shows it instantly in mono type, Comic prints it in a yellow caption box and Arcade shows it on a dark screen with a neon frame.

Accessibility

  • It opens on keyboard focus as well as hover, and Esc closes it.
  • The trigger is described by the tooltip (aria-describedby), so screen readers read it after the trigger's name.
  • Don't put the only label of a control in a tooltip: give the trigger its own name too.

API

Tooltip

PropTypeDefaultDescription
classNamestring—Extra classes for the bubble.
placement"left" | "right" | "bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right top" | "right bottom" | "end" | "end top" | "end bottom""top"Preferred side. It flips when there's no room.
offsetnumber10Gap to the trigger, in pixels.
crossOffsetnumber0Shift along the trigger's edge, in pixels.
shouldFlipbooleantrueFlip to the other side when it doesn't fit.

TooltipTrigger

PropTypeDefaultDescription
delaynumber500Milliseconds of hovering before it opens. Once one tooltip is open, the next opens right away.
closeDelaynumber200Milliseconds before it closes after the pointer leaves.
trigger"focus" | "hover"hover and focusBy default it opens on hover and on focus. focus opens it on focus only.
isDisabledboolean—Never shows the tooltip.
isOpenboolean—Controlled open state. Use with onOpenChange.
defaultOpenboolean—Open on first render, when uncontrolled.
onOpenChange(isOpen: boolean) => void—Called with true or false when it opens or closes.