Skip to content

Commit 9238d30

Browse files
committed
Manage tooltips
1 parent da9610e commit 9238d30

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

app/javascript/tooltip.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
function enableVisibaleTooltips() {
2-
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function(element) {
3-
new bootstrap.Tooltip(element);
1+
const tooltipMap = new WeakMap();
2+
3+
function manageTooltips() {
4+
const selector = '[data-bs-toggle="tooltip"]';
5+
const currentElements = new Set(document.querySelectorAll(selector));
6+
7+
// Dispose tooltips for elements no longer in the DOM
8+
for (const [element, tooltipInstance] of tooltipMap.entries()) {
9+
if (!currentElements.has(element)) {
10+
tooltipInstance.dispose();
11+
tooltipMap.delete(element);
12+
}
13+
}
14+
15+
// Initialize tooltips for new elements
16+
currentElements.forEach((element) => {
17+
if (!tooltipMap.has(element)) {
18+
const instance = new bootstrap.Tooltip(element);
19+
tooltipMap.set(element, instance);
20+
}
421
});
522
}
623

7-
window.addEventListener('turbo:load', enableVisibaleTooltips);
8-
window.addEventListener('turbo:frame-load', enableVisibaleTooltips);
24+
window.addEventListener('turbo:load', manageTooltips);
25+
window.addEventListener('turbo:frame-load', manageTooltips);

0 commit comments

Comments
 (0)