Tooltip

A tooltip is a short text tip for an element: the explanation of a button, an icon or a piece of text. It shows on hover and on keyboard focus, and it never takes focus.

Import
import { Tooltip } from 'svelte-ant-design';
Links
src/lib/Tooltip Report an issue Component README

When to use

  • Use a tooltip to explain a button, an icon or a short piece of text. It is often used instead of the native title attribute.
  • A tooltip is for a short explanation. Do not put links, buttons or other controls in it: it closes when focus leaves the trigger, so they cannot be used from the keyboard. A popover (not in the library yet) is for those.
  • The tip is hidden again when the pointer or the focus leaves, so put information that users need in the page itself.

Examples

Plain text with tabindex

Basic

Hover the button or press Tab to it: the tip shows on hover and on focus. The child is one element. A plain element such as a <span> can be hovered; give it tabindex="0" to make it reachable with the keyboard.

Placement

Twelve placements: the side of the trigger plus the alignment (TL is topLeft, LB is leftBottom, and so on; the tip names its placement). The buttons stand in a ring, so a tip beside a button has free room. The tip flips to the other side when there is none, and mirrors in right-to-left layouts.

Arrow

The arrow is shown by default. arrow={false} hides it, and arrow={{ pointAtCenter: true }} points it at the middle of the trigger instead of the aligned edge.

Triggers

hover, focus, click and contextMenu, one or several. The default is hover and focus, so that the keyboard works; Ant Design for React defaults to hover only. Escape closes the tip, and focus stays where it was.

Delays

mouseEnterDelay and mouseLeaveDelay are in seconds, both 0.1 by default. A pass over the trigger shorter than the delay shows nothing, and the pointer can move into the tip and rest in it.

no change yet

Control by the parent

bind:open keeps a value in sync with the tip; a plain open={x} is the initial value. onopenchange runs once per change the user causes (trigger, Escape, press outside), not for a change the parent makes. With an empty trigger only the parent opens it.

Colors

