@react-aria/live-announcer 3.0.6 → 3.1.0

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.
package/dist/main.js CHANGED
@@ -1,7 +1,3 @@
1
- var $9snhK$react = require("react");
2
- var $9snhK$reactdom = require("react-dom");
3
- var $9snhK$reactariavisuallyhidden = require("@react-aria/visually-hidden");
4
-
5
1
  function $parcel$exportWildcard(dest, source) {
6
2
  Object.keys(source).forEach(function(key) {
7
3
  if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
@@ -18,9 +14,6 @@ function $parcel$exportWildcard(dest, source) {
18
14
 
19
15
  return dest;
20
16
  }
21
- function $parcel$interopDefault(a) {
22
- return a && a.__esModule ? a.default : a;
23
- }
24
17
  function $parcel$export(e, n, v, s) {
25
18
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
26
19
  }
@@ -29,98 +22,77 @@ var $97cebfa4133ebec3$exports = {};
29
22
  $parcel$export($97cebfa4133ebec3$exports, "announce", () => $97cebfa4133ebec3$export$a9b970dcc4ae71a9);
30
23
  $parcel$export($97cebfa4133ebec3$exports, "clearAnnouncer", () => $97cebfa4133ebec3$export$d10ae4f68404609a);
31
24
  $parcel$export($97cebfa4133ebec3$exports, "destroyAnnouncer", () => $97cebfa4133ebec3$export$d8686216b8b81b2f);
32
-
33
-
34
-
35
25
  /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */ const $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY = 7000;
36
- let $97cebfa4133ebec3$var$liveRegionAnnouncer = /*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createRef();
37
- let $97cebfa4133ebec3$var$node = null;
38
- let $97cebfa4133ebec3$var$messageId = 0;
26
+ let $97cebfa4133ebec3$var$liveAnnouncer = null;
39
27
  function $97cebfa4133ebec3$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY) {
40
- $97cebfa4133ebec3$var$ensureInstance((announcer)=>announcer.announce(message, assertiveness, timeout)
41
- );
28
+ if (!$97cebfa4133ebec3$var$liveAnnouncer) $97cebfa4133ebec3$var$liveAnnouncer = new $97cebfa4133ebec3$var$LiveAnnouncer();
29
+ $97cebfa4133ebec3$var$liveAnnouncer.announce(message, assertiveness, timeout);
42
30
  }
43
31
  function $97cebfa4133ebec3$export$d10ae4f68404609a(assertiveness) {
44
- $97cebfa4133ebec3$var$ensureInstance((announcer)=>announcer.clear(assertiveness)
45
- );
32
+ if ($97cebfa4133ebec3$var$liveAnnouncer) $97cebfa4133ebec3$var$liveAnnouncer.clear(assertiveness);
46
33
  }
47
34
  function $97cebfa4133ebec3$export$d8686216b8b81b2f() {
48
- if ($97cebfa4133ebec3$var$liveRegionAnnouncer.current) {
49
- ($parcel$interopDefault($9snhK$reactdom)).unmountComponentAtNode($97cebfa4133ebec3$var$node);
50
- document.body.removeChild($97cebfa4133ebec3$var$node);
51
- $97cebfa4133ebec3$var$node = null;
35
+ if ($97cebfa4133ebec3$var$liveAnnouncer) {
36
+ $97cebfa4133ebec3$var$liveAnnouncer.destroy();
37
+ $97cebfa4133ebec3$var$liveAnnouncer = null;
52
38
  }
53
39
  }
54
- /**
55
- * Ensures we only have one instance of the announcer so that we don't have elements competing.
56
- */ function $97cebfa4133ebec3$var$ensureInstance(callback) {
57
- if (!$97cebfa4133ebec3$var$liveRegionAnnouncer.current) {
58
- $97cebfa4133ebec3$var$node = document.createElement('div');
59
- $97cebfa4133ebec3$var$node.dataset.liveAnnouncer = 'true';
60
- document.body.prepend($97cebfa4133ebec3$var$node);
61
- ($parcel$interopDefault($9snhK$reactdom)).render(/*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement($97cebfa4133ebec3$var$LiveRegionAnnouncer, {
62
- ref: $97cebfa4133ebec3$var$liveRegionAnnouncer
63
- }), $97cebfa4133ebec3$var$node, ()=>callback($97cebfa4133ebec3$var$liveRegionAnnouncer.current)
64
- );
65
- } else callback($97cebfa4133ebec3$var$liveRegionAnnouncer.current);
66
- }
67
- const $97cebfa4133ebec3$var$LiveRegionAnnouncer = /*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).forwardRef((_, ref)=>{
68
- let [assertiveMessages, setAssertiveMessages] = $9snhK$react.useState([]);
69
- let [politeMessages, setPoliteMessages] = $9snhK$react.useState([]);
70
- let clear = (assertiveness)=>{
71
- if (!assertiveness || assertiveness === 'assertive') setAssertiveMessages([]);
72
- if (!assertiveness || assertiveness === 'polite') setPoliteMessages([]);
73
- };
74
- let $97cebfa4133ebec3$export$a9b970dcc4ae71a9 = (message1, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY)=>{
75
- let id = $97cebfa4133ebec3$var$messageId++;
76
- if (assertiveness === 'assertive') setAssertiveMessages((messages)=>[
77
- ...messages,
78
- {
79
- id: id,
80
- text: message1
81
- }
82
- ]
83
- );
84
- else setPoliteMessages((messages)=>[
85
- ...messages,
86
- {
87
- id: id,
88
- text: message1
89
- }
90
- ]
91
- );
92
- if (message1 !== '') setTimeout(()=>{
93
- if (assertiveness === 'assertive') setAssertiveMessages((messages)=>messages.filter((message)=>message.id !== id
94
- )
95
- );
96
- else setPoliteMessages((messages)=>messages.filter((message)=>message.id !== id
97
- )
98
- );
40
+ // LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18
41
+ // ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a
42
+ // subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.
43
+ // As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer
44
+ // is simple enough to implement without React, so that's what we do here.
45
+ // See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638
46
+ class $97cebfa4133ebec3$var$LiveAnnouncer {
47
+ createLog(ariaLive) {
48
+ let node = document.createElement('div');
49
+ node.setAttribute('role', 'log');
50
+ node.setAttribute('aria-live', ariaLive);
51
+ node.setAttribute('aria-relevant', 'additions');
52
+ return node;
53
+ }
54
+ destroy() {
55
+ if (!this.node) return;
56
+ document.body.removeChild(this.node);
57
+ this.node = null;
58
+ }
59
+ announce(message, assertiveness = 'assertive', timeout = $97cebfa4133ebec3$var$LIVEREGION_TIMEOUT_DELAY) {
60
+ if (!this.node) return;
61
+ let node = document.createElement('div');
62
+ node.textContent = message;
63
+ if (assertiveness === 'assertive') this.assertiveLog.appendChild(node);
64
+ else this.politeLog.appendChild(node);
65
+ if (message !== '') setTimeout(()=>{
66
+ node.remove();
99
67
  }, timeout);
100
- };
101
- $9snhK$react.useImperativeHandle(ref, ()=>({
102
- announce: $97cebfa4133ebec3$export$a9b970dcc4ae71a9,
103
- clear: clear
104
- })
105
- );
106
- return(/*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement($9snhK$react.Fragment, null, /*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement($97cebfa4133ebec3$var$MessageBlock, {
107
- "aria-live": "assertive"
108
- }, assertiveMessages.map((message)=>/*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement("div", {
109
- key: message.id
110
- }, message.text)
111
- )), /*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement($97cebfa4133ebec3$var$MessageBlock, {
112
- "aria-live": "polite"
113
- }, politeMessages.map((message)=>/*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement("div", {
114
- key: message.id
115
- }, message.text)
116
- ))));
117
- });
118
- function $97cebfa4133ebec3$var$MessageBlock({ children: children , 'aria-live': ariaLive }) {
119
- return(/*#__PURE__*/ ($parcel$interopDefault($9snhK$react)).createElement($9snhK$reactariavisuallyhidden.VisuallyHidden, {
120
- role: "log",
121
- "aria-live": ariaLive,
122
- "aria-relevant": "additions"
123
- }, children));
68
+ }
69
+ clear(assertiveness) {
70
+ if (!this.node) return;
71
+ if (!assertiveness || assertiveness === 'assertive') this.assertiveLog.innerHTML = '';
72
+ if (!assertiveness || assertiveness === 'polite') this.politeLog.innerHTML = '';
73
+ }
74
+ constructor(){
75
+ this.node = document.createElement('div');
76
+ this.node.dataset.liveAnnouncer = 'true';
77
+ // copied from VisuallyHidden
78
+ Object.assign(this.node.style, {
79
+ border: 0,
80
+ clip: 'rect(0 0 0 0)',
81
+ clipPath: 'inset(50%)',
82
+ height: 1,
83
+ margin: '0 -1px -1px 0',
84
+ overflow: 'hidden',
85
+ padding: 0,
86
+ position: 'absolute',
87
+ width: 1,
88
+ whiteSpace: 'nowrap'
89
+ });
90
+ this.assertiveLog = this.createLog('assertive');
91
+ this.node.appendChild(this.assertiveLog);
92
+ this.politeLog = this.createLog('polite');
93
+ this.node.appendChild(this.politeLog);
94
+ document.body.prepend(this.node);
95
+ }
124
96
  }
125
97
 
126
98
 
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACsBA,EAAgE,AAAhE,4DAAgE,AAAhE,EAAgE,CAChE,KAAK,CAAC,8CAAwB,GAAG,IAAI;AAErC,GAAG,CAAC,yCAAmB,iBAAG,sCAAK,CAAC,SAAS;AACzC,GAAG,CAAC,0BAAI,GAAgB,IAAI;AAC5B,GAAG,CAAC,+BAAS,GAAG,CAAC;SAKD,yCAAQ,CACtB,OAAe,EACf,aAA4B,GAAG,CAAW,YAC1C,OAAO,GAAG,8CAAwB,EAClC,CAAC;IACD,oCAAc,EAAC,SAAS,GAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO;;AAChF,CAAC;SAKe,yCAAc,CAAC,aAA4B,EAAE,CAAC;IAC5D,oCAAc,EAAC,SAAS,GAAI,SAAS,CAAC,KAAK,CAAC,aAAa;;AAC3D,CAAC;SAKe,yCAAgB,GAAG,CAAC;IAClC,EAAE,EAAE,yCAAmB,CAAC,OAAO,EAAE,CAAC;QAChC,yCAAQ,CAAC,sBAAsB,CAAC,0BAAI;QACpC,QAAQ,CAAC,IAAI,CAA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/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\nexport * from './LiveAnnouncer';\n","/*\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\nimport React, {Fragment, ReactNode, RefObject, useImperativeHandle, useState} from 'react';\nimport ReactDOM from 'react-dom';\nimport {VisuallyHidden} from '@react-aria/visually-hidden';\n\ntype Assertiveness = 'assertive' | 'polite';\ninterface Announcer {\n announce(message: string, assertiveness: Assertiveness, timeout: number): void,\n clear(assertiveness: Assertiveness): void\n}\n\n/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */\nconst LIVEREGION_TIMEOUT_DELAY = 7000;\n\nlet liveRegionAnnouncer = React.createRef<Announcer>();\nlet node: HTMLElement = null;\nlet messageId = 0;\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 ensureInstance(announcer => announcer.announce(message, assertiveness, timeout));\n}\n\n/**\n * Stops all queued announcements.\n */\nexport function clearAnnouncer(assertiveness: Assertiveness) {\n ensureInstance(announcer => announcer.clear(assertiveness));\n}\n\n/**\n * Removes the announcer from the DOM.\n */\nexport function destroyAnnouncer() {\n if (liveRegionAnnouncer.current) {\n ReactDOM.unmountComponentAtNode(node);\n document.body.removeChild(node);\n node = null;\n }\n}\n\n/**\n * Ensures we only have one instance of the announcer so that we don't have elements competing.\n */\nfunction ensureInstance(callback: (announcer: Announcer) => void) {\n if (!liveRegionAnnouncer.current) {\n node = document.createElement('div');\n node.dataset.liveAnnouncer = 'true';\n document.body.prepend(node);\n ReactDOM.render(\n <LiveRegionAnnouncer ref={liveRegionAnnouncer} />,\n node,\n () => callback(liveRegionAnnouncer.current)\n );\n } else {\n callback(liveRegionAnnouncer.current);\n }\n}\n\nconst LiveRegionAnnouncer = React.forwardRef((_, ref: RefObject<Announcer>) => {\n let [assertiveMessages, setAssertiveMessages] = useState([]);\n let [politeMessages, setPoliteMessages] = useState([]);\n\n let clear = (assertiveness: Assertiveness) => {\n if (!assertiveness || assertiveness === 'assertive') {\n setAssertiveMessages([]);\n }\n\n if (!assertiveness || assertiveness === 'polite') {\n setPoliteMessages([]);\n }\n };\n\n let announce = (message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) => {\n let id = messageId++;\n\n if (assertiveness === 'assertive') {\n setAssertiveMessages(messages => [...messages, {id, text: message}]);\n } else {\n setPoliteMessages(messages => [...messages, {id, text: message}]);\n }\n\n if (message !== '') {\n setTimeout(() => {\n if (assertiveness === 'assertive') {\n setAssertiveMessages(messages => messages.filter(message => message.id !== id));\n } else {\n setPoliteMessages(messages => messages.filter(message => message.id !== id));\n }\n }, timeout);\n }\n };\n\n useImperativeHandle(ref, () => ({\n announce,\n clear\n }));\n\n return (\n <Fragment>\n <MessageBlock aria-live=\"assertive\">\n {assertiveMessages.map(message => <div key={message.id}>{message.text}</div>)}\n </MessageBlock>\n <MessageBlock aria-live=\"polite\">\n {politeMessages.map(message => <div key={message.id}>{message.text}</div>)}\n </MessageBlock>\n </Fragment>\n );\n});\n\ninterface MessageBlockProps {\n children: ReactNode,\n 'aria-live': Assertiveness\n }\n\nfunction MessageBlock({children, 'aria-live': ariaLive}: MessageBlockProps) {\n return (\n <VisuallyHidden\n role=\"log\"\n aria-live={ariaLive}\n aria-relevant=\"additions\">\n {children}\n </VisuallyHidden>\n );\n}\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;ACcA,EAAgE,AAAhE,4DAAgE,AAAhE,EAAgE,CAChE,KAAK,CAAC,8CAAwB,GAAG,IAAI;AAErC,GAAG,CAAC,mCAAa,GAAkB,IAAI;SAKvB,yCAAQ,CACtB,OAAe,EACf,aAA4B,GAAG,CAAW,YAC1C,OAAO,GAAG,8CAAwB,EAClC,CAAC;IACD,EAAE,GAAG,mCAAa,EAChB,mCAAa,GAAG,GAAG,CAAC,mCAAa;IAGnC,mCAAa,CAAC,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO;AACxD,CAAC;SAKe,yCAAc,CAAC,aAA4B,EAAE,CAAC;IAC5D,EAAE,EAAE,mCAAa,EACf,mCAAa,CAAC,KAAK,CAAC,aAAa;AAErC,CAAC;SAKe,yCAAgB,GAAG,CAAC;IAClC,EAAE,EAAE,mCAAa,EAAE,CAAC;QAClB,mCAAa,CAAC,OAAO;QACrB,mCAAa,GAAG,IAAI;IACtB,CAAC;AACH,CAAC;AAED,EAA2F,AAA3F,yFAA2F;AAC3F,EAA0F,AAA1F,wFAA0F;AAC1F,EAAmG,AAAnG,iGAAmG;AACnG,EAAiG,AAAjG,+FAAiG;AACjG,EAA0E,AAA1E,wEAA0E;AAC1E,EAAsH,AAAtH,oHAAsH;MAChH,mCAAa;IA+BjB,SAAS,CAAC,QAAgB,EAAE,CAAC;QAC3B,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAK;QACvC,IAAI,CAAC,YAAY,CAAC,CAAM,OAAE,CAAK;QAC/B,IAAI,CAAC,YAAY,CAAC,CAAW,YAAE,QAAQ;QACvC,IAAI,CA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I,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YAC9B,MAAM,EAAE,CAAC;YACT,IAAI,EAAE,CAAe;YACrB,QAAQ,EAAE,CAAY;YACtB,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAe;YACvB,QAAQ,EAAE,CAAQ;YAClB,OAAO,EAAE,CAAC;YACV,QAAQ,EAAE,CAAU;YACpB,KAAK,EAAE,CAAC;YACR,UAAU,EAAE,CAAQ;QACtB,CAAC;QAED,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,CAAW;QAC9C,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,YAAY;QAEvC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,CAAQ;QACxC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS;QAEpC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;IACjC,CAAC","sources":["packages/@react-aria/live-announcer/src/index.ts","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\nexport * from './LiveAnnouncer';\n","/*\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;\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;\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: 1,\n margin: '0 -1px -1px 0',\n overflow: 'hidden',\n padding: 0,\n position: 'absolute',\n width: 1,\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":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,7 +1,3 @@
1
- import $9Q8De$react, {useState as $9Q8De$useState, useImperativeHandle as $9Q8De$useImperativeHandle, Fragment as $9Q8De$Fragment} from "react";
2
- import $9Q8De$reactdom from "react-dom";
3
- import {VisuallyHidden as $9Q8De$VisuallyHidden} from "@react-aria/visually-hidden";
4
-
5
1
  function $parcel$export(e, n, v, s) {
6
2
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
3
  }
@@ -10,98 +6,77 @@ var $319e236875307eab$exports = {};
10
6
  $parcel$export($319e236875307eab$exports, "announce", () => $319e236875307eab$export$a9b970dcc4ae71a9);
11
7
  $parcel$export($319e236875307eab$exports, "clearAnnouncer", () => $319e236875307eab$export$d10ae4f68404609a);
12
8
  $parcel$export($319e236875307eab$exports, "destroyAnnouncer", () => $319e236875307eab$export$d8686216b8b81b2f);
13
-
14
-
15
-
16
9
  /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */ const $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY = 7000;
17
- let $319e236875307eab$var$liveRegionAnnouncer = /*#__PURE__*/ $9Q8De$react.createRef();
18
- let $319e236875307eab$var$node = null;
19
- let $319e236875307eab$var$messageId = 0;
10
+ let $319e236875307eab$var$liveAnnouncer = null;
20
11
  function $319e236875307eab$export$a9b970dcc4ae71a9(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY) {
21
- $319e236875307eab$var$ensureInstance((announcer)=>announcer.announce(message, assertiveness, timeout)
22
- );
12
+ if (!$319e236875307eab$var$liveAnnouncer) $319e236875307eab$var$liveAnnouncer = new $319e236875307eab$var$LiveAnnouncer();
13
+ $319e236875307eab$var$liveAnnouncer.announce(message, assertiveness, timeout);
23
14
  }
24
15
  function $319e236875307eab$export$d10ae4f68404609a(assertiveness) {
25
- $319e236875307eab$var$ensureInstance((announcer)=>announcer.clear(assertiveness)
26
- );
16
+ if ($319e236875307eab$var$liveAnnouncer) $319e236875307eab$var$liveAnnouncer.clear(assertiveness);
27
17
  }
28
18
  function $319e236875307eab$export$d8686216b8b81b2f() {
29
- if ($319e236875307eab$var$liveRegionAnnouncer.current) {
30
- $9Q8De$reactdom.unmountComponentAtNode($319e236875307eab$var$node);
31
- document.body.removeChild($319e236875307eab$var$node);
32
- $319e236875307eab$var$node = null;
19
+ if ($319e236875307eab$var$liveAnnouncer) {
20
+ $319e236875307eab$var$liveAnnouncer.destroy();
21
+ $319e236875307eab$var$liveAnnouncer = null;
33
22
  }
34
23
  }
35
- /**
36
- * Ensures we only have one instance of the announcer so that we don't have elements competing.
37
- */ function $319e236875307eab$var$ensureInstance(callback) {
38
- if (!$319e236875307eab$var$liveRegionAnnouncer.current) {
39
- $319e236875307eab$var$node = document.createElement('div');
40
- $319e236875307eab$var$node.dataset.liveAnnouncer = 'true';
41
- document.body.prepend($319e236875307eab$var$node);
42
- $9Q8De$reactdom.render(/*#__PURE__*/ $9Q8De$react.createElement($319e236875307eab$var$LiveRegionAnnouncer, {
43
- ref: $319e236875307eab$var$liveRegionAnnouncer
44
- }), $319e236875307eab$var$node, ()=>callback($319e236875307eab$var$liveRegionAnnouncer.current)
45
- );
46
- } else callback($319e236875307eab$var$liveRegionAnnouncer.current);
47
- }
48
- const $319e236875307eab$var$LiveRegionAnnouncer = /*#__PURE__*/ $9Q8De$react.forwardRef((_, ref)=>{
49
- let [assertiveMessages, setAssertiveMessages] = $9Q8De$useState([]);
50
- let [politeMessages, setPoliteMessages] = $9Q8De$useState([]);
51
- let clear = (assertiveness)=>{
52
- if (!assertiveness || assertiveness === 'assertive') setAssertiveMessages([]);
53
- if (!assertiveness || assertiveness === 'polite') setPoliteMessages([]);
54
- };
55
- let $319e236875307eab$export$a9b970dcc4ae71a9 = (message1, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY)=>{
56
- let id = $319e236875307eab$var$messageId++;
57
- if (assertiveness === 'assertive') setAssertiveMessages((messages)=>[
58
- ...messages,
59
- {
60
- id: id,
61
- text: message1
62
- }
63
- ]
64
- );
65
- else setPoliteMessages((messages)=>[
66
- ...messages,
67
- {
68
- id: id,
69
- text: message1
70
- }
71
- ]
72
- );
73
- if (message1 !== '') setTimeout(()=>{
74
- if (assertiveness === 'assertive') setAssertiveMessages((messages)=>messages.filter((message)=>message.id !== id
75
- )
76
- );
77
- else setPoliteMessages((messages)=>messages.filter((message)=>message.id !== id
78
- )
79
- );
24
+ // LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18
25
+ // ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a
26
+ // subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.
27
+ // As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer
28
+ // is simple enough to implement without React, so that's what we do here.
29
+ // See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638
30
+ class $319e236875307eab$var$LiveAnnouncer {
31
+ createLog(ariaLive) {
32
+ let node = document.createElement('div');
33
+ node.setAttribute('role', 'log');
34
+ node.setAttribute('aria-live', ariaLive);
35
+ node.setAttribute('aria-relevant', 'additions');
36
+ return node;
37
+ }
38
+ destroy() {
39
+ if (!this.node) return;
40
+ document.body.removeChild(this.node);
41
+ this.node = null;
42
+ }
43
+ announce(message, assertiveness = 'assertive', timeout = $319e236875307eab$var$LIVEREGION_TIMEOUT_DELAY) {
44
+ if (!this.node) return;
45
+ let node = document.createElement('div');
46
+ node.textContent = message;
47
+ if (assertiveness === 'assertive') this.assertiveLog.appendChild(node);
48
+ else this.politeLog.appendChild(node);
49
+ if (message !== '') setTimeout(()=>{
50
+ node.remove();
80
51
  }, timeout);
81
- };
82
- $9Q8De$useImperativeHandle(ref, ()=>({
83
- announce: $319e236875307eab$export$a9b970dcc4ae71a9,
84
- clear: clear
85
- })
86
- );
87
- return(/*#__PURE__*/ $9Q8De$react.createElement($9Q8De$Fragment, null, /*#__PURE__*/ $9Q8De$react.createElement($319e236875307eab$var$MessageBlock, {
88
- "aria-live": "assertive"
89
- }, assertiveMessages.map((message)=>/*#__PURE__*/ $9Q8De$react.createElement("div", {
90
- key: message.id
91
- }, message.text)
92
- )), /*#__PURE__*/ $9Q8De$react.createElement($319e236875307eab$var$MessageBlock, {
93
- "aria-live": "polite"
94
- }, politeMessages.map((message)=>/*#__PURE__*/ $9Q8De$react.createElement("div", {
95
- key: message.id
96
- }, message.text)
97
- ))));
98
- });
99
- function $319e236875307eab$var$MessageBlock({ children: children , 'aria-live': ariaLive }) {
100
- return(/*#__PURE__*/ $9Q8De$react.createElement($9Q8De$VisuallyHidden, {
101
- role: "log",
102
- "aria-live": ariaLive,
103
- "aria-relevant": "additions"
104
- }, children));
52
+ }
53
+ clear(assertiveness) {
54
+ if (!this.node) return;
55
+ if (!assertiveness || assertiveness === 'assertive') this.assertiveLog.innerHTML = '';
56
+ if (!assertiveness || assertiveness === 'polite') this.politeLog.innerHTML = '';
57
+ }
58
+ constructor(){
59
+ this.node = document.createElement('div');
60
+ this.node.dataset.liveAnnouncer = 'true';
61
+ // copied from VisuallyHidden
62
+ Object.assign(this.node.style, {
63
+ border: 0,
64
+ clip: 'rect(0 0 0 0)',
65
+ clipPath: 'inset(50%)',
66
+ height: 1,
67
+ margin: '0 -1px -1px 0',
68
+ overflow: 'hidden',
69
+ padding: 0,
70
+ position: 'absolute',
71
+ width: 1,
72
+ whiteSpace: 'nowrap'
73
+ });
74
+ this.assertiveLog = this.createLog('assertive');
75
+ this.node.appendChild(this.assertiveLog);
76
+ this.politeLog = this.createLog('polite');
77
+ this.node.appendChild(this.politeLog);
78
+ document.body.prepend(this.node);
79
+ }
105
80
  }
106
81
 
107
82
 
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;;;;ACsBA,EAAgE,AAAhE,4DAAgE,AAAhE,EAAgE,CAChE,KAAK,CAAC,8CAAwB,GAAG,IAAI;AAErC,GAAG,CAAC,yCAAmB,iBAAG,YAAK,CAAC,SAAS;AACzC,GAAG,CAAC,0BAAI,GAAgB,IAAI;AAC5B,GAAG,CAAC,+BAAS,GAAG,CAAC;SAKD,yCAAQ,CACtB,OAAe,EACf,aAA4B,GAAG,CAAW,YAC1C,OAAO,GAAG,8CAAwB,EAClC,CAAC;IACD,oCAAc,EAAC,SAAS,GAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO;;AAChF,CAAC;SAKe,yCAAc,CAAC,aAA4B,EAAE,CAAC;IAC5D,oCAAc,EAAC,SAAS,GAAI,SAAS,CAAC,KAAK,CAAC,aAAa;;AAC3D,CAAC;SAKe,yCAAgB,GAAG,CAAC;IAClC,EAAE,EAAE,yCAAmB,CAAC,OAAO,EAAE,CAAC;QAChC,eAAQ,CAAC,sBAAsB,CAAC,0BAAI;QACpC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,0BAAI;QAC9B,0BAAI,GAAG,IAAI;IACb,CAAC;AACH,C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aAEnE,iBAAiB,EAAC,QAAQ,GAAI,CAAC;mBAAG,QAAQ;gBAAE,CAAC;wBAAA,EAAE;oBAAE,IAAI,EAAE,QAAO;gBAAA,CAAC;YAAA,CAAC;;QAGlE,EAAE,EAAE,QAAO,KAAK,CAAE,GAChB,UAAU,KAAO,CAAC;YAChB,EAAE,EAAE,aAAa,KAAK,CAAW,YAC/B,oBAAoB,EAAC,QAAQ,GAAI,QAAQ,CAAC,MAAM,EAAC,OAAO,GAAI,OAAO,CAAC,EAAE,KAAK,EAAE;;;iBAE7E,iBAAiB,EAAC,QAAQ,GAAI,QAAQ,CAAC,MAAM,EAAC,OAAO,GAAI,OAAO,CAAC,EAAE,KAAK,EAAE;;;QAE9E,CAAC,EAAE,OAAO;IAEd,CAAC;IAED,0BAAmB,CAAC,GAAG,OAAS,CAAC;sBAC/B,yCAAQ;mBACR,KAAK;QACP,CAAC;;IAED,MAAM,0CACH,eAAQ,iDACN,kCAAY;QAAC,CAAS,YAAC,CAAW;OAChC,iBAAiB,CAAC,GAAG,EAAC,OAAO,4CAAK,CAAG;YAAC,GAAG,EAAE,OAAO,CAAC,EAAE;WAAG,OAAO,CAAC,IAAI;iDAEtE,kCAAY;QAAC,CAAS,YAAC,CAAQ;OAC7B,cAAc,CAAC,GAAG,EAAC,OAAO,4CAAK,CAAG;YAAC,GAAG,EAAE,OAAO,CAAC,EAAE;WAAG,OAAO,CAAC,IAAI;;AAI1E,CAAC;SAOQ,kCAAY,CAAC,CAAC,WAAA,QAAQ,GAAE,CAAW,YAAE,QAAQ,EAAmB,CAAC,EAAE,CAAC;IAC3E,MAAM,0CACH,qBAAc;QACb,IAAI,EAAC,CAAK;QACV,CAAS,YAAE,QAAQ;QACnB,CAAa,gBAAC,CAAW;OACxB,QAAQ;AAGf,CAAC","sources":["packages/@react-aria/live-announcer/src/index.ts","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\nexport * from './LiveAnnouncer';\n","/*\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\nimport React, {Fragment, ReactNode, RefObject, useImperativeHandle, useState} from 'react';\nimport ReactDOM from 'react-dom';\nimport {VisuallyHidden} from '@react-aria/visually-hidden';\n\ntype Assertiveness = 'assertive' | 'polite';\ninterface Announcer {\n announce(message: string, assertiveness: Assertiveness, timeout: number): void,\n clear(assertiveness: Assertiveness): void\n}\n\n/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */\nconst LIVEREGION_TIMEOUT_DELAY = 7000;\n\nlet liveRegionAnnouncer = React.createRef<Announcer>();\nlet node: HTMLElement = null;\nlet messageId = 0;\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 ensureInstance(announcer => announcer.announce(message, assertiveness, timeout));\n}\n\n/**\n * Stops all queued announcements.\n */\nexport function clearAnnouncer(assertiveness: Assertiveness) {\n ensureInstance(announcer => announcer.clear(assertiveness));\n}\n\n/**\n * Removes the announcer from the DOM.\n */\nexport function destroyAnnouncer() {\n if (liveRegionAnnouncer.current) {\n ReactDOM.unmountComponentAtNode(node);\n document.body.removeChild(node);\n node = null;\n }\n}\n\n/**\n * Ensures we only have one instance of the announcer so that we don't have elements competing.\n */\nfunction ensureInstance(callback: (announcer: Announcer) => void) {\n if (!liveRegionAnnouncer.current) {\n node = document.createElement('div');\n node.dataset.liveAnnouncer = 'true';\n document.body.prepend(node);\n ReactDOM.render(\n <LiveRegionAnnouncer ref={liveRegionAnnouncer} />,\n node,\n () => callback(liveRegionAnnouncer.current)\n );\n } else {\n callback(liveRegionAnnouncer.current);\n }\n}\n\nconst LiveRegionAnnouncer = React.forwardRef((_, ref: RefObject<Announcer>) => {\n let [assertiveMessages, setAssertiveMessages] = useState([]);\n let [politeMessages, setPoliteMessages] = useState([]);\n\n let clear = (assertiveness: Assertiveness) => {\n if (!assertiveness || assertiveness === 'assertive') {\n setAssertiveMessages([]);\n }\n\n if (!assertiveness || assertiveness === 'polite') {\n setPoliteMessages([]);\n }\n };\n\n let announce = (message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) => {\n let id = messageId++;\n\n if (assertiveness === 'assertive') {\n setAssertiveMessages(messages => [...messages, {id, text: message}]);\n } else {\n setPoliteMessages(messages => [...messages, {id, text: message}]);\n }\n\n if (message !== '') {\n setTimeout(() => {\n if (assertiveness === 'assertive') {\n setAssertiveMessages(messages => messages.filter(message => message.id !== id));\n } else {\n setPoliteMessages(messages => messages.filter(message => message.id !== id));\n }\n }, timeout);\n }\n };\n\n useImperativeHandle(ref, () => ({\n announce,\n clear\n }));\n\n return (\n <Fragment>\n <MessageBlock aria-live=\"assertive\">\n {assertiveMessages.map(message => <div key={message.id}>{message.text}</div>)}\n </MessageBlock>\n <MessageBlock aria-live=\"polite\">\n {politeMessages.map(message => <div key={message.id}>{message.text}</div>)}\n </MessageBlock>\n </Fragment>\n );\n});\n\ninterface MessageBlockProps {\n children: ReactNode,\n 'aria-live': Assertiveness\n }\n\nfunction MessageBlock({children, 'aria-live': ariaLive}: MessageBlockProps) {\n return (\n <VisuallyHidden\n role=\"log\"\n aria-live={ariaLive}\n aria-relevant=\"additions\">\n {children}\n </VisuallyHidden>\n );\n}\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;;;;;;ACcA,EAAgE,AAAhE,4DAAgE,AAAhE,EAAgE,CAChE,KAAK,CAAC,8CAAwB,GAAG,IAAI;AAErC,GAAG,CAAC,mCAAa,GAAkB,IAAI;SAKvB,yCAAQ,CACtB,OAAe,EACf,aAA4B,GAAG,CAAW,YAC1C,OAAO,GAAG,8CAAwB,EAClC,CAAC;IACD,EAAE,GAAG,mCAAa,EAChB,mCAAa,GAAG,GAAG,CAAC,mCAAa;IAGnC,mCAAa,CAAC,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO;AACxD,CAAC;SAKe,yCAAc,CAAC,aAA4B,EAAE,CAAC;IAC5D,EAAE,EAAE,mCAAa,EACf,mCAAa,CAAC,KAAK,CAAC,aAAa;AAErC,CAAC;SAKe,yCAAgB,GAAG,CAAC;IAClC,EAAE,EAAE,mCAAa,EAAE,CAAC;QAClB,mCAAa,CAAC,OAAO;QACrB,mCAAa,GAAG,IAAI;IACtB,CAAC;AACH,CAAC;AAED,EAA2F,AAA3F,yFAA2F;AAC3F,EAA0F,AAA1F,wFAA0F;AAC1F,EAAmG,AAAnG,iGAAmG;AACnG,EAAiG,AAAjG,+FAAiG;AACjG,EAA0E,AAA1E,wEAA0E;AAC1E,EAAsH,AAAtH,oHAAsH;MAChH,mCAAa;IA+BjB,SAAS,CAAC,QAAgB,EAAE,CAAC;QAC3B,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAK;QACvC,IAAI,CAAC,YAAY,CAAC,CAAM,OAAE,CAAK;QAC/B,IAAI,CAAC,YAAY,CAAC,CAAW,YAAE,QAAQ;QACvC,IAAI,CAAC,YAAY,CAAC,CAAe,gBAAE,CAAW;QAC9C,MAAM,CAAC,IAAI;IACb,CAAC;IAED,OAAO,GAAG,CA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B,QAAQ,EAAE,CAAY;YACtB,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAe;YACvB,QAAQ,EAAE,CAAQ;YAClB,OAAO,EAAE,CAAC;YACV,QAAQ,EAAE,CAAU;YACpB,KAAK,EAAE,CAAC;YACR,UAAU,EAAE,CAAQ;QACtB,CAAC;QAED,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,CAAW;QAC9C,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,YAAY;QAEvC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,CAAQ;QACxC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS;QAEpC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;IACjC,CAAC","sources":["packages/@react-aria/live-announcer/src/index.ts","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\nexport * from './LiveAnnouncer';\n","/*\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;\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;\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: 1,\n margin: '0 -1px -1px 0',\n overflow: 'hidden',\n padding: 0,\n position: 'absolute',\n width: 1,\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":"module.js.map"}
@@ -1 +1 @@
1
- {"mappings":"AAgBA,qBAAqB,WAAW,GAAG,QAAQ,CAAC;AAa5C;;GAEG;AACH,yBACE,OAAO,EAAE,MAAM,EACf,aAAa,GAAE,aAA2B,EAC1C,OAAO,SAA2B,QAGnC;AAED;;GAEG;AACH,+BAA+B,aAAa,EAAE,aAAa,QAE1D;AAED;;GAEG;AACH,yCAMC","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 */\n\nexport * from './LiveAnnouncer';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
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 */\n\nexport * 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.6",
3
+ "version": "3.1.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -17,16 +17,10 @@
17
17
  "url": "https://github.com/adobe/react-spectrum"
18
18
  },
19
19
  "dependencies": {
20
- "@babel/runtime": "^7.6.2",
21
- "@react-aria/utils": "^3.12.0",
22
- "@react-aria/visually-hidden": "^3.2.8"
23
- },
24
- "peerDependencies": {
25
- "react": "^16.8.0 || ^17.0.0-rc.1",
26
- "react-dom": "^16.8.0 || ^17.0.0-rc.1"
20
+ "@babel/runtime": "^7.6.2"
27
21
  },
28
22
  "publishConfig": {
29
23
  "access": "public"
30
24
  },
31
- "gitHead": "6a503b715e0dbbf92038cd7f08b1bcdde4c78e82"
25
+ "gitHead": "8f921ec5094e7c2b3c301bcb6133372e35a2052b"
32
26
  }
@@ -10,22 +10,12 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import React, {Fragment, ReactNode, RefObject, useImperativeHandle, useState} from 'react';
14
- import ReactDOM from 'react-dom';
15
- import {VisuallyHidden} from '@react-aria/visually-hidden';
16
-
17
13
  type Assertiveness = 'assertive' | 'polite';
18
- interface Announcer {
19
- announce(message: string, assertiveness: Assertiveness, timeout: number): void,
20
- clear(assertiveness: Assertiveness): void
21
- }
22
14
 
23
15
  /* Inspired by https://github.com/AlmeroSteyn/react-aria-live */
24
16
  const LIVEREGION_TIMEOUT_DELAY = 7000;
25
17
 
26
- let liveRegionAnnouncer = React.createRef<Announcer>();
27
- let node: HTMLElement = null;
28
- let messageId = 0;
18
+ let liveAnnouncer: LiveAnnouncer = null;
29
19
 
30
20
  /**
31
21
  * Announces the message using screen reader technology.
@@ -35,108 +25,118 @@ export function announce(
35
25
  assertiveness: Assertiveness = 'assertive',
36
26
  timeout = LIVEREGION_TIMEOUT_DELAY
37
27
  ) {
38
- ensureInstance(announcer => announcer.announce(message, assertiveness, timeout));
28
+ if (!liveAnnouncer) {
29
+ liveAnnouncer = new LiveAnnouncer();
30
+ }
31
+
32
+ liveAnnouncer.announce(message, assertiveness, timeout);
39
33
  }
40
34
 
41
35
  /**
42
36
  * Stops all queued announcements.
43
37
  */
44
38
  export function clearAnnouncer(assertiveness: Assertiveness) {
45
- ensureInstance(announcer => announcer.clear(assertiveness));
39
+ if (liveAnnouncer) {
40
+ liveAnnouncer.clear(assertiveness);
41
+ }
46
42
  }
47
43
 
48
44
  /**
49
45
  * Removes the announcer from the DOM.
50
46
  */
51
47
  export function destroyAnnouncer() {
52
- if (liveRegionAnnouncer.current) {
53
- ReactDOM.unmountComponentAtNode(node);
54
- document.body.removeChild(node);
55
- node = null;
48
+ if (liveAnnouncer) {
49
+ liveAnnouncer.destroy();
50
+ liveAnnouncer = null;
56
51
  }
57
52
  }
58
53
 
59
- /**
60
- * Ensures we only have one instance of the announcer so that we don't have elements competing.
61
- */
62
- function ensureInstance(callback: (announcer: Announcer) => void) {
63
- if (!liveRegionAnnouncer.current) {
64
- node = document.createElement('div');
65
- node.dataset.liveAnnouncer = 'true';
66
- document.body.prepend(node);
67
- ReactDOM.render(
68
- <LiveRegionAnnouncer ref={liveRegionAnnouncer} />,
69
- node,
70
- () => callback(liveRegionAnnouncer.current)
71
- );
72
- } else {
73
- callback(liveRegionAnnouncer.current);
54
+ // LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18
55
+ // ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a
56
+ // subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.
57
+ // As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer
58
+ // is simple enough to implement without React, so that's what we do here.
59
+ // See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638
60
+ class LiveAnnouncer {
61
+ node: HTMLElement;
62
+ assertiveLog: HTMLElement;
63
+ politeLog: HTMLElement;
64
+
65
+ 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: 1,
74
+ margin: '0 -1px -1px 0',
75
+ overflow: 'hidden',
76
+ padding: 0,
77
+ position: 'absolute',
78
+ width: 1,
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);
74
89
  }
75
- }
76
90
 
77
- const LiveRegionAnnouncer = React.forwardRef((_, ref: RefObject<Announcer>) => {
78
- let [assertiveMessages, setAssertiveMessages] = useState([]);
79
- let [politeMessages, setPoliteMessages] = useState([]);
91
+ createLog(ariaLive: string) {
92
+ let node = document.createElement('div');
93
+ node.setAttribute('role', 'log');
94
+ node.setAttribute('aria-live', ariaLive);
95
+ node.setAttribute('aria-relevant', 'additions');
96
+ return node;
97
+ }
80
98
 
81
- let clear = (assertiveness: Assertiveness) => {
82
- if (!assertiveness || assertiveness === 'assertive') {
83
- setAssertiveMessages([]);
99
+ destroy() {
100
+ if (!this.node) {
101
+ return;
84
102
  }
85
103
 
86
- if (!assertiveness || assertiveness === 'polite') {
87
- setPoliteMessages([]);
104
+ document.body.removeChild(this.node);
105
+ this.node = null;
106
+ }
107
+
108
+ announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) {
109
+ if (!this.node) {
110
+ return;
88
111
  }
89
- };
90
112
 
91
- let announce = (message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) => {
92
- let id = messageId++;
113
+ let node = document.createElement('div');
114
+ node.textContent = message;
93
115
 
94
116
  if (assertiveness === 'assertive') {
95
- setAssertiveMessages(messages => [...messages, {id, text: message}]);
117
+ this.assertiveLog.appendChild(node);
96
118
  } else {
97
- setPoliteMessages(messages => [...messages, {id, text: message}]);
119
+ this.politeLog.appendChild(node);
98
120
  }
99
121
 
100
122
  if (message !== '') {
101
123
  setTimeout(() => {
102
- if (assertiveness === 'assertive') {
103
- setAssertiveMessages(messages => messages.filter(message => message.id !== id));
104
- } else {
105
- setPoliteMessages(messages => messages.filter(message => message.id !== id));
106
- }
124
+ node.remove();
107
125
  }, timeout);
108
126
  }
109
- };
110
-
111
- useImperativeHandle(ref, () => ({
112
- announce,
113
- clear
114
- }));
115
-
116
- return (
117
- <Fragment>
118
- <MessageBlock aria-live="assertive">
119
- {assertiveMessages.map(message => <div key={message.id}>{message.text}</div>)}
120
- </MessageBlock>
121
- <MessageBlock aria-live="polite">
122
- {politeMessages.map(message => <div key={message.id}>{message.text}</div>)}
123
- </MessageBlock>
124
- </Fragment>
125
- );
126
- });
127
-
128
- interface MessageBlockProps {
129
- children: ReactNode,
130
- 'aria-live': Assertiveness
131
- }
132
-
133
- function MessageBlock({children, 'aria-live': ariaLive}: MessageBlockProps) {
134
- return (
135
- <VisuallyHidden
136
- role="log"
137
- aria-live={ariaLive}
138
- aria-relevant="additions">
139
- {children}
140
- </VisuallyHidden>
141
- );
127
+ }
128
+
129
+ clear(assertiveness: Assertiveness) {
130
+ if (!this.node) {
131
+ return;
132
+ }
133
+
134
+ if (!assertiveness || assertiveness === 'assertive') {
135
+ this.assertiveLog.innerHTML = '';
136
+ }
137
+
138
+ if (!assertiveness || assertiveness === 'polite') {
139
+ this.politeLog.innerHTML = '';
140
+ }
141
+ }
142
142
  }