Components

Tooltip

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

Usage guidelines#

  • Prefer using tooltips as visual labels only: Tooltips should act as supplementary visual labels for sighted mouse and keyboard users. Tooltips alone are not accessible to touch or screen reader users. See Alternatives to tooltips for more details.
  • Provide an accessible name for the trigger: Tooltips are visual-only elements and are not a replacement for labeling the trigger. The tooltip's trigger must have an aria-label attribute that closely matches the tooltip's content to ensure consistency for screen reader users.

Anatomy#

Import the component and assemble its parts:

Anatomytsx
import { Tooltip } from '@egoist/solid-base/tooltip';

<Tooltip.Provider>
  <Tooltip.Root>
    <Tooltip.Trigger />
    <Tooltip.Portal>
      <Tooltip.Positioner>
        <Tooltip.Popup>
          <Tooltip.Arrow />
          <Tooltip.Viewport />
        </Tooltip.Popup>
      </Tooltip.Positioner>
    </Tooltip.Portal>
  </Tooltip.Root>
</Tooltip.Provider>;

Alternatives to tooltips#

Tooltips should be supplementary popups that provide non-essential clarity in high-density UIs. A user should not miss critical information if they never see a tooltip.

Tooltips don't work well with touch input. Unlike mouse pointers with hover capability, there's no easily discoverable way to reveal a tooltip before tapping its trigger on a touch device.

iOS doesn't provide a system-standard, touch-friendly tooltip affordance, while Android may show a tooltip on long press. However, on the web, long press is often used to trigger contextual menus in the browser, which can lead to potential conflicts. For this reason, tooltips are disabled on touch devices.

Infotips#

Popups that open when hovering an info icon should use Popover with the openOnHover prop on the trigger instead of a tooltip. This way, touch users and screen reader users can access the content.

To know when to reach for a popover instead of a tooltip, consider the purpose of the trigger element: if the trigger's purpose is to open the popup itself, it's a popover. If the trigger's purpose is unrelated to opening the popup, it's a tooltip.

Description text#

Tooltips are designed for sighted users and are not a reliable way to deliver important information to touch users or assistive technologies. If the description is important to understanding the element, don't hide it behind a tooltip — use inline text or Popover if space is limited, so the information is accessible to everyone.

Since tooltips serve sighted mouse and keyboard users, iconography should clearly communicate the purpose of icon-only triggers, especially on mobile where the text label may not be visible.

If the description is not critical, a tooltip can still be used to provide extra clarity for sighted mouse or keyboard users.

Contextual feedback messages#

Use the Toast component's anchoring ability for more ergonomic DX, to ensure the message is announced to screen readers, and to support complex content.

Examples#

Detached triggers#

A tooltip can be controlled by a trigger located either inside or outside the <Tooltip.Root> component. For simple, one-off interactions, place the <Tooltip.Trigger> inside <Tooltip.Root>, 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 <Tooltip.Trigger> outside of <Tooltip.Root> and linking them with a handle created by the Tooltip.createHandle() function.

The imperative methods on the handle, such as open() and close(), require a <Tooltip.Root> using the same handle to be mounted. Calls made while no root is attached to the handle — before one mounts, or after it unmounts — are ignored. Each time a root mounts, it starts from fresh state: a call made while no root was attached is not replayed, and no open state carries over from a previous mount.

Detached triggerstsx
const demoTooltip = Tooltip.createHandle();

<Tooltip.Trigger handle={demoTooltip}>Button</Tooltip.Trigger>

<Tooltip.Root handle={demoTooltip}>
  ...
</Tooltip.Root>

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 <Tooltip.Trigger> components inside a single <Tooltip.Root>.

Multiple triggers within the Root parttsx
<Tooltip.Root>
  <Tooltip.Trigger>Trigger 1</Tooltip.Trigger>
  <Tooltip.Trigger>Trigger 2</Tooltip.Trigger>
  ...
</Tooltip.Root>
Multiple detached triggerstsx
const demoTooltip = Tooltip.createHandle();

<Tooltip.Trigger handle={demoTooltip}>Trigger 1</Tooltip.Trigger>
<Tooltip.Trigger handle={demoTooltip}>Trigger 2</Tooltip.Trigger>

<Tooltip.Root handle={demoTooltip}>
  ...
</Tooltip.Root>

The tooltip can render different content depending on which trigger opened it. This is achieved by passing a payload to the <Tooltip.Trigger> and using a function as the child of <Tooltip.Root>.

The payload can be strongly typed by providing a type argument to the createHandle() function:

Detached triggers with payloadtsx
const demoTooltip = Tooltip.createHandle<{ text: string }>();

<Tooltip.Trigger handle={demoTooltip} payload={{ text: 'Trigger 1' }}>
  Trigger 1
</Tooltip.Trigger>

<Tooltip.Trigger handle={demoTooltip} payload={{ text: 'Trigger 2' }}>
  Trigger 2
</Tooltip.Trigger>

