@axinom/mosaic-ui 0.79.0-rc.8 → 0.79.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.
Files changed (120) hide show
  1. package/dist/cjs/components/Accordion/Accordion.d.ts.map +1 -1
  2. package/dist/cjs/components/Accordion/AccordionItem/AccordionItem.d.ts.map +1 -1
  3. package/dist/cjs/components/Buttons/Button/Button.d.ts.map +1 -1
  4. package/dist/cjs/components/Buttons/Button/Button.model.d.ts +19 -2
  5. package/dist/cjs/components/Buttons/Button/Button.model.d.ts.map +1 -1
  6. package/dist/cjs/components/DateTime/DatePicker/DatePicker.d.ts.map +1 -1
  7. package/dist/cjs/components/DynamicDataList/DynamicDataList.model.d.ts +3 -1
  8. package/dist/cjs/components/DynamicDataList/DynamicDataList.model.d.ts.map +1 -1
  9. package/dist/cjs/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.d.ts.map +1 -1
  10. package/dist/cjs/components/DynamicDataList/DynamicListDataEntry/Renderers/createInputRenderer/createInputRenderer.d.ts.map +1 -1
  11. package/dist/cjs/components/DynamicDataList/DynamicListRow/DynamicListRow.d.ts.map +1 -1
  12. package/dist/cjs/components/Filters/Filter/Filter.d.ts.map +1 -1
  13. package/dist/cjs/components/Filters/SelectionTypes/DateTimeFilter/DateTimeFilter.d.ts.map +1 -1
  14. package/dist/cjs/components/FormElements/CustomTags/CustomTags.d.ts.map +1 -1
  15. package/dist/cjs/components/FormElements/DateTimeField/DateTimeText.d.ts.map +1 -1
  16. package/dist/cjs/components/FormElements/FormGroup/FormGroup.d.ts.map +1 -1
  17. package/dist/cjs/components/FormElements/ReadOnlyText/ReadOnlyTextField.d.ts +1 -1
  18. package/dist/cjs/components/FormElements/ReadOnlyText/ReadOnlyTextField.d.ts.map +1 -1
  19. package/dist/cjs/components/FormElements/Select/Select.d.ts.map +1 -1
  20. package/dist/cjs/components/FormElements/Tags/Tags.d.ts.map +1 -1
  21. package/dist/cjs/components/InlineMenu/InlineMenu.d.ts.map +1 -1
  22. package/dist/cjs/components/List/ListCheckBox/ListCheckBox.d.ts +2 -0
  23. package/dist/cjs/components/List/ListCheckBox/ListCheckBox.d.ts.map +1 -1
  24. package/dist/cjs/components/List/ListHeader/ListHeader.d.ts.map +1 -1
  25. package/dist/cjs/components/List/ListRow/ListRow.d.ts.map +1 -1
  26. package/dist/cjs/components/Modal/Modal.d.ts +9 -1
  27. package/dist/cjs/components/Modal/Modal.d.ts.map +1 -1
  28. package/dist/cjs/components/Modal/OverlayStack/overlayStack.d.ts +39 -0
  29. package/dist/cjs/components/Modal/OverlayStack/overlayStack.d.ts.map +1 -0
  30. package/dist/cjs/components/Modal/OverlayStack/useOverlayLayer.d.ts +20 -0
  31. package/dist/cjs/components/Modal/OverlayStack/useOverlayLayer.d.ts.map +1 -0
  32. package/dist/cjs/components/Modal/focusTrap.d.ts +9 -0
  33. package/dist/cjs/components/Modal/focusTrap.d.ts.map +1 -0
  34. package/dist/cjs/components/Modal/index.d.ts +1 -0
  35. package/dist/cjs/components/Modal/index.d.ts.map +1 -1
  36. package/dist/cjs/components/Tabs/TabList/ScrollContainer/ScrollContainer.d.ts.map +1 -1
  37. package/dist/cjs/index.js +4 -4
  38. package/dist/cjs/index.js.map +1 -1
  39. package/dist/cjs/initialize.d.ts +7 -0
  40. package/dist/cjs/initialize.d.ts.map +1 -1
  41. package/dist/esm/components/Accordion/Accordion.d.ts.map +1 -1
  42. package/dist/esm/components/Accordion/AccordionItem/AccordionItem.d.ts.map +1 -1
  43. package/dist/esm/components/Buttons/Button/Button.d.ts.map +1 -1
  44. package/dist/esm/components/Buttons/Button/Button.model.d.ts +19 -2
  45. package/dist/esm/components/Buttons/Button/Button.model.d.ts.map +1 -1
  46. package/dist/esm/components/DateTime/DatePicker/DatePicker.d.ts.map +1 -1
  47. package/dist/esm/components/DynamicDataList/DynamicDataList.model.d.ts +3 -1
  48. package/dist/esm/components/DynamicDataList/DynamicDataList.model.d.ts.map +1 -1
  49. package/dist/esm/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.d.ts.map +1 -1
  50. package/dist/esm/components/DynamicDataList/DynamicListDataEntry/Renderers/createInputRenderer/createInputRenderer.d.ts.map +1 -1
  51. package/dist/esm/components/DynamicDataList/DynamicListRow/DynamicListRow.d.ts.map +1 -1
  52. package/dist/esm/components/Filters/Filter/Filter.d.ts.map +1 -1
  53. package/dist/esm/components/Filters/SelectionTypes/DateTimeFilter/DateTimeFilter.d.ts.map +1 -1
  54. package/dist/esm/components/FormElements/CustomTags/CustomTags.d.ts.map +1 -1
  55. package/dist/esm/components/FormElements/DateTimeField/DateTimeText.d.ts.map +1 -1
  56. package/dist/esm/components/FormElements/FormGroup/FormGroup.d.ts.map +1 -1
  57. package/dist/esm/components/FormElements/ReadOnlyText/ReadOnlyTextField.d.ts +1 -1
  58. package/dist/esm/components/FormElements/ReadOnlyText/ReadOnlyTextField.d.ts.map +1 -1
  59. package/dist/esm/components/FormElements/Select/Select.d.ts.map +1 -1
  60. package/dist/esm/components/FormElements/Tags/Tags.d.ts.map +1 -1
  61. package/dist/esm/components/InlineMenu/InlineMenu.d.ts.map +1 -1
  62. package/dist/esm/components/List/ListCheckBox/ListCheckBox.d.ts +2 -0
  63. package/dist/esm/components/List/ListCheckBox/ListCheckBox.d.ts.map +1 -1
  64. package/dist/esm/components/List/ListHeader/ListHeader.d.ts.map +1 -1
  65. package/dist/esm/components/List/ListRow/ListRow.d.ts.map +1 -1
  66. package/dist/esm/components/Modal/Modal.d.ts +9 -1
  67. package/dist/esm/components/Modal/Modal.d.ts.map +1 -1
  68. package/dist/esm/components/Modal/OverlayStack/overlayStack.d.ts +39 -0
  69. package/dist/esm/components/Modal/OverlayStack/overlayStack.d.ts.map +1 -0
  70. package/dist/esm/components/Modal/OverlayStack/useOverlayLayer.d.ts +20 -0
  71. package/dist/esm/components/Modal/OverlayStack/useOverlayLayer.d.ts.map +1 -0
  72. package/dist/esm/components/Modal/focusTrap.d.ts +9 -0
  73. package/dist/esm/components/Modal/focusTrap.d.ts.map +1 -0
  74. package/dist/esm/components/Modal/index.d.ts +1 -0
  75. package/dist/esm/components/Modal/index.d.ts.map +1 -1
  76. package/dist/esm/components/Tabs/TabList/ScrollContainer/ScrollContainer.d.ts.map +1 -1
  77. package/dist/esm/index.js +4 -4
  78. package/dist/esm/index.js.map +1 -1
  79. package/dist/esm/initialize.d.ts +7 -0
  80. package/dist/esm/initialize.d.ts.map +1 -1
  81. package/package.json +2 -2
  82. package/src/components/Accordion/Accordion.tsx +1 -0
  83. package/src/components/Accordion/AccordionItem/AccordionItem.tsx +1 -0
  84. package/src/components/Buttons/Button/Button.model.ts +21 -1
  85. package/src/components/Buttons/Button/Button.stories.tsx +1 -0
  86. package/src/components/Buttons/Button/Button.tsx +10 -0
  87. package/src/components/DateTime/DatePicker/DatePicker.tsx +2 -0
  88. package/src/components/DynamicDataList/DynamicDataList.model.ts +2 -0
  89. package/src/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.tsx +17 -2
  90. package/src/components/DynamicDataList/DynamicListDataEntry/Renderers/createInputRenderer/createInputRenderer.tsx +2 -0
  91. package/src/components/DynamicDataList/DynamicListRow/DynamicListRow.tsx +7 -1
  92. package/src/components/FieldSelection/FieldSelection.tsx +1 -0
  93. package/src/components/Filters/Filter/Filter.tsx +4 -0
  94. package/src/components/Filters/SelectionTypes/DateTimeFilter/DateTimeFilter.tsx +1 -0
  95. package/src/components/FormElements/CustomTags/CustomTags.tsx +2 -0
  96. package/src/components/FormElements/DateTimeField/DateTimeText.tsx +1 -0
  97. package/src/components/FormElements/FormGroup/FormGroup.tsx +1 -0
  98. package/src/components/FormElements/ReadOnlyText/ReadOnlyTextField.tsx +5 -0
  99. package/src/components/FormElements/Select/Select.tsx +1 -0
  100. package/src/components/FormElements/Tags/Tags.tsx +1 -0
  101. package/src/components/InlineMenu/InlineMenu.tsx +1 -0
  102. package/src/components/List/ListCheckBox/ListCheckBox.tsx +5 -1
  103. package/src/components/List/ListHeader/ListHeader.tsx +1 -0
  104. package/src/components/List/ListRow/ListRow.tsx +3 -0
  105. package/src/components/Modal/Modal.module.scss +15 -0
  106. package/src/components/Modal/Modal.spec.tsx +328 -1
  107. package/src/components/Modal/Modal.stories.tsx +65 -0
  108. package/src/components/Modal/Modal.tsx +106 -4
  109. package/src/components/Modal/OverlayStack/overlayStack.spec.ts +162 -0
  110. package/src/components/Modal/OverlayStack/overlayStack.ts +111 -0
  111. package/src/components/Modal/OverlayStack/useOverlayLayer.ts +67 -0
  112. package/src/components/Modal/focusTrap.spec.ts +44 -0
  113. package/src/components/Modal/focusTrap.ts +92 -0
  114. package/src/components/Modal/index.ts +6 -0
  115. package/src/components/NavigationAwareStation/NavigationAwareStation.stories.tsx +1 -0
  116. package/src/components/PageHeader/PageHeader.stories.tsx +0 -1
  117. package/src/components/PageHeader/PageHeaderAction/PageHeaderAction.stories.tsx +5 -1
  118. package/src/components/Tabs/TabList/ScrollContainer/ScrollContainer.tsx +2 -0
  119. package/src/initialize.ts +12 -0
  120. package/src/styles/variables.scss +2 -0
