- Plugins
- Tooltip
Plugins
Tailwind CSS Tooltip Plugin API
Headless, unstyled Tooltip plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Tooltip plugin via npm, or skip this step if you are already using Preline UI as a package.
npm i @floating-ui/dom @preline/tooltip
Example
Hover over the buttons below to see the four tooltip directions: top, right, bottom, and left. Remove the [--placement:*] option to enable automatic positioning.
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div class="hs-tooltip inline-block">
<button type="button" class="hs-tooltip-toggle">
Hover me
<span class="hs-tooltip-content hs-tooltip-shown:opacity-100 hs-tooltip-shown:visible opacity-0 transition-opacity inline-block absolute invisible z-10 py-1 px-2 bg-gray-900 text-white" role="tooltip">
Tooltip on top
</span>
</button>
</div>
Methods
The HSTooltip object is contained within the global window object
Force show tooltip (public method).
const tooltip = new HSTooltip(document.querySelector('#tooltip'));
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
tooltip.show();
});
Force show tooltip (static method).
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
HSTooltip.show(document.querySelector('#tooltip'));
});
Open item (mixed).
const { element } = HSTooltip.getInstance('#tooltip', true);
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
element.show();
});
Events
An example where a function is run every time a tooltip shows.
const el = HSTooltip.getInstance('#tooltip');
el.on('show', (instance) => {...});
Demo examples
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.