Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 751baab

Browse files
authored
fix: only destroy tippy instances that exists (#646)
1 parent e7adba0 commit 751baab

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

resources/assets/js/tippy.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,38 @@ const tooltipSettings = {
1818
},
1919
};
2020

21-
tippy("[data-tippy-content]", tooltipSettings);
21+
const initTippy = (parentEl = document.body) => {
22+
tippy(parentEl.querySelectorAll("[data-tippy-content]"), tooltipSettings);
2223

23-
tippy("[data-tippy-hover]", {
24-
...tooltipSettings,
25-
touch: "hold",
26-
trigger: "mouseenter",
27-
content: (reference) => reference.dataset.tippyHover,
28-
});
24+
tippy(parentEl.querySelectorAll("[data-tippy-hover]"), {
25+
...tooltipSettings,
26+
touch: "hold",
27+
trigger: "mouseenter",
28+
content: (reference) => reference.dataset.tippyHover,
29+
});
30+
};
31+
32+
const destroyTippy = (parentEl = document.body) => {
33+
parentEl
34+
.querySelectorAll("[data-tippy-content], [data-tippy-hover]")
35+
.forEach((el) => {
36+
if (!el._tippy) {
37+
console.error(
38+
"Tippy tooltip instance not found. Ensure all tippy instances are properly initialized.",
39+
el
40+
);
41+
return;
42+
}
43+
44+
el._tippy.destroy();
45+
});
46+
};
47+
48+
initTippy();
49+
50+
window.initTippy = initTippy;
51+
52+
window.destroyTippy = destroyTippy;
2953

3054
document.addEventListener("scroll", () =>
3155
visibleTooltips.forEach((instance) => instance.hide(0))
@@ -44,25 +68,13 @@ window.initClipboard = () => {
4468
};
4569

4670
if (typeof Livewire !== "undefined") {
47-
Livewire.hook("message.received", (message, component) => {
48-
component.el
49-
.querySelectorAll("[data-tippy-content]")
50-
.forEach((el) => el._tippy.destroy());
51-
});
71+
Livewire.hook("message.received", (message, component) =>
72+
destroyTippy(component.el)
73+
);
5274

53-
Livewire.hook("message.processed", (message, component) => {
54-
tippy(component.el.querySelectorAll("[data-tippy-content]"), {
55-
trigger: "mouseenter focus",
56-
duration: 0,
57-
});
58-
59-
tippy(component.el.querySelectorAll("[data-tippy-hover]"), {
60-
touch: "hold",
61-
trigger: "mouseenter",
62-
content: (reference) => reference.dataset.tippyHover,
63-
duration: 0,
64-
});
65-
});
75+
Livewire.hook("message.processed", (message, component) =>
76+
initTippy(component.el)
77+
);
6678
}
6779

6880
window.tippy = tippy;

0 commit comments

Comments
 (0)