<Tooltip.Root handle={demoTooltip}>
  {(state) => (
    <Tooltip.Portal>
      <Tooltip.Positioner sideOffset={8}>
        <Tooltip.Popup class={styles.Popup}>
          <Tooltip.Arrow class={styles.Arrow} />
          <Show when={state.payload}>
            {(payload) => <span>Tooltip opened by {payload().text}</span>}
          </Show>
        </Tooltip.Popup>
      </Tooltip.Positioner>
    </Tooltip.Portal>
  )}
</Tooltip.Root>

Controlled mode with multiple triggers#

You can control the tooltip's open state externally using the open and onOpenChange props on <Tooltip.Root>. This allows you to manage the tooltip's visibility based on your application's state. When using multiple triggers, you have to manage which trigger is active with the triggerId prop on <Tooltip.Root> and the id prop on each <Tooltip.Trigger>.

Note that there is no separate onTriggerIdChange prop. Instead, the onOpenChange callback receives an additional argument, eventDetails, which contains the trigger element that initiated the state change.

Animating the Tooltip#

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

Position and size

To animate the tooltip's position, apply CSS transitions to the left, right, top, and bottom properties of the Positioner part. To animate its size, transition the width and height of the Popup part.

Content

The tooltip also supports content transitions. This is useful when different triggers display different content within the same tooltip.

To enable content animations, wrap the content in the <Tooltip.Viewport> part. This part provides features to create direction-aware animations. It renders a div with a data-activation-direction attribute that indicates the new trigger's position relative to the previous one. The value is a space-separated set of up to two tokens (one per axis) — left or right for the horizontal axis and up or down for the vertical axis (for example, right down). Match a single token with the ~= attribute selector, such as [data-activation-direction~='right'].

Inside the <Tooltip.Viewport>, the content is further wrapped in divs with data attributes to help with styling:

  • data-current: The currently visible content when no transitions are present or the incoming content.
  • data-previous: The outgoing content during a transition.

You can use these attributes to style the enter and exit animations.

API reference#

<Tooltip.Provider>

Provides a shared delay for multiple tooltips. The grouping logic ensures that once a tooltip becomes visible, the adjacent tooltips will be shown instantly.

PropTypeDefault
delaynumber—

How long to wait before opening the tooltip on hover. Specified in milliseconds.

closeDelaynumber—

How long to wait before closing a tooltip. Specified in milliseconds.

timeoutnumber400

Another tooltip will open instantly if the previous tooltip is closed within this timeout. Specified in milliseconds.

<Tooltip.Root>

Groups all parts of the tooltip. Doesn't render its own HTML element.

PropTypeDefault
defaultOpenbooleanfalse

Whether the tooltip is initially open.

To render a controlled tooltip, use the open prop instead.

openboolean—

Whether the tooltip is currently open.

onOpenChange| ((open: boolean, eventDetails: TooltipRoot.ChangeEventDetails) => void)—

Event handler called when the tooltip is opened or closed.

onOpenChangeComplete((open: boolean) => void)—

Event handler called after any animations complete when the tooltip is opened or closed.

disableHoverablePopupbooleanfalse

Whether the tooltip contents can be hovered without closing the tooltip.

trackCursorAxis'none' | 'x' | 'y' | 'both''none'

Determines which axis the tooltip should track the cursor on.

actionsRefActionsRef<TooltipRoot.Actions>—

A ref to imperative actions: a ref object ({ current: null }) or a callback receiving them (and null on unmount). - unmount: Ends the closing phase of the tooltip after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the tooltip completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the tooltip imperatively when called.

disabledbooleanfalse

Whether the tooltip is disabled.

handleTooltipHandle<Payload>—

A handle to associate the tooltip with a trigger. If specified, allows external triggers to control the tooltip's open state. Can be created with the Tooltip.createHandle() method.

triggerIdstring | null—

ID of the trigger that the tooltip is associated with. This is useful in conjunction with the open prop to create a controlled tooltip. There's no need to specify this prop when the tooltip is uncontrolled (that is, when the open prop is not set).

defaultTriggerIdstring | null—

ID of the trigger that the tooltip is associated with. This is useful in conjunction with the defaultOpen prop to create an initially open tooltip.

<Tooltip.Trigger>

An element to attach the tooltip to. Renders a <button> element.

PropTypeDefault
handleTooltipHandle<Payload>—

A handle to associate the trigger with a tooltip.

payloadPayload—

A payload to pass to the tooltip when it is opened.

delaynumber600

How long to wait before opening the tooltip on hover. Specified in milliseconds.

closeOnClickbooleantrue

Whether the tooltip should close when this trigger is clicked.

closeDelaynumber0

How long to wait before closing the tooltip. Specified in milliseconds.

disabledbooleanfalse

If true, the tooltip will not open when interacting with this trigger. Note that this doesn't apply the disabled attribute to the trigger element. If you want to disable the trigger element itself, you can pass the disabled prop to the trigger element via the render prop.

