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.
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
titleattribute. - 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
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
</script>
<!-- The child is one element. Hover it, or press Tab to focus it: both show the tip. -->
<Tooltip title="prompt text">
<Button>Tooltip will show on hover and on focus</Button>
</Tooltip>
<!-- A plain element can be hovered; give it tabindex="0" so that the keyboard can reach it too. -->
<Tooltip title="prompt text">
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<span class="plain" tabindex="0">Plain text with tabindex</span>
</Tooltip>
<style>
.plain {
margin-inline-start: 16px;
text-decoration: underline dotted;
cursor: help;
}
</style>
<script lang="ts">
import { Button, Tooltip, type TooltipPlacement } from 'svelte-ant-design';
// The buttons stand in a ring, and the middle is empty: a tip that points to the left or to the right then has free
// space beside it instead of covering the next button. The label is short; the tip says the full name.
const top: Array<[TooltipPlacement, string]> = [['topLeft', 'TL'], ['top', 'Top'], ['topRight', 'TR']];
const left: Array<[TooltipPlacement, string]> = [['leftTop', 'LT'], ['left', 'Left'], ['leftBottom', 'LB']];
const right: Array<[TooltipPlacement, string]> = [['rightTop', 'RT'], ['right', 'Right'], ['rightBottom', 'RB']];
const bottom: Array<[TooltipPlacement, string]> = [['bottomLeft', 'BL'], ['bottom', 'Bottom'], ['bottomRight', 'BR']];
</script>
{#snippet side(items: Array<[TooltipPlacement, string]>)}
{#each items as [placement, label] (placement)}
<Tooltip title={placement} {placement}>
<Button>{label}</Button>
</Tooltip>
{/each}
{/snippet}
<div class="ring">
<div class="top">{@render side(top)}</div>
<div class="left">{@render side(left)}</div>
<div class="right">{@render side(right)}</div>
<div class="bottom">{@render side(bottom)}</div>
</div>
<style>
/* A ring: a row on top, a column on each side, a row at the bottom. It shrinks with the screen, so the page does not
scroll sideways on a phone. */
.ring {
display: grid;
grid-template-columns: max-content minmax(0, 1fr) max-content;
grid-template-areas:
'. top .'
'left . right'
'. bottom .';
gap: 12px;
width: min(100%, 420px);
margin-inline: auto;
padding: 40px 0;
}
.top,
.bottom {
display: flex;
justify-content: center;
gap: 8px;
}
.left,
.right {
display: flex;
flex-direction: column;
gap: 8px;
}
.left {
align-items: flex-start;
}
.right {
align-items: flex-end;
}
.top {
grid-area: top;
}
.left {
grid-area: left;
}
.right {
grid-area: right;
}
.bottom {
grid-area: bottom;
}
</style>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
</script>
<!-- Shown by default. -->
<Tooltip title="prompt text" placement="topLeft">
<Button>Arrow at the edge</Button>
</Tooltip>
<!-- pointAtCenter points the arrow at the middle of a short trigger, wherever the tip is aligned. -->
<Tooltip title="prompt text" placement="topLeft" arrow={{ pointAtCenter: true }}>
<Button>pointAtCenter</Button>
</Tooltip>
<Tooltip title="prompt text" arrow={false}>
<Button>No arrow</Button>
</Tooltip>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
</script>
<!-- The default is hover and focus. A single value or an array. -->
<Tooltip title="hover" trigger="hover">
<Button>hover</Button>
</Tooltip>
<Tooltip title="focus: it opens when the field gets focus" trigger="focus">
<input class="field" aria-label="Focus to see the tip" placeholder="focus" />
</Tooltip>
<Tooltip title="click: press again, press elsewhere or Escape to close it" trigger="click">
<Button>click</Button>
</Tooltip>
<Tooltip title="contextMenu: the browser menu is suppressed" trigger="contextMenu">
<Button>right click</Button>
</Tooltip>
<Tooltip title="hover or click" trigger={['hover', 'click']}>
<Button>hover or click</Button>
</Tooltip>
<!-- An empty array: no trigger at all, only the open prop opens it (see Control). -->
<style>
.field {
height: 32px;
padding: 0 11px;
border: 1px solid var(--ads-color-border);
border-radius: 6px;
background: var(--ads-color-bg-container);
color: var(--ads-color-text);
font: inherit;
}
</style>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
</script>
<!-- Both delays are in seconds and default to 0.1. The pointer may cross into the tip and rest in it. -->
<Tooltip title="shown after 0.8 s" mouseEnterDelay={0.8}>
<Button>mouseEnterDelay 0.8</Button>
</Tooltip>
<Tooltip title="stays 1.5 s after you leave; move into it to keep it" mouseLeaveDelay={1.5}>
<Button>mouseLeaveDelay 1.5</Button>
</Tooltip>
<Tooltip title="instant" mouseEnterDelay={0} mouseLeaveDelay={0}>
<Button>no delay</Button>
</Tooltip>
no change yet
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
let open = $state(false);
let log = $state('no change yet');
let host = $state<HTMLElement>();
</script>
<!-- bind:open keeps the parent in step; onopenchange runs once per change the user causes. -->
<Tooltip bind:open title="Controlled tooltip" onopenchange={(next) => (log = `onopenchange(${next})`)}>
<Button>Hover or focus me</Button>
</Tooltip>
<Button onclick={() => (open = !open)}>{open ? 'Hide' : 'Show'} from the parent</Button>
<span role="status">{log}</span>
<!-- An empty trigger list and open: only the parent decides. container keeps a tip that is always there inside the
region of the page, so that it is not a loose block outside every landmark. -->
<div class="always" bind:this={host}>
<Tooltip open title="Always open" trigger={[]} placement="right" container={host}>
<Button>No triggers</Button>
</Tooltip>
</div>
<style>
.always {
position: relative;
display: inline-block;
margin-inline-start: 12px;
}
</style>
<script lang="ts">
import { Button, Tooltip, TOOLTIP_PRESET_COLORS } from 'svelte-ant-design';
const custom = ['#f50', '#2db7f5', '#87d068', '#108ee9', 'hsl(280 60% 25%)'];
</script>
<!-- A name from the palette is a preset. The text is black or white, whichever contrasts more. -->
{#each TOOLTIP_PRESET_COLORS as color (color)}
<Tooltip title="{color} tip" {color}>
<Button>{color}</Button>
</Tooltip>
{/each}
<span class="break"></span>
<!-- Any other string is a CSS color. The arrow follows it, and so does the text color. -->
{#each custom as color (color)}
<Tooltip title="{color} tip" {color}>
<Button>{color}</Button>
</Tooltip>
{/each}
<style>
/* The preview is a wrapping row: this empty element is a line break in it. */
.break {
flex-basis: 100%;
height: 0;
}
</style>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
</script>
{#snippet rich()}
<strong>Bold</strong> and <em>italic</em>, a snippet can hold markup (but not links or buttons: use a popover for those).
{/snippet}
<!-- A string is text, never HTML. -->
<Tooltip title="<b>text</b>, not markup">
<Button>String</Button>
</Tooltip>
<!-- A snippet is the content. -->
<Tooltip title={rich}>
<Button>Snippet</Button>
</Tooltip>
<!-- A number is shown, and 0 too. -->
<Tooltip title={0}>
<Button>Number</Button>
</Tooltip>
<!-- The text wraps at 250 px, and a long word breaks. -->
<Tooltip title="A longer text wraps at a maximum width of 250 pixels, and Supercalifragilisticexpialidocious-is-a-word-that-breaks.">
<Button>Long text</Button>
</Tooltip>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
let title = $state('A tip');
</script>
<!-- An empty title (undefined, null or '') means no tooltip: it never opens, even with open. -->
<Tooltip {title}>
<Button>{title ? 'Has a tip' : 'No tip'}</Button>
</Tooltip>
<Button onclick={() => (title = title ? '' : 'A tip')}>Empty the title</Button>
<!-- A disabled button shows its tip (checked in Chromium, Firefox and WebKit, current and at the lowest versions). -->
<Tooltip title="Why it is disabled">
<Button disabled>Disabled</Button>
</Tooltip>
<!-- For a control that swallows pointer events by itself, a span around it is the safe form. -->
<Tooltip title="Why it is disabled">
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<span class="wrap" tabindex="0"><Button disabled>Disabled in a span</Button></span>
</Tooltip>
<style>
.wrap {
display: inline-block;
}
</style>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
let host = $state<HTMLElement>();
let visible = $state(false);
</script>
<!-- autoAdjustOverflow flips the tip to the other side when there is no room (this trigger is at the left edge). -->
<div class="edge">
<Tooltip title="flips to the right" placement="left">
<Button>Flips</Button>
</Tooltip>
<Tooltip title="stays on the left, and runs off the screen" placement="left" autoAdjustOverflow={false}>
<Button>autoAdjustOverflow off</Button>
</Tooltip>
</div>
<!-- zIndex overrides the tooltip layer (1070 by default). -->
<Tooltip title="zIndex 3000" zIndex={3000}>
<Button>zIndex</Button>
</Tooltip>
<!-- container moves the tip into an element; the tip is clipped by it. -->
<div class="host" bind:this={host}>
<Tooltip title="Inside the box" container={host}>
<Button>container</Button>
</Tooltip>
</div>
<!-- destroyOnHidden removes the content from the DOM on every close; the text below is its state. -->
<Tooltip title="Removed when closed" destroyOnHidden onopenchange={(next) => (visible = next)}>
<Button>destroyOnHidden</Button>
</Tooltip>
<span role="status">{visible ? 'in the DOM' : 'removed'}</span>
<style>
.edge {
display: flex;
gap: 12px;
margin-bottom: 12px;
}
.host {
position: relative;
display: inline-block;
padding: 28px 12px 4px;
margin: 0 12px;
border: 1px dashed #8c8c8c;
}
</style>
<script lang="ts">
import { Button, Tooltip } from 'svelte-ant-design';
</script>
<!-- root: the positioned element, container: the visible box, arrow: the little square pointing at the trigger. -->
<Tooltip
title="With classes"
classNames={{ root: 'tt-demo-root', container: 'tt-demo-box', arrow: 'tt-demo-arrow' }}
>
<Button>classNames</Button>
</Tooltip>
<!-- styles are inline styles per part; custom properties work too. -->
<Tooltip
title="With styles"
styles={{
container: { 'font-weight': 700, 'letter-spacing': '0.02em', '--ads-tooltip-bg': '#237804' },
arrow: { opacity: 0.9 }
}}
>
<Button>styles</Button>
</Tooltip>
<style>
:global(.tt-demo-root) {
filter: drop-shadow(0 4px 8px rgb(0 0 0 / 35%));
}
:global(.tt-demo-box) {
outline: 2px solid #faad14;
outline-offset: 1px;
}
:global(.tt-demo-arrow) {
outline: 2px solid #faad14;
}
</style>
<script lang="ts">
import { Button, Checkbox, Switch, Tooltip } from 'svelte-ant-design';
let target = $state<HTMLElement | null>(null);
let on = $state(false);
</script>
<Tooltip title="Saves the draft">
<Button type="primary">Save</Button>
</Tooltip>
<Tooltip title="Turns the alerts on or off">
<Switch aria-label="Alerts" />
</Tooltip>
<!-- The whole label is the trigger: moving from the box to its text does not close the tip. -->
<Tooltip title="You can change this later">
<Checkbox>Agree with the terms</Checkbox>
</Tooltip>
<!-- An icon-only button needs a name of its own: a tooltip describes, it does not name. -->
<Tooltip title="Add to favorites">
<Button shape="circle" aria-label="Add to favorites">+</Button>
</Tooltip>
<!-- anchor places the tip at an element that you hold, with no wrapper around it. -->
<button type="button" class="native" bind:this={target} onclick={() => (on = !on)}>Anchor: click me</button>
<Tooltip title="Placed at the button on the left" anchor={target} open={on} trigger={[]} placement="bottom" />
<style>
.native {
height: 32px;
padding: 0 15px;
border: 1px solid var(--ads-color-border);
border-radius: 6px;
background: var(--ads-color-bg-container);
color: var(--ads-color-text);
font: inherit;
cursor: pointer;
}
</style>
API
| Property | Description | Type | Default |
|---|---|---|---|
| title | The 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 | — |
| children | The 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 | — |
| open | Whether the tip is shown. Use bind:open for two-way use; a plain value is the initial state. | boolean | false |
| onopenchange | Called once per change the user causes (trigger, Escape, press outside), with the new value. Not called when the parent changes open. | (open: boolean) => void | — |
| placement | Preferred 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' |
| trigger | What 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'] |
| mouseEnterDelay | Delay before the tip shows on hover, in seconds. | number | 0.1 |
| mouseLeaveDelay | Delay before the tip hides after the pointer left, in seconds. The pointer may move into the tip and rest in it. | number | 0.1 |
| arrow | Shows an arrow. { pointAtCenter: true } points it at the middle of the trigger instead of its aligned edge. | boolean | { pointAtCenter: boolean } | true |
| autoAdjustOverflow | Flip to the opposite side when there is no room, and shift along the edge for an unaligned placement. | boolean | true |
| color | The 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 | — |
| zIndex | Overrides the z-index of the tooltip layer (--ads-tooltip-z-index, 1070). | number | — |
| container | Where 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) | — |
| destroyOnHidden | Remove the content from the DOM on every close. By default it stays mounted after the first opening. | boolean | false |
| classNames | Extra 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>> | — |
| styles | Inline 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>>> | — |
| anchor | The 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
| Token | Description | Default |
|---|---|---|
| --ads-tooltip-bg | Background of the box. In the dark theme #424242. The arrow takes it too. | #262626 |
| --ads-tooltip-color | Text color of the default box and the base of a preset. | #ffffff |
| --ads-tooltip-radius | Corner radius of the box (never more than 8px). | 6px |
| --ads-tooltip-padding | Padding of the box. | 6px 8px |
| --ads-tooltip-min-height | Minimum height of the box. | 32px |
| --ads-tooltip-max-width | Maximum width of the box; longer text wraps. | 250px |
| --ads-tooltip-z-index | Layer of the tooltip, above the popover (1030) and the dropdown and select (1050) layers. | 1070 |
| --ads-tooltip-preset-pink | Background 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-red | Background 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-yellow | Background 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-orange | Background 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-cyan | Background 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-green | Background 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-blue | Background 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-purple | Background 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-geekblue | Background 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-magenta | Background 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-volcano | Background 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-gold | Background 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-lime | Background 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 anid. While it is shown, the trigger getsaria-describedbypointing 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
Escapecloses 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>needstabindex="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#424242in the dark theme, 10.0:1. The text of the 13 presets and of any customcoloris black or white, whichever contrasts more, so it reaches at least 4.5:1 in the light theme, in the dark theme and withtokens-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).