@react-aria/live-announcer 3.0.0-nightly.3102 → 3.0.0-nightly.3110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,10 +17,9 @@ $parcel$export(module.exports, "destroyAnnouncer", () => $97cebfa4133ebec3$expor
17
17
  * OF ANY KIND, either express or implied. See the License for the specific language
18
18
  * governing permissions and limitations under the License.
19
19
  */ /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */ const $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY = 7000;
20
- let $97cebfa4133ebec3$var$liveAnnouncer = null;
21
- function $97cebfa4133ebec3$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY) {
20
+ function $97cebfa4133ebec3$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY, mode = 'message') {
22
21
  if (!$97cebfa4133ebec3$var$liveAnnouncer) $97cebfa4133ebec3$var$liveAnnouncer = new $97cebfa4133ebec3$var$LiveAnnouncer();
23
- $97cebfa4133ebec3$var$liveAnnouncer.announce(message, assertiveness, timeout);
22
+ $97cebfa4133ebec3$var$liveAnnouncer.announce(message, assertiveness, timeout, mode);
24
23
  }
25
24
  function $97cebfa4133ebec3$export$d10ae4f68404609a(assertiveness) {
26
25
  if ($97cebfa4133ebec3$var$liveAnnouncer) $97cebfa4133ebec3$var$liveAnnouncer.clear(assertiveness);
@@ -50,44 +49,58 @@ class $97cebfa4133ebec3$var$LiveAnnouncer {
50
49
  document.body.removeChild(this.node);
51
50
  this.node = null;
52
51
  }
53
- announce(message, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY) {
52
+ announce(message, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY, mode = 'message') {
53
+ var _this_assertiveLog, _this_politeLog;
54
54
  if (!this.node) return;
55
55
  let node = document.createElement('div');
56
- node.textContent = message;
57
- if (assertiveness === 'assertive') this.assertiveLog.appendChild(node);
58
- else this.politeLog.appendChild(node);
56
+ if (mode === 'message') node.textContent = message;
57
+ else {
58
+ // To read an aria-labelledby, the element must have an appropriate role, such as img.
59
+ node.setAttribute('role', 'img');
60
+ node.setAttribute('aria-labelledby', message);
61
+ }
62
+ if (assertiveness === 'assertive') (_this_assertiveLog = this.assertiveLog) === null || _this_assertiveLog === void 0 ? void 0 : _this_assertiveLog.appendChild(node);
63
+ else (_this_politeLog = this.politeLog) === null || _this_politeLog === void 0 ? void 0 : _this_politeLog.appendChild(node);
59
64
  if (message !== '') setTimeout(()=>{
60
65
  node.remove();
61
66
  }, timeout);
62
67
  }
63
68
  clear(assertiveness) {
64
69
  if (!this.node) return;
65
- if (!assertiveness || assertiveness === 'assertive') this.assertiveLog.innerHTML = '';
66
- if (!assertiveness || assertiveness === 'polite') this.politeLog.innerHTML = '';
70
+ if ((!assertiveness || assertiveness === 'assertive') && this.assertiveLog) this.assertiveLog.innerHTML = '';
71
+ if ((!assertiveness || assertiveness === 'polite') && this.politeLog) this.politeLog.innerHTML = '';
67
72
  }
68
73
  constructor(){
69
- this.node = document.createElement('div');
70
- this.node.dataset.liveAnnouncer = 'true';
71
- // copied from VisuallyHidden
72
- Object.assign(this.node.style, {
73
- border: 0,
74
- clip: 'rect(0 0 0 0)',
75
- clipPath: 'inset(50%)',
76
- height: '1px',
77
- margin: '-1px',
78
- overflow: 'hidden',
79
- padding: 0,
80
- position: 'absolute',
81
- width: '1px',
82
- whiteSpace: 'nowrap'
83
- });
84
- this.assertiveLog = this.createLog('assertive');
85
- this.node.appendChild(this.assertiveLog);
86
- this.politeLog = this.createLog('polite');
87
- this.node.appendChild(this.politeLog);
88
- document.body.prepend(this.node);
74
+ this.node = null;
75
+ this.assertiveLog = null;
76
+ this.politeLog = null;
77
+ if (typeof document !== 'undefined') {
78
+ this.node = document.createElement('div');
79
+ this.node.dataset.liveAnnouncer = 'true';
80
+ // copied from VisuallyHidden
81
+ Object.assign(this.node.style, {
82
+ border: 0,
83
+ clip: 'rect(0 0 0 0)',
84
+ clipPath: 'inset(50%)',
85
+ height: '1px',
86
+ margin: '-1px',
87
+ overflow: 'hidden',
88
+ padding: 0,
89
+ position: 'absolute',
90
+ width: '1px',
91
+ whiteSpace: 'nowrap'
92
+ });
93
+ this.assertiveLog = this.createLog('assertive');
94
+ this.node.appendChild(this.assertiveLog);
95
+ this.politeLog = this.createLog('polite');
96
+ this.node.appendChild(this.politeLog);
97
+ document.body.prepend(this.node);
98
+ }
89
99
  }
90
100
  }
101
+ // singleton, setup immediately so that the DOM is primed for the first announcement as soon as possible
102
+ // Safari has a race condition where the first announcement is not read if we wait until the first announce call
103
+ let $97cebfa4133ebec3$var$liveAnnouncer = new $97cebfa4133ebec3$var$LiveAnnouncer();
91
104
 
92
105
 
93
106
  //# sourceMappingURL=LiveAnnouncer.main.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC,GAID,8DAA8D,GAC9D,MAAM,iDAA2B;AAEjC,IAAI,sCAAsC;AAKnC,SAAS,0CACd,OAAe,EACf,gBAA+B,WAAW,EAC1C,UAAU,8CAAwB;IAElC,IAAI,CAAC,qCACH,sCAAgB,IAAI;IAGtB,oCAAc,QAAQ,CAAC,SAAS,eAAe;AACjD;AAKO,SAAS,0CAAe,aAA4B;IACzD,IAAI,qCACF,oCAAc,KAAK,CAAC;AAExB;AAKO,SAAS;IACd,IAAI,qCAAe;QACjB,oCAAc,OAAO;QACrB,sCAAgB;IAClB;AACF;AAEA,2FAA2F;AAC3F,0FAA0F;AAC1F,mGAAmG;AACnG,iGAAiG;AACjG,0EAA0E;AAC1E,sHAAsH;AACtH,MAAM;IA+BJ,UAAU,QAAgB,EAAE;QAC1B,IAAI,OAAO,SAAS,aAAa,CAAC;QAClC,KAAK,YAAY,CAAC,QAAQ;QAC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SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAChC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS;QAEpC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;IACjC;AAqDF","sources":["packages/@react-aria/live-announcer/src/LiveAnnouncer.tsx"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\ntype Assertiveness = 'assertive' | 'polite';\n\n/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */\nconst LIVEREGION_TIMEOUT_DELAY = 7000;\n\nlet liveAnnouncer: LiveAnnouncer | null = null;\n\n/**\n * Announces the message using screen reader technology.\n */\nexport function announce(\n message: string,\n assertiveness: Assertiveness = 'assertive',\n timeout = LIVEREGION_TIMEOUT_DELAY\n) {\n if (!liveAnnouncer) {\n liveAnnouncer = new LiveAnnouncer();\n }\n\n liveAnnouncer.announce(message, assertiveness, timeout);\n}\n\n/**\n * Stops all queued announcements.\n */\nexport function clearAnnouncer(assertiveness: Assertiveness) {\n if (liveAnnouncer) {\n liveAnnouncer.clear(assertiveness);\n }\n}\n\n/**\n * Removes the announcer from the DOM.\n */\nexport function destroyAnnouncer() {\n if (liveAnnouncer) {\n liveAnnouncer.destroy();\n liveAnnouncer = null;\n }\n}\n\n// LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18\n// ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a\n// subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.\n// As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer\n// is simple enough to implement without React, so that's what we do here.\n// See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638\nclass LiveAnnouncer {\n node: HTMLElement | null;\n assertiveLog: HTMLElement;\n politeLog: HTMLElement;\n\n constructor() {\n this.node = document.createElement('div');\n this.node.dataset.liveAnnouncer = 'true';\n // copied from VisuallyHidden\n Object.assign(this.node.style, {\n border: 0,\n clip: 'rect(0 0 0 0)',\n clipPath: 'inset(50%)',\n height: '1px',\n margin: '-1px',\n overflow: 'hidden',\n padding: 0,\n position: 'absolute',\n width: '1px',\n whiteSpace: 'nowrap'\n });\n\n this.assertiveLog = this.createLog('assertive');\n this.node.appendChild(this.assertiveLog);\n\n this.politeLog = this.createLog('polite');\n this.node.appendChild(this.politeLog);\n\n document.body.prepend(this.node);\n }\n\n createLog(ariaLive: string) {\n let node = document.createElement('div');\n node.setAttribute('role', 'log');\n node.setAttribute('aria-live', ariaLive);\n node.setAttribute('aria-relevant', 'additions');\n return node;\n }\n\n destroy() {\n if (!this.node) {\n return;\n }\n\n document.body.removeChild(this.node);\n this.node = null;\n }\n\n announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) {\n if (!this.node) {\n return;\n }\n\n let node = document.createElement('div');\n node.textContent = message;\n\n if (assertiveness === 'assertive') {\n this.assertiveLog.appendChild(node);\n } else {\n this.politeLog.appendChild(node);\n }\n\n if (message !== '') {\n setTimeout(() => {\n node.remove();\n }, timeout);\n }\n }\n\n clear(assertiveness: Assertiveness) {\n if (!this.node) {\n return;\n }\n\n if (!assertiveness || assertiveness === 'assertive') {\n this.assertiveLog.innerHTML = '';\n }\n\n if (!assertiveness || assertiveness === 'polite') {\n this.politeLog.innerHTML = '';\n }\n }\n}\n"],"names":[],"version":3,"file":"LiveAnnouncer.main.js.map"}
1
+ {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC,GAID,8DAA8D,GAC9D,MAAM,iDAA2B;AAK1B,SAAS,0CACd,OAAe,EACf,gBAA+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;IAEA,MAAM,aAA4B,EAAE;QAClC,IAAI,CAAC,IAAI,CAAC,IAAI,EACZ;QAGF,IAAI,AAAC,CAAA,CAAC,iBAAiB,kBAAkB,WAAU,KAAM,IAAI,CAAC,YAAY,EACxE,IAAI,CAAC,YAAY,CAAC,SAAS,GAAG;QAGhC,IAAI,AAAC,CAAA,CAAC,iBAAiB,kBAAkB,QAAO,KAAM,IAAI,CAAC,SAAS,EAClE,IAAI,CAAC,SAAS,CAAC,SAAS,GAAG;IAE/B;IApFA,aAAc;aAJd,OAA2B;aAC3B,eAAmC;aACnC,YAAgC;QAG9B,IAAI,OAAO,aAAa,aAAa;YACnC,IAAI,CAAC,IAAI,GAAG,SAAS,aAAa,CAAC;YACnC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,GAAG;YAClC,6BAA6B;YAC7B,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;gBAC7B,QAAQ;gBACR,MAAM;gBACN,UAAU;gBACV,QAAQ;gBACR,QAAQ;gBACR,UAAU;gBACV,SAAS;gBACT,UAAU;gBACV,OAAO;gBACP,YAAY;YACd;YAEA,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC;YACnC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,YAAY;YAEvC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;YAChC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS;YAEpC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QACjC;IACF;AA2DF;AAEA,wGAAwG;AACxG,gHAAgH;AAChH,IAAI,sCAAsC,IAAI","sources":["packages/@react-aria/live-announcer/src/LiveAnnouncer.tsx"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\ntype Assertiveness = 'assertive' | 'polite';\n\n/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */\nconst LIVEREGION_TIMEOUT_DELAY = 7000;\n\n/**\n * Announces the message using screen reader technology.\n */\nexport function announce(\n message: string,\n assertiveness: Assertiveness = 'assertive',\n timeout = LIVEREGION_TIMEOUT_DELAY,\n mode: 'message' | 'ids' = 'message'\n) {\n if (!liveAnnouncer) {\n liveAnnouncer = new LiveAnnouncer();\n }\n\n liveAnnouncer.announce(message, assertiveness, timeout, mode);\n}\n\n/**\n * Stops all queued announcements.\n */\nexport function clearAnnouncer(assertiveness: Assertiveness) {\n if (liveAnnouncer) {\n liveAnnouncer.clear(assertiveness);\n }\n}\n\n/**\n * Removes the announcer from the DOM.\n */\nexport function destroyAnnouncer() {\n if (liveAnnouncer) {\n liveAnnouncer.destroy();\n liveAnnouncer = null;\n }\n}\n\n// LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18\n// ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a\n// subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.\n// As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer\n// is simple enough to implement without React, so that's what we do here.\n// See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638\nclass LiveAnnouncer {\n node: HTMLElement | null = null;\n assertiveLog: HTMLElement | null = null;\n politeLog: HTMLElement | null = null;\n\n constructor() {\n if (typeof document !== 'undefined') {\n this.node = document.createElement('div');\n this.node.dataset.liveAnnouncer = 'true';\n // copied from VisuallyHidden\n Object.assign(this.node.style, {\n border: 0,\n clip: 'rect(0 0 0 0)',\n clipPath: 'inset(50%)',\n height: '1px',\n margin: '-1px',\n overflow: 'hidden',\n padding: 0,\n position: 'absolute',\n width: '1px',\n whiteSpace: 'nowrap'\n });\n\n this.assertiveLog = this.createLog('assertive');\n this.node.appendChild(this.assertiveLog);\n\n this.politeLog = this.createLog('polite');\n this.node.appendChild(this.politeLog);\n\n document.body.prepend(this.node);\n }\n }\n\n createLog(ariaLive: string) {\n let node = document.createElement('div');\n node.setAttribute('role', 'log');\n node.setAttribute('aria-live', ariaLive);\n node.setAttribute('aria-relevant', 'additions');\n return node;\n }\n\n destroy() {\n if (!this.node) {\n return;\n }\n\n document.body.removeChild(this.node);\n this.node = null;\n }\n\n announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY, mode: 'message' | 'ids' = 'message') {\n if (!this.node) {\n return;\n }\n\n let node = document.createElement('div');\n if (mode === 'message') {\n node.textContent = message;\n } else {\n // To read an aria-labelledby, the element must have an appropriate role, such as img.\n node.setAttribute('role', 'img');\n node.setAttribute('aria-labelledby', message);\n }\n\n if (assertiveness === 'assertive') {\n this.assertiveLog?.appendChild(node);\n } else {\n this.politeLog?.appendChild(node);\n }\n\n if (message !== '') {\n setTimeout(() => {\n node.remove();\n }, timeout);\n }\n }\n\n clear(assertiveness: Assertiveness) {\n if (!this.node) {\n return;\n }\n\n if ((!assertiveness || assertiveness === 'assertive') && this.assertiveLog) {\n this.assertiveLog.innerHTML = '';\n }\n\n if ((!assertiveness || assertiveness === 'polite') && this.politeLog) {\n this.politeLog.innerHTML = '';\n }\n }\n}\n\n// singleton, setup immediately so that the DOM is primed for the first announcement as soon as possible\n// Safari has a race condition where the first announcement is not read if we wait until the first announce call\nlet liveAnnouncer: LiveAnnouncer | null = new LiveAnnouncer();\n"],"names":[],"version":3,"file":"LiveAnnouncer.main.js.map"}
@@ -9,10 +9,9 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */ /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */ const $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY = 7000;
12
- let $319e236875307eab$var$liveAnnouncer = null;
13
- function $319e236875307eab$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY) {
12
+ function $319e236875307eab$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY, mode = 'message') {
14
13
  if (!$319e236875307eab$var$liveAnnouncer) $319e236875307eab$var$liveAnnouncer = new $319e236875307eab$var$LiveAnnouncer();
15
- $319e236875307eab$var$liveAnnouncer.announce(message, assertiveness, timeout);
14
+ $319e236875307eab$var$liveAnnouncer.announce(message, assertiveness, timeout, mode);
16
15
  }
