- Docs
- Tooltip
Tooltip Component
A customizable tooltip component that provides contextual information when users hover or focus on an element.
The Tooltip component is a lightweight, accessible UI element used to display additional information when users hover over or focus on an element.
It leverages the
Popover
component from Flexilla for its core functionality and positioning, using flexipop
under the hood. Installation
To start, install the
@flexilla/tooltip
package, or skip this step if you’ve already installed @flexilla/flexilla
. Structure
Similar to Popover and Dropdown, the Tooltip require a trigger and a tooltip element:
-
data-tooltip-trigger
: The element that triggers the tooltip. -
data-tooltip-id
: The unique identifier that links the trigger to the tooltip element. - Tooltip Element: The element that displays the tooltip content, linked via the
data-tooltip-id
provided by the trigger.
Here’s the markup
Example
Initializing the Component
Once installed, import the Tooltip component and initialize it in your project. Since the Tooltip uses the same Popover functionality, the configuration is very similar to the Popover component.
Options
The Tooltip component inherits its core options from the Popover, with default behavior disabling repositioning on scroll and window resize. You can still customize various aspects of the Tooltip behavior as follows:
Customizing the Tooltip
You can customize the tooltip’s placement, appearance, and behavior using the options object. Here’s an example with a custom
fade-in
animation when the tooltip appears: Attributes
Attributes options
-
data-placement
: define the placement of the tooltip -
data-offset-distance
: the equivalent of setting offsetDistance in options -
data-default-state
: define a default state of the tooltip (open|close), the equivalent ofoptions.defaultState
, must be (open|close) -
data-prevent-close-outside
: adding this attribute is similar to setting the optionpreventFromCloseOutside
, no value required -
data-prevent-close-inside
: adding this attribute is similar to setting the optionpreventCloseFromInside
, no value required
data-state
Attribute
The
data-state
attribute is used to indicate the state of the tooltip element: -
open
: Indicates the tooltip is currently visible. -
close
: Indicates the tooltip is hidden. You can use this to change the tooltip Element behavior with CSS (hide, show)
aria-expanded
Attribute
The
aria-expanded
attribute indicate if the trigger is expanded or not, if expanded true then normal the data-state is open too. You use it in CSS like to animate the indicator inside the trigger or something else
Methods
The Tooltip component also exposes methods for programmatic control:
show()
Manually show the tooltip.
hide()
Manually hide the tooltip.
setShowOptions(/* options */)
Like
show()
method but here you can set the placement and offsetDistance if needed.