Tooltip
<mo-tooltip> | MOTooltip
Tooltips display additional information based on a specific action.
A tooltip’s target is its first child element, so you should only wrap one element inside of the tooltip. If you need the tooltip to show up for multiple elements, nest them inside a container first.
Tooltips use display: contents
so they won’t interfere with how elements are positioned in a
flex or grid layout.
<mo-tooltip content="This is a tooltip"> <mo-button>Hover Me</mo-button> </mo-tooltip>
import { MOButton, MOTooltip } from '@metsooutotec/modes-web-components/dist/react'; const App = () => ( <MOTooltip content="This is a tooltip"> <MOButton>Hover Me</MOButton> </MOTooltip> );
Examples
Placement
Use the placement
attribute to set the preferred placement of the tooltip.
<div class="tooltip-placement-example"> <div class="tooltip-placement-example-row"> <mo-tooltip content="top-start" placement="top-start"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="top" placement="top"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="top-end" placement="top-end"> <mo-button></mo-button> </mo-tooltip> </div> <div class="tooltip-placement-example-row"> <mo-tooltip content="left-start" placement="left-start"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="right-start" placement="right-start"> <mo-button></mo-button> </mo-tooltip> </div> <div class="tooltip-placement-example-row"> <mo-tooltip content="left" placement="left"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="right" placement="right"> <mo-button></mo-button> </mo-tooltip> </div> <div class="tooltip-placement-example-row"> <mo-tooltip content="left-end" placement="left-end"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="right-end" placement="right-end"> <mo-button></mo-button> </mo-tooltip> </div> <div class="tooltip-placement-example-row"> <mo-tooltip content="bottom-start" placement="bottom-start"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="bottom" placement="bottom"> <mo-button></mo-button> </mo-tooltip> <mo-tooltip content="bottom-end" placement="bottom-end"> <mo-button></mo-button> </mo-tooltip> </div> </div> <style> .tooltip-placement-example { width: 250px; } .tooltip-placement-example-row:after { content: ''; display: table; clear: both; } .tooltip-placement-example mo-button { float: left; width: 2.5rem; margin-right: 0.25rem; margin-bottom: 0.25rem; } .tooltip-placement-example-row:nth-child(1) mo-tooltip:first-child mo-button, .tooltip-placement-example-row:nth-child(5) mo-tooltip:first-child mo-button { margin-left: calc(40px + 0.25rem); } .tooltip-placement-example-row:nth-child(2) mo-tooltip:nth-child(2) mo-button, .tooltip-placement-example-row:nth-child(3) mo-tooltip:nth-child(2) mo-button, .tooltip-placement-example-row:nth-child(4) mo-tooltip:nth-child(2) mo-button { margin-left: calc((40px * 3) + (0.25rem * 3)); } </style>
import { MOButton, MOTooltip } from '@metsooutotec/modes-web-components/dist/react'; const css = ` .tooltip-placement-example { width: 250px; } .tooltip-placement-example-row:after { content: ''; display: table; clear: both; } .tooltip-placement-example mo-button { float: left; width: 2.5rem; margin-right: 0.25rem; margin-bottom: 0.25rem; } .tooltip-placement-example-row:nth-child(1) mo-tooltip:first-child mo-button, .tooltip-placement-example-row:nth-child(5) mo-tooltip:first-child mo-button { margin-left: calc(40px + 0.25rem); } .tooltip-placement-example-row:nth-child(2) mo-tooltip:nth-child(2) mo-button, .tooltip-placement-example-row:nth-child(3) mo-tooltip:nth-child(2) mo-button, .tooltip-placement-example-row:nth-child(4) mo-tooltip:nth-child(2) mo-button { margin-left: calc((40px * 3) + (0.25rem * 3)); } `; const App = () => ( <> <div className="tooltip-placement-example"> <div className="tooltip-placement-example-row"> <MOTooltip content="top-start" placement="top-start"> <MOButton /> </MOTooltip> <MOTooltip content="top" placement="top"> <MOButton /> </MOTooltip> <MOTooltip content="top-end" placement="top-end"> <MOButton /> </MOTooltip> </div> <div className="tooltip-placement-example-row"> <MOTooltip content="left-start" placement="left-start"> <MOButton /> </MOTooltip> <MOTooltip content="right-start" placement="right-start"> <MOButton /> </MOTooltip> </div> <div className="tooltip-placement-example-row"> <MOTooltip content="left" placement="left"> <MOButton /> </MOTooltip> <MOTooltip content="right" placement="right"> <MOButton /> </MOTooltip> </div> <div className="tooltip-placement-example-row"> <MOTooltip content="left-end" placement="left-end"> <MOButton /> </MOTooltip> <MOTooltip content="right-end" placement="right-end"> <MOButton /> </MOTooltip> </div> <div className="tooltip-placement-example-row"> <MOTooltip content="bottom-start" placement="bottom-start"> <MOButton /> </MOTooltip> <MOTooltip content="bottom" placement="bottom"> <MOButton /> </MOTooltip> <MOTooltip content="bottom-end" placement="bottom-end"> <MOButton /> </MOTooltip> </div> </div> <style>{css}</style> </> );
Click trigger
Set the trigger
attribute to click
to toggle the tooltip on click instead of
hover.
<mo-tooltip content="Click again to dismiss" trigger="click"> <mo-button>Click to Toggle</mo-button> </mo-tooltip>
import { MOButton, MOTooltip } from '@metsooutotec/modes-web-components/dist/react'; const App = () => ( <MOTooltip content="Click again to dismiss" trigger="click"> <MOButton>Click to Toggle</MOButton> </MOTooltip> );
Manual trigger
Tooltips can be controller programmatically by setting the trigger
attribute to
manual
. Use the open
attribute to control when the tooltip is shown.
<mo-button style="margin-right: 4rem;">Toggle Manually</mo-button> <mo-tooltip content="This is an avatar" trigger="manual" class="manual-tooltip"> <mo-avatar></mo-avatar> </mo-tooltip> <script> const tooltip = document.querySelector('.manual-tooltip'); const toggle = tooltip.previousElementSibling; toggle.addEventListener('click', () => (tooltip.open = !tooltip.open)); </script>
import { useState } from 'react'; import { MOAvatar, MOButton, MOTooltip } from '@metsooutotec/modes-web-components/dist/react'; const App = () => { const [open, setOpen] = useState(false); return ( <> <MOButton style={{ marginRight: '4rem' }} onClick={() => setOpen(!open)}> Toggle Manually </MOButton> <MOTooltip open={open} content="This is an avatar" trigger="manual"> <MOAvatar /> </MOTooltip> </> ); };
HTML in tooltips
Use the content
slot to create tooltips with HTML content. Tooltips are designed only for text
and presentational elements. Avoid placing interactive content, such as buttons, links, and form controls,
in a tooltip.
<mo-tooltip> <div slot="content">I'm not <strong>just</strong> a tooltip, I'm a <em>tooltip</em> with HTML!</div> <mo-button>Hover me</mo-button> </mo-tooltip>
import { MOButton, MOTooltip } from '@metsooutotec/modes-web-components/dist/react'; const App = () => ( <MOTooltip content="This is a tooltip"> <div slot="content"> I'm not <strong>just</strong> a tooltip, I'm a <em>tooltip</em> with HTML! </div> <MOButton>Hover Me</MOButton> </MOTooltip> );
Hoisting
Tooltips will be clipped if they’re inside a container that has overflow: auto|hidden|scroll
.
The hoist
attribute forces the tooltip to use a fixed positioning strategy, allowing it to
break out of the container. In this case, the tooltip will be positioned relative to its containing block,
which is usually the viewport unless an ancestor uses a transform
, perspective
, or
filter
.
Refer to this page
for more details.
<div class="tooltip-hoist"> <mo-tooltip content="This is a tooltip"> <mo-button>No Hoist</mo-button> </mo-tooltip> <mo-tooltip content="This is a tooltip" hoist> <mo-button>Hoist</mo-button> </mo-tooltip> </div> <style> .tooltip-hoist { border: solid 2px var(--mo-panel-border-color); overflow: hidden; padding: var(--mo-spacing-medium); position: relative; } </style>
import { MOButton, MOTooltip } from '@metsooutotec/modes-web-components/dist/react'; const css = ` .tooltip-hoist { border: solid 2px var(--mo-panel-border-color); overflow: hidden; padding: var(--mo-spacing-medium); position: relative; } `; const App = () => ( <> <div class="tooltip-hoist"> <MOTooltip content="This is a tooltip"> <MOButton>No Hoist</MOButton> </MOTooltip> <MOTooltip content="This is a tooltip" hoist> <MOButton>Hoist</MOButton> </MOTooltip> </div> <style>{css}</style> </> );
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component using a bundler:
import '@metsooutotec/modes-web-components/dist/components/tooltip/tooltip.js';
To import this component as a React component:
import MOTooltip from '@metsooutotec/modes-web-components/dist/react/tooltip/';
To import this component using a script tag:
<script type="module" src="https://modes-web.metso.com/dist/components/cdn/components/tooltip/tooltip.js"></script>
Slots
Name | Description |
---|---|
(default) | The tooltip’s target element. Avoid slotting in more than one element, as subsequent ones will be ignored. |
content
|
The content to render in the tooltip. Alternatively, you can use the content attribute.
|
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
content
|
The tooltip’s content. If you need to display HTML, use the content slot instead.
|
string
|
''
|
|
placement
|
The preferred placement of the tooltip. Note that the actual placement may vary as needed to keep the tooltip inside of the viewport. |
'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end' | 'bottom' |
'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end'
|
'top'
|
|
disabled
|
Disables the tooltip so it won’t show when triggered. |
|
boolean
|
false
|
distance
|
The distance in pixels from which to offset the tooltip away from its target. |
number
|
8
|
|
open
|
Indicates whether or not the tooltip is open. You can use this in lieu of the show/hide methods. |
|
boolean
|
false
|
skidding
|
The distance in pixels from which to offset the tooltip along its target. |
number
|
0
|
|
showDelay
|
The delay in milliseconds before the tooltip is shown after triggered. |
number | undefined
|
- | |
hideDelay
|
The delay in milliseconds before the tooltip is hidden after triggered. |
number | undefined
|
- | |
trigger
|
Controls how the tooltip is activated. Possible options include click ,
hover , focus , and manual . Multiple options can be passed by
separating them with a space. When manual is used, the tooltip must be activated programmatically.
|
string
|
'hover focus'
|
|
hoist
|
Enable this option to prevent the tooltip from being clipped when the component is placed inside a
container with
overflow: auto|hidden|scroll . Hoisting uses a fixed positioning strategy that works in
many, but not all, scenarios.
|
boolean
|
false
|
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
Name | React Event | Description | Event Detail |
---|---|---|---|
mo-show |
onMoShow |
Emitted when the tooltip begins to show. | - |
mo-after-show |
onMoAfterShow |
Emitted after the tooltip has shown and all animations are complete. | - |
mo-hide |
onMoHide |
Emitted when the tooltip begins to hide. | - |
mo-after-hide |
onMoAfterHide |
Emitted after the tooltip has hidden and all animations are complete. | - |
Learn more about events.
Methods
Name | Description | Arguments |
---|---|---|
show() |
Shows the tooltip. | - |
hide() |
Hides the tooltip | - |
Learn more about methods.
Custom Properties
Name | Description | Default |
---|---|---|
--max-width |
The maximum width of the tooltip before its content will wrap. | |
--hide-delay |
The amount of time to wait before hiding the tooltip when hovering. | |
--show-delay |
The amount of time to wait before showing the tooltip when hovering. |
Learn more about customizing CSS custom properties.
Parts
Name | Description |
---|---|
base |
The component’s base wrapper, an <mo-popup> element. |
base__popup |
The popup’s exported popup part. Use this to target the tooltip’s popup container.
|
base__arrow |
The popup’s exported arrow part. Use this to target the tooltip’s arrow. |
body |
The tooltip’s body where its content is rendered. |
Learn more about customizing CSS parts.
Animations
Name | Description |
---|---|
tooltip.show |
The animation to use when showing the tooltip. |
tooltip.hide |
The animation to use when hiding the tooltip. |
Learn more about customizing animations.
Dependencies
This component automatically imports the following dependencies.