Source: themes/claro/js/details.es6.js

  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).closest('summary').trigger('click');
  48. event.preventDefault();
  49. }
  50. });
  51. },
  52. };
  53. /**
  54. * Theme override providing a wrapper for summarized details content.
  55. *
  56. * @return {string}
  57. * The markup for the element that will contain the summarized content.
  58. */
  59. Drupal.theme.detailsSummarizedContentWrapper = () =>
  60. `<span class="claro-details__summary-summary"></span>`;
  61. /**
  62. * Theme override of summarized details content text.
  63. *
  64. * @param {string|null} [text]
  65. * (optional) The summarized content displayed in the summary.
  66. * @return {string}
  67. * The formatted summarized content text.
  68. */
  69. Drupal.theme.detailsSummarizedContentText = (text) => text || '';
  70. })(jQuery, Modernizr, Drupal);