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());
        }
    }
});

Tooltip
//<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;
    }
});

Tooltip for multiple targets
//<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

Common

listenersEvents

Other

allowOver: Boolean= false

Keep the tooltip open if user moves the mouse over it.

If this is not explicitly configured as false, then this is automatically set when there are any visible, interactive child items added such as tools, or items which are interactive such as buttons or input fields.

anchorToTarget: Boolean= true

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.

autoClose: Boolean= true

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}.

autoHide: Boolean= true

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.

autoShow: Boolean= false

Show immediately when created

dismissDelay: Number= 0

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.

forElement: HTMLElement

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.

forSelector: String

A CSS selector which targets child elements of the forElement that should produce a tooltip when hovered over.

getHtml: function | String

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}`;
    }
});
ParameterTypeDescription
contextObject
context.tipTooltip

The tooltip instance

context.elementHTMLElement

The Element for which the Tooltip is monitoring mouse movement

context.activeTargetHTMLElement

The target element that triggered the show

context.eventEvent

The raw DOM event

Returns: String | Promise
hideDelay: Number | Boolean= 500

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

hideOnDelegateChange: Boolean= false

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

loadingMsg: String= Loading...

The message to show while an async tooltip is fetching its content.

mouseOffsetX: Number= 15

Horizontal offset from mouse when anchorToTarget is false.

Direction independent, the value is internally flipped (by multiplying it with -1) for RTL.

mouseOffsetY: Number= 15

Vertical offset from mouse when anchorToTarget is false

rendition: plain | rich | String | nullAlso a property

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.

showOnHover: Boolean= false

Show on hover

trackMouse: Boolean= false

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.

columnWidget
defaultFocusContainer
drawerPanel
labelPositionContainer
modalPopup
resizableResizable
rtlRTL
spanWidget

Accessibility

ariaLabelWidget
keyMapKeyMap

Content

bbarPanel
defaultsContainer
footerPanel
headerPanel
itemsContainer
lazyItemsContainer
namedItemsContainer
stripsPanel
tbarPanel
textContentContainer
toolsPanel

CSS

bodyClsPanel
borderContainer
clsWidget
colorWidget
htmlClsWidget
itemClsContainer
styleWidget
uiPanel

DOM

adoptWidget
appendToWidget
contentWidget
datasetWidget
htmlWidget
idWidget
tagWidget

Float & align

alignWidget
anchorWidget
centeredWidget
floatingWidget
xWidget
yWidget

Layout

alignSelfWidget
dockWidget
flexWidget
heightWidget
hiddenWidget
hideWhenEmptyContainer
layoutContainer
layoutStyleContainer
marginWidget
maxHeightWidget
maxWidthWidget
minHeightWidget
minWidthWidget
textAlignWidget
weightWidget
widthWidget

misc

tabBarItemsContainer

Misc

dataFieldWidget
disabledWidget
iconPanel
localeClassLocalizable
localizableLocalizable
maskedWidget
ownerWidget
readOnlyWidget
refWidget
rippleWidget
tabWidget
titlePanel
tooltipWidget

Record

recordContainer

Scrolling

State

stateIdState

Properties

103

Class hierarchy

isTooltip: Boolean= truereadonly
Identifies an object as an instance of Tooltip class, or subclass thereof.
isTooltip: Boolean= truereadonlystatic
Identifies an object as an instance of Tooltip class, or subclass thereof.
isContainerContainer
isDelayableDelayable
isEventsEvents
isKeyMapKeyMap
isLocalizableLocalizable
isPanelPanel
isPopupPopup
isResizableResizable
isStateState
isToolableToolable
isWidgetWidget

DOM

html: String

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!');
        }
    }
});
appendToWidget
contentWidget
datasetWidget
elementWidget
idWidget
styleWidget

Other

activeTarget: HTMLElementreadonly

The HTML element that triggered this Tooltip to show

currentOverElement: HTMLElementreadonlystatic

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

rendition: plain | rich | String | nullAlso a config

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.

showOverflow: Booleanstatic

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.

triggeredByEvent: Eventreadonly

The DOM event that triggered this tooltip to show

$namestaticWidget
columnWidget
firstItemContainer
hasChangesContainer
isValidContainer
itemsContainer
labelPositionContainer
lastItemContainer
resizableResizable
rtlRTL
spanWidget
toolsPanel
typestaticWidget
valuesContainer

Accessibility

keyMapKeyMap

Content

bbarPanel
tbarPanel

CSS

clsWidget

Float & align

xWidget
yWidget

Layout

alignSelfWidget
flexWidget
heightWidget
layoutContainer
layoutStyleContainer
marginWidget
maxHeightWidget
maxWidthWidget
minHeightWidget
minWidthWidget
widthWidget

Lifecycle

configBase

Misc

cellInfoWidget
disabledWidget
localeHelperLocalizable
localeManagerLocalizable
readOnlyWidget
refWidget
tabWidget
titlePanel
tooltipWidget

Record

recordContainer

State

stateState

Visibility

hiddenWidget
isVisibleWidget

Widget hierarchy

ownerWidget
parentWidget
widgetMapContainer

Functions

76

Other

Shows a spinner and a message to indicate an async flow is ongoing

ParameterTypeDescription
messageString

The message, defaults to loadingMsg

addContainer
closePopup
composeWidget
createOnFrameDelayable
disableWidget
enableWidget
focusWidget
getAtContainer
getWidgetByIdContainer
insertContainer
LstaticLocalizable
maskWidget
onEvents
recomposeWidget
relayAllEvents
removeContainer
removeAllContainer
resetValuesContainer
setValuesContainer
triggerEvents
unEvents
unmaskWidget

Configuration

applyDefaultsstaticBase

Events

Float & align

alignToWidget
setXYWidget
showByWidget
toFrontWidget

Lifecycle

createstaticWidget
destroystaticBase
initClassstaticWidget

Misc

attachTooltipstaticWidget
fromElementstaticWidget
fromSelectorstaticWidget
getByIdstaticWidget
isOfTypeNamestaticBase
mixinstaticBase
optionalLstaticLocalizable

State

Visibility

hideWidget
showWidget

Widget hierarchy

closestWidget
containsWidget
ownsWidget
queryWidget
queryAllWidget
upWidget

Events

23

Triggered before tooltip widget is shown. Return false to prevent the action.

// Adding a listener using the "on" method
tooltip.on('beforeShow', ({ source }) => {

});
ParameterTypeDescription
sourceTooltip

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 }) => {

});
ParameterTypeDescription
sourceTheTooltip

tooltip instance.

eventEvent

The mouseover event.

catchAllEvents
destroyEvents
expandPanel
focusInWidget
focusOutWidget
hideWidget
paintWidget
readOnlyWidget
recomposeWidget
resizeWidget
showWidget

Event handlers

23

Called before tooltip widget is shown. Return false to prevent the action.

new Tooltip({
    onBeforeShow({ source }) {

    }
});
ParameterTypeDescription
sourceTooltip

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 }) {

    }
});
ParameterTypeDescription
sourceTheTooltip

tooltip instance.

eventEvent

The mouseover event.

onDestroyEvents
onFocusInWidget
onHideWidget
onPaintWidget
onResizeWidget
onShowWidget

Typedefs

7

CSS variables

73
NameDescription
--b-tooltip-plain-paddingPlainPadding for plain tooltips
--b-tooltip-rich-paddingRichPadding for rich tooltips
--b-tooltip-rich-border-radiusRichBorder radius for rich tooltips
--b-tooltip-plain-font-sizePlainFont size for plain tooltips
--b-tooltip-text-gapText gap for tooltips
--b-tooltip-flex-directionRichFlex direction for rich tooltips
--b-tooltip-align-itemsRichAlign items setting for rich tooltips
--b-tooltip-content-displayRichTooltip content's display setting for rich tooltips
--b-tooltip-plain-colorPlainText color for plain tooltips
--b-tooltip-rich-colorRichText color for rich tooltips
--b-tooltip-rich-backgroundRichBackground color for rich tooltips
--b-tooltip-z-indexZ-index for tooltips. They should appear above popups even when those popups move to front
--b-tooltip-plain-backgroundPlainBackground color for plain tooltips