A tooltip is a short label for another element, shown on hover and on focus: it names an icon button, or says what a control does, in a few words. It holds nothing to act on.
Usage
The tooltip describes its target: 300ms after the pointer enters it or it takes focus, the
tooltip shows, below it by default. The web component's target is the element its for
attribute names.
import { createTooltip, createIconButton } from 'material';
const trigger = createIconButton({ icon: heartOutlineIcon, ariaLabel: 'Favorite' });
document.body.append(trigger.element);
const tooltip = createTooltip({ target: trigger.element, text: 'Add to favorites' });
document.body.append(tooltip.element);
<m-icon-button id="tooltip-target" aria-label="Favorite"></m-icon-button>
<m-tooltip text="Add to favorites" for="tooltip-target"></m-tooltip>
<script type="module">
document.querySelector('#tooltip-target').setAttribute('icon', heartOutlineIcon);
</script>
import { Tooltip, IconButton } from 'material/react';
import { heartOutlineIcon } from './app';
export function Example() {
return (
<>
<IconButton id="tooltip-target" icon={heartOutlineIcon} ariaLabel="Favorite" />
<Tooltip text="Add to favorites" for="tooltip-target" />
</>
);
}
<script setup lang="ts">
import { MTooltip, MIconButton } from 'material/vue';
import { heartOutlineIcon } from './app';
</script>
<template>
<MIconButton id="tooltip-target" :icon="heartOutlineIcon" aria-label="Favorite" />
<MTooltip text="Add to favorites" for="tooltip-target" />
</template>
<script lang="ts">
import { Tooltip, IconButton } from 'material/svelte';
import { heartOutlineIcon } from './app';
</script>
<IconButton id="tooltip-target" icon={heartOutlineIcon} ariaLabel="Favorite" />
<Tooltip text="Add to favorites" for="tooltip-target" />
import { Tooltip, IconButton } from 'material/solid';
import { heartOutlineIcon } from './app';
export function Example() {
return (
<>
<IconButton id="tooltip-target" icon={heartOutlineIcon} ariaLabel="Favorite" />
<Tooltip text="Add to favorites" for="tooltip-target" />
</>
);
}
Examples
Position
position is top, right, bottom or left, each also with -start or -end to align it
with an edge of the target. It is kept inside the window's width.
import { createTooltip, createIconButton } from 'material';
const trigger = createIconButton({ icon: shareIcon, ariaLabel: 'Share' });
document.body.append(trigger.element);
const tooltip = createTooltip({ target: trigger.element, text: 'Share', position: 'top' });
document.body.append(tooltip.element);
<m-icon-button id="tooltip-target" aria-label="Share"></m-icon-button>
<m-tooltip text="Share" position="top" for="tooltip-target"></m-tooltip>
<script type="module">
document.querySelector('#tooltip-target').setAttribute('icon', shareIcon);
</script>
import { Tooltip, IconButton } from 'material/react';
import { shareIcon } from './app';
export function Example() {
return (
<>
<IconButton id="tooltip-target" icon={shareIcon} ariaLabel="Share" />
<Tooltip text="Share" position="top" for="tooltip-target" />
</>
);
}
<script setup lang="ts">
import { MTooltip, MIconButton } from 'material/vue';
import { shareIcon } from './app';
</script>
<template>
<MIconButton id="tooltip-target" :icon="shareIcon" aria-label="Share" />
<MTooltip text="Share" position="top" for="tooltip-target" />
</template>
<script lang="ts">
import { Tooltip, IconButton } from 'material/svelte';
import { shareIcon } from './app';
</script>
<IconButton id="tooltip-target" icon={shareIcon} ariaLabel="Share" />
<Tooltip text="Share" position="top" for="tooltip-target" />
import { Tooltip, IconButton } from 'material/solid';
import { shareIcon } from './app';
export function Example() {
return (
<>
<IconButton id="tooltip-target" icon={shareIcon} ariaLabel="Share" />
<Tooltip text="Share" position="top" for="tooltip-target" />
</>
);
}
Timing and triggers
showDelay and hideDelay are in milliseconds. showOnHover: false leaves it to focus, and
showOnFocus: false to the pointer; show() and hide() drive it from script.
import { createTooltip, createIconButton } from 'material';
const trigger = createIconButton({ icon: settingsIcon, ariaLabel: 'Settings' });
document.body.append(trigger.element);
const tooltip = createTooltip({
target: trigger.element,
text: 'Settings',
showDelay: 600,
hideDelay: 0,
showOnHover: false,
});
document.body.append(tooltip.element);
<m-icon-button id="tooltip-target" aria-label="Settings"></m-icon-button>
<m-tooltip text="Settings" show-delay="600" hide-delay="0" no-show-on-hover for="tooltip-target"></m-tooltip>
<script type="module">
document.querySelector('#tooltip-target').setAttribute('icon', settingsIcon);
</script>
import { Tooltip, IconButton } from 'material/react';
import { settingsIcon } from './app';
export function Example() {
return (
<>
<IconButton id="tooltip-target" icon={settingsIcon} ariaLabel="Settings" />
<Tooltip text="Settings" showDelay={600} hideDelay={0} noShowOnHover for="tooltip-target" />
</>
);
}
<script setup lang="ts">
import { MTooltip, MIconButton } from 'material/vue';
import { settingsIcon } from './app';
</script>
<template>
<MIconButton id="tooltip-target" :icon="settingsIcon" aria-label="Settings" />
<MTooltip text="Settings" :show-delay="600" :hide-delay="0" no-show-on-hover for="tooltip-target" />
</template>
<script lang="ts">
import { Tooltip, IconButton } from 'material/svelte';
import { settingsIcon } from './app';
</script>
<IconButton id="tooltip-target" icon={settingsIcon} ariaLabel="Settings" />
<Tooltip text="Settings" showDelay={600} hideDelay={0} noShowOnHover for="tooltip-target" />
import { Tooltip, IconButton } from 'material/solid';
import { settingsIcon } from './app';
export function Example() {
return (
<>
<IconButton id="tooltip-target" icon={settingsIcon} ariaLabel="Settings" />
<Tooltip text="Settings" showDelay={600} hideDelay={0} noShowOnHover for="tooltip-target" />
</>
);
}
setTarget() moves a tooltip to another element, so one tooltip can serve a list. layer: 'top' shows it in the browser's top layer, above any clipping or stacking; the web component
always is. When a tooltip's target opens a menu, pressing the target or pressing Enter or Space
hides the tooltip at once; it stays hidden while the target's menu is open and returns only on a
hover or focus after the menu closes.
API
Options
textThe label
stringundefinedtargetThe element it describes
HTMLElementundefinedpositionWhere it sits against the target
TooltipPosition'bottom'variantdefault and plain are M3's plain tooltip, on inverse-surface; rich is M3's rich tooltip, on surface-container
'default' | 'plain' | 'rich''default'visibleWhether it shows at once
booleanfalseshowDelay / hideDelayHow long before it shows or hides, in ms
number300 / 100showOnHover / showOnFocusWhether the pointer, and focus, show it
booleantruelayerShows it in the top layer, after its target in the target's tree
'top'undefinedzIndexIts z-index, outside the top layer
numberundefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'The web component takes for, text (or its own text), position, variant, show-delay,
hide-delay, no-show-on-hover and no-show-on-focus, and a target property that wins over
for.
Methods
show(immediate?) / hide(immediate?)Shows or hides it, at once when immediate
immediate?: booleanTooltipComponentisVisible()Whether it is shown
booleangetText() / setText(text)The label
text: stringstring / TooltipComponentgetPosition() / setPosition(position)Where it sits
position: TooltipPositionstring / TooltipComponentsetTarget(target)Describes another element
target: HTMLElementTooltipComponentupdatePosition()Places it again against its target
TooltipComponentdestroy()Releases its target and removes it
voidelementThe tooltip
HTMLElementtargetThe element it describes
HTMLElement | nullA tooltip has no events.
Styling
.mtrl-tooltip, .mtrl-tooltip--visible { }
.mtrl-tooltip--default, .mtrl-tooltip--plain, .mtrl-tooltip--rich { }
.mtrl-tooltip--top, .mtrl-tooltip--bottom { }
.mtrl-tooltip__arrow { }
Measurements
| Attribute | Value |
|---|---|
| Container | inverse-surface, opaque, no elevation, 4dp corners, 200dp wide at most |
| Text | Body Small, inverse-on-surface |
| Padding | 4dp above and below, 8dp at the sides |
| Rich | surface-container, 12dp corners, elevation 2, 320dp wide at most; Body Medium, on-surface-variant; 12dp above and below, 16dp at the sides |
| Distance from the target | 8dp |