util.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @license
  3. * Copyright 2018 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { numbers, strings } from './constants';
  24. var ARIA_LIVE_DELAY_MS = numbers.ARIA_LIVE_DELAY_MS;
  25. var DATA_LIVE_LABEL_TEXT = strings.DATA_LIVE_LABEL_TEXT;
  26. /** Accounce helper for snackbar */
  27. export function announce(ariaEl, labelEl) {
  28. if (labelEl === void 0) { labelEl = ariaEl; }
  29. var priority = ariaEl.getAttribute('aria-live');
  30. // Trim text to ignore ` ` (see below).
  31. // textContent is only null if the node is a document, DOCTYPE, or notation.
  32. var labelText = labelEl.textContent.trim();
  33. if (!labelText || !priority) {
  34. return;
  35. }
  36. // Temporarily disable `aria-live` to prevent JAWS+Firefox from announcing the
  37. // message twice.
  38. ariaEl.setAttribute('aria-live', 'off');
  39. // Temporarily clear `textContent` to force a DOM mutation event that will be
  40. // detected by screen readers. `aria-live` elements are only announced when
  41. // the element's `textContent` *changes*, so snackbars sent to the browser in
  42. // the initial HTML response won't be read unless we clear the element's
  43. // `textContent` first. Similarly, displaying the same snackbar message twice
  44. // in a row doesn't trigger a DOM mutation event, so screen readers won't
  45. // announce the second message unless we first clear `textContent`.
  46. //
  47. // We have to clear the label text two different ways to make it work in all
  48. // browsers and screen readers:
  49. //
  50. // 1. `textContent = ''` is required for IE11 + JAWS
  51. // 2. `innerHTML = ' '` is required for Chrome + JAWS and NVDA
  52. //
  53. // All other browser/screen reader combinations support both methods.
  54. //
  55. // The wrapper `<span>` visually hides the space character so that it doesn't
  56. // cause jank when added/removed. N.B.: Setting `position: absolute`,
  57. // `opacity: 0`, or `height: 0` prevents Chrome from detecting the DOM change.
  58. //
  59. // This technique has been tested in:
  60. //
  61. // * JAWS 2019:
  62. // - Chrome 70
  63. // - Firefox 60 (ESR)
  64. // - IE 11
  65. // * NVDA 2018:
  66. // - Chrome 70
  67. // - Firefox 60 (ESR)
  68. // - IE 11
  69. // * ChromeVox 53
  70. labelEl.textContent = '';
  71. // For the second case, assigning a string directly to innerHTML results in a
  72. // Trusted Types violation. Work around that by using document.createElement.
  73. var span = document.createElement('span');
  74. span.setAttribute('style', 'display: inline-block; width: 0; height: 1px;');
  75. span.textContent = '\u00a0'; // Equivalent to &nbsp;
  76. labelEl.appendChild(span);
  77. // Prevent visual jank by temporarily displaying the label text in the
  78. // ::before pseudo-element. CSS generated content is normally announced by
  79. // screen readers (except in IE 11; see
  80. // https://tink.uk/accessibility-support-for-css-generated-content/); however,
  81. // `aria-live` is turned off, so this DOM update will be ignored by screen
  82. // readers.
  83. labelEl.dataset[DATA_LIVE_LABEL_TEXT] = labelText;
  84. setTimeout(function () {
  85. // Allow screen readers to announce changes to the DOM again.
  86. ariaEl.setAttribute('aria-live', priority);
  87. // Remove the message from the ::before pseudo-element.
  88. delete labelEl.dataset[DATA_LIVE_LABEL_TEXT];
  89. // Restore the original label text, which will be announced by screen
  90. // readers.
  91. labelEl.textContent = labelText;
  92. }, ARIA_LIVE_DELAY_MS);
  93. }
  94. //# sourceMappingURL=util.js.map