generated from greenelab/lab-website-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip.js
41 lines (37 loc) · 1.13 KB
/
tooltip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
shows a popup of text on hover/focus of any element with the data-tooltip
attribute.
*/
{
const onLoad = () => {
// make sure Tippy library available
if (typeof tippy === "undefined") return;
// get elements with non-empty tooltips
const elements = [...document.querySelectorAll("[data-tooltip]")].filter(
(element) => element.dataset.tooltip.trim() && !element._tippy
);
// add tooltip to elements
tippy(elements, {
content: (element) => element.dataset.tooltip.trim(),
delay: [200, 0],
offset: [0, 20],
allowHTML: true,
interactive: true,
appendTo: () => document.body,
aria: {
content: "describedby",
expanded: null,
},
onShow: ({ reference, popper }) => {
const dark = reference.closest("[data-dark]")?.dataset.dark;
if (dark === "false") popper.dataset.dark = true;
if (dark === "true") popper.dataset.dark = false;
},
// onHide: () => false, // debug
});
};
// after page loads
window.addEventListener("load", onLoad);
// after tags load
window.addEventListener("tagsfetched", onLoad);
}