idstring—

ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the tooltip in controlled mode (with the TooltipRoot triggerId prop).

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-trigger-disabled

Present when the trigger is disabled, either by the disabled prop or by a parent <Tooltip.Root> component.

<Tooltip.Portal>

A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.

PropTypeDefault
keepMountedbooleanfalse

Whether to keep the portal mounted in the DOM while the popup is hidden.

containerPortalContainer—

A parent element to render the portal element into.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

<Tooltip.Positioner>

Positions the tooltip against the trigger. Renders a <div> element.

PropTypeDefault
sideSide'top'

Which side of the anchor element to align the popup against. May automatically change to avoid collisions.

alignAlign'center'

How to align the popup relative to the specified side.

anchor| Element | null | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)—

An element to position the popup against. By default, the popup will be positioned against the trigger.

Accepts an element, a virtual element, a ref object, or a function (such as a signal accessor) that returns an element or a virtual element.

positionMethod'absolute' | 'fixed''absolute'

Determines which CSS position property to use.

sideOffsetnumber | OffsetFunction0

Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.

The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.

alignOffsetnumber | OffsetFunction0

Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.

The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.

collisionBoundaryBoundary'clipping-ancestors'

An element or a rectangle that delimits the area that the popup is confined to.

collisionPaddingPadding5

Additional space to maintain from the edge of the collision boundary.

stickybooleanfalse

Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.

arrowPaddingnumber5

Minimum distance to maintain between the arrow and the edges of the popup.

Use it to prevent the arrow element from hanging out of the rounded corners of a popup.

disableAnchorTrackingbooleanfalse

Whether to disable the popup from tracking any layout shift of its positioning anchor.

collisionAvoidanceCollisionAvoidance—

Determines how to handle collisions when positioning the popup.

side controls overflow on the preferred placement axis (top/bottom or left/right): - 'flip': keep the requested side when it fits; otherwise try the opposite side (top and bottom, or left and right). - 'shift': never change side; keep the requested side and move the popup within the clipping boundary so it stays visible. - 'none': do not correct side-axis overflow.

align controls overflow on the alignment axis (start/center/end): - 'flip': keep side, but swap start and end when the requested alignment overflows. - 'shift': keep side and requested alignment, then nudge the popup along the alignment axis to fit. - 'none': do not correct alignment-axis overflow.

fallbackAxisSide controls fallback behavior on the perpendicular axis when the preferred axis cannot fit: - 'start': allow perpendicular fallback and try the logical start side first (top before bottom, or left before right in LTR). - 'end': allow perpendicular fallback and try the logical end side first (bottom before top, or right before left in LTR). - 'none': do not fallback to the perpendicular axis.

When side is 'shift', explicitly setting align only supports 'shift' or 'none'. If align is omitted, it defaults to 'flip'.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

CSS variableDescription
--available-width

The available width between the trigger and the edge of the viewport.

--available-height

The available height between the trigger and the edge of the viewport.

--anchor-width

The anchor's width.

--anchor-height

The anchor's height.

--transform-origin

The coordinates that this element is anchored to. Used for animations and transitions.

--positioner-width

The width of the tooltip's positioner. It is important to set width to this value when using CSS to animate size changes.

--positioner-height

The height of the tooltip's positioner. It is important to set height to this value when using CSS to animate size changes.

<Tooltip.Popup>

A container for the tooltip contents. Renders a <div> element.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-instant

Present if animations should be instant.

<Tooltip.Arrow>

Displays an element positioned against the tooltip anchor. Renders a <div> element.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-uncentered

Present when the tooltip arrow is uncentered.

data-instant

Present if animations should be instant.

<Tooltip.Viewport>

A viewport for displaying content transitions. This component is only required if one popup can be opened by multiple triggers, its content changes based on the trigger, and switching between them is animated. Renders a <div> element.

PropTypeDefault
classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-current

Applied to the direct child of the viewport when no transitions are present or the new content when it's entering.

data-previous

Applied to the direct child of the viewport that contains the exiting content when transitions are present.

data-activation-direction

Indicates the direction from which the popup was activated. This can be used to create directional animations based on how the popup was triggered. Contains space-separated values for both horizontal and vertical axes.

data-transitioning

Indicates that the viewport is currently transitioning between old and new content.

data-instant

Present if animations should be instant.

CSS variableDescription
--popup-width

The width of the parent popup. This variable is placed on the 'previous' container and stores the width of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content.

--popup-height

The height of the parent popup. This variable is placed on the 'previous' container and stores the height of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content.

The Viewport is optional — reach for it only when a single popup is opened by multiple triggers, its content differs per trigger, and the switch between them is animated. When used, set width: var(--positioner-width) and height: var(--positioner-height) on the Positioner so its box is frozen to the measured size during the transition; otherwise content-driven resizing can make the popup thrash or flip to another side.

createHandle#

Tooltip.createHandle<Payload>() creates a Tooltip.Handle that connects detached triggers to a root. The handle exposes open(triggerId), close() and a reactive isOpen getter.