legacyTwitterBootstrap.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * This is a re-implementation of some bootstrap functionality that we still
  3. * depend on in some html templates.
  4. */
  5. /**
  6. * Similar to jQuery's `on`, adds an event listener to the root document which
  7. * will only fire when the selector matches the element which triggered the
  8. * event.
  9. */
  10. const addSelectorEventListener = <K extends keyof DocumentEventMap>(
  11. type: K,
  12. selector: string,
  13. listener: (ev: DocumentEventMap[K]) => any
  14. ) =>
  15. document.addEventListener(type, event => {
  16. const {target} = event;
  17. if (target === null) {
  18. return;
  19. }
  20. if (!(target instanceof HTMLElement)) {
  21. return;
  22. }
  23. if (!target.matches(selector)) {
  24. return;
  25. }
  26. listener(event);
  27. });
  28. /**
  29. * Tab toggle handlers.
  30. *
  31. * @deprecated
  32. */
  33. addSelectorEventListener('click', '[data-toggle="tab"]', event => {
  34. event.preventDefault();
  35. const triggerElement = event.target as HTMLElement;
  36. const targetSelector = triggerElement.getAttribute('href');
  37. if (targetSelector === null) {
  38. return;
  39. }
  40. const targetPanel = document.querySelector<HTMLElement>(targetSelector);
  41. if (targetPanel === null) {
  42. return;
  43. }
  44. const container = targetPanel.parentElement!;
  45. const tabs = triggerElement.closest('ul');
  46. const targetTab = triggerElement.closest('li');
  47. const lastActiveTab = tabs?.querySelector(':scope > .active');
  48. // Reset the old active tab
  49. lastActiveTab?.classList?.remove('active');
  50. lastActiveTab?.querySelector(':scope > a')?.setAttribute('aria-expanded', 'false');
  51. container.querySelector<HTMLElement>(':scope > .active')?.classList?.remove('active');
  52. // Activate the target
  53. targetTab?.classList.add('active');
  54. targetTab?.querySelector(':scope > a')?.setAttribute('aria-expanded', 'true');
  55. targetPanel.classList.add('active');
  56. });
  57. /**
  58. * Remove alerts when the close button is clicked
  59. *
  60. * @deprecated
  61. */
  62. addSelectorEventListener('click', '[data-dismiss="alert"]', event => {
  63. (event.target as HTMLElement).closest('.alert')?.remove();
  64. });