announce.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license
  3. * Copyright 2020 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. /**
  24. * Priorities for the announce function
  25. */
  26. export var AnnouncerPriority;
  27. (function (AnnouncerPriority) {
  28. AnnouncerPriority["POLITE"] = "polite";
  29. AnnouncerPriority["ASSERTIVE"] = "assertive";
  30. })(AnnouncerPriority || (AnnouncerPriority = {}));
  31. /**
  32. * Announces the given message with optional priority, defaulting to "polite"
  33. */
  34. export function announce(message, priority) {
  35. Announcer.getInstance().say(message, priority);
  36. }
  37. var Announcer = /** @class */ (function () {
  38. // Constructor made private to ensure only the singleton is used
  39. function Announcer() {
  40. this.liveRegions = new Map();
  41. }
  42. Announcer.getInstance = function () {
  43. if (!Announcer.instance) {
  44. Announcer.instance = new Announcer();
  45. }
  46. return Announcer.instance;
  47. };
  48. Announcer.prototype.say = function (message, priority) {
  49. if (priority === void 0) { priority = AnnouncerPriority.POLITE; }
  50. var liveRegion = this.getLiveRegion(priority);
  51. // Reset the region to pick up the message, even if the message is the
  52. // exact same as before.
  53. liveRegion.textContent = '';
  54. // Timeout is necessary for screen readers like NVDA and VoiceOver.
  55. setTimeout(function () {
  56. liveRegion.textContent = message;
  57. }, 1);
  58. };
  59. Announcer.prototype.getLiveRegion = function (priority) {
  60. var existingLiveRegion = this.liveRegions.get(priority);
  61. if (existingLiveRegion &&
  62. document.body.contains(existingLiveRegion)) {
  63. return existingLiveRegion;
  64. }
  65. var liveRegion = this.createLiveRegion(priority);
  66. this.liveRegions.set(priority, liveRegion);
  67. return liveRegion;
  68. };
  69. Announcer.prototype.createLiveRegion = function (priority) {
  70. var el = document.createElement('div');
  71. el.style.position = 'absolute';
  72. el.style.top = '-9999px';
  73. el.style.left = '-9999px';
  74. el.style.height = '1px';
  75. el.style.overflow = 'hidden';
  76. el.setAttribute('aria-atomic', 'true');
  77. el.setAttribute('aria-live', priority);
  78. document.body.appendChild(el);
  79. return el;
  80. };
  81. return Announcer;
  82. }());
  83. //# sourceMappingURL=announce.js.map