color takes one of the 13 preset names of the palette or any CSS color (#hex, rgb(), hsl(), var(--x)). The text is black or white, whichever contrasts more, so it stays readable on yellow and lime, where Ant Design draws white. The arrow follows the color.

Content

title is a string (always text, never HTML), a number or a snippet. The text wraps at 250 px. A tooltip is for a short explanation: links, buttons and other controls belong in a popover.

No tip and disabled triggers

An empty title (not given, null or an empty string) means no tooltip: it never opens, even with open. A disabled button still shows its tip: checked in Chromium, Firefox and WebKit, current and at the lowest supported versions (not in a real Safari). A <span> around a control that swallows pointer events by itself is the safe form.

removed

Overflow, layer and container

autoAdjustOverflow flips the tip when there is no room (and shifts it along the edge for the unaligned placements). zIndex overrides the layer (1070). container moves the tip into an element, for example a scoped theme or a clipped box. destroyOnHidden removes the content on every close.

Classes and styles

classNames and styles address three parts: root (the positioned element), container (the visible box) and arrow. The tokens --ads-tooltip-bg and --ads-tooltip-color on the container recolor the box and the arrow together.

Around other components

The child can be a Button, Switch, Checkbox or any element. A tooltip describes: an icon-only button still needs aria-label. anchor places the tip at an element that you hold, with no child.

API

PropertyDescriptionTypeDefault
titleThe text or the content of the tip: a string (always text, never HTML), a number or a snippet. Empty (not given, null, '') means no tooltip: it never opens, even when open is true. 0 is shown.string | number | Snippet | null—
childrenThe trigger: one element. The tip is placed next to it and listens to its hover, focus, click and context menu. The component adds an element with no box of its own around it (display: contents), so the layout does not change. A text-only child cannot be a trigger.Snippet—
openWhether the tip is shown. Use bind:open for two-way use; a plain value is the initial state.booleanfalse
onopenchangeCalled once per change the user causes (trigger, Escape, press outside), with the new value. Not called when the parent changes open.(open: boolean) => void—
placementPreferred placement: the side of the trigger plus the alignment. Mirrored in right-to-left layouts, flipped at the edge of the screen.'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight' | 'left' | 'leftTop' | 'leftBottom' | 'right' | 'rightTop' | 'rightBottom''top'
triggerWhat opens the tip: one value or an array. An empty array means it is only controlled. The default includes focus so that keyboard users get the tip; Ant Design for React defaults to hover.'hover' | 'focus' | 'click' | 'contextMenu' | readonly ('hover' | 'focus' | 'click' | 'contextMenu')[]['hover', 'focus']
mouseEnterDelayDelay before the tip shows on hover, in seconds.number0.1
mouseLeaveDelayDelay before the tip hides after the pointer left, in seconds. The pointer may move into the tip and rest in it.number0.1
arrowShows an arrow. { pointAtCenter: true } points it at the middle of the trigger instead of its aligned edge.boolean | { pointAtCenter: boolean }true
autoAdjustOverflowFlip to the opposite side when there is no room, and shift along the edge for an unaligned placement.booleantrue
colorThe background: one of the 13 preset names (pink, red, yellow, orange, cyan, green, blue, purple, geekblue, magenta, volcano, gold, lime) or any CSS color. A name of the list is a preset, so write #0000ff for the CSS blue. The text is black or white, whichever contrasts more. A color the browser cannot parse leaves the default box.string—
zIndexOverrides the z-index of the tooltip layer (--ads-tooltip-z-index, 1070).number—
containerWhere the tip is moved to: an element, or a function that receives the trigger and returns one. Default document.body. Use it when the tip must be inside a scoped theme or a clipped box.HTMLElement | ((anchor: HTMLElement | null) => HTMLElement)—
destroyOnHiddenRemove the content from the DOM on every close. By default it stays mounted after the first opening.booleanfalse
classNamesExtra classes for the parts root (the positioned element), container (the visible box) and arrow, appended to the component's own.Partial<Record<'root' | 'container' | 'arrow', string>>—
stylesInline styles for the same parts. Property names are kebab-case or custom properties, numbers are written as they are (no automatic px). On root they are set one property at a time, so the placement is kept.Partial<Record<'root' | 'container' | 'arrow', Record<string, string | number>>>—
anchorThe element to place the tip next to, instead of the first element of children. Use it with no child, or to place the tip at another element.HTMLElement | null—
Differences from Ant Design: the trigger defaults to hover and focus (React: hover only); the 13 preset colors get black or white text for contrast, where Ant Design draws white; there is no defaultOpen (use bind:open), fresh, forceAlign or align; classNames and styles are objects, not functions; the ConfigProvider settings and tooltip.unique will come with the ConfigProvider component; the child is wrapped in an element without a box of its own, and anchor replaces the Angular nzTooltipOrigin.

Design tokens

TokenDescriptionDefault
--ads-tooltip-bgBackground of the box. In the dark theme #424242. The arrow takes it too.#262626
--ads-tooltip-colorText color of the default box and the base of a preset.#ffffff
--ads-tooltip-radiusCorner radius of the box (never more than 8px).6px
--ads-tooltip-paddingPadding of the box.6px 8px
--ads-tooltip-min-heightMinimum height of the box.32px
--ads-tooltip-max-widthMaximum width of the box; longer text wraps.250px
--ads-tooltip-z-indexLayer of the tooltip, above the popover (1030) and the dropdown and select (1050) layers.1070
--ads-tooltip-preset-pinkBackground of the pink preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-pink-text is its text color.#eb2f96
--ads-tooltip-preset-redBackground of the red preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-red-text is its text color.#f5222d
--ads-tooltip-preset-yellowBackground of the yellow preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-yellow-text is its text color.#fadb14
--ads-tooltip-preset-orangeBackground of the orange preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-orange-text is its text color.#fa8c16
--ads-tooltip-preset-cyanBackground of the cyan preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-cyan-text is its text color.#13c2c2
--ads-tooltip-preset-greenBackground of the green preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-green-text is its text color.#52c41a
--ads-tooltip-preset-blueBackground of the blue preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-blue-text is its text color.#1677ff
--ads-tooltip-preset-purpleBackground of the purple preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-purple-text is its text color.#722ed1
--ads-tooltip-preset-geekblueBackground of the geekblue preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-geekblue-text is its text color.#2f54eb
--ads-tooltip-preset-magentaBackground of the magenta preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-magenta-text is its text color.#eb2f96
--ads-tooltip-preset-volcanoBackground of the volcano preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-volcano-text is its text color.#fa541c
--ads-tooltip-preset-goldBackground of the gold preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-gold-text is its text color.#faad14
--ads-tooltip-preset-limeBackground of the lime preset, in the light theme; the dark theme has its own value. --ads-tooltip-preset-lime-text is its text color.#a0d911

Accessibility

  • The tip has role="tooltip" and an id. While it is shown, the trigger gets aria-describedby pointing at it (the first focusable element inside it when the trigger itself is not focusable, as a label around a checkbox; merged with a value you set yourself); the attribute is removed when the tip closes, so it never points at an id that is not on the page.
  • By default the tip opens on keyboard focus as well as on hover, and Escape closes it while focus stays on the trigger. The tip never takes focus and is not in the Tab order, and the pointer can move into it and rest there.
  • The trigger has to be focusable for the keyboard to reach the tip: a Button, a link or a field is, while a plain <span> needs tabindex="0".
  • A tooltip describes; it does not name. An icon-only button still needs aria-label.
  • Contrast: the default box is white on #262626, 15.1:1, and white on #424242 in the dark theme, 10.0:1. The text of the 13 presets and of any custom color is black or white, whichever contrasts more, so it reaches at least 4.5:1 in the light theme, in the dark theme and with tokens-accessible.css. Ant Design draws white on every preset, which falls below 4.5:1 on most of them.
  • In forced-colors modes such as Windows High Contrast the box and the arrow are drawn with a border in a system colour, so they do not melt into the page.
  • Checked with a real screen reader (Orca with Firefox): after the name and the role of a trigger it reads the text of the tip as its description, first the description the trigger already had, a long tip whole, and nothing extra for a trigger="click" tip that is not shown. Not checked: NVDA, JAWS and VoiceOver, whether a tip that opens after the screen reader has spoken the focus is announced (it depends on a timing of Orca's own), and a real touch device (a tap shows the tip and a tap elsewhere closes it; this is tested with emulated touch only).