14.0.0 • Published 5 months ago

@material/tooltip v14.0.0

Weekly downloads
8,298
License
MIT
Repository
github
Last release
5 months ago

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

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

Styes

@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";
MixinDescription
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 SignatureDescription
setTooltipPosition(pos: Position) => voidSpecify how the tooltip should be aligned with the anchor element. See tooltip positioning section for more information.
setAnchorBoundaryType(type: AnchorBoundaryType) => voidSpecify 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.

14.0.0

2 years ago

13.0.0

3 years ago

12.0.0

3 years ago

11.0.0

3 years ago

10.0.0

3 years ago

9.0.0

3 years ago

8.0.0

3 years ago