responsive-details.es6.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @file
  3. * Provides responsive behaviors to HTML details elements.
  4. */
  5. (function ($, Drupal) {
  6. /**
  7. * Initializes the responsive behaviors for details elements.
  8. *
  9. * @type {Drupal~behavior}
  10. *
  11. * @prop {Drupal~behaviorAttach} attach
  12. * Attaches the responsive behavior to status report specific details elements.
  13. */
  14. Drupal.behaviors.responsiveDetails = {
  15. attach(context) {
  16. const $details = $(context).find('details').once('responsive-details');
  17. if (!$details.length) {
  18. return;
  19. }
  20. function detailsToggle(matches) {
  21. if (matches) {
  22. $details.attr('open', true);
  23. $summaries.attr('aria-expanded', true);
  24. $summaries.on('click.details-open', false);
  25. }
  26. else {
  27. // If user explicitly opened one, leave it alone.
  28. const $notPressed = $details
  29. .find('> summary[aria-pressed!=true]')
  30. .attr('aria-expanded', false);
  31. $notPressed
  32. .parent('details')
  33. .attr('open', false);
  34. // After resize, allow user to close previously opened details.
  35. $summaries.off('.details-open');
  36. }
  37. }
  38. function handleDetailsMQ(event) {
  39. detailsToggle(event.matches);
  40. }
  41. var $summaries = $details.find('> summary');
  42. const mql = window.matchMedia('(min-width:48em)');
  43. mql.addListener(handleDetailsMQ);
  44. detailsToggle(mql.matches);
  45. },
  46. };
  47. }(jQuery, Drupal));