This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Tooltip

A popup that appears when an element is hovered or focused, showing a hint for sighted users.

Examples#

Detached triggers#

A tooltip can be controlled by a trigger located either inside or outside the Tooltip component. For simple, one-off interactions, place the TooltipTrigger inside Tooltip, as shown in the example at the top of this page.

However, if defining the tooltip's content next to its trigger is not practical, you can use a detached trigger. This involves placing the TooltipTrigger outside of Tooltip and linking them with the same handle. The root gets the handle as its id, and the trigger gets data-phem-tooltip-for.

Multiple triggers#

A single tooltip can be opened by multiple trigger elements. You can achieve this by using the same handle for several detached triggers, or by placing multiple TooltipTrigger components inside a single Tooltip.

The tooltip can render different content depending on which trigger opened it. Give each trigger a payload: the TooltipViewport shows the payload of the active trigger.

Controlled mode#

You can control the tooltip's open state with the open and triggerId props of Tooltip: a render with new values opens the tooltip next to the trigger with that id, or closes it. The controller keeps the values of the browser in data-phem-tooltip-open-value and data-phem-tooltip-trigger-id-value.

Here, the button is in the root, and opens the tooltip without a request with the phem-tooltip#show action and a triggerId param.

Animating the Tooltip#

You can animate a tooltip as it moves between different trigger elements. This includes animating its position, size, and content.

To animate the tooltip's position, apply CSS transitions to the top and left properties of the TooltipPopup. To animate its size, transition its width and height to var(--popup-width) and var(--popup-height).

To enable content animations, wrap the content in the TooltipViewport part. It gets a data-activation-direction attribute that indicates the new trigger's position relative to the previous one: up to two tokens, left or right and up or down. Match a single token with the ~= attribute selector. Inside the viewport, the content is wrapped in divs with data-current (the incoming content) and data-previous (the outgoing content).

API reference#

Import the components of the family:

use Phem\Ui\Tooltip;
use function Phem\Ui\{TooltipTrigger, TooltipPopup, TooltipArrow, TooltipViewport};

Tooltip#

A short text next to an element, on hover and on the focus of the keyboard: Base UI Tooltip.Root, on the phem-tooltip controller.

The props of the Base UI Positioner are props of the root. Tooltips that open one after the other open at once (data-instant="delay"), as the Base UI Tooltip.Provider does: the controller does it for all the tooltips of the page.

A Toggle or a ToolbarButton in a Tooltip is a trigger of the tooltip, as a TooltipTrigger, for example a toggle of a ToggleGroup in a Toolbar.

A handle links the root to detached triggers: a TooltipTrigger with the same handle, anywhere in the page, opens this tooltip. The root then gets the handle as its id. One tooltip can have several triggers, and the payload of each trigger goes in the TooltipViewport. open and triggerId are the state: a render with new values opens the tooltip next to the trigger with that id, or closes it.

PropTypeDefault
handle?stringnull
openboolfalse
triggerId?stringnull
delayint600
closeDelayint0
hoverablebooltrue
closeOnClickbooltrue
side'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'Side::Top
align'start' | 'center' | 'end'Align::Center
sideOffsetint0
alignOffsetint0
children?Htmlnull
...attrsarray[]

TooltipTrigger#

The element that shows the tooltip. Give it its own label (aria-label): a touch shows no tooltip.

With a handle, it is a detached trigger of the Tooltip with the same handle: it can be anywhere in the page. The payload is the text of the TooltipViewport while this trigger is active.

In a group with one border around the triggers (a toolbar), the trigger has no border. A trigger alone has its own border: give it the bordered prop. A bordered trigger has the radius on the outer corners of the triggers in its parent (first-of-type: and last-of-type:): all its corners when it is the only trigger, and the outer corners of a row of joined triggers (border-s-0 after the first one). The Tooltip and the TooltipPopup next to it do not count. To change these corners, use the same variants: first-of-type:rounded-s-none.

PropTypeDefault
handle?stringnull
payload?stringnull
borderedboolfalse
children?Htmlnull
...restmixed—

TooltipPopup#

The box of the tooltip, with its text. The controller places it next to the trigger.

PropTypeDefault
children?Htmlnull
...restmixed—

TooltipArrow#

The arrow of the popup, at the side of the trigger.

PropTypeDefault
...restmixed—

TooltipViewport#

The content of a tooltip with several triggers: the payload of the active trigger, or its children. When the active trigger changes, the old content gets data-previous and the new content data-current, and the viewport gets data-activation-direction, for an animation.

PropTypeDefault
children?Htmlnull
...restmixed—

The source of the examples: examples/ui/Tooltip.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close