Tooltip
A simple Tooltip class that offers a flexible way to show a popover when hovering or clicking elements. The easiest way of assigning a tooltip to a widget is by setting tooltip, see example below.
new Button({
text : 'Hover me',
tooltip : 'Click me and you won\'t believe what happens next'
});
By default, tooltips of widgets use a singleton Tooltip instance which may be accessed from the
Widget class under the name Widget.tooltip.
This is configured according to the config object on pointer over.
To request a separate instance be created just for this widget, add newInstance : true
to the configuration:
new Button {
text : 'Hover me',
tooltip : {
html : 'Click me and you won\'t believe what happens next',
newInstance : true
}
});
You can ask for the singleton instance to display configured tips for your own DOM structure using
data-btip element attributes:
<button class="my-button" data-btip="Contextual help for my button" data-btip-scroll-action="realign">Hover me</button>
Showing async content
To load remote content into a simple tooltip, just load your data in the beforeShow listener (but ensure that the activeTarget is the same when the data arrives)
new Tooltip({
listeners : {
beforeShow : ({ source : tip }) => {
tip.html = AjaxHelper.get('someurl').then(response => response.text());
}
}
});
//<code-header>
fiddle.title = 'Tooltip';
//</code-header>
new Button({
appendTo : targetElement,
text : 'Hover me',
tooltip : 'A simple textual button tooltip'
});
new Button({
appendTo : targetElement,
text : 'Async content',
tooltip : {
listeners : {
beforeShow : ({ source: tip }) => {
tip.html = new Promise(resolve => {
setTimeout(() => resolve('Remotely fetched content!'), 2000);
});
// fetch('someurl').then(response => tip.html = 'Done!');
}
}
}
});
new Button({
appendTo : targetElement,
text : 'Tooltip with title and widgets',
tooltip : {
title : 'Tooltip containing a Grid ',
maximizable : true,
minWidth : 500,
layout : 'fit',
items : {
grid : {
type : 'grid',
border : true,
columns : [
{
text : 'Name',
field : 'name',
flex : 1
},
{
text : 'Age',
field : 'age',
flex : 1,
type : 'number'
},
{
type : 'check',
text : 'Active',
field : 'active',
widgets : [{ type : 'slidetoggle' }]
}
],
data : [
{
id : 2,
name : 'John B Adams',
age : 65,
active : true
},
{
id : 3,
name : 'John Doe',
age : 40,
active : false
},
{
id : 5,
name : 'Li Wei',
age : 35,
active : true
}
]
}
},
bbar : {
items : {
button : {
text : 'Custom button',
onClick() {
Toast.show('Hey there');
}
}
}
}
}
});Showing a tooltip for multiple targets
If you have multiple targets that should show a tooltip when hovered over, look at forSelector and getHtml.
new Tooltip({
forSelector : 'img.avatar',
getHtml : ({ source : tip, activeTarget }) => {
return activeTarget.dataset.name;
}
});
//<code-header>
fiddle.title = 'Tooltip for multiple targets';
//</code-header>
const names = ['Arnold', 'Dave', 'Emilia', 'Gloria', 'Rob'];
// Generate some avatars
names.forEach(name => DomHelper.createElement({
parent : targetElement,
tag : 'img',
class : 'avatar',
style : {
width : '5em',
'border-radius' : '50%'
},
dataset : {
name
},
src : `data/Grid/images/transparent-users/${name.toLowerCase()}.png`
}));
new Tooltip({
forSelector : 'img.avatar',
rootElement : document.body,
getHtml : ({ source : tip, activeTarget }) => {
return activeTarget.dataset.name;
}
});Configs
134
Configs
134Common
Other
Set to true to anchor tooltip to the triggering target. If set to false, the tooltip
will align to the mouse position. When set to false, it will also set anchor: false
to hide anchor arrow.
By default, a Tooltip is transient, and will hide when the user clicks or
taps outside its widget. Configure as false to make a Tooltip non-transient when user clicks outside it.
If you would like the Tooltip to stay visible when mouse leaves the Tooltip target, please see {#config-autoHide}.
By default, a Tooltip is transient, and will hide when the mouse exits the target
element. Configure as false to make a Tooltip non-transient.
Show immediately when created
The time (in milliseconds) that the Tooltip should stay visible for when it shows over its target. If the tooltip is anchored to its target, then moving the mouse during this time resets the timer so that the tooltip will remain visible.
Defaults to 0 which means the Tooltip will persist until the mouse leaves the target.
DOM element to attach tooltip to. By default, the mouse entering this element will kick off a timer (see hoverDelay) to show itself.
If the forSelector is specified, then mouse entering matching elements within the forElement
will trigger the show timer to start.
Note that when moving from matching element to matching element within the forElement, the tooltip
will remain visible for hideDelay milliseconds after exiting one element, so that rapidly
entering another matching element will not cause hide+show flicker. To prevent this behaviour configure
with hideDelay: 0.
A CSS selector which targets child elements of the forElement that should produce a tooltip when hovered over.
A method, or the name of a method called to update the tooltip's content when the cursor is moved over a target. It receives one argument containing context about the tooltip and show operation. The function should return a string, or a Promise yielding a string.
new Grid({
title : 'Client list',
appendTo : myElement,
store : myStore,
columns : myColumns,
tbar : {
items : {
text : 'Reload,
tooltip : {
// Will look in ownership hierarchy for the method
// which will be found on the grid.
getHtml : 'up.getReloadButtonTip'
}
}
},
getReloadButtonTip() {
return `Reload ${this.title}`;
}
});
| Parameter | Type | Description |
|---|---|---|
context | Object | |
context.tip | Tooltip | The tooltip instance |
context.element | HTMLElement | The Element for which the Tooltip is monitoring mouse movement |
context.activeTarget | HTMLElement | The target element that triggered the show |
context.event | Event | The raw DOM event |
The time (in milliseconds) for which the Tooltip remains visible when the mouse leaves the target.
May be configured as false to persist visible after the mouse exits the target element. Configure it
as 0 to always retrigger hoverDelay even when moving mouse inside fromElement
By default, when moving rapidly from target to target, if, when mouseovering a new target, the tip is still visible, the tooltip does not hide, it remains visible, but updates its content however it is configured to do so.
Configure hideOnDelegateChange : true to have the tip hide, and then trigger
a new show delay upon entry of a new target while still visible.
The amount of time to hover before showing
The message to show while an async tooltip is fetching its content.
Horizontal offset from mouse when anchorToTarget is false.
Direction independent, the value is internally flipped (by multiplying it with -1) for RTL.
Vertical offset from mouse when anchorToTarget is false
Predefined style to use for the tooltip. Possible values are:
'plain'- Compact rendition for simple text'rich'- Rich rendition for complex content
The supplied value will be part of the tooltip's class list, as b-{rendition}-tooltip.
When left out, the rendition to use will be determined by the content of the tooltip.
Show on hover
By default, once a tooltip is shown aligned as requested, it stays put.
Setting this to true causes the tooltip to be aligned by the mouse,
offset by [mouseOffsetX, mouseOffsetY] and
keeps the tooltip aligned to the mouse maintaining the configured offsets
as the mouse moves within its activating element.
Content
CSS
DOM
Float & align
Layout
misc
Misc
Scrolling
Properties
103
Properties
103Class hierarchy
DOM
Get/set the HTML to display. When specifying HTML, this widget's element will also have b-html added to its
classList, to allow targeted styling. To create async tooltip and show the loadingMsg, see code below:
For example:
new Tooltip({
listeners : {
beforeShow : ({ source : tip }) => {
tip.showAsyncMessage();
AjaxHelper.get('someurl').then(response => tip.html = 'Done!');
}
}
});
Other
The HTML element that triggered this Tooltip to show
Updated dynamically with the current element that the mouse is over. For use when showing a Tooltip from code which is not triggered by a pointer event so that a tooltip can be positioned.
The amount of time to hover before showing
Predefined style to use for the tooltip. Possible values are:
'plain'- Compact rendition for simple text'rich'- Rich rendition for complex content
The supplied value will be part of the tooltip's class list, as b-{rendition}-tooltip.
When left out, the rendition to use will be determined by the content of the tooltip.
Set this to true to have the shared tooltip pop up
to show the full text for elements which have overflowing text and have text-overflow:ellipsis.
The DOM event that triggered this tooltip to show
CSS
Layout
Misc
State
Widget hierarchy
Functions
76
Functions
76Other
Shows a spinner and a message to indicate an async flow is ongoing
| Parameter | Type | Description |
|---|---|---|
message | String | The message, defaults to loadingMsg |
Configuration
Events
Misc
Widget hierarchy
Events
23
Events
23Triggered before tooltip widget is shown. Return false to prevent the action.
// Adding a listener using the "on" method
tooltip.on('beforeShow', ({ source }) => {
});| Parameter | Type | Description |
|---|---|---|
source | Tooltip | The Tooltip |
Triggered when a mouseover event is detected on a potential target element. Return false to prevent the action
// Adding a listener using the "on" method
tooltip.on('pointerOver', ({ sourceThe, event }) => {
});| Parameter | Type | Description |
|---|---|---|
sourceThe | Tooltip | tooltip instance. |
event | Event | The mouseover event. |
Event handlers
23
Event handlers
23Called before tooltip widget is shown. Return false to prevent the action.
new Tooltip({
onBeforeShow({ source }) {
}
});| Parameter | Type | Description |
|---|---|---|
source | Tooltip | The Tooltip |
Called when a mouseover event is detected on a potential target element. Return false to prevent the action
new Tooltip({
onPointerOver({ sourceThe, event }) {
}
});| Parameter | Type | Description |
|---|---|---|
sourceThe | Tooltip | tooltip instance. |
event | Event | The mouseover event. |
Typedefs
7
Typedefs
7CSS variables
73
CSS variables
73| Name | Description |
|---|---|
--b-tooltip-plain-padding | PlainPadding for plain tooltips |
--b-tooltip-rich-padding | RichPadding for rich tooltips |
--b-tooltip-rich-border-radius | RichBorder radius for rich tooltips |
--b-tooltip-plain-font-size | PlainFont size for plain tooltips |
--b-tooltip-text-gap | Text gap for tooltips |
--b-tooltip-flex-direction | RichFlex direction for rich tooltips |
--b-tooltip-align-items | RichAlign items setting for rich tooltips |
--b-tooltip-content-display | RichTooltip content's display setting for rich tooltips |
--b-tooltip-plain-color | PlainText color for plain tooltips |
--b-tooltip-rich-color | RichText color for rich tooltips |
--b-tooltip-rich-background | RichBackground color for rich tooltips |
--b-tooltip-z-index | Z-index for tooltips. They should appear above popups even when those popups move to front |
--b-tooltip-plain-background | PlainBackground color for plain tooltips |