index-bs5.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Tooltip } from 'bootstrap-5'
  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. const allTooltips = document.querySelectorAll('[data-bs-toggle="tooltip"]')
  16. const possibleBadgeTooltips = document.querySelectorAll('[data-badge-tooltip]')
  17. // eslint-disable-next-line no-unused-vars
  18. const footLangTooltip = new Tooltip(footerLanguageElement)
  19. allTooltips.forEach(element => {
  20. // eslint-disable-next-line no-unused-vars
  21. const tooltip = new Tooltip(element)
  22. })
  23. possibleBadgeTooltips.forEach(element => {
  24. // Put data-badge-tooltip on .badge-content
  25. // then tooltip is only shown if content is clipped due to max-width on .badge
  26. // Due to font loading, the width calculated on page load might change, so we might
  27. // incorrectly determine a tooltip is not needed. This is why max-width will always be set to none
  28. // if no tooltip is shown so that content is fully visible in those scenarios.
  29. if (element.parentElement) {
  30. const parentWidth = getElementWidth(element.parentElement)
  31. if (element.scrollWidth > parentWidth) {
  32. // eslint-disable-next-line no-unused-vars
  33. const tooltip = new Tooltip(element)
  34. } else {
  35. element.parentElement.style.maxWidth = 'none'
  36. }
  37. }
  38. })