tvt-tooltip

  • public/js/tvt-tooltip.js
  • public/css/tvt-tooltip.css
  • resources/views/components/assets/tvt-tooltip.blade.php

Tooltips and popovers, no dependencies. A tooltip opens while the mouse is on its trigger or the keyboard focus is in it; a popover opens on click and closes on a click outside or Esc. On a phone both open on a tap and close on a tap outside.

No script to write: a trigger with data-tvt-tooltip names its panel, a [data-tvt-tooltip-content] styled with Tailwind classes, usually the element right after it ("next"), so a card repeated on a page needs no ids. The component that holds them includes <x-assets.tvt-tooltip />. The panel opens in the browser's top layer, above everything, so no overflow: hidden card and no modal clips it.

1. Tooltip

The panel opens above its trigger, or on the side of data-tvt-tooltip-placement: top, bottom, left, right, or auto (the first of top, bottom, right, left with room). When that side has no room it goes to the opposite one, then to any side with room. The arrow takes the panel's background and border colours, whatever classes they come from.

The trigger gets aria-describedby, so a screen reader reads the panel with it. A trigger that is not a button or a link gets tabindex="0" in the markup, to be reached with Tab.

Price not confirmed
HTML
<button type="button" data-tvt-tooltip="next" data-tvt-tooltip-placement="bottom">Check price</button>
<div data-tvt-tooltip-content role="tooltip" class="py-1 px-2 font-medium text-xs text-slate-800 bg-slate-100 border border-slate-400 rounded-md shadow-md">
    Please click here to check the price.
</div>

2. Popover

data-tvt-tooltip-trigger="click" makes it a popover: it opens and closes on the trigger's click and closes on a click outside, on Esc (the focus goes back to the trigger) or from a [data-tvt-dismiss="tooltip"] inside it. A modal toggle inside it (data-tvt-toggle="modal") closes it too, so the modal does not open under it. One opens at a time. The trigger gets aria-controls and aria-expanded.

A panel with buttons or links in it is not a tooltip: leave out role="tooltip".

Supplier:
Dealer Office AmaraTour

Cancellation terms

Free cancellation until 14 days before check-in. After that, 50% of the price is kept.

HTML
<button type="button" data-tvt-tooltip="next" data-tvt-tooltip-trigger="click" data-tvt-tooltip-placement="bottom">
    Cancellation terms
</button>
<div data-tvt-tooltip-content class="w-72 px-4 py-3 text-sm bg-white border border-slate-400 rounded-lg shadow-md">
    <p>Free cancellation until 14 days before check-in.</p>
    <button type="button" data-tvt-dismiss="tooltip">Got it</button>
</div>

3. Click on small screens, hover from a breakpoint

data-tvt-tooltip-trigger-sm / -md / -lg / -xl / -2xl give the trigger from that width up (Tailwind's breakpoints). <x-common.price> does it with its info-trigger-mobile, -desktop and -desktop-bp props: click below lg, hover from it. Resize the window to see it change. Whatever the width, a tap on a phone opens it.

-14% €145000
€125000
HTML
<button type="button" data-tvt-tooltip="next" data-tvt-tooltip-trigger="click" data-tvt-tooltip-trigger-lg="hover">
    <i class="fa-light fa-circle-info" aria-hidden="true"></i>
</button>
<div data-tvt-tooltip-content role="tooltip" class="...">...</div>
Blade
<x-common.price :show-info="true" info-trigger-mobile="click" info-trigger-desktop="hover" info-trigger-desktop-bp="lg" />

4. Inside a clipped box, near the edges

The box below is overflow: hidden and scrolls: the panels still show in full, and follow their trigger while it scrolls. Near the edges of the window a panel is shifted to stay inside it (at least 8px from the edge), and its arrow still points at the trigger. A panel never gets wider than the window.

w-160 (40rem): on a phone it is as wide as the window less 16px, and its text wraps.

Scroll this box with a panel open.

5. From a script

tvtShowTooltip(trigger) opens a trigger's panel as a popover (it stays until a click outside or Esc), tvtHideTooltip(triggerOrPanel) closes it, tvtHideAllTooltips() closes every one. The trigger fires tvt:tooltip-show and tvt:tooltip-hide (detail: { panel }, bubbling). Markup added after load works without a call; tvtInitTooltips(root) only sets its ARIA beforehand.

Target

Open any tooltip on this page.

JS
const trigger = document.querySelector('#check-price');
tvtShowTooltip(trigger);
tvtHideTooltip(trigger);
tvtHideAllTooltips();

document.addEventListener('tvt:tooltip-show', (e) => console.log(e.target, e.detail.panel));

Attributes, events, functions

On the trigger

  • data-tvt-tooltip: next (or empty), an id, or a selector
  • -trigger: hover (default) or click
  • -trigger-sm … -trigger-2xl: from that breakpoint up
  • -placement: top (default), bottom, left, right, auto
  • tabindex="0" when it is not a button or a link

The panel

  • [data-tvt-tooltip-content], role="tooltip" when it has nothing to click
  • colours, size, padding: Tailwind classes; max-w-* to keep it narrower than the window
  • [data-tvt-dismiss="tooltip"] inside it closes it
  • --tvt-tooltip-offset (10px), -edge (8px), -arrow-size (6px): e.g. [--tvt-tooltip-offset:4px]

Events, functions

  • tvt:tooltip-show, tvt:tooltip-hide { panel }, on the trigger
  • tvtShowTooltip(trigger), tvtHideTooltip(triggerOrPanel)
  • tvtHideAllTooltips()
  • tvtInitTooltips(root): ARIA of markup added after load