Skip to content

Commit 5c4d8e8

Browse files
committed
Move initialization of tooltips to app/javascript
With this new method, Tooltips are correctly initialized and potential loading errors/race conditions are prevented.
1 parent 3004560 commit 5c4d8e8

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

app/assets/javascripts/base.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ $(document).on('turbo-migration:load', function () {
5555
});
5656
}
5757

58-
// Enable all tooltips
59-
$('[data-bs-toggle="tooltip"]').tooltip();
60-
6158
// Ensure that the tab button will be in active (selected) state for each page that has 'option' query (e.g. groups or messages page)
6259
$('#' + $('#option').val()).addClass('selected');
6360
});

app/assets/javascripts/bootstrap.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/javascript/tooltips.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let tooltipList = [];
2+
3+
export function initializeTooltips() {
4+
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
5+
tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
6+
}
7+
8+
export function destroyTooltips() {
9+
tooltipList.map(tooltip => tooltip.dispose());
10+
tooltipList = [];
11+
}

app/javascript/turbo-migration.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { initializeTooltips, destroyTooltips } from './tooltips';
2+
13
// `turbo:load` is dispatched earlier than the previous `turbolinks:load` event.
24
// This is causing issues for our migration, since some assets are not fully loaded
35
// when the event is dispatched. To ensure that the DOM content is fully rendered,
@@ -46,10 +48,16 @@ function forwardTurboLoad(event) {
4648
requestAnimationFrame(() => {
4749
const delayedEvent = new CustomEvent('turbo-migration:load', { detail: { ...event.detail } });
4850
document.dispatchEvent(delayedEvent);
51+
52+
initializeTooltips();
4953
});
5054
}
5155

5256
const flushQueue = (queue) => {
5357
queue.forEach(forwardTurboLoad);
5458
queue.length = 0;
5559
};
60+
61+
document.addEventListener('turbo:visit', (event) => {
62+
destroyTooltips();
63+
})

0 commit comments

Comments
 (0)