@wistia/ui 2.11.0-beta.f716262f.bd7b993 → 2.11.0-beta.fee00fd8.a58d2f5

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/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  /*
3
- * @license @wistia/ui v2.11.0-beta.f716262f.bd7b993
3
+ * @license @wistia/ui v2.11.0-beta.fee00fd8.a58d2f5
4
4
  *
5
5
  * Copyright (c) 2024-2026, Wistia, Inc. and its affiliates.
6
6
  *
@@ -240,6 +240,7 @@ const visuallyHiddenStyle = {
240
240
  const AriaLiveContext = createContext(void 0);
241
241
  //#endregion
242
242
  //#region src/providers/AriaLiveProvider/AriaLiveProvider.tsx
243
+ const ANNOUNCEMENT_CLEAR_DELAY_MS = 5e3;
243
244
  const debugStyle = {
244
245
  position: "absolute",
245
246
  border: "3px dashed red",
@@ -249,35 +250,58 @@ const debugStyle = {
249
250
  minHeight: "50px",
250
251
  width: "calc(100% - 10px)"
251
252
  };
253
+ const LiveRegions = styled.div(({ $debug }) => $debug ? debugStyle : visuallyHiddenStyle);
254
+ const LiveRegion = ({ announcements, politeness }) => /* @__PURE__ */ jsx("div", {
255
+ "aria-live": politeness,
256
+ role: "log",
257
+ children: announcements.filter((announcement) => announcement.politeness === politeness).map((announcement) => /* @__PURE__ */ jsx("div", { children: announcement.message }, announcement.id))
258
+ });
252
259
  const AriaLiveProvider = ({ children, debug = false }) => {
253
- const [message, setMessage] = useState(null);
254
- const liveRegionRef = useRef(null);
260
+ const [announcements, setAnnouncements] = useState([]);
261
+ const nextId = useRef(0);
262
+ const pendingKeys = useRef(/* @__PURE__ */ new Set());
255
263
  useEffect(() => {
256
- const liveRegion = document.createElement("div");
257
- liveRegion.setAttribute("role", "alert");
258
- liveRegion.setAttribute("aria-live", "polite");
259
- liveRegion.setAttribute("aria-atomic", "true");
260
- Object.assign(liveRegion.style, debug ? debugStyle : visuallyHiddenStyle);
261
- document.body.append(liveRegion);
262
- liveRegionRef.current = liveRegion;
264
+ if (announcements.length === 0) return;
265
+ const soonest = Math.min(...announcements.map((announcement) => announcement.expiresAt));
266
+ const timer = setTimeout(() => {
267
+ const now = Date.now();
268
+ setAnnouncements((current) => current.filter((announcement) => announcement.expiresAt > now));
269
+ }, Math.max(soonest - Date.now(), 0));
263
270
  return () => {
264
- if (liveRegionRef.current) liveRegionRef.current.remove();
271
+ clearTimeout(timer);
265
272
  };
266
- }, [debug]);
267
- useEffect(() => {
268
- if (liveRegionRef.current && isNonEmptyString(message)) liveRegionRef.current.textContent = message;
269
- }, [message]);
270
- const announce = useCallback((announcement, politeness = "polite") => {
271
- if (liveRegionRef.current) {
272
- liveRegionRef.current.setAttribute("aria-live", politeness);
273
- setMessage(announcement);
274
- setTimeout(() => setMessage(null), 1e3);
275
- }
273
+ }, [announcements]);
274
+ const announce = useCallback((message, politeness = "polite") => {
275
+ if (!isNonEmptyString(message)) return;
276
+ const key = `${politeness}:${message}`;
277
+ if (pendingKeys.current.has(key)) return;
278
+ if (pendingKeys.current.size === 0) queueMicrotask(() => {
279
+ pendingKeys.current.clear();
280
+ });
281
+ pendingKeys.current.add(key);
282
+ const id = nextId.current;
283
+ nextId.current += 1;
284
+ const expiresAt = Date.now() + ANNOUNCEMENT_CLEAR_DELAY_MS;
285
+ setAnnouncements((current) => [...current, {
286
+ expiresAt,
287
+ id,
288
+ message,
289
+ politeness
290
+ }]);
276
291
  }, []);
277
292
  const value = useMemo(() => ({ announce }), [announce]);
278
- return /* @__PURE__ */ jsx(AriaLiveContext, {
293
+ return /* @__PURE__ */ jsxs(AriaLiveContext, {
279
294
  value,
280
- children
295
+ children: [children, /* @__PURE__ */ jsxs(LiveRegions, {
296
+ $debug: debug,
297
+ children: [/* @__PURE__ */ jsx(LiveRegion, {
298
+ announcements,
299
+ politeness: "assertive"
300
+ }), /* @__PURE__ */ jsx(LiveRegion, {
301
+ announcements,
302
+ politeness: "polite"
303
+ })]
304
+ })]
281
305
  });
282
306
  };
283
307
  //#endregion