Tooltip

A bubble anchored to its child, revealed on hover and on focus. Filling it is what makes it appear, so a tooltip with nothing to say never shows up.

API

Props

proptypedefault
as?ElementType"div"the anchor element
children?ReactNodewhat the tooltip is anchored to
content?ReactNodethe content of the bubble; null or undefined keeps it hidden
position?TooltipPosition"down"up, down, left or right
label?stringname of the bubble while it carries no content
id?stringid of the bubble, to reference it from aria-describedby

Examples

Usage

tsx
<Tooltip content="copy to clipboard" position="down">
  <Button aria-label="copy">
    <Button.Icon icon={<MdContentCopy />} />
  </Button>
</Tooltip>
previewhover or tab to the buttons
copy to clipboard
save
delete for good

Details

Content is the switch

There is no open prop. A tooltip with a content reveals itself on hover and on focus-within; one whose content is null or undefined never does. So a conditional message needs no conditional rendering — pass the value and the tooltip follows it.

tsx
<Tooltip content={error}>
  <Field value={value} />
</Tooltip>

The last non-empty content is kept while the bubble fades out, so a tooltip that empties does not go blank mid-transition. That is the reason the component holds a ref at all.

Position

Four fixed positions, placed with absolute offsets from the anchor. There is no flipping or collision detection: it is CSS with no measuring, so it costs nothing, and near a viewport edge choosing the right side is your call rather than the component's.

The anchor is inline-block and sized to its content, so it wraps a control without changing the layout around it.

What it is announced as

The bubble is a role="tooltip", which on its own is not read out: a tooltip is discovered through the element it describes. Give it an id and reference it with aria-describedby when the text adds information the control does not already carry.

tsx
<Tooltip id="save-hint" content="saves without closing">
  <Button aria-describedby="save-hint">
    <Button.Label label="save" />
  </Button>
</Tooltip>

Do not use it as the only name of a control: a Button with just an icon needs its own aria-label, and the tooltip repeats that name for sighted users rather than replacing it. Note also that a tooltip appears on hover and on keyboard focus, but not on touch, so it can never be the only place where something is explained.