Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 2.74 KB

tooltip.md

File metadata and controls

55 lines (42 loc) · 2.74 KB

Tooltip

Names an icon-only control and shows its shortcut.

Example

import { Button, Tooltip } from "@usememos/ui";
import { Archive } from "lucide-react";

<Tooltip.Provider>
  <Tooltip.Root>
    <Tooltip.Trigger render={<Button variant="ghost" icon={<Archive />} aria-label="Archive" />} />
    <Tooltip.Popup shortcut="⌘ E">Archive</Tooltip.Popup>
  </Tooltip.Root>
</Tooltip.Provider>

Parts

Part Base UI part Renders
Tooltip.Provider Tooltip.Provider Nothing. Shares the hover delay, so the next tooltip in a toolbar opens at once
Tooltip.Root Tooltip.Root Nothing. Groups a trigger and its popup
Tooltip.Trigger Tooltip.Trigger The control, usually a Button through render
Tooltip.Popup Tooltip.Portal, Positioner, Popup The label, a <div> portaled to the body

Props

interface Tooltip.TriggerProps extends BaseTooltip.Trigger.Props {
  delay?: number; // hover time before opening, ms; default 400
}
interface Tooltip.PopupProps extends BaseTooltip.Popup.Props {
  shortcut?: React.ReactNode; // after the label, at 65% opacity
  side?: Side; align?: Align; // Positioner props; default side "top"
  container?: HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null; // portal target
}

Base UI's props pass through: open, defaultOpen, onOpenChange, disabled on the root; closeOnClick on the trigger; delay, closeDelay, timeout on the provider.

Rules

  • Give every icon-only Button and Toggle a tooltip with the same text as its aria-label. Two or three words.
  • Dialog, Drawer and Toast include the Tooltip on their built-in Close; it uses closeLabel and the popup's container. Combobox includes one on its chevron, using triggerLabel. These tooltips stay inside a shadow root when their trigger is mounted there.
  • Never put essential information only in a tooltip: it doesn't open on touch screens.
  • For text that explains something, use a Popover instead.
  • Wrap a toolbar, or the whole app, in one Tooltip.Provider.
  • On a trigger that also opens a Menu, Popover or Dialog, stack the triggers: <Tooltip.Trigger render={<Menu.Trigger render={<Button … />} />} />. See the recipe.

Style

  • primary background, on-primary text, label-sm, radius-md, space-1 by space-2 padding, at most tooltip-width wide.
  • Waits 400ms on hover before opening; keyboard focus has no opening delay. Opening and closing use the shared Motion fade and space-1 offset over duration-fade. Both transitions are disabled under reduced motion. Moving between tooltips in a group skips the delay and replayed entrance.