Tooltip. Overview

Preview.

What does the component do?

The tooltip displays additional information about an element without interrupting the flow.

When should the component be used?

  • To explain icons, abbreviations or technical terms.
  • For supplementary notes on controls.

Similar component.

Popover
Usage

Rules.

  • The text is short, precise and understandable.
  • Use text only. No interactive elements such as buttons or links.
  • Essential content and mandatory information do not belong in a tooltip.
  • The tooltip opens on hover, and on touch devices by long press. Keyboard users must also be able to access the information.

Specification.

Examples Default.
Attribute Usage.
Long Content.
Below.
Above.
On The Left.
On The Right.
Copy HTML to clipboard.
Implementation

Web Components.

The web component is in the package @sbb-esta/lyne-elements.

Storybook with live examples Source code on GitHub

Documentation.

The <sbb-tooltip> component displays contextual information related to an element.

The standard way to use it is through the trigger property, referencing the element which activates the tooltip.

<sbb-button id="tooltip-trigger">Button</sbb-button>
<sbb-tooltip trigger="tooltip-trigger">Tooltip message</sbb-tooltip>
Attribute usage.

It's also possible to use the component by setting the sbb-tooltip attribute on the trigger element.
The attribute's value is the tooltip's rendered message
(Note: it does not work if used in a Shadow DOM).

<sbb-button sbb-tooltip="Tooltip message">Button</sbb-button>

If the component is used this way, it's also possible to set the open and close delays
using the sbb-tooltip-open-delay and the sbb-tooltip-close-delay attributes.

<sbb-button
  sbb-tooltip="Tooltip message"
  sbb-tooltip-open-delay="250"
  sbb-tooltip-close-delay="250"
>
  Button
</sbb-button>

To disable the tooltip, you can set the sbb-tooltip-disabled attribute on the trigger element.
When disabled, the tooltip will not open.

<sbb-button sbb-tooltip="Tooltip message" sbb-tooltip-disabled>Button</sbb-button>
Interactions.

The tooltip opens when the user hovers the trigger element and closes on mouse leave. A delay can be optionally set, both on open and close actions.

On touch devices, the tooltip opens on long press and closes automatically after a longPressCloseDelay (default: 1500 ms).

Positioning.

The tooltip uses the CSS anchor positioning to anchor itself to the trigger element.
Specifically, it uses the "position-area"
and "position-try-fallbacks" CSS properties to define where the tooltip should be positioned.

By default, the tooltip appears above the trigger element and, if there is insufficient space, it automatically chooses the best available position.

You can control the positioning of the tooltip with the positions property.
The first entry is used as primary position, the following entries as fallbacks.

The attribute accepts a comma-separated list, the property accepts either a comma-separated string or an array.

<sbb-tooltip trigger="trigger-id" positions="block-end, block-start, inline-end">
  Tooltip
</sbb-tooltip>

Alternatively, you can override the --sbb-overlay-position-area and --sbb-overlay-position-try-fallbacks CSS variables.
These are used if the positions property is not set.

// Primary position
--sbb-overlay-position-area: block-start;

// Fallback positions. The first one that fits will be used.
--sbb-overlay-position-try-fallbacks:
  block-start span-inline-end, block-start span-inline-start, block-end, block-end span-inline-end,
  block-end span-inline-start;

ⓘ The CSS anchor positioning feature is not yet fully supported by all browsers.

Therefore, a polyfill is used which limits the available positions to the following:

| Logical positions | Physical positions |

| ------------------------------- | ------------------- |

| block-start | top |

| block-end | bottom |

| inline-start | left |

| inline-end | right |

| block-start span-inline-start | top span-left |

| block-start span-inline-end | top span-right |

| block-end span-inline-start | bottom span-left |

| block-end span-inline-end | bottom span-right |

| inline-start span-block-start | left span-top |

| inline-start span-block-end | left span-bottom |

| inline-end span-block-start | right span-top |

| inline-end span-block-end | right span-bottom |

Attribute usage.

When using the sbb-tooltip attribute, you can configure the position by setting the sbb-tooltip-positions attribute on the trigger element.
The value of the attribute is a comma-separated list of positions and is applied to the positions property of the created tooltip.

<sbb-button
  sbb-tooltip="Tooltip message"
  sbb-tooltip-positions="block-end, block-start, inline-end"
>
  Button
</sbb-button>
Configuration.

The open and close delays can be configured via global configuration. These values will be used as default, unless explicitly set on the element.

import { mergeConfig } from '@sbb-esta/lyne-elements/core.js';

mergeConfig({
  tooltip: {
    openDelay: 0, // Delay before the tooltip opens (in ms)
    closeDelay: 0, // Delay before the tooltip closes (in ms)
    longPressCloseDelay: 1500, // Duration before the tooltip auto-closes after a long press (in ms)
  },
});
Accessibility.

The <sbb-tooltip> adds an ariaDescribedby reference to an element containing the tooltip's message.
If the tooltip is disabled, the reference is removed.

Avoid interactions that exclusively show a tooltip with pointer events like click and mouseenter.
Always ensure that keyboard users can perform the same set of actions available to mouse and touch users.

Angular.

This component is also available for Angular in the package @sbb-esta/lyne-angular.

Angular overview Angular API Angular examples
API

class: SbbTooltipElement, sbb-tooltip.

Properties.

Name Attribute Privacy Type Default Description
closeDelay close-delay public number 0 Close the tooltip after a given delay in milliseconds. Global configuration is used as default, if not set.
disabled disabled public boolean false Whether the component is disabled.
isOpen - public boolean Whether the element is open.
longPressCloseDelay long-press-close-delay public number 1500 Automatically close the tooltip after it has been open by long press. Global configuration is used as default, if not set.
openDelay open-delay public number 0 Open the tooltip after a given delay in milliseconds. Global configuration is used as default, if not set.
positions positions public string[] [] The positions of the tooltip, relative to the trigger. Provide either a comma-separated list or an array of positions. The first entry is the primary position (--sbb-overlay-position-area), the following ones are the fallback positions (--sbb-overlay-position-try-fallbacks). If not set, the default positions of the trigger (tooltipPositions) or the CSS variables are used.
trigger trigger public HTMLElement | null null The element that will trigger the tooltip overlay. For attribute usage, provide an id reference.

Methods.

Name Privacy Description Parameters Return Inherited From
close public Closes the component. void SbbOpenCloseBaseElement
escapeStrategy public The method which is called on escape key press. Defaults to calling close() void SbbOpenCloseBaseElement
open public Opens the component. void SbbOpenCloseBaseElement

Events.

Name Type Description Inherited From
beforeclose Event Emits whenever the component begins the closing transition. Can be canceled. SbbOpenCloseBaseElement
beforeopen Event Emits whenever the component starts the opening transition. Can be canceled. SbbOpenCloseBaseElement
close Event Emits whenever the component is closed. SbbOpenCloseBaseElement
open Event Emits whenever the component is opened. SbbOpenCloseBaseElement

CSS Properties.

Name Default Description
--sbb-overlay-position-area block-end The primary position for the tooltip.
--sbb-overlay-position-try-fallbacks block-end span-inline-end, block-end span-inline-start, block-start, block-start span-inline-end, block-start span-inline-start The list of fallback positions, separated by ',', for the tooltip
--sbb-tooltip-z-index var(--sbb-overlay-default-z-index) To specify a custom stack order, the z-index can be overridden by defining this CSS variable. The default z-index of the component is set to var(--sbb-overlay-default-z-index) with a value of 1000.

Slots.

Name Description
Use the unnamed slot to add the text into the tooltip.