details.es6.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @file
  3. * Claro's polyfill enhancements for HTML5 details.
  4. */
  5. (($, Modernizr, Drupal) => {
  6. /**
  7. * Workaround for Firefox.
  8. *
  9. * Firefox applies the focus state only for keyboard navigation.
  10. * We have to manually trigger focus to make the behavior consistent across
  11. * browsers.
  12. *
  13. * @type {Drupal~behavior}
  14. */
  15. Drupal.behaviors.claroDetails = {
  16. attach(context) {
  17. $(context)
  18. .once('claroDetails')
  19. .on('click', event => {
  20. if (event.target.nodeName === 'SUMMARY') {
  21. $(event.target).trigger('focus');
  22. }
  23. });
  24. },
  25. };
  26. /**
  27. * Workaround for non-supporting browsers.
  28. *
  29. * This shim extends HTML5 Shiv used by core.
  30. *
  31. * HTML5 Shiv toggles focused details for hitting enter. We copy that for
  32. * space key as well to make the behavior consistent across browsers.
  33. *
  34. * @type {Drupal~behavior}
  35. */
  36. Drupal.behaviors.claroDetailsToggleShim = {
  37. attach(context) {
  38. if (Modernizr.details || !Drupal.CollapsibleDetails.instances.length) {
  39. return;
  40. }
  41. $(context)
  42. .find('details .details-title')
  43. .once('claroDetailsToggleShim')
  44. .on('keypress', event => {
  45. const keyCode = event.keyCode || event.charCode;
  46. if (keyCode === 32) {
  47. $(event.target)
  48. .closest('summary')
  49. .trigger('click');
  50. event.preventDefault();
  51. }
  52. });
  53. },
  54. };
  55. })(jQuery, Modernizr, Drupal);