index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Tooltip } from 'bootstrap'
  2. function getElementWidth(el: Element) {
  3. const elComputedStyle = window.getComputedStyle(el)
  4. const elPaddingX =
  5. parseFloat(elComputedStyle.paddingLeft) +
  6. parseFloat(elComputedStyle.paddingRight)
  7. const elBorderX =
  8. parseFloat(elComputedStyle.borderLeftWidth) +
  9. parseFloat(elComputedStyle.borderRightWidth)
  10. return el.scrollWidth - elPaddingX - elBorderX
  11. }
  12. const footerLanguageElement = document.querySelector(
  13. '[data-ol-lang-selector-tooltip]'
  14. ) as Element
  15. if (footerLanguageElement) {
  16. // eslint-disable-next-line no-new
  17. new Tooltip(footerLanguageElement)
  18. }
  19. const allTooltips = document.querySelectorAll('[data-bs-toggle="tooltip"]')
  20. allTooltips.forEach(element => {
  21. // eslint-disable-next-line no-new
  22. new Tooltip(element)
  23. })
  24. const possibleBadgeTooltips = document.querySelectorAll('[data-badge-tooltip]')
  25. possibleBadgeTooltips.forEach(element => {
  26. // Put data-badge-tooltip on .badge-content
  27. // then tooltip is only shown if content is clipped due to max-width on .badge
  28. // Due to font loading, the width calculated on page load might change, so we might
  29. // incorrectly determine a tooltip is not needed. This is why max-width will always be set to none
  30. // if no tooltip is shown so that content is fully visible in those scenarios.
  31. if (element.parentElement) {
  32. const parentWidth = getElementWidth(element.parentElement)
  33. if (element.scrollWidth > parentWidth) {
  34. // eslint-disable-next-line no-new
  35. new Tooltip(element)
  36. } else {
  37. element.parentElement.style.maxWidth = 'none'
  38. }
  39. }
  40. })