17
16
  function $319e236875307eab$export$d10ae4f68404609a(assertiveness) {
18
17
  if ($319e236875307eab$var$liveAnnouncer) $319e236875307eab$var$liveAnnouncer.clear(assertiveness);
@@ -42,44 +41,58 @@ class $319e236875307eab$var$LiveAnnouncer {
42
41
  document.body.removeChild(this.node);
43
42
  this.node = null;
44
43
  }
45
- announce(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY) {
44
+ announce(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY, mode = 'message') {
45
+ var _this_assertiveLog, _this_politeLog;
46
46
  if (!this.node) return;
47
47
  let node = document.createElement('div');
48
- node.textContent = message;
49
- if (assertiveness === 'assertive') this.assertiveLog.appendChild(node);
50
- else this.politeLog.appendChild(node);
48
+ if (mode === 'message') node.textContent = message;
49
+ else {
50
+ // To read an aria-labelledby, the element must have an appropriate role, such as img.
51
+ node.setAttribute('role', 'img');
52
+ node.setAttribute('aria-labelledby', message);
53
+ }
54
+ if (assertiveness === 'assertive') (_this_assertiveLog = this.assertiveLog) === null || _this_assertiveLog === void 0 ? void 0 : _this_assertiveLog.appendChild(node);
55
+ else (_this_politeLog = this.politeLog) === null || _this_politeLog === void 0 ? void 0 : _this_politeLog.appendChild(node);
51
56
  if (message !== '') setTimeout(()=>{
52
57
  node.remove();
53
58
  }, timeout);
54
59
  }
55
60
  clear(assertiveness) {
56
61
  if (!this.node) return;
57
- if (!assertiveness || assertiveness === 'assertive') this.assertiveLog.innerHTML = '';
58
- if (!assertiveness || assertiveness === 'polite') this.politeLog.innerHTML = '';
62
+ if ((!assertiveness || assertiveness === 'assertive') && this.assertiveLog) this.assertiveLog.innerHTML = '';
63
+ if ((!assertiveness || assertiveness === 'polite') && this.politeLog) this.politeLog.innerHTML = '';
59
64
  }
60
65
  constructor(){
61
- this.node = document.createElement('div');
62
- this.node.dataset.liveAnnouncer = 'true';
63
- // copied from VisuallyHidden
64
- Object.assign(this.node.style, {
65
- border: 0,
66
- clip: 'rect(0 0 0 0)',
67
- clipPath: 'inset(50%)',
68
- height: '1px',
69
- margin: '-1px',
70
- overflow: 'hidden',
71
- padding: 0,
72
- position: 'absolute',
73
- width: '1px',
74
- whiteSpace: 'nowrap'
75
- });
76
- this.assertiveLog = this.createLog('assertive');
77
- this.node.appendChild(this.assertiveLog);
78
- this.politeLog = this.createLog('polite');
79
- this.node.appendChild(this.politeLog);
80
- document.body.prepend(this.node);
66
+ this.node = null;
67
+ this.assertiveLog = null;
68
+ this.politeLog = null;
69
+ if (typeof document !== 'undefined') {
70
+ this.node = document.createElement('div');
71
+ this.node.dataset.liveAnnouncer = 'true';
72
+ // copied from VisuallyHidden
73
+ Object.assign(this.node.style, {
74
+ border: 0,
75
+ clip: 'rect(0 0 0 0)',
76
+ clipPath: 'inset(50%)',
77
+ height: '1px',
78
+ margin: '-1px',
79
+ overflow: 'hidden',
80
+ padding: 0,
81
+ position: 'absolute',
82
+ width: '1px',
83
+ whiteSpace: 'nowrap'
84
+ });
85
+ this.assertiveLog = this.createLog('assertive');
86
+ this.node.appendChild(this.assertiveLog);
87
+ this.politeLog = this.createLog('polite');
88
+ this.node.appendChild(this.politeLog);
89
+ document.body.prepend(this.node);
90
+ }
81
91
  }
82
92
  }
93
+ // singleton, setup immediately so that the DOM is primed for the first announcement as soon as possible
94
+ // Safari has a race condition where the first announcement is not read if we wait until the first announce call
95
+ let $319e236875307eab$var$liveAnnouncer = new $319e236875307eab$var$LiveAnnouncer();
83
96
 
84
97
 
85
98
  export {$319e236875307eab$export$a9b970dcc4ae71a9 as announce, $319e236875307eab$export$d10ae4f68404609a as clearAnnouncer, $319e236875307eab$export$d8686216b8b81b2f as destroyAnnouncer};
@@ -9,10 +9,9 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */ /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */ const $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY = 7000;
12
- let $319e236875307eab$var$liveAnnouncer = null;
13
- function $319e236875307eab$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY) {
12
+ function $319e236875307eab$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY, mode = 'message') {
14
13
  if (!$319e236875307eab$var$liveAnnouncer) $319e236875307eab$var$liveAnnouncer = new $319e236875307eab$var$LiveAnnouncer();
15
- $319e236875307eab$var$liveAnnouncer.announce(message, assertiveness, timeout);
14
+ $319e236875307eab$var$liveAnnouncer.announce(message, assertiveness, timeout, mode);
16
15
  }
