From 53424999dc3dd5cb48d939e82b5188d36a0800b4 Mon Sep 17 00:00:00 2001 From: Cristina Date: Tue, 18 Nov 2025 10:57:46 +0100 Subject: [PATCH] Created Vue Tooltip directive This directive initializes and disposes Bootstrap tooltip --- public/js/directives/tooltip.js | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 public/js/directives/tooltip.js diff --git a/public/js/directives/tooltip.js b/public/js/directives/tooltip.js new file mode 100644 index 000000000..dabe75b3a --- /dev/null +++ b/public/js/directives/tooltip.js @@ -0,0 +1,62 @@ +/** + * v-tooltip - This directive makes it easy to initialize Bootstrap tooltips in Vue. + * + * Features: + * - Automatically initializes a Bootstrap tooltip on mount. + * - Re-initializes only when the bound value changes. + * - Cleans up the tooltip on unmount. + + * Usage examples: + * + * 1) Shortest way: + * + * Hover me! + * + * + * 2) With binding value. New value will trigger update features (destroy old + create new tooltip creation) + * + * Hover me! + * + * + * 3) Allowing HTML inside tooltip: + * + * Hover me! + * + * + */ +export default { + mounted(el, binding) { + const opts = { + title: binding.value ?? el.getAttribute('title'), // fallback if no binding + html: el.getAttribute('data-bs-html') === 'true', + customClass: el.getAttribute('data-bs-custom-class') || '' + }; + + // Create tooltip + el._tooltip = new bootstrap.Tooltip(el, opts); + }, + updated(el, binding) { + // Only dispose and create new Tooltip if value (the title-string) has changed + if (binding.value !== binding.oldValue){ + + if (el._tooltip) { + el._tooltip.dispose(); + } + + const opts = { + title: binding.value ?? el.getAttribute('title'), // fallback if no binding + html: el.getAttribute('data-bs-html') === 'true', + customClass: el.getAttribute('data-bs-custom-class') || '' + }; + + el._tooltip = new bootstrap.Tooltip(el, opts); + } + }, + unmounted(el) { + // Cleanup + if (el._tooltip) { + el._tooltip.dispose(); + delete el._tooltip; + } + } +}