@@ -1,7 +1,15 @@
1
1
  import { fireEvent, render, screen } from '@testing-library/react';
2
- import React from 'react';
2
+ import React, { useContext } from 'react';
3
3
  import { describe, expect, it, vi } from 'vitest';
4
+ import { noop } from '../../helpers/utils';
5
+ import { type UiConfig, initializeUi } from '../../initialize';
6
+ import { BulkEditContext } from '../Explorer/BulkEdit/BulkEditContext';
7
+ import {
8
+ QuickEditContext,
9
+ type QuickEditContextType,
10
+ } from '../Explorer/QuickEdit/QuickEditContext';
4
11
  import { Modal } from './Modal';
12
+ import { createOverlayStack } from './OverlayStack/overlayStack';
5
13
 
6
14
  describe('Actions', () => {
7
15
  it('renders the component without crashing', () => {
@@ -135,3 +143,322 @@ describe('Actions', () => {
135
143
  expect(screen.queryByTestId('modal')).toBeNull();
136
144
  });
137
145
  });
146
+
147
+ describe('Modal stack', () => {
148
+ const containers = (): HTMLElement[] =>
149
+ Array.from(document.querySelectorAll<HTMLElement>('.modal-container'));
150
+
151
+ const TwoModals: React.FC<{
152
+ isUpperShown?: boolean;
153
+ onLowerBackDropClicked?: () => void;
154
+ onUpperBackDropClicked?: () => void;
155
+ }> = ({
156
+ isUpperShown = true,
157
+ onLowerBackDropClicked = noop,
158
+ onUpperBackDropClicked = noop,
159
+ }) => (
160
+ <>
161
+ <Modal isShown={true} onBackDropClicked={onLowerBackDropClicked}>
162
+ <button>Lower</button>
163
+ </Modal>
164
+ <Modal isShown={isUpperShown} onBackDropClicked={onUpperBackDropClicked}>
165
+ <button>Upper</button>
166
+ </Modal>
167
+ </>
168
+ );
169
+
170
+ it('dims the page only with the bottom modal and offsets the ones above', () => {
171
+ render(<TwoModals />);
172
+
173
+ const [lower, upper] = containers();
174
+
175
+ expect(lower).not.toHaveClass('stacked');
176
+ expect(upper).toHaveClass('stacked');
177
+ expect(lower.style.getPropertyValue('--modal-depth')).toBe('0');
178
+ expect(upper.style.getPropertyValue('--modal-depth')).toBe('1');
179
+ });
180
+
181
+ it(`emits 'onBackDropClicked' of the topmost modal only when Escape is pressed`, () => {
182
+ const onLowerBackDropClicked = vi.fn();
183
+ const onUpperBackDropClicked = vi.fn();
184
+ render(
185
+ <TwoModals
186
+ onLowerBackDropClicked={onLowerBackDropClicked}
187
+ onUpperBackDropClicked={onUpperBackDropClicked}
188
+ />,
189
+ );
190
+
191
+ fireEvent.keyDown(document, { key: 'Escape' });
192
+
193
+ expect(onUpperBackDropClicked).toHaveBeenCalledTimes(1);
194
+ expect(onLowerBackDropClicked).not.toHaveBeenCalled();
195
+ });
196
+
197
+ it('makes the modals below the topmost one inert', () => {
198
+ render(<TwoModals />);
199
+
200
+ const [lower, upper] = containers();
201
+
202
+ expect(lower).toHaveAttribute('inert');
203
+ expect(lower).toHaveAttribute('aria-hidden', 'true');
204
+ expect(upper).not.toHaveAttribute('inert');
205
+ expect(upper).not.toHaveAttribute('aria-hidden');
206
+ expect(screen.getAllByRole('dialog', { hidden: true })[1]).toHaveAttribute(
207
+ 'aria-modal',
208
+ 'true',
209
+ );
210
+ });
211
+
212
+ it('hands the top back to the modal below once the upper one is gone', () => {
213
+ const onLowerBackDropClicked = vi.fn();
214
+ const { rerender } = render(
215
+ <TwoModals onLowerBackDropClicked={onLowerBackDropClicked} />,
216
+ );
217
+
218
+ rerender(
219
+ <TwoModals
220
+ isUpperShown={false}
221
+ onLowerBackDropClicked={onLowerBackDropClicked}
222
+ />,
223
+ );
224
+ fireEvent.animationEnd(screen.getAllByTestId('modal')[1]);
225
+
226
+ const [lower] = containers();
227
+
228
+ expect(containers()).toHaveLength(1);
229
+ expect(lower).not.toHaveAttribute('inert');
230
+ expect(lower).not.toHaveClass('stacked');
231
+
232
+ fireEvent.keyDown(document, { key: 'Escape' });
233
+
234
+ expect(onLowerBackDropClicked).toHaveBeenCalledTimes(1);
235
+ });
236
+
237
+ it('ignores Escape while the modal is closing', () => {
238
+ const onBackDropClicked = vi.fn();
239
+ const { rerender } = render(
240
+ <Modal isShown={true} onBackDropClicked={onBackDropClicked} />,
241
+ );
242
+
243
+ rerender(<Modal isShown={false} onBackDropClicked={onBackDropClicked} />);
244
+ fireEvent.keyDown(document, { key: 'Escape' });
245
+
246
+ expect(onBackDropClicked).not.toHaveBeenCalled();
247
+ });
248
+
249
+ it('moves focus into the modal and back to the opener once it is gone', () => {
250
+ const Host: React.FC<{ isShown: boolean }> = ({ isShown }) => (
251
+ <>
252
+ <button>Open</button>
253
+ <Modal isShown={isShown}>
254
+ <span>Content</span>
255
+ </Modal>
256
+ </>
257
+ );
258
+ const { rerender } = render(<Host isShown={false} />);
259
+ screen.getByText('Open').focus();
260
+
261
+ rerender(<Host isShown={true} />);
262
+
263
+ expect(screen.getByTestId('modal')).toHaveFocus();
264
+
265
+ rerender(<Host isShown={false} />);
266
+ fireEvent.animationEnd(screen.getByTestId('modal'));
267
+
268
+ expect(screen.getByText('Open')).toHaveFocus();
269
+ });
270
+
271
+ it('leaves focus with content that took it on mount', () => {
272
+ render(
273
+ <Modal isShown={true}>
274
+ <input aria-label="Name" autoFocus />
275
+ </Modal>,
276
+ );
277
+
278
+ expect(screen.getByLabelText('Name')).toHaveFocus();
279
+ });
280
+
281
+ it('returns focus to the opener when the content took it on mount', () => {
282
+ const Host: React.FC<{ isShown: boolean }> = ({ isShown }) => (
283
+ <>
284
+ <button>Open</button>
285
+ <Modal isShown={isShown}>
286
+ <input aria-label="Name" autoFocus />
287
+ </Modal>
288
+ </>
289
+ );
290
+ const { rerender } = render(<Host isShown={false} />);
291
+ screen.getByText('Open').focus();
292
+
293
+ rerender(<Host isShown={true} />);
294
+
295
+ expect(screen.getByLabelText('Name')).toHaveFocus();
296
+
297
+ rerender(<Host isShown={false} />);
298
+ fireEvent.animationEnd(screen.getByTestId('modal'));
299
+
300
+ expect(screen.getByText('Open')).toHaveFocus();
301
+ });
302
+
303
+ it('returns focus to the opener when a modal mounted open is unmounted', () => {
304
+ const Host: React.FC<{ isShown: boolean }> = ({ isShown }) => (
305
+ <>
306
+ <button>Open</button>
307
+ {isShown && (
308
+ <Modal isShown={true}>
309
+ <input aria-label="Name" autoFocus />
310
+ </Modal>
311
+ )}
312
+ </>
313
+ );
314
+ const { rerender } = render(<Host isShown={false} />);
315
+ screen.getByText('Open').focus();
316
+
317
+ rerender(<Host isShown={true} />);
318
+
319
+ expect(screen.getByLabelText('Name')).toHaveFocus();
320
+
321
+ rerender(<Host isShown={false} />);
322
+
323
+ expect(screen.getByText('Open')).toHaveFocus();
324
+ });
325
+
326
+ it('returns focus to an opener inside the modal below', () => {
327
+ // jsdom doesn't implement `inert`, so this does what a browser does:
328
+ // elements inside an inert subtree can't take focus.
329
+ const focus = HTMLElement.prototype.focus;
330
+ const focusSpy = vi
331
+ .spyOn(HTMLElement.prototype, 'focus')
332
+ .mockImplementation(function (this: HTMLElement, options) {
333
+ if (!this.closest('[inert]')) {
334
+ focus.call(this, options);
335
+ }
336
+ });
337
+
338
+ try {
339
+ const Host: React.FC<{ isUpperShown: boolean }> = ({ isUpperShown }) => (
340
+ <>
341
+ <Modal isShown={true}>
342
+ <button>Open upper</button>
343
+ </Modal>
344
+ <Modal isShown={isUpperShown}>
345
+ <button>Upper</button>
346
+ </Modal>
347
+ </>
348
+ );
349
+ const { rerender } = render(<Host isUpperShown={false} />);
350
+ screen.getByText('Open upper').focus();
351
+
352
+ rerender(<Host isUpperShown={true} />);
353
+
354
+ expect(screen.getAllByTestId('modal')[1]).toHaveFocus();
355
+
356
+ rerender(<Host isUpperShown={false} />);
357
+ fireEvent.animationEnd(screen.getAllByTestId('modal')[1]);
358
+
359
+ expect(screen.getByText('Open upper')).toHaveFocus();
360
+ } finally {
361
+ focusSpy.mockRestore();
362
+ }
363
+ });
364
+
365
+ it('wraps Tab around inside the topmost modal', () => {
366
+ render(
367
+ <Modal isShown={true}>
368
+ <button>First</button>
369
+ <button>Last</button>
370
+ </Modal>,
371
+ );
372
+ const first = screen.getByText('First');
373
+ const last = screen.getByText('Last');
374
+
375
+ last.focus();
376
+ fireEvent.keyDown(last, { key: 'Tab' });
377
+
378
+ expect(first).toHaveFocus();
379
+
380
+ fireEvent.keyDown(first, { key: 'Tab', shiftKey: true });
381
+
382
+ expect(last).toHaveFocus();
383
+ });
384
+
385
+ it('wraps Tab around at the first and last control that is rendered', () => {
386
+ render(
387
+ <Modal isShown={true}>
388
+ <div style={{ visibility: 'hidden' }}>
389
+ <button>Hidden by its parent</button>
390
+ </div>
391
+ <button>First</button>
392
+ <button>Last</button>
393
+ <button hidden>Hidden</button>
394
+ <button style={{ display: 'none' }}>Not displayed</button>
395
+ <div style={{ display: 'none' }}>
396
+ <button>Inside a parent that is not displayed</button>
397
+ </div>
398
+ <button style={{ visibility: 'hidden' }}>Invisible</button>
399
+ </Modal>,
400
+ );
401
+ const first = screen.getByText('First');
402
+ const last = screen.getByText('Last');
403
+
404
+ last.focus();
405
+ fireEvent.keyDown(last, { key: 'Tab' });
406
+
407
+ expect(first).toHaveFocus();
408
+
409
+ fireEvent.keyDown(first, { key: 'Tab', shiftKey: true });
410
+
411
+ expect(last).toHaveFocus();
412
+ });
413
+
414
+ it('resets the quick edit and bulk edit contexts for its content', () => {
415
+ const Probe: React.FC = () => {
416
+ const quickEdit = useContext(QuickEditContext);
417
+ const bulkEdit = useContext(BulkEditContext);
418
+
419
+ return (
420
+ <span>
421
+ {quickEdit === undefined && bulkEdit === undefined
422
+ ? 'reset'
423
+ : 'leaked'}
424
+ </span>
425
+ );
426
+ };
427
+
428
+ render(
429
+ <QuickEditContext.Provider
430
+ value={
431
+ {
432
+ selectedItem: {},
433
+ refresh: async () => undefined,
434
+ } as QuickEditContextType<unknown>
435
+ }
436
+ >
437
+ <BulkEditContext.Provider
438
+ value={{ cancel: noop, isBulkEditMode: true }}
439
+ >
440
+ <Modal isShown={true}>
441
+ <Probe />
442
+ </Modal>
443
+ </BulkEditContext.Provider>
444
+ </QuickEditContext.Provider>,
445
+ );
446
+
447
+ expect(screen.getByText('reset')).toBeInTheDocument();
448
+ });
449
+
450
+ it('registers with the stack passed to initializeUi', () => {
451
+ const stack = createOverlayStack();
452
+ const register = vi.spyOn(stack, 'register');
453
+
454
+ try {
455
+ initializeUi({ overlayStack: stack } as unknown as UiConfig);
456
+
457
+ render(<Modal isShown={true} />);
458
+
459
+ expect(register).toHaveBeenCalledTimes(1);
460
+ } finally {
461
+ initializeUi({} as UiConfig);
462
+ }
463
+ });
464
+ });
@@ -47,6 +47,40 @@ const StoryComponent: React.FC<{ cancel: () => void }> = ({
47
47
  );
48
48
  };
49
49
 
50
+ const StackedStoryComponent: React.FC<{ level: number; close: () => void }> = ({
51
+ level,
52
+ close,
53
+ }): JSX.Element => {
54
+ const [isNextShown, setIsNextShown] = useState<boolean>(false);
55
+ const closeNext = (): void => setIsNextShown(false);
56
+
57
+ return (
58
+ <div className={classes.container}>
59
+ <PageHeader
60
+ title={`Modal ${level}`}
61
+ actions={[
62
+ {
63
+ label: 'Close',
64
+ icon: IconName.X,
65
+ kind: 'action',
66
+ onClick: close,
67
+ },
68
+ ]}
69
+ />
70
+ <div className={classes.body}>
71
+ <p>Press Escape or click the background to close the topmost modal.</p>
72
+ <TextButton
73
+ text={'Open another modal'}
74
+ onButtonClicked={() => setIsNextShown(true)}
75
+ />
76
+ </div>
77
+ <Modal isShown={isNextShown} onBackDropClicked={closeNext}>
78
+ <StackedStoryComponent level={level + 1} close={closeNext} />
79
+ </Modal>
80
+ </div>
81
+ );
82
+ };
83
+
50
84
  export const Default: StoryObj<typeof Modal> = {
51
85
  argTypes: {
52
86
  isShown: {
@@ -93,3 +127,34 @@ export const useModalHook: StoryObj<typeof Modal> = {
93
127
  );
94
128
  }),
95
129
  };
130
+
131
+ /**
132
+ * Modals opened on top of each other. Only the bottom one dims the page, each
133
+ * one sits a little lower than the one below it, and only the topmost one
134
+ * reacts to Escape and keeps keyboard focus.
135
+ */
136
+ export const Stacked: StoryObj<typeof Modal> = {
137
+ parameters: {
138
+ controls: {
139
+ hideNoControlsWarning: true,
140
+ exclude: /.*/g,
141
+ },
142
+ },
143
+ render: () =>
144
+ React.createElement(() => {
145
+ const [isShown, setIsShown] = useState<boolean>(false);
146
+ const onClose = (): void => setIsShown(false);
147
+
148
+ return (
149
+ <div>
150
+ <TextButton
151
+ text={'Show Modal'}
152
+ onButtonClicked={() => setIsShown(true)}
153
+ />
154
+ <Modal isShown={isShown} onBackDropClicked={onClose}>
155
+ <StackedStoryComponent level={1} close={onClose} />
156
+ </Modal>
157
+ </div>
158
+ );
159
+ }),
160
+ };
@@ -1,8 +1,12 @@
1
1
  import clsx from 'clsx';
2
- import React, { useEffect, useState } from 'react';
2
+ import React, { useEffect, useRef, useState } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
4
  import { noop } from '../../helpers/utils';
5
+ import { BulkEditContext } from '../Explorer/BulkEdit/BulkEditContext';
6
+ import { QuickEditContext } from '../Explorer/QuickEdit/QuickEditContext';
5
7
  import classes from './Modal.module.scss';
8
+ import { useOverlayLayer } from './OverlayStack/useOverlayLayer';
9
+ import { trapTabKey } from './focusTrap';
6
10
 
7
11
  export interface ModalProps {
8
12
  /** Whether the Modal is shown or not */
@@ -23,14 +27,36 @@ export interface ModalProps {
23
27
  onOpen?: () => void;
24
28
  /** Callback to emit when the when the modal is unmounted */
25
29
  onClose?: () => void;
26
- /** Callback to emit when the back-drop has been clicked */
30
+ /**
31
+ * Callback to emit when the back-drop has been clicked, or Escape was pressed
32
+ * while this modal is the topmost one
33
+ */
27
34
  onBackDropClicked?: () => void;
28
35
  /** CSS Class name for additional styles */
29
36
  className?: string;
30
37
  }
31
38
 
39
+ /**
40
+ * Resets the contexts that would otherwise reach into a modal from the station
41
+ * that opened it. A modal is portaled out of that station's DOM, but React
42
+ * context still flows into it, so a form in a modal opened from quick edit or
43
+ * bulk edit would act as part of that mode.
44
+ */
45
+ const ContextBoundary: React.FC = ({ children }) => (
46
+ <QuickEditContext.Provider value={undefined as never}>
47
+ <BulkEditContext.Provider value={undefined as never}>
48
+ {children}
49
+ </BulkEditContext.Provider>
50
+ </QuickEditContext.Provider>
51
+ );
52
+
32
53
  /**
33
54
  * Renders an overlay and it's children
55
+ *
56
+ * Modals opened on top of each other form a stack: only the bottom one dims
57
+ * the page, each one sits a little lower than the one below it, and only the
58
+ * topmost one reacts to Escape and keeps keyboard focus.
59
+ *
34
60
  * @example
35
61
  * <Modal isShown={true} onBackDropClicked={() => console.log('back-drop clicked')}>
36
62
  * <div>Component(s) to render</div>
@@ -54,6 +80,33 @@ export const Modal: React.FC<ModalProps> = ({
54
80
  const [isExiting, setIsExiting] = useState<boolean>(false);
55
81
  const [isFullyOpen, setIsFullyOpen] = useState<boolean>(false);
56
82
 
83
+ const containerRef = useRef<HTMLDivElement>(null);
84
+ const childRef = useRef<HTMLDivElement>(null);
85
+
86
+ const { depth, isTopmost } = useOverlayLayer(
87
+ isVisible,
88
+ () => {
89
+ if (isShown) {
90
+ onBackDropClicked();
91
+ }
92
+ },
93
+ containerRef,
94
+ );
95
+
96
+ // The element to give focus back to once the modal is gone. It is read while
97
+ // rendering the first frame of an opening: content that takes focus on mount
98
+ // (e.g. with `autoFocus`) does so while React commits that frame, before any
99
+ // effect of this component runs.
100
+ const openerRef = useRef<HTMLElement | null>();
101
+ if (!isVisible) {
102
+ openerRef.current = undefined;
103
+ } else if (openerRef.current === undefined) {
104
+ openerRef.current =
105
+ document.activeElement instanceof HTMLElement
106
+ ? document.activeElement
107
+ : null;
108
+ }
109
+
57
110
  useEffect(() => {
58
111
  if (isShown) {
59
112
  setIsVisible(true);
@@ -64,6 +117,36 @@ export const Modal: React.FC<ModalProps> = ({
64
117
  }
65
118
  }, [isShown, isVisible]);
66
119
 
120
+ // Moves focus into the modal when it opens, and back to where it came from
121
+ // once the modal is gone.
122
+ useEffect(() => {
123
+ if (!isVisible) {
124
+ return;
125
+ }
126
+
127
+ const opener = openerRef.current;
128
+ const child = childRef.current;
129
+
130
+ // Content that focused itself on mount (e.g. with `autoFocus`) keeps it.
131
+ if (child && !child.contains(document.activeElement)) {
132
+ child.focus({ preventScroll: true });
133
+ }
134
+
135
+ return () => {
136
+ // Only if focus was left behind, so a modal that closes below another
137
+ // layer doesn't pull focus out of that layer.
138
+ const active = document.activeElement;
139
+ const focusWasLeft =
140
+ active === null ||
141
+ active === document.body ||
142
+ (child?.contains(active) ?? false);
143
+
144
+ if (focusWasLeft && opener?.isConnected) {
145
+ opener.focus({ preventScroll: true });
146
+ }
147
+ };
148
+ }, [isVisible]);
149
+
67
150
  const handleAnimationEnd = (
68
151
  e: React.AnimationEvent<HTMLDivElement>,
69
152
  ): void => {
@@ -81,12 +164,19 @@ export const Modal: React.FC<ModalProps> = ({
81
164
  }
82
165
  };
83
166
 
167
+ const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>): void => {
168
+ if (e.key === 'Tab' && isTopmost && childRef.current) {
169
+ trapTabKey(e, childRef.current);
170
+ }
171
+ };
172
+
84
173
  const customStyles = {
85
174
  paddingTop,
86
175
  paddingRight,
87
176
  paddingBottom,
88
177
  paddingLeft,
89
178
  zIndex,
179
+ '--modal-depth': String(depth),
90
180
  } as React.CSSProperties;
91
181
 
92
182
  const childCustomStyles = {
@@ -99,22 +189,34 @@ export const Modal: React.FC<ModalProps> = ({
99
189
 
100
190
  return createPortal(
101
191
  <div
102
- className={clsx(classes.container, 'modal-container', className)}
192
+ ref={containerRef}
193
+ className={clsx(
194
+ classes.container,
195
+ { [classes.stacked]: depth > 0 },
196
+ 'modal-container',
197
+ className,
198
+ )}
103
199
  onClick={() => isShown && onBackDropClicked()}
104
200
  style={customStyles}
201
+ aria-hidden={isTopmost ? undefined : true}
105
202
  >
106
203
  <div
204
+ ref={childRef}
107
205
  className={clsx(
108
206
  classes.child,
109
207
  isExiting ? classes.slideOut : classes.slideIn,
110
208
  )}
111
209
  onClick={(e) => e.stopPropagation()}
210
+ onKeyDown={handleKeyDown}
112
211
  style={childCustomStyles}
212
+ role="dialog"
213
+ aria-modal={isTopmost}
214
+ tabIndex={-1}
113
215
  data-test-id="modal"
114
216
  data-test-state={isFullyOpen ? 'open' : 'closed'}
115
217
  onAnimationEnd={handleAnimationEnd}
116
218
  >
117
- {children}
219
+ <ContextBoundary>{children}</ContextBoundary>
118
220
  </div>
119
221
  </div>,
120
222
  document.body,