Tooltip unstable
A popup that shows a short visual label for an element while it's hovered or focused.
Tooltip is headless: it owns the hover and focus timing, the data
attributes and the popup's place in the top layer, and the consumer draws
the markup. The consumer owns whether the tooltip is open, passes it as
open, and stores what the RequestedOpen and RequestedClose events
ask for.
The tooltip asks to close when the pointer leaves, focus moves away, the
trigger is activated, Escape is pressed, or the mouse is pressed outside
the trigger and the positioner. The last two close it whatever keeps it
open: keyboard focus, closeDelay, or a consumer that opened it itself.
A tooltip is a visual label for sighted mouse and keyboard users. Screen
readers don't get it by default, so the trigger still needs its own
accessible name, such as an aria-label that matches the tooltip's text.
A tooltip that adds to the label, such as a shortcut or why the trigger is
disabled, can be the trigger's accessible description with
describesTrigger. Touch users without a screen reader still never see
it, so keep essential information out of tooltips.
Anatomy
| Part | Element |
|---|---|
trigger |
<button> or <a href> the tooltip describes |
positioner |
<div>, a manual popover placed by the tooltip |
popup |
<div> inside the positioner |
arrow |
<div> inside the popup |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children:
tooltip.positioner(h.div).children(tooltip.popup(h.div).children("Bold")).
A part's result is a builder, so it can also be another kit call's
element. That puts a tooltip on a ui-kit Button:
Button(h, { element: tooltip.trigger(h.button) }).class("tool").children("B").
The positioner is a manual popover, so it renders in the top layer and
needs no portal. While it shows, the tooltip measures the trigger and
writes the positioner's left and top, again when the page scrolls,
the window resizes, or the trigger or the positioner changes size or
moves. It needs no CSS anchor positioning. sideOffset is padding on the
positioner's side facing the trigger, so the gap is part of the positioner
and the pointer can cross it to the popup without leaving the tooltip.
The positioner's position, inset and margin come after the
consumer's own inline style. The browser gives a popover a border,
padding, a background and overflow: auto, so reset those on the
positioner and style the popup instead.
With topLayer: false, the positioner renders in place as an ordinary
element, hidden while closed, and the tooltip is placed and clipped
inside its containing block, as Positioning describes.
CSS variables
Set on the positioner:
--anchor-width,--anchor-height: the trigger's size--available-width,--available-height: the space between the trigger and the edge of the collision boundary, for amax-height--transform-origin: the point on the trigger the popup grows from
Data attributes
Each present with an empty value or absent, except data-side,
data-align and data-instant, which hold a value:
trigger:data-popup-open,data-trigger-disabledpositioner:data-open/data-closed,data-side,data-align,data-anchor-hiddenpopup:data-open/data-closed,data-side,data-align,data-instantarrow:data-open/data-closed,data-side,data-align,data-instant,data-uncentered
data-instant is focus when focus opened the tooltip, dismiss when
a click on the trigger or Escape closed it, and delay when a
TooltipProvider let it open without its hover delay or another tooltip
of the group replaced it, so a transition can be skipped.
data-side and data-align report where the popup is, which can differ
from the props after a flip. data-anchor-hidden is present while the
trigger is scrolled out of view, and data-uncentered while the arrow
can't point at the trigger's middle without leaving the popup.
Keyboard
Focusing the trigger with the keyboard opens the tooltip without a delay,
and moving focus away closes it. The trigger must match :focus-visible
when it receives focus to open the tooltip or keep it open after the
pointer leaves. Other focus events are ignored. Escape closes an open
tooltip wherever focus is, so a tooltip opened by hovering can be
dismissed without moving the pointer, and an enclosing dialog stays open
until the next Escape. Activating the trigger closes it:
a click, or the keys the element activates on (Enter or Space on a
button, Enter on a link). A link trigger needs an href: without one it
isn't focusable and isn't a link.
Placement and timing
When the popup doesn't fit, it flips to the opposite side and shifts along
the edge, keeping collisionPadding from the viewport. The tooltip
places the arrow too: it gets position: absolute and a left or top
that points it at the trigger, so give it only its size and the offset
across the edge in your CSS. To animate the popover, use
@starting-style and transition-behavior: allow-discrete. The
Positioning module does the placing, and a popup of your own can use it.
Each tooltip has one trigger. On its own it waits its own delay on
every hover. Below a TooltipProvider, once one tooltip of the group
has been open, hovering another opens it at once, until timeout after
the last one closed. One tooltip of the group shows at a time: when another is
shown, the previous one asks to close. Wrap the group, with nothing to add to each
tooltip: TooltipProvider(h, { props: {}, view: () => h.div(Tooltip(h, ...), Tooltip(h, ...)) }).
On its way from the trigger to the popup, the pointer can cross the
triangle between where it left the trigger and the positioner's near edge
without starting the close, so a narrow trigger and a wide popup can be
crossed diagonally. A pointer that stops in that triangle keeps the
tooltip open until it moves out. With disableHoverablePopup, the
tooltip closes closeDelay after the pointer leaves the trigger.
Example A toolbar button with a tooltip whose open state lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Tooltip } from "effect/ui/kit"import { Effect, Schema } from "effect"
const ToolbarMessage = UiSchema.TaggedEnum({ RequestedOpenBoldTip: {}, RequestedCloseBoldTip: {} })
const Toolbar = Html.component({ name: "Toolbar", model: Schema.Struct({ boldTip: Schema.Boolean }), message: ToolbarMessage}).make({ init: () => ({ boldTip: false }), update: { RequestedOpenBoldTip: ($) => $.evolve({ boldTip: true }), RequestedCloseBoldTip: ($) => $.evolve({ boldTip: false }) }, view: (h, model) => Tooltip(h, { props: { open: model.boldTip, sideOffset: 8 }, onEmit: { RequestedOpen: () => ToolbarMessage.RequestedOpenBoldTip(), RequestedClose: () => ToolbarMessage.RequestedCloseBoldTip() }, view: (tooltip) => h.div( tooltip.trigger(h.button.class("tool").attr("aria-label", "Bold")).children("B"), tooltip.positioner(h.div.class("tip-positioner")).children( tooltip.popup(h.div.class("tip")).children(tooltip.arrow(h.div.class("tip-arrow")), "Bold") ) ) })})
// The positioner is a manual popover.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Toolbar, {}))if (!/<div class="tip-positioner"[^>]* popover="manual"/.test(html)) throw new Error("the positioner isn't a popover")Components
A headless tooltip. Call it with the consumer's h and a view closure
that draws the button the tooltip describes with tooltip.trigger, and the
popup's markup with tooltip.positioner, tooltip.popup and tooltip.arrow.
Details
The parts also return open, side and align, resolved, for markup
that depends on them. open is false while the tooltip is disabled.
Every part takes the consumer's builder and returns it with the part
applied, so the consumer goes on chaining and adds the children. The
result is still a builder, so it can be another kit call's element,
such as a ui-kit Button's. The positioner and the popup can stay in the
view while the tooltip is closed: the popover hides them.
With describesTrigger, unless disabled, the popup gets a generated
id and role="tooltip". The trigger's aria-describedby includes that
ID alongside the consumer's own IDs. Keep the positioner and the popup
in the view while the tooltip is closed so the description remains
available to screen readers before the tooltip opens.
The tooltip's own attributes win over the ones the consumer set on the
builder it passes in: the data attributes on every part, popover and
role="presentation" on the positioner, aria-hidden on the arrow, and
the popup's id and role with describesTrigger unless disabled.
That includes the ones a state leaves absent, so a data-open on the
consumer's popup is removed while the tooltip is closed. The
positioner's placement and the arrow's offset are added after the
consumer's inline style, including one chained after the part. Other
attributes the consumer chains after a part are the consumer's. A
handler the consumer adds to a part runs after the tooltip's own.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const Tooltip: Headless<{ readonly align?: "start" | "end" | "center"; readonly alignOffset?: number; readonly anchor?: string | { readonly height: number; readonly width: number; readonly x: number; readonly y: number; }; readonly arrowPadding?: number; readonly closeDelay?: number; readonly closeOnClick?: boolean; readonly collisionAvoidance?: { readonly align?: "shift" | "none" | "flip"; readonly fallbackAxisSide?: "start" | "none" | "end"; readonly side?: "shift" | "none" | "flip"; }; readonly collisionBoundary?: "clipping-ancestors" | { readonly height: number; readonly width: number; readonly x: number; readonly y: number; } | { readonly id: string; }; readonly collisionPadding?: number | { readonly bottom?: number; readonly left?: number; readonly right?: number; readonly top?: number; }; readonly delay?: number; readonly describesTrigger?: boolean; readonly disableAnchorTracking?: boolean; readonly disabled?: boolean; readonly disableHoverablePopup?: boolean; readonly open: boolean; readonly positionMethod?: "fixed" | "absolute"; readonly side?: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; readonly sideOffset?: number; readonly sticky?: boolean; readonly topLayer?: boolean;}, { readonly _tag: "RequestedOpen";} | { readonly _tag: "RequestedClose";}, { align: "start" | "end" | "center"; arrow: Part<"div", [], never>; open: boolean; popup: Part<"div", [], never>; positioner: Part<"div", [], never>; side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; trigger: Part<"a" | "button", [], never>;}, never, never, never, never, never, never, { readonly focused: boolean; readonly inArea: boolean; readonly instant: "delay" | "focus" | "dismiss" | null; readonly onTrigger: boolean; readonly placement: { readonly align: "start" | "end" | "center"; readonly anchorHidden: boolean; readonly arrowUncentered: boolean; readonly side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; } | null; readonly turn: number; readonly yieldedTo: number | null;}, { readonly _tag: "ClickedTrigger";} | { readonly _tag: "PressedEscape";} | { readonly _tag: "BlurredTrigger";} | { readonly _tag: "LeftTrigger";} | { readonly _tag: "LeftHoverArea";} | { readonly _tag: "ReturnedToHoverArea";} | { readonly _tag: "PressedOutside";} | { readonly _tag: "CompletedWaitBeforeClosing";} | { readonly _tag: "CompletedWaitBeforeOpening";} | { readonly _tag: "PressedTrigger";} | { readonly _tag: "ChangedPlacement"; readonly placement: { readonly align: "start" | "end" | "center"; readonly anchorHidden: boolean; readonly arrowUncentered: boolean; readonly side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; };} | { readonly _tag: "EnteredTrigger";} | { readonly _tag: "FocusedTrigger"; readonly visible: boolean;} | { readonly _tag: "ShowedPositioner";} | { readonly _tag: "ChangedGroup"; readonly newest: number | null; readonly next: number;}, QueryEntries<{}, {}>>TooltipProvider
unstableGroups the tooltips below it, so that once one has been open, hovering
another opens it at once instead of after its delay.
Details
Wrap a toolbar or a row of buttons. The tooltips below need nothing
extra: each Tooltip reads the nearest provider if there is one, and
behaves as on its own if there isn't.
Each tooltip reports whether it shows, from its open prop. So the group
follows the tooltips the consumer actually shows, not the RequestedOpen and
RequestedClose requests: a refused close keeps the group open, and a close
the consumer makes on its own starts the window. While any tooltip shows, and
for timeout milliseconds after the last one closes, a hover on another
trigger opens it with no delay, and its popup and arrow get
data-instant="delay". A tooltip that closes while another in the group
shows gets it too, so the swap skips both transitions. After the window, the
next hover waits the tooltip's delay again.
Only one tooltip of the group shows at a time. When a tooltip is shown,
every other one that still shows asks to close with one RequestedClose,
even if keyboard focus or closeDelay would hold it open. A consumer
that refuses keeps it on screen, and the request isn't repeated until yet
another tooltip takes over. A tooltip knows it was replaced by its turn:
each one takes the group's next turn once it is shown, however it was
opened, and the highest turn that shows is the newest. Two tooltips
shown in the same update take the same turn, so neither replaces the
other.
The decision is made when the pointer enters the trigger: a hover already waiting out its delay isn't cut short.
Every tooltip below re-renders when the group opens, when another tooltip takes over, when its last tooltip closes and when the window ends. Wrap a toolbar or a row group, not a whole table.
One Model field for which tooltip is open, such as
tip: "bold" | "italic" | null with open: model.tip === "bold", closes
the previous tooltip in the same update that accepts the next one's
RequestedOpen. With a field per tooltip, the previous one's
RequestedClose closes it.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const TooltipProvider: Headless<{ readonly timeout?: number;}, never, {}, never, never, Shared<"TooltipProvider">, { instant: boolean; next: number; open: boolean; turns: readonly Array<number>;}, number | null, never, { readonly instant: boolean; readonly next: number; readonly open: boolean; readonly turns: readonly Array<number>;}, { readonly _tag: "ChangedShown"; readonly turns: readonly Array<number>;} | { readonly _tag: "CompletedWaitBeforeCooling";}, QueryEntries<{}, {}>>Models
How a tooltip lines up with its trigger along the side it shows on.
Signature
type TooltipAlign = typeof TooltipAlign.TypeWhat a Tooltip emits.
Signature
type TooltipEvents = typeof TooltipEvents.TypeThe props of a Tooltip.
Signature
type TooltipProps = typeof TooltipProps.TypeThe props of a TooltipProvider.
Signature
type TooltipProviderProps = typeof TooltipProviderProps.TypeThe side of the trigger a tooltip shows on.
Signature
type TooltipSide = typeof TooltipSide.TypeSchemas
TooltipAlign
unstableHow a tooltip lines up with its trigger along the side it shows on.
Signature
declare const TooltipAlign: Literals<readonly ["start", "center", "end"]>TooltipEvents
unstableRequestedOpen asks for the tooltip to open after the hover delay or when
the trigger receives keyboard focus. RequestedClose asks for it to close
when the pointer leaves, focus moves away, or the user activates the
trigger, presses Escape or presses outside the tooltip. The consumer
decides whether to change open.
Signature
declare const TooltipEvents: TaggedEnum<{ RequestedClose: TaggedStruct<"RequestedClose", {}>; RequestedOpen: TaggedStruct<"RequestedOpen", {}>;}>TooltipProps
unstableControls whether the tooltip is open, how its trigger responds to input, and where the popup appears.
Details
open: whether the tooltip shows. Required: the consumer owns it.disabled: the tooltip never shows and emits nothing. The trigger stays enabled: disable the button itself if it should be.delay: milliseconds the pointer rests on the trigger before the tooltip opens, 600 by default.closeDelay: milliseconds after the pointer leaves the trigger, the positioner and the triangle between them before the tooltip closes, 0 by default.closeOnClick: whether activating the trigger, by a click or the keys the element activates on, closes the tooltip, true by default.disableHoverablePopup: the pointer on the popup doesn't keep the tooltip open, and the positioner takes no pointer events.describesTrigger: makes the popup the trigger's accessible description unlessdisabled. Use it for text that adds to the trigger's label rather than repeating it. False by default.side: the side of the trigger the popup shows on,"top"by default."inline-start"and"inline-end"follow the writing direction.align: how the popup lines up with the trigger along that side,"center"by default.sideOffset: the gap between the trigger and the popup, in pixels.alignOffset: pixels the popup moves along the side, toward the trigger's other end when aligned to the start or the end, and right or down when centred.collisionAvoidance: what happens when the popup doesn't fit:sideandaligneach"flip","shift"or"none", both"flip"by default, andfallbackAxisSide, the side on the other axis to try when neither side fits,"end"by default.collisionBoundary: the area the popup keeps inside, the viewport by default, an element's{ id }, or a rectangle in viewport coordinates.collisionPadding: pixels kept from the boundary's edges, 5 by default, as one number or per side.sticky: the popup stays in view when the trigger scrolls out of it.arrowPadding: pixels kept between the arrow and the popup's corners, 5 by default.positionMethod:"absolute", the default, or"fixed", which doesn't make the page taller when the popup reaches past its end.disableAnchorTracking: the popup is placed only when it opens, when the window resizes and when its own size changes, not when the page scrolls or the trigger moves or changes size.anchor: the id of an element to place the popup against instead of the trigger.topLayer:falsedraws the positioner in place, out of the top layer, as the module describes.truewhen left out.
Signature
declare const TooltipProps: Struct<{ readonly align: optional<Literals<readonly ["start", "center", "end"]>>; readonly alignOffset: optional<Number>; readonly anchor: optional<Union<readonly [String, Struct<{ readonly height: Number; readonly width: Number; readonly x: Number; readonly y: Number; }>]>>; readonly arrowPadding: optional<Number>; readonly closeDelay: optional<Number>; readonly closeOnClick: optional<Boolean>; readonly collisionAvoidance: optional<Struct<{ readonly align: optional<Literals<readonly ["flip", "shift", "none"]>>; readonly fallbackAxisSide: optional<Literals<readonly ["start", "end", "none"]>>; readonly side: optional<Literals<readonly ["flip", "shift", "none"]>>; }>>; readonly collisionBoundary: optional<Union<readonly [Literal<"clipping-ancestors">, Struct<{ readonly id: String; }>, Struct<{ readonly height: Number; readonly width: Number; readonly x: Number; readonly y: Number; }>]>>; readonly collisionPadding: optional<Union<readonly [Number, Struct<{ readonly bottom: optional<Number>; readonly left: optional<Number>; readonly right: optional<Number>; readonly top: optional<Number>; }>]>>; readonly delay: optional<Number>; readonly describesTrigger: optional<Boolean>; readonly disableAnchorTracking: optional<Boolean>; readonly disabled: optional<Boolean>; readonly disableHoverablePopup: optional<Boolean>; readonly open: Boolean; readonly positionMethod: optional<Literals<readonly ["absolute", "fixed"]>>; readonly side: optional<Literals<readonly ["top", "bottom", "left", "right", "inline-start", "inline-end"]>>; readonly sideOffset: optional<Number>; readonly sticky: optional<Boolean>; readonly topLayer: optional<Boolean>;}>TooltipProviderProps
unstableThe props of a TooltipProvider.
Details
timeout: milliseconds after the last tooltip in the group closes during which hovering another opens it at once, 400 by default.
Signature
declare const TooltipProviderProps: Struct<{ readonly timeout: optional<Number>;}>TooltipSide
unstableThe side of the trigger a tooltip shows on.
Signature
declare const TooltipSide: Literals<readonly ["top", "bottom", "left", "right", "inline-start", "inline-end"]>