← Components

Containment · Material 3: Tooltips (plain)

Tooltip

A short label on hover or focus, on an inverse surface so it always reads. Slides between neighbouring triggers.

Examples and variants

Toolbar

Hover one tool, then move across: the label slides.

Example
function TooltipToolbar() {
  const tools = [
    { label: "Cut", icon: <ScissorsIcon /> },
    { label: "Copy", icon: <CopyIcon /> },
    { label: "Paste", icon: <ClipboardIcon /> },
  ]
  return (
    <TooltipProvider>
      <div className="flex gap-2">
        {tools.map((tool) => (
          <Tooltip key={tool.label}>
            <TooltipTrigger render={<Button size="icon" variant="secondary" aria-label={tool.label} />}>{tool.icon}</TooltipTrigger>
            <TooltipContent>{tool.label}</TooltipContent>
          </Tooltip>
        ))}
      </div>
    </TooltipProvider>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/tooltip.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install @base-ui/react cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/tooltip.tsxtooltip
      components/ui/tooltip.tsx
      "use client"
      
      import * as React from "react"
      import { Tooltip as TooltipPrimitive } from "@base-ui/react/tooltip"
      import { cn } from "cn"
      import { GooLayer } from "@/components/motion/goo-layer"
      import { tokenMs } from "@/lib/motion/token-ms"
      import { copyMotion, motionLevel } from "@/lib/overlay"
      
      const place = { current: null as { x: number; y: number; width: number } | null }
      
      function TooltipProvider({ children, ...props }: TooltipPrimitive.Provider.Props) {
        const delay = React.useSyncExternalStore(
          () => () => {},
          () => tokenMs("--duration-medium"),
          () => 0
        )
        const closeDelay = React.useSyncExternalStore(
          () => () => {},
          () => tokenMs("--duration-quick"),
          () => 0
        )
        return (
          <TooltipPrimitive.Provider {...props} delay={delay} closeDelay={closeDelay}>
            {children}
          </TooltipPrimitive.Provider>
        )
      }
      
      function Tooltip(props: TooltipPrimitive.Root.Props) {
        return <TooltipPrimitive.Root {...props} />
      }
      
      function TooltipTrigger(props: TooltipPrimitive.Trigger.Props) {
        return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} />
      }
      
      function TooltipContent({ className, children, ...props }: TooltipPrimitive.Popup.Props) {
        const [drop, setDrop] = React.useState(false)
        const clean = React.useRef<(() => void) | null>(null)
      
        const setPopup = React.useCallback((node: HTMLDivElement | null) => {
          clean.current?.()
          clean.current = null
          if (!node) return
          copyMotion(node)
          let timer = 0
          let frame = 0
          let tries = 0
          let open = false
          const play = () => {
            const showing = node.hasAttribute("data-open") || node.hasAttribute("data-starting-style")
            if (!showing) {
              open = false
              tries = 0
              setDrop(false)
              return
            }
            if (open) return
            const box = node.getBoundingClientRect()
            if (box.width === 0) {
              if (tries++ < 12) frame = requestAnimationFrame(play)
              return
            }
            open = true
            const level = motionLevel()
            const previous = place.current
            let sliding = false
            if (previous && level === "liquid") {
              const dx = previous.x - box.x
              const dy = previous.y - box.y
              if (Math.hypot(dx, dy) > 1) {
                sliding = true
                node.animate(
                  [
                    { transform: `translate(${dx}px, ${dy}px)`, width: `${previous.width}px` },
                    { transform: "none", width: `${box.width}px` },
                  ],
                  {
                    duration: tokenMs("--duration-fast", node),
                    easing: getComputedStyle(node).getPropertyValue("--ease-out").trim() || "ease",
                  }
                )
              }
            }
            setDrop(!sliding && level === "liquid")
            place.current = { x: box.x, y: box.y, width: box.width }
            window.clearTimeout(timer)
            if (!sliding && level === "liquid") {
              timer = window.setTimeout(() => setDrop(false), tokenMs("--goo-duration", node))
            }
          }
          play()
          const observer = new MutationObserver(play)
          observer.observe(node, { attributes: true, attributeFilter: ["data-open", "data-starting-style"] })
          clean.current = () => {
            observer.disconnect()
            window.clearTimeout(timer)
            cancelAnimationFrame(frame)
          }
        }, [])
      
        return (
          <TooltipPrimitive.Portal>
            <TooltipPrimitive.Positioner className="z-tooltip" sideOffset={6}>
              <TooltipPrimitive.Popup
                ref={setPopup}
                data-slot="tooltip-content"
                data-drop={drop ? "" : undefined}
                className={cn(
                  "tooltip-surface overlay-grow max-w-(--layout-tooltip-max) rounded-(--radius-pill) px-2 py-1 text-xs",
                  className
                )}
                {...props}
              >
                {drop ? (
                  <span className="tooltip-drop pointer-events-none absolute inset-0">
                    <GooLayer width="100%" height="100%" viewBox="0 0 100 40" className="overflow-visible">
                      <circle className="tooltip-bud" cx="50" cy="32" r="8" fill="var(--tooltip-container)" />
                      <rect className="tooltip-pill" x="6" y="4" width="88" height="32" rx="16" fill="var(--tooltip-container)" />
                    </GooLayer>
                  </span>
                ) : null}
                <span className="tooltip-label">{children}</span>
              </TooltipPrimitive.Popup>
            </TooltipPrimitive.Positioner>
          </TooltipPrimitive.Portal>
        )
      }
      
      export { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent }
      

Imports

What the examples on this page import:

Imports
import { CopyIcon, ScissorsIcon, ClipboardIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"

Anatomy

  1. 1Container · --tooltip-container (inverse), --tooltip-corner, --tooltip-padding-*.
  2. 2Label · --tooltip-label, --tooltip-label-size.

Props

PropTypeDefaultDescription
TooltipProviderwrapper—Shares delay so neighbours slide instead of reopening.
TooltipContent side"top" | "bottom" | "left" | "right""top"Placement.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--tooltip-containervar(--surface-inverse)oklch(0.22 0.01 260)oklch(0.95 0 0)
--tooltip-labelvar(--on-surface-inverse)oklch(0.98 0.004 95)oklch(0.14 0 0)
--tooltip-cornervar(--shape-full)999pxsame
--tooltip-padding-xvar(--space-2)8pxsame
--tooltip-padding-yvar(--space-1)4pxsame
--tooltip-label-sizevar(--type-caption-size)11pxsame
--tooltip-max-widthvar(--layout-tooltip-max)240pxsame
--tooltip-offsetvar(--space-2)8pxsame

Motion

TriggerWhat happensTokens
Open (after delay)A drop pulls out of the trigger and rounds into the label.--duration-medium, --goo-duration
Move to a neighbourSlides and resizes to the next trigger.--duration-fast

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Shows on keyboard focus too.
  • Never put interactive content inside.
  • The trigger still needs its own aria-label.

Usage

Do

  • Name icon buttons; show shortcuts.

Don’t

  • Don't hide essential information in a tooltip.

Code

components/ui/tooltip.tsx
"use client"

import * as React from "react"
import { Tooltip as TooltipPrimitive } from "@base-ui/react/tooltip"
import { cn } from "cn"
import { GooLayer } from "@/components/motion/goo-layer"
import { tokenMs } from "@/lib/motion/token-ms"
import { copyMotion, motionLevel } from "@/lib/overlay"

const place = { current: null as { x: number; y: number; width: number } | null }

function TooltipProvider({ children, ...props }: TooltipPrimitive.Provider.Props) {
  const delay = React.useSyncExternalStore(
    () => () => {},
    () => tokenMs("--duration-medium"),
    () => 0
  )
  const closeDelay = React.useSyncExternalStore(
    () => () => {},
    () => tokenMs("--duration-quick"),
    () => 0
  )
  return (
    <TooltipPrimitive.Provider {...props} delay={delay} closeDelay={closeDelay}>
      {children}
    </TooltipPrimitive.Provider>
  )
}

function Tooltip(props: TooltipPrimitive.Root.Props) {
  return <TooltipPrimitive.Root {...props} />
}

function TooltipTrigger(props: TooltipPrimitive.Trigger.Props) {
  return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} />
}

function TooltipContent({ className, children, ...props }: TooltipPrimitive.Popup.Props) {
  const [drop, setDrop] = React.useState(false)
  const clean = React.useRef<(() => void) | null>(null)

  const setPopup = React.useCallback((node: HTMLDivElement | null) => {
    clean.current?.()
    clean.current = null
    if (!node) return
    copyMotion(node)
    let timer = 0
    let frame = 0
    let tries = 0
    let open = false
    const play = () => {
      const showing = node.hasAttribute("data-open") || node.hasAttribute("data-starting-style")
      if (!showing) {
        open = false
        tries = 0
        setDrop(false)
        return
      }
      if (open) return
      const box = node.getBoundingClientRect()
      if (box.width === 0) {
        if (tries++ < 12) frame = requestAnimationFrame(play)
        return
      }
      open = true
      const level = motionLevel()
      const previous = place.current
      let sliding = false
      if (previous && level === "liquid") {
        const dx = previous.x - box.x
        const dy = previous.y - box.y
        if (Math.hypot(dx, dy) > 1) {
          sliding = true
          node.animate(
            [
              { transform: `translate(${dx}px, ${dy}px)`, width: `${previous.width}px` },
              { transform: "none", width: `${box.width}px` },
            ],
            {
              duration: tokenMs("--duration-fast", node),
              easing: getComputedStyle(node).getPropertyValue("--ease-out").trim() || "ease",
            }
          )
        }
      }
      setDrop(!sliding && level === "liquid")
      place.current = { x: box.x, y: box.y, width: box.width }
      window.clearTimeout(timer)
      if (!sliding && level === "liquid") {
        timer = window.setTimeout(() => setDrop(false), tokenMs("--goo-duration", node))
      }
    }
    play()
    const observer = new MutationObserver(play)
    observer.observe(node, { attributes: true, attributeFilter: ["data-open", "data-starting-style"] })
    clean.current = () => {
      observer.disconnect()
      window.clearTimeout(timer)
      cancelAnimationFrame(frame)
    }
  }, [])

  return (
    <TooltipPrimitive.Portal>
      <TooltipPrimitive.Positioner className="z-tooltip" sideOffset={6}>
        <TooltipPrimitive.Popup
          ref={setPopup}
          data-slot="tooltip-content"
          data-drop={drop ? "" : undefined}
          className={cn(
            "tooltip-surface overlay-grow max-w-(--layout-tooltip-max) rounded-(--radius-pill) px-2 py-1 text-xs",
            className
          )}
          {...props}
        >
          {drop ? (
            <span className="tooltip-drop pointer-events-none absolute inset-0">
              <GooLayer width="100%" height="100%" viewBox="0 0 100 40" className="overflow-visible">
                <circle className="tooltip-bud" cx="50" cy="32" r="8" fill="var(--tooltip-container)" />
                <rect className="tooltip-pill" x="6" y="4" width="88" height="32" rx="16" fill="var(--tooltip-container)" />
              </GooLayer>
            </span>
          ) : null}
          <span className="tooltip-label">{children}</span>
        </TooltipPrimitive.Popup>
      </TooltipPrimitive.Positioner>
    </TooltipPrimitive.Portal>
  )
}

export { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent }