@astryxdesign/core 0.2.0-canary.f306f13 → 0.2.0-canary.fc53ff7

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.
Files changed (162) hide show
  1. package/dist/AvatarGroup/AvatarGroupOverflow.d.ts +1 -1
  2. package/dist/AvatarGroup/AvatarGroupOverflow.d.ts.map +1 -1
  3. package/dist/AvatarGroup/AvatarGroupOverflow.js +6 -2
  4. package/dist/Button/Button.d.ts +1 -1
  5. package/dist/Button/Button.d.ts.map +1 -1
  6. package/dist/Button/Button.js +4 -2
  7. package/dist/CheckboxInput/CheckboxInput.d.ts.map +1 -1
  8. package/dist/CheckboxInput/CheckboxInput.js +2 -2
  9. package/dist/CheckboxList/CheckboxListItem.d.ts.map +1 -1
  10. package/dist/CheckboxList/CheckboxListItem.js +19 -4
  11. package/dist/CodeBlock/CodeBlock.js +2 -2
  12. package/dist/ContextMenu/ContextMenu.d.ts.map +1 -1
  13. package/dist/ContextMenu/ContextMenu.js +10 -1
  14. package/dist/DropdownMenu/DropdownMenuRadioGroup.d.ts +8 -11
  15. package/dist/DropdownMenu/DropdownMenuRadioGroup.d.ts.map +1 -1
  16. package/dist/DropdownMenu/DropdownMenuRadioGroup.js +3 -1
  17. package/dist/DropdownMenu/DropdownMenuRadioItem.d.ts +1 -1
  18. package/dist/DropdownMenu/DropdownMenuRadioItem.js +1 -1
  19. package/dist/DropdownMenu/DropdownMenuSubMenu.d.ts.map +1 -1
  20. package/dist/DropdownMenu/DropdownMenuSubMenu.js +9 -1
  21. package/dist/DropdownMenu/menuItemHover.d.ts +1 -0
  22. package/dist/DropdownMenu/menuItemHover.d.ts.map +1 -1
  23. package/dist/DropdownMenu/menuItemHover.js +1 -0
  24. package/dist/Field/FieldLabel.d.ts +2 -1
  25. package/dist/Field/FieldLabel.d.ts.map +1 -1
  26. package/dist/Field/FieldLabel.js +7 -3
  27. package/dist/FieldStatus/FieldStatus.d.ts.map +1 -1
  28. package/dist/FieldStatus/FieldStatus.js +12 -2
  29. package/dist/Item/Item.d.ts +16 -3
  30. package/dist/Item/Item.d.ts.map +1 -1
  31. package/dist/Item/Item.js +31 -6
  32. package/dist/List/ListItem.d.ts +11 -1
  33. package/dist/List/ListItem.d.ts.map +1 -1
  34. package/dist/List/ListItem.js +2 -0
  35. package/dist/MetadataList/MetadataList.d.ts +1 -1
  36. package/dist/MetadataList/MetadataList.d.ts.map +1 -1
  37. package/dist/MetadataList/MetadataList.js +4 -2
  38. package/dist/MultiSelector/MultiSelector.d.ts.map +1 -1
  39. package/dist/MultiSelector/MultiSelector.js +14 -3
  40. package/dist/RadioList/RadioListItem.d.ts.map +1 -1
  41. package/dist/RadioList/RadioListItem.js +1 -1
  42. package/dist/SegmentedControl/SegmentedControlItem.d.ts.map +1 -1
  43. package/dist/SegmentedControl/SegmentedControlItem.js +3 -2
  44. package/dist/Selector/Selector.d.ts.map +1 -1
  45. package/dist/Selector/Selector.js +5 -0
  46. package/dist/Skeleton/Skeleton.d.ts.map +1 -1
  47. package/dist/Skeleton/Skeleton.js +2 -2
  48. package/dist/Switch/Switch.d.ts.map +1 -1
  49. package/dist/Switch/Switch.js +15 -4
  50. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.d.ts.map +1 -1
  51. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.js +1 -1
  52. package/dist/Thumbnail/Thumbnail.d.ts.map +1 -1
  53. package/dist/Thumbnail/Thumbnail.js +42 -43
  54. package/dist/Toast/ToastViewport.d.ts.map +1 -1
  55. package/dist/Toast/ToastViewport.js +61 -9
  56. package/dist/ToggleButton/ToggleButton.d.ts.map +1 -1
  57. package/dist/ToggleButton/ToggleButton.js +3 -1
  58. package/dist/Token/Token.d.ts +4 -1
  59. package/dist/Token/Token.d.ts.map +1 -1
  60. package/dist/Token/Token.js +68 -43
  61. package/dist/Token/TokenLink.d.ts +62 -0
  62. package/dist/Token/TokenLink.d.ts.map +1 -0
  63. package/dist/Token/TokenLink.js +76 -0
  64. package/dist/TopNav/TopNavMegaMenu.d.ts.map +1 -1
  65. package/dist/TopNav/TopNavMegaMenu.js +9 -3
  66. package/dist/Typeahead/BaseTypeahead.d.ts +9 -0
  67. package/dist/Typeahead/BaseTypeahead.d.ts.map +1 -1
  68. package/dist/Typeahead/BaseTypeahead.js +2 -0
  69. package/dist/Typeahead/Typeahead.d.ts.map +1 -1
  70. package/dist/Typeahead/Typeahead.js +8 -1
  71. package/dist/astryx.css +51 -8
  72. package/dist/astryx.umd.js +50 -50
  73. package/dist/astryx.umd.js.map +4 -4
  74. package/dist/hooks/containerReveal.pool.stylex.d.ts +281 -0
  75. package/dist/hooks/containerReveal.pool.stylex.d.ts.map +1 -0
  76. package/dist/hooks/containerReveal.pool.stylex.js +281 -0
  77. package/dist/hooks/index.d.ts +2 -0
  78. package/dist/hooks/index.d.ts.map +1 -1
  79. package/dist/hooks/index.js +2 -1
  80. package/dist/hooks/useContainerReveal.d.ts +83 -0
  81. package/dist/hooks/useContainerReveal.d.ts.map +1 -0
  82. package/dist/hooks/useContainerReveal.js +123 -0
  83. package/dist/hooks/useInputStatusIcon.d.ts.map +1 -1
  84. package/dist/hooks/useInputStatusIcon.js +11 -0
  85. package/dist/hooks/useKeyboardHint.d.ts +1 -1
  86. package/dist/hooks/useKeyboardHint.d.ts.map +1 -1
  87. package/dist/hooks/useKeyboardHint.js +4 -2
  88. package/locales/en.json +32 -0
  89. package/locales/pseudo.json +24 -0
  90. package/package.json +1 -1
  91. package/src/AvatarGroup/AvatarGroup.test.tsx +21 -1
  92. package/src/AvatarGroup/AvatarGroupOverflow.tsx +4 -2
  93. package/src/Breadcrumbs/Breadcrumbs.test.tsx +1 -1
  94. package/src/Button/Button.test.tsx +16 -0
  95. package/src/Button/Button.tsx +4 -2
  96. package/src/CheckboxInput/CheckboxInput.test.tsx +23 -0
  97. package/src/CheckboxInput/CheckboxInput.tsx +17 -2
  98. package/src/CheckboxList/CheckboxList.test.tsx +94 -3
  99. package/src/CheckboxList/CheckboxListItem.tsx +17 -10
  100. package/src/CodeBlock/CodeBlock.test.tsx +17 -0
  101. package/src/CodeBlock/CodeBlock.tsx +2 -2
  102. package/src/ContextMenu/ContextMenu.test.tsx +17 -2
  103. package/src/ContextMenu/ContextMenu.tsx +10 -1
  104. package/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs +4 -12
  105. package/src/DropdownMenu/DropdownMenuRadioGroup.tsx +9 -10
  106. package/src/DropdownMenu/DropdownMenuRadioItem.tsx +1 -1
  107. package/src/DropdownMenu/DropdownMenuSelectable.test.tsx +20 -2
  108. package/src/DropdownMenu/DropdownMenuSubMenu.test.tsx +29 -1
  109. package/src/DropdownMenu/DropdownMenuSubMenu.tsx +13 -5
  110. package/src/DropdownMenu/menuItemHover.ts +1 -0
  111. package/src/Field/Field.doc.mjs +4 -0
  112. package/src/Field/FieldLabel.test.tsx +25 -0
  113. package/src/Field/FieldLabel.tsx +11 -2
  114. package/src/FieldStatus/FieldStatus.doc.mjs +1 -0
  115. package/src/FieldStatus/FieldStatus.test.tsx +61 -0
  116. package/src/FieldStatus/FieldStatus.tsx +14 -3
  117. package/src/Item/Item.doc.mjs +5 -0
  118. package/src/Item/Item.test.tsx +88 -0
  119. package/src/Item/Item.tsx +57 -11
  120. package/src/List/ListItem.doc.mjs +12 -0
  121. package/src/List/ListItem.tsx +13 -0
  122. package/src/MetadataList/MetadataList.test.tsx +24 -0
  123. package/src/MetadataList/MetadataList.tsx +6 -2
  124. package/src/MultiSelector/MultiSelector.test.tsx +76 -0
  125. package/src/MultiSelector/MultiSelector.tsx +28 -7
  126. package/src/RadioList/RadioList.test.tsx +26 -4
  127. package/src/RadioList/RadioListItem.tsx +8 -1
  128. package/src/SegmentedControl/SegmentedControl.test.tsx +28 -0
  129. package/src/SegmentedControl/SegmentedControlItem.tsx +20 -2
  130. package/src/Selector/Selector.test.tsx +22 -0
  131. package/src/Selector/Selector.tsx +8 -1
  132. package/src/Skeleton/Skeleton.test.tsx +16 -0
  133. package/src/Skeleton/Skeleton.tsx +13 -1
  134. package/src/Switch/Switch.test.tsx +37 -0
  135. package/src/Switch/Switch.tsx +52 -3
  136. package/src/Table/plugins/rowExpansion/useTableRowExpansion.test.tsx +19 -0
  137. package/src/Table/plugins/rowExpansion/useTableRowExpansion.tsx +3 -1
  138. package/src/Thumbnail/Thumbnail.tsx +18 -32
  139. package/src/Toast/ToastViewport.test.tsx +218 -8
  140. package/src/Toast/ToastViewport.tsx +71 -12
  141. package/src/ToggleButton/ToggleButton.test.tsx +24 -0
  142. package/src/ToggleButton/ToggleButton.tsx +19 -1
  143. package/src/Token/Token.test.tsx +145 -0
  144. package/src/Token/Token.tsx +73 -33
  145. package/src/Token/TokenLink.tsx +107 -0
  146. package/src/TopNav/TopNavMegaMenu.tsx +26 -2
  147. package/src/Typeahead/BaseTypeahead.tsx +12 -0
  148. package/src/Typeahead/Typeahead.test.tsx +62 -0
  149. package/src/Typeahead/Typeahead.tsx +6 -0
  150. package/src/__tests__/forcedColors.ts +60 -0
  151. package/src/hooks/containerReveal.pool.stylex.ts +464 -0
  152. package/src/hooks/index.ts +7 -0
  153. package/src/hooks/useContainerReveal.test.tsx +116 -0
  154. package/src/hooks/useContainerReveal.ts +180 -0
  155. package/src/hooks/useInputStatusIcon.test.tsx +59 -0
  156. package/src/hooks/useInputStatusIcon.tsx +12 -1
  157. package/src/hooks/useKeyboardHint.test.tsx +22 -0
  158. package/src/hooks/useKeyboardHint.tsx +6 -2
  159. package/dist/Thumbnail/thumbnail.markers.stylex.d.ts +0 -9
  160. package/dist/Thumbnail/thumbnail.markers.stylex.d.ts.map +0 -1
  161. package/dist/Thumbnail/thumbnail.markers.stylex.js +0 -14
  162. package/src/Thumbnail/thumbnail.markers.stylex.ts +0 -12
@@ -3,19 +3,56 @@
3
3
  /**
4
4
  * @file ToastViewport.test.tsx
5
5
  * @input Uses vitest, @testing-library/react, ToastViewport + useToast
6
- * @output Unit tests for toast keyboard reach and focus management
7
- * @position Testing; validates ToastViewport.tsx + Toast.tsx focus behavior
6
+ * @output Unit tests for toast keyboard reach, focus management, and
7
+ * screen-reader announcements
8
+ * @position Testing; validates ToastViewport.tsx + Toast.tsx focus behavior and
9
+ * the announce-at-dispatch flow through the singleton live regions
8
10
  *
9
- * SYNC: When ToastViewport.tsx or Toast.tsx focus handling changes, update these tests
11
+ * SYNC: When ToastViewport.tsx / Toast.tsx focus handling or the toast
12
+ * announcement flow changes, update these tests
10
13
  */
11
14
 
12
- import {describe, it, expect, vi, beforeAll} from 'vitest';
13
- import {render, screen, fireEvent, act} from '@testing-library/react';
15
+ import {describe, it, expect, vi, beforeAll, afterEach} from 'vitest';
16
+ import {
17
+ render,
18
+ screen,
19
+ fireEvent,
20
+ act,
21
+ waitFor,
22
+ within,
23
+ } from '@testing-library/react';
14
24
  import React from 'react';
25
+ import {type AnnounceFn, __resetLiveRegionsForTest} from '../hooks/useAnnounce';
15
26
  import {ToastViewport} from './ToastViewport';
16
27
  import {useToast} from './useToast';
17
28
  import type {ToastOptions} from './types';
18
29
 
30
+ // Spy on the announcement sink so tests can prove a toast is announced exactly
31
+ // once. The mock wraps the real useAnnounce, so the singleton live regions are
32
+ // still populated (the region-content assertions below depend on it) while
33
+ // every call is also recorded on announceSpy.
34
+ const {announceSpy} = vi.hoisted(() => ({announceSpy: vi.fn()}));
35
+ vi.mock('../hooks/useAnnounce', async importActual => {
36
+ const actual = await importActual<{
37
+ useAnnounce: () => AnnounceFn;
38
+ __resetLiveRegionsForTest: () => void;
39
+ }>();
40
+ const {useCallback} = await import('react');
41
+ return {
42
+ ...actual,
43
+ useAnnounce: (): AnnounceFn => {
44
+ const realAnnounce = actual.useAnnounce();
45
+ return useCallback<AnnounceFn>(
46
+ (message, politeness) => {
47
+ announceSpy(message, politeness);
48
+ realAnnounce(message, politeness);
49
+ },
50
+ [realAnnounce],
51
+ );
52
+ },
53
+ };
54
+ });
55
+
19
56
  // Popover API is not implemented in jsdom.
20
57
  beforeAll(() => {
21
58
  if (typeof HTMLElement.prototype.showPopover === 'undefined') {
@@ -24,6 +61,13 @@ beforeAll(() => {
24
61
  }
25
62
  });
26
63
 
64
+ // Toast text is mirrored into the singleton live regions, which outlive each
65
+ // render — reset them so text from one test never leaks into the next.
66
+ afterEach(() => {
67
+ __resetLiveRegionsForTest();
68
+ announceSpy.mockClear();
69
+ });
70
+
27
71
  // Module-level constant default props (avoids unstable-default-props lint).
28
72
  const EMPTY_OPTIONS: ToastOptions = {body: 'placeholder'};
29
73
 
@@ -169,7 +213,10 @@ describe('Toast blur timer pause', () => {
169
213
  act(() => {
170
214
  fireEvent.click(screen.getByText('Trigger Auto'));
171
215
  });
172
- expect(screen.getByText('Auto toast')).toBeInTheDocument();
216
+ // Scope to the viewport: the toast text is also mirrored into the
217
+ // singleton live region, which lives outside the notifications region.
218
+ const viewport = screen.getByRole('region', {name: 'Notifications'});
219
+ expect(within(viewport).getByText('Auto toast')).toBeInTheDocument();
173
220
 
174
221
  // Window loses focus — timer should pause.
175
222
  act(() => {
@@ -179,7 +226,7 @@ describe('Toast blur timer pause', () => {
179
226
  vi.advanceTimersByTime(5000);
180
227
  });
181
228
  // Still present because the timer was paused while blurred.
182
- expect(screen.getByText('Auto toast')).toBeInTheDocument();
229
+ expect(within(viewport).getByText('Auto toast')).toBeInTheDocument();
183
230
 
184
231
  // Window regains focus — timer resumes and the toast dismisses.
185
232
  act(() => {
@@ -196,7 +243,9 @@ describe('Toast blur timer pause', () => {
196
243
  completeExit(toastId);
197
244
  });
198
245
  }
199
- expect(screen.queryByText('Auto toast')).not.toBeInTheDocument();
246
+ expect(
247
+ within(viewport).queryByText('Auto toast'),
248
+ ).not.toBeInTheDocument();
200
249
  } finally {
201
250
  vi.useRealTimers();
202
251
  }
@@ -213,6 +262,167 @@ describe('ToastViewport region ARIA', () => {
213
262
  });
214
263
  });
215
264
 
265
+ describe('toast announcements via singleton live regions', () => {
266
+ function politeRegion(): HTMLElement | null {
267
+ return document.querySelector('[data-astryx-live-region="polite"]');
268
+ }
269
+ function assertiveRegion(): HTMLElement | null {
270
+ return document.querySelector('[data-astryx-live-region="assertive"]');
271
+ }
272
+
273
+ const RICH_INFO: ToastOptions = {
274
+ body: (
275
+ <>
276
+ <strong>Update ready</strong>
277
+ <div>Restart to apply</div>
278
+ </>
279
+ ),
280
+ };
281
+ const ERROR_TOAST: ToastOptions = {body: 'Upload failed', type: 'error'};
282
+ const SAVING_V1: ToastOptions = {uniqueID: 'save', body: 'Saving changes'};
283
+ const SAVING_V2: ToastOptions = {uniqueID: 'save', body: 'Changes saved'};
284
+ const IGNORE_KEPT: ToastOptions = {
285
+ uniqueID: 'dup',
286
+ collisionBehavior: 'ignore',
287
+ body: 'Kept',
288
+ };
289
+ const IGNORE_DROPPED: ToastOptions = {
290
+ uniqueID: 'dup',
291
+ collisionBehavior: 'ignore',
292
+ body: 'Dropped',
293
+ };
294
+
295
+ it('announces an info toast politely with its flattened text content', async () => {
296
+ renderViewport(<ShowToastButton options={RICH_INFO} triggerLabel="Show" />);
297
+ act(() => {
298
+ fireEvent.click(screen.getByText('Show'));
299
+ });
300
+ // Announced synchronously at dispatch, exactly once.
301
+ expect(announceSpy).toHaveBeenCalledTimes(1);
302
+ expect(announceSpy).toHaveBeenCalledWith(
303
+ 'Update ready Restart to apply',
304
+ 'polite',
305
+ );
306
+ // The flattened text reaches the polite singleton region (the sink).
307
+ await waitFor(() => {
308
+ expect(politeRegion()).toHaveTextContent('Update ready Restart to apply');
309
+ });
310
+ // Status toasts never touch the assertive region.
311
+ expect(assertiveRegion()).toHaveTextContent('');
312
+ });
313
+
314
+ it('announces an error toast assertively', async () => {
315
+ renderViewport(
316
+ <ShowToastButton options={ERROR_TOAST} triggerLabel="Show" />,
317
+ );
318
+ act(() => {
319
+ fireEvent.click(screen.getByText('Show'));
320
+ });
321
+ expect(announceSpy).toHaveBeenCalledTimes(1);
322
+ expect(announceSpy).toHaveBeenCalledWith('Upload failed', 'assertive');
323
+ await waitFor(() => {
324
+ expect(assertiveRegion()).toHaveTextContent('Upload failed');
325
+ });
326
+ expect(politeRegion()).toHaveTextContent('');
327
+ });
328
+
329
+ it('announces exactly once at dispatch, even under StrictMode', () => {
330
+ render(
331
+ <React.StrictMode>
332
+ <ToastViewport isTopLayer={false}>
333
+ <ShowToastButton options={INFO_A} triggerLabel="Show" />
334
+ </ToastViewport>
335
+ </React.StrictMode>,
336
+ );
337
+ act(() => {
338
+ fireEvent.click(screen.getByText('Show'));
339
+ });
340
+ // The announcement lives in the imperative dispatch path (addToast), not a
341
+ // render effect, so StrictMode's double render and double-invoked state
342
+ // updater cannot announce the same toast twice.
343
+ expect(announceSpy).toHaveBeenCalledTimes(1);
344
+ expect(announceSpy).toHaveBeenCalledWith('Toast A', 'polite');
345
+ });
346
+
347
+ it('re-announces a uniqueID toast when its content is overwritten', async () => {
348
+ renderViewport(
349
+ <>
350
+ <ShowToastButton options={SAVING_V1} triggerLabel="Show v1" />
351
+ <ShowToastButton options={SAVING_V2} triggerLabel="Show v2" />
352
+ </>,
353
+ );
354
+ act(() => {
355
+ fireEvent.click(screen.getByText('Show v1'));
356
+ });
357
+ expect(announceSpy).toHaveBeenCalledTimes(1);
358
+ expect(announceSpy).toHaveBeenLastCalledWith('Saving changes', 'polite');
359
+ // Overwriting via uniqueID replaces the toast in place — the new content is
360
+ // a fresh dispatch and must be announced again.
361
+ act(() => {
362
+ fireEvent.click(screen.getByText('Show v2'));
363
+ });
364
+ expect(announceSpy).toHaveBeenCalledTimes(2);
365
+ expect(announceSpy).toHaveBeenLastCalledWith('Changes saved', 'polite');
366
+ await waitFor(() => {
367
+ expect(politeRegion()).toHaveTextContent('Changes saved');
368
+ });
369
+ // Still a single toast on screen — overwritten in place, not stacked.
370
+ const viewport = screen.getByRole('region', {name: 'Notifications'});
371
+ expect(
372
+ within(viewport).queryAllByText(/Saving changes|Changes saved/),
373
+ ).toHaveLength(1);
374
+ });
375
+
376
+ it('does not re-announce an unchanged toast when an unrelated render occurs', () => {
377
+ renderViewport(
378
+ <>
379
+ <ShowToastButton options={INFO_A} triggerLabel="Show A" />
380
+ <ShowToastButton options={ERROR_TOAST} triggerLabel="Show B" />
381
+ </>,
382
+ );
383
+ act(() => {
384
+ fireEvent.click(screen.getByText('Show A'));
385
+ });
386
+ expect(announceSpy).toHaveBeenCalledTimes(1);
387
+ // A second toast arriving re-renders the viewport with a new toast list.
388
+ // Toast A is not re-dispatched, so it must not be announced again — only
389
+ // the newly dispatched toast produces a call.
390
+ act(() => {
391
+ fireEvent.click(screen.getByText('Show B'));
392
+ });
393
+ expect(announceSpy).toHaveBeenCalledTimes(2);
394
+ expect(announceSpy).toHaveBeenNthCalledWith(1, 'Toast A', 'polite');
395
+ expect(announceSpy).toHaveBeenNthCalledWith(
396
+ 2,
397
+ 'Upload failed',
398
+ 'assertive',
399
+ );
400
+ });
401
+
402
+ it('does not announce a toast whose uniqueID collision is ignored', () => {
403
+ renderViewport(
404
+ <>
405
+ <ShowToastButton options={IGNORE_KEPT} triggerLabel="Show 1" />
406
+ <ShowToastButton options={IGNORE_DROPPED} triggerLabel="Show 2" />
407
+ </>,
408
+ );
409
+ act(() => {
410
+ fireEvent.click(screen.getByText('Show 1'));
411
+ });
412
+ expect(announceSpy).toHaveBeenCalledTimes(1);
413
+ expect(announceSpy).toHaveBeenLastCalledWith('Kept', 'polite');
414
+ // The colliding toast is suppressed (collisionBehavior: 'ignore'), so it is
415
+ // neither shown nor announced.
416
+ act(() => {
417
+ fireEvent.click(screen.getByText('Show 2'));
418
+ });
419
+ expect(announceSpy).toHaveBeenCalledTimes(1);
420
+ const viewport = screen.getByRole('region', {name: 'Notifications'});
421
+ expect(within(viewport).getByText('Kept')).toBeInTheDocument();
422
+ expect(within(viewport).queryByText('Dropped')).not.toBeInTheDocument();
423
+ });
424
+ });
425
+
216
426
  describe('toast timer lifecycle (#3589)', () => {
217
427
  it('fires onHide exactly once when dismissed twice during the exit window', () => {
218
428
  const onHide = vi.fn();
@@ -3,6 +3,7 @@
3
3
  'use client';
4
4
 
5
5
  import {
6
+ isValidElement,
6
7
  useCallback,
7
8
  useEffect,
8
9
  useLayoutEffect,
@@ -10,10 +11,12 @@ import {
10
11
  useRef,
11
12
  useState,
12
13
  } from 'react';
14
+ import type {ReactNode} from 'react';
13
15
  import * as stylex from '@stylexjs/stylex';
14
16
  import {spacingVars, durationVars, easeVars} from '../theme/tokens.stylex';
15
17
  import {mergeProps} from '../utils';
16
18
  import {INTERACTIVE_SELECTORS} from '../hooks/useClickableContainer';
19
+ import {useAnnounce} from '../hooks/useAnnounce';
17
20
  import {Toast} from './Toast';
18
21
  import {ToastContext, type ToastContextValue} from './ToastContext';
19
22
  import type {ToastEntry, ToastPosition, ToastDismissReason} from './types';
@@ -76,6 +79,29 @@ const styles = stylex.create({
76
79
  },
77
80
  });
78
81
 
82
+ // Flatten a toast's rendered content (title, description, etc.) to the plain
83
+ // text that should be spoken by a screen reader. Only text is announced —
84
+ // interactive endContent is deliberately excluded (it is reachable via F6).
85
+ function getNodeText(node: ReactNode): string {
86
+ if (node == null || typeof node === 'boolean') {
87
+ return '';
88
+ }
89
+ if (typeof node === 'string') {
90
+ return node;
91
+ }
92
+ if (typeof node === 'number') {
93
+ return String(node);
94
+ }
95
+ if (Array.isArray(node)) {
96
+ return node.map(getNodeText).filter(Boolean).join(' ');
97
+ }
98
+ if (isValidElement(node)) {
99
+ const {children} = node.props as {children?: ReactNode};
100
+ return getNodeText(children);
101
+ }
102
+ return '';
103
+ }
104
+
79
105
  export interface ToastViewportProps {
80
106
  position?: ToastPosition;
81
107
  maxVisible?: number;
@@ -159,21 +185,54 @@ export function ToastViewport({
159
185
  [],
160
186
  );
161
187
 
162
- const addToast = useCallback((entry: ToastEntry) => {
163
- setToasts(prev => {
188
+ // Announce toasts through the persistent singleton live regions. Each
189
+ // <Toast> also renders its own role="status"/"alert" region, but that region
190
+ // is "born with content" — mounted together with its text — which many
191
+ // screen readers do not announce (see useAnnounce.ts); the singleton regions
192
+ // are mounted empty and only mutated afterwards, so they are what actually
193
+ // guarantees the announcement (the per-toast markup is kept for browse-mode
194
+ // discoverability). The announcement happens in addToast — the imperative
195
+ // dispatch path invoked once per useToast() call from an event handler,
196
+ // never from render — so each toast is announced exactly once by
197
+ // construction, independent of the React render lifecycle (StrictMode
198
+ // double-render/double-effect, viewport remounts, and unrelated list
199
+ // re-renders never re-announce). It is client-only (addToast never runs
200
+ // during SSR), so it is SSR-safe.
201
+ const announce = useAnnounce();
202
+
203
+ const addToast = useCallback(
204
+ (entry: ToastEntry) => {
164
205
  const {uniqueID, collisionBehavior = 'overwrite'} = entry.options;
165
- if (uniqueID) {
166
- const existing = prev.find(t => t.options.uniqueID === uniqueID);
167
- if (existing) {
168
- if (collisionBehavior === 'ignore') {
169
- return prev;
206
+ // Resolve an ignored collision synchronously against the committed list
207
+ // so a suppressed toast is neither shown nor announced. The remaining
208
+ // announce + setToasts run outside the setToasts updater, which React may
209
+ // invoke more than once — keeping the announcement exactly-once.
210
+ if (
211
+ uniqueID &&
212
+ collisionBehavior === 'ignore' &&
213
+ toastsRef.current.some(t => t.options.uniqueID === uniqueID)
214
+ ) {
215
+ return;
216
+ }
217
+ const text = getNodeText(entry.options.body);
218
+ if (text) {
219
+ // Error toasts map to the assertive region (role="alert"); everything
220
+ // else to the polite region (role="status") — mirrors Toast.tsx.
221
+ announce(text, entry.options.type === 'error' ? 'assertive' : 'polite');
222
+ }
223
+ setToasts(prev => {
224
+ if (uniqueID) {
225
+ const existing = prev.find(t => t.options.uniqueID === uniqueID);
226
+ if (existing) {
227
+ // An ignored collision already returned above; overwrite in place.
228
+ return prev.map(t => (t.options.uniqueID === uniqueID ? entry : t));
170
229
  }
171
- return prev.map(t => (t.options.uniqueID === uniqueID ? entry : t));
172
230
  }
173
- }
174
- return [...prev, entry];
175
- });
176
- }, []);
231
+ return [...prev, entry];
232
+ });
233
+ },
234
+ [announce],
235
+ );
177
236
 
178
237
  const removeToast = useCallback((id: string, reason: ToastDismissReason) => {
179
238
  // An exiting toast stays in toastsRef until its exit transition ends, so
@@ -14,6 +14,10 @@ import userEvent from '@testing-library/user-event';
14
14
  import {useState, type MouseEvent} from 'react';
15
15
  import {ToggleButton} from './ToggleButton';
16
16
  import {ToggleButtonGroup} from './ToggleButtonGroup';
17
+ import {
18
+ getAllInjectedCss,
19
+ getForcedColorsRules,
20
+ } from '../__tests__/forcedColors';
17
21
 
18
22
  // =============================================================================
19
23
  // ToggleButton — Standalone
@@ -517,3 +521,23 @@ describe('ToggleButtonGroup (multiple)', () => {
517
521
  );
518
522
  });
519
523
  });
524
+
525
+ // jsdom cannot emulate forced-colors rendering, so these assert that the
526
+ // compiled output includes the forced-colors rules; visual behavior needs
527
+ // manual verification under Windows High Contrast.
528
+ describe('forced colors (WCAG 1.4.11)', () => {
529
+ it('compiles forced-colors overrides so the pressed state survives Windows High Contrast', () => {
530
+ render(<ToggleButton label="Bold" isPressed onPressedChange={() => {}} />);
531
+ const css = getForcedColorsRules();
532
+ // The painted pressed overlay is stripped; Highlight/HighlightText marks
533
+ // the pressed toggle (critical for icon-only toggles, which otherwise
534
+ // lose all pressed indication).
535
+ expect(css).toContain('background-color: highlight;');
536
+ expect(css).toContain('color: highlighttext;');
537
+ // ToggleButton renders a <button>; without opting out of UA remapping it
538
+ // keeps the native ButtonFace surface and ignores the Highlight fill,
539
+ // leaving HighlightText text on a white surface. forced-color-adjust: none
540
+ // makes both render as authored.
541
+ expect(getAllInjectedCss()).toContain('forced-color-adjust: none;');
542
+ });
543
+ });
@@ -40,7 +40,25 @@ import {themeProps} from '../utils/themeProps';
40
40
  */
41
41
  const pressedStyles = stylex.create({
42
42
  background: {
43
- backgroundColor: colorVars['--color-overlay-pressed'],
43
+ // forced-color-adjust must be `none` here: ToggleButton renders a <button>,
44
+ // and the UA keeps native form-control colors (ButtonFace surface) for it
45
+ // under forced colors, ignoring the authored Highlight fill — the label kept
46
+ // its HighlightText color, giving white text on a white surface. Opting the
47
+ // pressed button out of UA remapping makes both the Highlight surface and
48
+ // the HighlightText label render as authored, restoring figure-ground.
49
+ forcedColorAdjust: 'none',
50
+ backgroundColor: {
51
+ default: colorVars['--color-overlay-pressed'],
52
+ // Forced colors (Windows High Contrast) strips the painted pressed
53
+ // overlay, which would leave icon-only toggles with no pressed
54
+ // indication at all. Highlight/HighlightText is the platform convention
55
+ // for a selected/pressed control (WCAG 1.4.11).
56
+ '@media (forced-colors: active)': 'Highlight',
57
+ },
58
+ color: {
59
+ default: null,
60
+ '@media (forced-colors: active)': 'HighlightText',
61
+ },
44
62
  },
45
63
  });
46
64
 
@@ -330,6 +330,151 @@ describe('Token accessibility', () => {
330
330
  });
331
331
  });
332
332
 
333
+ describe('Token link with remove button', () => {
334
+ it('does not nest the remove button inside the link', () => {
335
+ render(<Token label="Tag" href="/test" onRemove={() => {}} />);
336
+ const link = screen.getByRole('link', {name: 'Tag'});
337
+ const removeButton = screen.getByRole('button', {name: 'Remove Tag'});
338
+ expect(link.contains(removeButton)).toBe(false);
339
+ });
340
+
341
+ it('keeps the link keyboard-focusable and fires onRemove from the sibling button', async () => {
342
+ const user = userEvent.setup();
343
+ const handleRemove = vi.fn();
344
+ render(<Token label="Tag" href="/test" onRemove={handleRemove} />);
345
+
346
+ const link = screen.getByRole('link', {name: 'Tag'});
347
+ expect(link.tagName).toBe('A');
348
+ expect(link).toHaveAttribute('href', '/test');
349
+
350
+ // Tab reaches the link first, then the remove button
351
+ await user.tab();
352
+ expect(link).toHaveFocus();
353
+ await user.tab();
354
+ const removeButton = screen.getByRole('button', {name: 'Remove Tag'});
355
+ expect(removeButton).toHaveFocus();
356
+
357
+ // Activating the remove button fires onRemove without involving the link
358
+ await user.keyboard('{Enter}');
359
+ expect(handleRemove).toHaveBeenCalledTimes(1);
360
+ });
361
+
362
+ it('wrapper structure matches the onClick branch pattern', () => {
363
+ render(
364
+ <Token
365
+ label="Tag"
366
+ href="/test"
367
+ onRemove={() => {}}
368
+ data-testid="link-token"
369
+ />,
370
+ );
371
+ render(
372
+ <Token
373
+ label="Tag"
374
+ onClick={() => {}}
375
+ onRemove={() => {}}
376
+ data-testid="click-token"
377
+ />,
378
+ );
379
+
380
+ const linkRoot = screen.getByTestId('link-token');
381
+ const clickRoot = screen.getByTestId('click-token');
382
+
383
+ // Both render a <span> container with the same visual classes
384
+ expect(linkRoot.tagName).toBe('SPAN');
385
+ expect(linkRoot.className).toBe(clickRoot.className);
386
+ });
387
+
388
+ it('clicking the container activates the link', () => {
389
+ render(
390
+ <Token
391
+ label="Tag"
392
+ href="/test"
393
+ onRemove={() => {}}
394
+ icon={<span data-testid="icon">★</span>}
395
+ data-testid="link-token"
396
+ />,
397
+ );
398
+ const link = screen.getByRole('link', {name: 'Tag'});
399
+ const handleLinkClick = vi.fn((e: MouseEvent) => e.preventDefault());
400
+ link.addEventListener('click', handleLinkClick);
401
+
402
+ // Clicking outside the anchor (e.g. the icon) still activates the link
403
+ fireEvent.click(screen.getByTestId('icon'));
404
+ expect(handleLinkClick).toHaveBeenCalledTimes(1);
405
+ });
406
+
407
+ it('clicking the remove button does not activate the link', () => {
408
+ const handleRemove = vi.fn();
409
+ render(<Token label="Tag" href="/test" onRemove={handleRemove} />);
410
+ const link = screen.getByRole('link', {name: 'Tag'});
411
+ const handleLinkClick = vi.fn((e: MouseEvent) => e.preventDefault());
412
+ link.addEventListener('click', handleLinkClick);
413
+
414
+ fireEvent.click(screen.getByRole('button', {name: 'Remove Tag'}));
415
+ expect(handleRemove).toHaveBeenCalledTimes(1);
416
+ expect(handleLinkClick).not.toHaveBeenCalled();
417
+ });
418
+
419
+ it('cmd/ctrl+click on the container opens the link in a new tab', () => {
420
+ const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null);
421
+ render(
422
+ <Token
423
+ label="Tag"
424
+ href="/test"
425
+ onRemove={() => {}}
426
+ icon={<span data-testid="icon">★</span>}
427
+ />,
428
+ );
429
+
430
+ // Clicking the surface (not the anchor) with a modifier opens a new tab
431
+ // rather than navigating in place — provided by useClickableContainer.
432
+ fireEvent.click(screen.getByTestId('icon'), {metaKey: true});
433
+ expect(openSpy).toHaveBeenCalledWith('/test', '_blank', 'noopener');
434
+ openSpy.mockRestore();
435
+ });
436
+
437
+ it('middle-click on the container opens the link in a new tab', () => {
438
+ const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null);
439
+ render(
440
+ <Token
441
+ label="Tag"
442
+ href="/test"
443
+ onRemove={() => {}}
444
+ icon={<span data-testid="icon">★</span>}
445
+ />,
446
+ );
447
+
448
+ fireEvent.mouseUp(screen.getByTestId('icon'), {button: 1});
449
+ expect(openSpy).toHaveBeenCalledWith('/test', '_blank', 'noopener');
450
+ openSpy.mockRestore();
451
+ });
452
+
453
+ it('a modified click that opens a new tab does not fire onRemove', () => {
454
+ const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null);
455
+ const handleRemove = vi.fn();
456
+ render(
457
+ <Token
458
+ label="Tag"
459
+ href="/test"
460
+ onRemove={handleRemove}
461
+ icon={<span data-testid="icon">★</span>}
462
+ />,
463
+ );
464
+
465
+ fireEvent.click(screen.getByTestId('icon'), {metaKey: true});
466
+ fireEvent.mouseUp(screen.getByTestId('icon'), {button: 1});
467
+ expect(handleRemove).not.toHaveBeenCalled();
468
+ expect(openSpy).toHaveBeenCalledTimes(2);
469
+ openSpy.mockRestore();
470
+ });
471
+
472
+ it('link without onRemove still renders the anchor as the root', () => {
473
+ const {container} = render(<Token label="Link" href="/test" />);
474
+ expect(container.firstElementChild?.tagName).toBe('A');
475
+ });
476
+ });
477
+
333
478
  describe('Token text overflow', () => {
334
479
  it('label element has overflow hidden and text-overflow ellipsis styles', () => {
335
480
  const {container} = render(<Token label="A very long label text" />);