tabs.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function (Drupal, once) {
  8. function init(el) {
  9. var tabs = el.querySelector('.tabs');
  10. var expandedClass = 'is-expanded';
  11. var activeTab = tabs.querySelector('.is-active');
  12. function isTabsMobileLayout() {
  13. return tabs.querySelector('.tabs__trigger').clientHeight > 0;
  14. }
  15. function handleTriggerClick(e) {
  16. if (!tabs.classList.contains(expandedClass)) {
  17. e.currentTarget.setAttribute('aria-expanded', 'true');
  18. tabs.classList.add(expandedClass);
  19. } else {
  20. e.currentTarget.setAttribute('aria-expanded', 'false');
  21. tabs.classList.remove(expandedClass);
  22. }
  23. }
  24. if (isTabsMobileLayout() && !activeTab.matches('.tabs__tab:first-child')) {
  25. var newActiveTab = activeTab.cloneNode(true);
  26. var firstTab = tabs.querySelector('.tabs__tab:first-child');
  27. tabs.insertBefore(newActiveTab, firstTab);
  28. tabs.removeChild(activeTab);
  29. }
  30. tabs.querySelector('.tabs__trigger').addEventListener('click', handleTriggerClick);
  31. }
  32. Drupal.behaviors.primaryTabs = {
  33. attach: function attach(context) {
  34. once('olivero-tabs', '[data-drupal-nav-primary-tabs]', context).forEach(init);
  35. }
  36. };
  37. })(Drupal, once);