17
16
  function $319e236875307eab$export$d10ae4f68404609a(assertiveness) {
18
17
  if ($319e236875307eab$var$liveAnnouncer) $319e236875307eab$var$liveAnnouncer.clear(assertiveness);
@@ -42,44 +41,58 @@ class $319e236875307eab$var$LiveAnnouncer {
42
41
  document.body.removeChild(this.node);
43
42
  this.node = null;
44
43
  }
45
- announce(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY) {
44
+ announce(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY, mode = 'message') {
45
+ var _this_assertiveLog, _this_politeLog;
46
46
  if (!this.node) return;
47
47
  let node = document.createElement('div');
48
- node.textContent = message;
49
- if (assertiveness === 'assertive') this.assertiveLog.appendChild(node);
50
- else this.politeLog.appendChild(node);
48
+ if (mode === 'message') node.textContent = message;
49
+ else {
50
+ // To read an aria-labelledby, the element must have an appropriate role, such as img.
51
+ node.setAttribute('role', 'img');
52
+ node.setAttribute('aria-labelledby', message);
53
+ }
54
+ if (assertiveness === 'assertive') (_this_assertiveLog = this.assertiveLog) === null || _this_assertiveLog === void 0 ? void 0 : _this_assertiveLog.appendChild(node);
55
+ else (_this_politeLog = this.politeLog) === null || _this_politeLog === void 0 ? void 0 : _this_politeLog.appendChild(node);
51
56
  if (message !== '') setTimeout(()=>{
52
57
  node.remove();
53
58
  }, timeout);
54
59
  }
55
60
  clear(assertiveness) {
56
61
  if (!this.node) return;
57
- if (!assertiveness || assertiveness === 'assertive') this.assertiveLog.innerHTML = '';
58
- if (!assertiveness || assertiveness === 'polite') this.politeLog.innerHTML = '';
62
+ if ((!assertiveness || assertiveness === 'assertive') && this.assertiveLog) this.assertiveLog.innerHTML = '';
63
+ if ((!assertiveness || assertiveness === 'polite') && this.politeLog) this.politeLog.innerHTML = '';
59
64
  }
60
65
  constructor(){
61
- this.node = document.createElement('div');
62
- this.node.dataset.liveAnnouncer = 'true';
63
- // copied from VisuallyHidden
64
- Object.assign(this.node.style, {
65
- border: 0,
66
- clip: 'rect(0 0 0 0)',
67
- clipPath: 'inset(50%)',
68
- height: '1px',
69
- margin: '-1px',
70
- overflow: 'hidden',
71
- padding: 0,
72
- position: 'absolute',
73
- width: '1px',
74
- whiteSpace: 'nowrap'
75
- });
76
- this.assertiveLog = this.createLog('assertive');
77
- this.node.appendChild(this.assertiveLog);
78
- this.politeLog = this.createLog('polite');
79
- this.node.appendChild(this.politeLog);
80
- document.body.prepend(this.node);
66
+ this.node = null;
67
+ this.assertiveLog = null;
68
+ this.politeLog = null;
69
+ if (typeof document !== 'undefined') {
70
+ this.node = document.createElement('div');
71
+ this.node.dataset.liveAnnouncer = 'true';
72
+ // copied from VisuallyHidden
73
+ Object.assign(this.node.style, {
74
+ border: 0,
75
+ clip: 'rect(0 0 0 0)',
76
+ clipPath: 'inset(50%)',
77
+ height: '1px',
78
+ margin: '-1px',
79
+ overflow: 'hidden',
80
+ padding: 0,
81
+ position: 'absolute',
82
+ width: '1px',
83
+ whiteSpace: 'nowrap'
84
+ });
85
+ this.assertiveLog = this.createLog('assertive');
86
+ this.node.appendChild(this.assertiveLog);
87
+ this.politeLog = this.createLog('polite');
88
+ this.node.appendChild(this.politeLog);
89
+ document.body.prepend(this.node);
90
+ }
81
91
  }
82
92
  }
93
+ // singleton, setup immediately so that the DOM is primed for the first announcement as soon as possible
94
+ // Safari has a race condition where the first announcement is not read if we wait until the first announce call
95
+ let $319e236875307eab$var$liveAnnouncer = new $319e236875307eab$var$LiveAnnouncer();
83
96
 
84
97
 
85
98
  export {$319e236875307eab$export$a9b970dcc4ae71a9 as announce, $319e236875307eab$export$d10ae4f68404609a as clearAnnouncer, $319e236875307eab$export$d8686216b8b81b2f as destroyAnnouncer};
@@ -1 +1 @@
1
- {"mappings":"AAAA;;;;;;;;;;CAUC,GAID,8DAA8D,GAC9D,MAAM,iDAA2B;AAEjC,IAAI,sCAAsC;AAKnC,SAAS,0CACd,OAAe,EACf,gBAA+B,WAAW,EAC1C,UAAU,8CAAwB;IAElC,IAAI,CAAC,qCACH,sCAAgB,IAAI;IAGtB,oCAAc,QAAQ,CAAC,SAAS,eAAe;AACjD;AAKO,SAAS,0CAAe,aAA4B;IACzD,IAAI,qCACF,oCAAc,KAAK,CAAC;AAExB;AAKO,SAAS;IACd,IAAI,qCAAe;QACjB,oCAAc,OAAO;QACrB,sCAAgB;IAClB;AACF;AAEA,2FAA2F;AAC3F,0FAA0F;AAC1F,mGAAmG;AACnG,iGAAiG;AACjG,0EAA0E;AAC1E,sHAAsH;AACtH,MAAM;IA+BJ,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SAAS,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,YAAY;QAEvC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAChC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS;QAEpC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;IACjC;AAqDF","sources":["packages/@react-aria/live-announcer/src/LiveAnnouncer.tsx"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\ntype Assertiveness = 'assertive' | 'polite';\n\n/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */\nconst LIVEREGION_TIMEOUT_DELAY = 7000;\n\nlet liveAnnouncer: LiveAnnouncer | null = null;\n\n/**\n * Announces the message using screen reader technology.\n */\nexport function announce(\n message: string,\n assertiveness: Assertiveness = 'assertive',\n timeout = LIVEREGION_TIMEOUT_DELAY\n) {\n if (!liveAnnouncer) {\n liveAnnouncer = new LiveAnnouncer();\n }\n\n liveAnnouncer.announce(message, assertiveness, timeout);\n}\n\n/**\n * Stops all queued announcements.\n */\nexport function clearAnnouncer(assertiveness: Assertiveness) {\n if (liveAnnouncer) {\n liveAnnouncer.clear(assertiveness);\n }\n}\n\n/**\n * Removes the announcer from the DOM.\n */\nexport function destroyAnnouncer() {\n if (liveAnnouncer) {\n liveAnnouncer.destroy();\n liveAnnouncer = null;\n }\n}\n\n// LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18\n// ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a\n// subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.\n// As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer\n// is simple enough to implement without React, so that's what we do here.\n// See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638\nclass LiveAnnouncer {\n node: HTMLElement | null;\n assertiveLog: HTMLElement;\n politeLog: HTMLElement;\n\n constructor() {\n this.node = document.createElement('div');\n this.node.dataset.liveAnnouncer = 'true';\n // copied from VisuallyHidden\n Object.assign(this.node.style, {\n border: 0,\n clip: 'rect(0 0 0 0)',\n clipPath: 'inset(50%)',\n height: '1px',\n margin: '-1px',\n overflow: 'hidden',\n padding: 0,\n position: 'absolute',\n width: '1px',\n whiteSpace: 'nowrap'\n });\n\n this.assertiveLog = this.createLog('assertive');\n this.node.appendChild(this.assertiveLog);\n\n this.politeLog = this.createLog('polite');\n this.node.appendChild(this.politeLog);\n\n document.body.prepend(this.node);\n }\n\n createLog(ariaLive: string) {\n let node = document.createElement('div');\n node.setAttribute('role', 'log');\n node.setAttribute('aria-live', ariaLive);\n node.setAttribute('aria-relevant', 'additions');\n return node;\n }\n\n destroy() {\n if (!this.node) {\n return;\n }\n\n document.body.removeChild(this.node);\n this.node = null;\n }\n\n announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) {\n if (!this.node) {\n return;\n }\n\n let node = document.createElement('div');\n node.textContent = message;\n\n if (assertiveness === 'assertive') {\n this.assertiveLog.appendChild(node);\n } else {\n this.politeLog.appendChild(node);\n }\n\n if (message !== '') {\n setTimeout(() => {\n node.remove();\n }, timeout);\n }\n }\n\n clear(assertiveness: Assertiveness) {\n if (!this.node) {\n return;\n }\n\n if (!assertiveness || assertiveness === 'assertive') {\n this.assertiveLog.innerHTML = '';\n }\n\n if (!assertiveness || assertiveness === 'polite') {\n this.politeLog.innerHTML = '';\n }\n }\n}\n"],"names":[],"version":3,"file":"LiveAnnouncer.module.js.map"}
1
+ {"mappings":"AAAA;;;;;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ved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\ntype Assertiveness = 'assertive' | 'polite';\n\n/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */\nconst LIVEREGION_TIMEOUT_DELAY = 7000;\n\n/**\n * Announces the message using screen reader technology.\n */\nexport function announce(\n message: string,\n assertiveness: Assertiveness = 'assertive',\n timeout = LIVEREGION_TIMEOUT_DELAY,\n mode: 'message' | 'ids' = 'message'\n) {\n if (!liveAnnouncer) {\n liveAnnouncer = new LiveAnnouncer();\n }\n\n liveAnnouncer.announce(message, assertiveness, timeout, mode);\n}\n\n/**\n * Stops all queued announcements.\n */\nexport function clearAnnouncer(assertiveness: Assertiveness) {\n if (liveAnnouncer) {\n liveAnnouncer.clear(assertiveness);\n }\n}\n\n/**\n * Removes the announcer from the DOM.\n */\nexport function destroyAnnouncer() {\n if (liveAnnouncer) {\n liveAnnouncer.destroy();\n liveAnnouncer = null;\n }\n}\n\n// LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18\n// ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a\n// subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.\n// As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer\n// is simple enough to implement without React, so that's what we do here.\n// See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638\nclass LiveAnnouncer {\n node: HTMLElement | null = null;\n assertiveLog: HTMLElement | null = null;\n politeLog: HTMLElement | null = null;\n\n constructor() {\n if (typeof document !== 'undefined') {\n this.node = document.createElement('div');\n this.node.dataset.liveAnnouncer = 'true';\n // copied from VisuallyHidden\n Object.assign(this.node.style, {\n border: 0,\n clip: 'rect(0 0 0 0)',\n clipPath: 'inset(50%)',\n height: '1px',\n margin: '-1px',\n overflow: 'hidden',\n padding: 0,\n position: 'absolute',\n width: '1px',\n whiteSpace: 'nowrap'\n });\n\n this.assertiveLog = this.createLog('assertive');\n this.node.appendChild(this.assertiveLog);\n\n this.politeLog = this.createLog('polite');\n this.node.appendChild(this.politeLog);\n\n document.body.prepend(this.node);\n }\n }\n\n createLog(ariaLive: string) {\n let node = document.createElement('div');\n node.setAttribute('role', 'log');\n node.setAttribute('aria-live', ariaLive);\n node.setAttribute('aria-relevant', 'additions');\n return node;\n }\n\n destroy() {\n if (!this.node) {\n return;\n }\n\n document.body.removeChild(this.node);\n this.node = null;\n }\n\n announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY, mode: 'message' | 'ids' = 'message') {\n if (!this.node) {\n return;\n }\n\n let node = document.createElement('div');\n if (mode === 'message') {\n node.textContent = message;\n } else {\n // To read an aria-labelledby, the element must have an appropriate role, such as img.\n node.setAttribute('role', 'img');\n node.setAttribute('aria-labelledby', message);\n }\n\n if (assertiveness === 'assertive') {\n this.assertiveLog?.appendChild(node);\n } else {\n this.politeLog?.appendChild(node);\n }\n\n if (message !== '') {\n setTimeout(() => {\n node.remove();\n }, timeout);\n }\n }\n\n clear(assertiveness: Assertiveness) {\n if (!this.node) {\n return;\n }\n\n if ((!assertiveness || assertiveness === 'assertive') && this.assertiveLog) {\n this.assertiveLog.innerHTML = '';\n }\n\n if ((!assertiveness || assertiveness === 'polite') && this.politeLog) {\n this.politeLog.innerHTML = '';\n }\n }\n}\n\n// singleton, setup immediately so that the DOM is primed for the first announcement as soon as possible\n// Safari has a race condition where the first announcement is not read if we wait until the first announce call\nlet liveAnnouncer: LiveAnnouncer | null = new LiveAnnouncer();\n"],"names":[],"version":3,"file":"LiveAnnouncer.module.js.map"}
package/dist/types.d.ts CHANGED
@@ -2,7 +2,7 @@ type Assertiveness = 'assertive' | 'polite';
2
2
  /**
3
3
  * Announces the message using screen reader technology.
4
4
  */
5
- export function announce(message: string, assertiveness?: Assertiveness, timeout?: number): void;
5
+ export function announce(message: string, assertiveness?: Assertiveness, timeout?: number, mode?: 'message' | 'ids'): void;
6
6
  /**
7
7
  * Stops all queued announcements.
8
8
  */
@@ -1 +1 @@
1
- {"mappings":"AAYA,qBAAqB,WAAW,GAAG,QAAQ,CAAC;AAO5C;;GAEG;AACH,yBACE,OAAO,EAAE,MAAM,EACf,aAAa,GAAE,aAA2B,EAC1C,OAAO,SAA2B,QAOnC;AAED;;GAEG;AACH,+BAA+B,aAAa,EAAE,aAAa,QAI1D;AAED;;GAEG;AACH,yCAKC","sources":["packages/@react-aria/live-announcer/src/packages/@react-aria/live-announcer/src/LiveAnnouncer.tsx","packages/@react-aria/live-announcer/src/packages/@react-aria/live-announcer/src/index.ts","packages/@react-aria/live-announcer/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\nexport {announce, clearAnnouncer, destroyAnnouncer} from './LiveAnnouncer';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":"AAYA,qBAAqB,WAAW,GAAG,QAAQ,CAAC;AAK5C;;GAEG;AACH,yBACE,OAAO,EAAE,MAAM,EACf,aAAa,GAAE,aAA2B,EAC1C,OAAO,SAA2B,EAClC,IAAI,GAAE,SAAS,GAAG,KAAiB,QAOpC;AAED;;GAEG;AACH,+BAA+B,aAAa,EAAE,aAAa,QAI1D;AAED;;GAEG;AACH,yCAKC","sources":["packages/@react-aria/live-announcer/src/packages/@react-aria/live-announcer/src/LiveAnnouncer.tsx","packages/@react-aria/live-announcer/src/packages/@react-aria/live-announcer/src/index.ts","packages/@react-aria/live-announcer/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\nexport {announce, clearAnnouncer, destroyAnnouncer} from './LiveAnnouncer';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-aria/live-announcer",
3
- "version": "3.0.0-nightly.3102+32574f874",
3
+ "version": "3.0.0-nightly.3110+3555336a9",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -27,5 +27,5 @@
27
27
  "publishConfig": {
28
28
  "access": "public"
29
29
  },
30
- "gitHead": "32574f874fd8a8b667a409938457819ac33f8e84"
30
+ "gitHead": "3555336a951aed44bb51e55c131dcf7176fc4b59"
31
31
  }
@@ -15,21 +15,20 @@ type Assertiveness = 'assertive' | 'polite';
15
15
  /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */
16
16
  const LIVEREGION_TIMEOUT_DELAY = 7000;
17
17
 
18
- let liveAnnouncer: LiveAnnouncer | null = null;
19
-
20
18
  /**
21
19
  * Announces the message using screen reader technology.
22
20
  */
23
21
  export function announce(
24
22
  message: string,
25
23
  assertiveness: Assertiveness = 'assertive',
26
- timeout = LIVEREGION_TIMEOUT_DELAY
24
+ timeout = LIVEREGION_TIMEOUT_DELAY,
25
+ mode: 'message' | 'ids' = 'message'
27
26
  ) {
28
27
  if (!liveAnnouncer) {
29
28
  liveAnnouncer = new LiveAnnouncer();
30
29
  }
31
30
 
32
- liveAnnouncer.announce(message, assertiveness, timeout);
31
+ liveAnnouncer.announce(message, assertiveness, timeout, mode);
33
32
  }
34
33
 
35
34
  /**
@@ -58,34 +57,36 @@ export function destroyAnnouncer() {
58
57
  // is simple enough to implement without React, so that's what we do here.
59
58
  // See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638
60
59
  class LiveAnnouncer {
61
- node: HTMLElement | null;
62
- assertiveLog: HTMLElement;
63
- politeLog: HTMLElement;
60
+ node: HTMLElement | null = null;
61
+ assertiveLog: HTMLElement | null = null;
62
+ politeLog: HTMLElement | null = null;
64
63
 
65
64
  constructor() {
66
- this.node = document.createElement('div');
67
- this.node.dataset.liveAnnouncer = 'true';
68
- // copied from VisuallyHidden
69
- Object.assign(this.node.style, {
70
- border: 0,
71
- clip: 'rect(0 0 0 0)',
72
- clipPath: 'inset(50%)',
73
- height: '1px',
74
- margin: '-1px',
75
- overflow: 'hidden',
76
- padding: 0,
77
- position: 'absolute',
78
- width: '1px',
79
- whiteSpace: 'nowrap'
80
- });
81
-
82
- this.assertiveLog = this.createLog('assertive');
83
- this.node.appendChild(this.assertiveLog);
84
-
85
- this.politeLog = this.createLog('polite');
86
- this.node.appendChild(this.politeLog);
87
-
88
- document.body.prepend(this.node);
65
+ if (typeof document !== 'undefined') {
66
+ this.node = document.createElement('div');
67
+ this.node.dataset.liveAnnouncer = 'true';
68
+ // copied from VisuallyHidden
69
+ Object.assign(this.node.style, {
70
+ border: 0,
71
+ clip: 'rect(0 0 0 0)',
72
+ clipPath: 'inset(50%)',
73
+ height: '1px',
74
+ margin: '-1px',
75
+ overflow: 'hidden',
76
+ padding: 0,
77
+ position: 'absolute',
78
+ width: '1px',
79
+ whiteSpace: 'nowrap'
80
+ });
81
+
82
+ this.assertiveLog = this.createLog('assertive');
83
+ this.node.appendChild(this.assertiveLog);
84
+
85
+ this.politeLog = this.createLog('polite');
86
+ this.node.appendChild(this.politeLog);
87
+
88
+ document.body.prepend(this.node);
89
+ }
89
90
  }
90
91
 
91
92
  createLog(ariaLive: string) {
@@ -105,18 +106,24 @@ class LiveAnnouncer {
105
106
  this.node = null;
106
107
  }
107
108
 
108
- announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) {
109
+ announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY, mode: 'message' | 'ids' = 'message') {
109
110
  if (!this.node) {
110
111
  return;
111
112
  }
112
113
 
113
114
  let node = document.createElement('div');
114
- node.textContent = message;
115
+ if (mode === 'message') {
116
+ node.textContent = message;
117
+ } else {
118
+ // To read an aria-labelledby, the element must have an appropriate role, such as img.
119
+ node.setAttribute('role', 'img');
120
+ node.setAttribute('aria-labelledby', message);
121
+ }
115
122
 
116
123
  if (assertiveness === 'assertive') {
117
- this.assertiveLog.appendChild(node);
124
+ this.assertiveLog?.appendChild(node);
118
125
  } else {
119
- this.politeLog.appendChild(node);
126
+ this.politeLog?.appendChild(node);
120
127
  }
121
128
 
122
129
  if (message !== '') {
@@ -131,12 +138,16 @@ class LiveAnnouncer {
131
138
  return;
132
139
  }
133
140
 
134
- if (!assertiveness || assertiveness === 'assertive') {
141
+ if ((!assertiveness || assertiveness === 'assertive') && this.assertiveLog) {
135
142
  this.assertiveLog.innerHTML = '';
136
143
  }
137
144
 
138
- if (!assertiveness || assertiveness === 'polite') {
145
+ if ((!assertiveness || assertiveness === 'polite') && this.politeLog) {
139
146
  this.politeLog.innerHTML = '';
140
147
  }
141
148
  }
142
149
  }
150
+
151
+ // singleton, setup immediately so that the DOM is primed for the first announcement as soon as possible
152
+ // Safari has a race condition where the first announcement is not read if we wait until the first announce call
153
+ let liveAnnouncer: LiveAnnouncer | null = new LiveAnnouncer();