JSPM

@material/tooltip

8.0.0-canary.2e8c3dd2e.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 498287
  • Score
    100M100P100Q175531F
  • License MIT

The Material Components Web tooltip component.

Package Exports

  • @material/tooltip
  • @material/tooltip/index

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@material/tooltip) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Tooltip

Tooltips display informative text when users hover over, focus on, or tap an element.

Tooltip example

Content

Using tooltips

Tooltips, when activated, display a text label identifying an element, such as a description of its function. Tooltips should include only short, descriptive text and avoid restating visible UI text.

Common use cases include:

  • Displaying full text that has been truncated
  • Identifying a UI affordance
  • Describing differences between similar elements
  • Distinguishing actions with related iconography

Tooltip positioning

Tooltip positioning is based on the anchor element (the element that, on user interaction, results in showing or hiding of a tooltip). They appear directly below this anchor element and can be placed flush with either the end, center, or start of the anchor.

End, center, and start alignment of tooltip on icon button in a LTR page

*Tooltips aligned with the end, center, and start of an anchor element (in a LTR page flow).*

A threshold distance of 32px is expected to be maintained between the tooltip and the viewport edge. A valid tooltip position is calculated based on which of the position options (start, center, or end) maintain this threshold. If all possible alignment options violate the threshold, then a valid tooltip position is one that does not collide with the viewport.

A user specified position is honored only if the specified position is considered valid based on the logic outlined above.

Installing tooltips

npm install @material/tooltip

Styles

@use "@material/tooltip/styles";

JavaScript instantiation

import {MDCTooltip} from '@material/tooltip';
const tooltip = new MDCTooltip(document.querySelector('.mdc-tooltip'));

See Importing the JS component for more information on how to import JavaScript.

Making tooltips accessible

Each tooltip element placed into the DOM is expected to have a unique id. Their corresponding anchor element must be labeled with the aria-describedby attribute, establishing a relationship between the two elements.

Tooltips

Tooltip example

<div id="tooltip-id" class="mdc-tooltip" role="tooltip" aria-hidden="true">
  <div class="mdc-tooltip__surface">
    lorem ipsum dolor
  </div>
</div>

To ensure proper positioning of the tooltip, it's important that this tooltip element is an immediate child of the <body>, rather than nested underneath the anchor element or other elements.

<a aria-describedby="tooltip-id" href="www.google.com"> Link </a>

The aria-describedby attribute (which is given the id for the associated tooltip) designates an element as being the anchor element for a particular tooltip.

Other MDC components can be designated as anchor elements by adding this attribute.

MDC Button:

<button class="mdc-button mdc-button--outlined" aria-describedby="tooltip-id">
  <div class="mdc-button__ripple"></div>
  <span class="mdc-button__label">Button</span>
</button>

MDC Icon Button:

<button class="mdc-icon-button material-icons" aria-describedby="tooltip-id">favorite</button>

API

Sass mixins

Access to theme mixins require importing the tooltip's theme style module.

@use "@material/tooltip";
Mixin Description
fill-color($color) Sets the fill color of the tooltip.
label-ink-color($color) Sets the color of the tooltip's label text.
shape-radius($radius, $rtl-reflexive) Sets the rounded shape to tooltip surface with given radius size. Set $rtl-reflexive to true to flip radius values in RTL context, defaults to false.

MDCTooltip Methods

Method Signature Description
setTooltipPosition(pos: Position) => void Specify how the tooltip should be aligned with the anchor element. See tooltip positioning section for more information.
setAnchorBoundaryType(type: AnchorBoundaryType) => void Specify whether the anchor element is bounded (element has an identifiable boundary such as a button) or unbounded (element does not have a visually declared boundary such as a text link). Tooltips are placed closer to bounded anchor elements compared to unbounded anchor elements. If no type is specified, defaults to bounded

Usage Within Frameworks

If you are using a JavaScript framework, such as React or Angular, you can create a Tooltip for your framework. Depending on your needs, you can use the Simple Approach: Wrapping MDC Web Vanilla Components, or the Advanced Approach: Using Foundations and Adapters. Please follow the instructions here.

See MDCTooltipAdapter and MDCTooltipFoundation for up-to-date code documentation of tooltip foundation APIs.