Tooltip

A short label shown on hover or focus.

Keyboard focus opens a tooltip at once; hovering waits for its delay. A tooltip can’t hold anything interactive; use a popover for that.

Preview
basic.tsx
import { Archive } from "lucide-react"

import { Button } from "~/components/ui/button"
import { Tooltip, TooltipContent, TooltipTrigger } from "~/components/ui/tooltip"

export default function Example() {
  return (
    <Tooltip>
      <TooltipTrigger render={<Button variant="outline" size="icon" aria-label="Archive" />}>
        <Archive />
      </TooltipTrigger>
      <TooltipContent>Archive project</TooltipContent>
    </Tooltip>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byWorkspaceblock
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/tooltip.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/tooltip.tsx
import {
  Tooltip,
  TooltipContent as TooltipContentPrimitive,
  TooltipTrigger,
} from "@ui-sdk/react/tooltip"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/** The tooltip's text, inverted against the page. Keep it short; it can't hold anything interactive. */
function TooltipContent({
  className,
  sideOffset = 4,
  ...props
}: React.ComponentProps<typeof TooltipContentPrimitive>) {
  return (
    <TooltipContentPrimitive
      sideOffset={sideOffset}
      className={cnState(
        [
          textVariants({ size: "xs" }),
          "inline-flex w-fit max-w-xs items-center gap-xs rounded-xl bg-foreground px-md py-xs text-background",
          "origin-(--transform-origin) transition-[opacity,scale] duration-100 ease-out data-starting-style:scale-95 data-starting-style:opacity-0 data-ending-style:scale-95 data-ending-style:opacity-0 motion-reduce:transition-none",
        ].join(" "),
        className,
      )}
      {...props}
    />
  )
}

export { Tooltip, TooltipContent, TooltipTrigger }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Tooltip

A short label or hint that appears when the pointer rests on its trigger or keyboard focus reaches it.

closeDelay

Milliseconds before the tooltip closes once the pointer leaves the trigger and the tooltip.

Type number · Default 0

closeOnPress

Whether pressing the trigger closes the tooltip. Set it to false to keep it open, like for a copy button whose tooltip turns into "Copied".

Type boolean · Default true

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

controller

Opens, closes and inspects the tooltip from anywhere, and connects triggers outside it. Create one with createTooltipController().

Type PopupController<any, TooltipOpenChangeReason, {}, string, TooltipPayloadChangeReason>

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultOpen

Whether the tooltip starts open, when uncontrolled.

Type boolean · Default false

delay

Milliseconds the pointer rests on the trigger before the tooltip opens. While another tooltip is open, or was a moment ago, the next one opens at once, without a provider.

Type number · Default 600

disabled

Stops the tooltip from opening. The trigger keeps working.

Type boolean · Default false

onOpenChange

Runs when the tooltip wants to open or close. details.reason says why, and details.cancel() stops it.

Type (open: boolean, details: ChangeDetails<TooltipOpenChangeReason>) => void

onOpenChangeComplete

Runs once the tooltip has finished opening or closing, after any animation.

Type (open: boolean) => void

onPayloadChange

Runs when the payload the content shows changes: a trigger with another payload opens the tooltip, or takes the open tooltip over, which fires no onOpenChange, and so does a controller's open({ payload }) on the open tooltip. details.cancel() keeps the previous payload.

Type (payload: unknown, details: ChangeDetails<TooltipPayloadChangeReason>) => void

open

Whether the tooltip is open. Pass it to control the tooltip.

Type boolean

shortcut

The keyboard shortcut of what the trigger does, like "mod+s", as a hint. A TooltipShortcut shows it, and the trigger gets aria-keyshortcuts. It doesn't bind the keys: the trigger's own shortcut, or registerShortcut, does.

Type string

toggleable

Whether pressing the trigger opens and closes the tooltip, so touch screens can show it too. For info buttons whose only job is the tip. The text is announced when a press opens it.

Type boolean · Default false

type

Whether the tooltip names its trigger ("label") or adds a description to it ("description"). By default it names a trigger that has no name of its own, like an icon button, and describes any other.

Type "label" | "description"

TooltipContent

The tooltip's text, inverted against the page. Keep it short; it can't hold anything interactive.

align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the tooltip lines up with the trigger along that side.

Type "center" | "start" | "end" · Default "center"

alignOffset

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Type number · Default 0

anchor

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type string | Anchor | null

arrowPadding

Space to keep between the arrow and the popup's corners, in pixels.

Type number · Default 6

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: TooltipContentState<unknown>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: TooltipContentState<unknown>) => string)

collisionPadding

Space to keep from the viewport edges, in pixels.

Type number · Default 8

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<TooltipContentState<unknown>>

side

Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the trigger the tooltip opens on.

Type "start" | "end" | "top" | "bottom" | "left" | "right" · Default "top"

sideOffset

Gap between the anchor and the popup, in pixels. Gap between the trigger and the tooltip, in pixels.

Type number · Default 4

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: TooltipContentState<unknown>) => CSSProperties)

TooltipTrigger

The element the tooltip names or describes, usually a button.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: TooltipTriggerState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: TooltipTriggerState) => string)

controller

Connects a trigger outside the tooltip, like one per table row, to the tooltip with this controller.

Type PopupController<any, TooltipOpenChangeReason, {}, string, TooltipPayloadChangeReason>

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

payload

Data for the content to render when this trigger opens the tooltip, like the row it belongs to.

Type unknown

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<TooltipTriggerState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: TooltipTriggerState) => CSSProperties)