@baukit/a11y-core 0.10.30 → 0.10.31
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-focus-trap.d.ts","sourceRoot":"","sources":["../src/use-focus-trap.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,OAAO,EACb,MAAM,mBAAmB,CAAC;AAgB3B,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IACnE,cAAc,IAAI,IAAI,CAAC;IACvB,eAAe,CAAC,IAAI,IAAI,CAAC;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,OAAO,CAAC;IAChB,YAAY,EAAE,OAAO,CAAC;IACtB,eAAe,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACtC,QAAQ,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;IACpC,yFAAyF;IACzF,YAAY,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS,CAAC;IAC3C,wEAAwE;IACxE,WAAW,CAAC,EAAE,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;CACtD;AAED,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAC/C;AAYD,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,cAAc,GAAG,gBAAgB,EAAE,CAE/E;AAED,yFAAyF;AACzF,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,SAAS,WAAW,EAAE,EACjC,aAAa,EAAE,OAAO,EACtB,cAAc,EAAE,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CAAC,SAAS,EAAE,cAAc,GAAG,gBAAgB,EAAE,CAE/E;AAED,yFAAyF;AACzF,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,SAAS,WAAW,EAAE,EACjC,aAAa,EAAE,OAAO,EACtB,cAAc,EAAE,OAAO,GACtB,OAAO,CAST;AAED,wFAAwF;AACxF,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,cAAc,GAAG,IAAI,EAChC,gBAAgB,EAAE,WAAW,GAAG,IAAI,GACnC,IAAI,CAGN;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,cAAc,GAAG,IAAI,EAChC,KAAK,EAAE,iBAAiB,GACvB,QAAQ,GAAG,SAAS,GAAG,SAAS,CAsBlC;AA8BD,wFAAwF;AACxF,wBAAgB,YAAY,CAAC,EAC3B,MAAM,EACN,YAAY,EACZ,eAAe,EACf,QAAQ,EACR,YAAY,EACZ,WAAW,GACZ,EAAE,gBAAgB,GAAG,cAAc,CA6DnC"}
|
package/dist/use-focus-trap.js
CHANGED
|
@@ -67,6 +67,13 @@ export function applyFocusTrapKey(container, event) {
|
|
|
67
67
|
event.preventDefault();
|
|
68
68
|
return 'wrapped';
|
|
69
69
|
}
|
|
70
|
+
function containsFocus(container) {
|
|
71
|
+
for (let element = document.activeElement; element !== null; element = element.parentElement) {
|
|
72
|
+
if (element === container)
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
70
77
|
function hasOutsideFocus(container) {
|
|
71
78
|
const active = document.activeElement;
|
|
72
79
|
if (active === null ||
|
|
@@ -75,11 +82,7 @@ function hasOutsideFocus(container) {
|
|
|
75
82
|
active.closest('[inert], [hidden], [aria-hidden="true"]') !== null) {
|
|
76
83
|
return false;
|
|
77
84
|
}
|
|
78
|
-
|
|
79
|
-
if (element === container)
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
return true;
|
|
85
|
+
return !containsFocus(container);
|
|
83
86
|
}
|
|
84
87
|
function isConnected(target) {
|
|
85
88
|
return target != null && 'isConnected' in target && target.isConnected === true;
|
|
@@ -89,6 +92,7 @@ export function useFocusTrap({ active, containerRef, initialFocusRef, onEscape,
|
|
|
89
92
|
const onEscapeRef = useRef(onEscape);
|
|
90
93
|
const restoreFocusRef = useRef(restoreFocus);
|
|
91
94
|
const returnFocusRef = useRef(returnFocus);
|
|
95
|
+
const openerRef = useRef(undefined);
|
|
92
96
|
const restoreTimerRef = useRef(undefined);
|
|
93
97
|
useEffect(() => {
|
|
94
98
|
onEscapeRef.current = onEscape;
|
|
@@ -100,18 +104,23 @@ export function useFocusTrap({ active, containerRef, initialFocusRef, onEscape,
|
|
|
100
104
|
useEffect(() => {
|
|
101
105
|
if (!hasDocument() || !active)
|
|
102
106
|
return;
|
|
107
|
+
const container = hostElement(containerRef);
|
|
108
|
+
if (openerRef.current === undefined ||
|
|
109
|
+
(restoreTimerRef.current === undefined && !containsFocus(container))) {
|
|
110
|
+
openerRef.current = activeFocusTarget();
|
|
111
|
+
}
|
|
103
112
|
if (restoreTimerRef.current !== undefined) {
|
|
104
113
|
clearTimeout(restoreTimerRef.current);
|
|
105
114
|
restoreTimerRef.current = undefined;
|
|
106
115
|
}
|
|
107
|
-
const previouslyFocused =
|
|
108
|
-
const container = hostElement(containerRef);
|
|
116
|
+
const previouslyFocused = openerRef.current;
|
|
109
117
|
focusOverlayEntry(asFocusContainer(containerRef), asFocusTarget(initialFocusRef));
|
|
110
118
|
return () => {
|
|
111
119
|
// useInert releases outside siblings in a sibling effect cleanup. Restore on
|
|
112
120
|
// the next task so the browser does not reject focus on a still-inert trigger.
|
|
113
121
|
restoreTimerRef.current = setTimeout(() => {
|
|
114
122
|
restoreTimerRef.current = undefined;
|
|
123
|
+
openerRef.current = undefined;
|
|
115
124
|
if (hasOutsideFocus(container))
|
|
116
125
|
return;
|
|
117
126
|
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{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AM,CAAC;oBAAE,MAAM,CAAC,KAAK,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;YACjE,CAAC,EAAE,CAAC,CAAC,CAAC;QACR,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC,CAAC;IAE5C,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,KAAwB,EAAE,EAAE;QAC3B,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,MAAM;YAAE,OAAO;QACtC,IAAI,iBAAiB,CAAC,gBAAgB,CAAC,YAAY,CAAC,EAAE,KAAK,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1E,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC;QAC1B,CAAC;IACH,CAAC,EACD,CAAC,MAAM,EAAE,YAAY,CAAC,CACvB,CAAC;IAEF,OAAO,EAAE,SAAS,EAAE,CAAC;AACvB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/a11y-core",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.31",
|
|
4
4
|
"description": "Accessibility behavior shared by web and React Native products: overlay focus, inert background content, announcements, reduced motion, and keyboard movement.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Patrick Koss",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { act, cleanup, render, renderHook } from '@testing-library/react';
|
|
3
|
-
import { createElement, useRef } from 'react';
|
|
3
|
+
import { createElement, StrictMode, useRef, type RefObject } from 'react';
|
|
4
4
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
5
|
|
|
6
6
|
import type { FocusContainer, FocusTarget, HostRef } from './dom-boundary.js';
|
|
@@ -248,6 +248,173 @@ describe('applyFocusTrapKey', () => {
|
|
|
248
248
|
});
|
|
249
249
|
|
|
250
250
|
describe('useFocusTrap', () => {
|
|
251
|
+
function Dialog({
|
|
252
|
+
active = true,
|
|
253
|
+
containerRef,
|
|
254
|
+
initialFocusRef,
|
|
255
|
+
}: {
|
|
256
|
+
active?: boolean;
|
|
257
|
+
containerRef?: RefObject<HTMLDivElement | null> | undefined;
|
|
258
|
+
initialFocusRef?: RefObject<HTMLInputElement | null> | undefined;
|
|
259
|
+
}) {
|
|
260
|
+
const localRef = useRef<HTMLDivElement>(null);
|
|
261
|
+
const ref = containerRef ?? localRef;
|
|
262
|
+
useFocusTrap({ active, containerRef: ref, initialFocusRef });
|
|
263
|
+
return active
|
|
264
|
+
? createElement(
|
|
265
|
+
'div',
|
|
266
|
+
{ ref },
|
|
267
|
+
createElement('button', { id: 'first' }, 'first'),
|
|
268
|
+
createElement('input', { id: 'initial', ref: initialFocusRef }),
|
|
269
|
+
)
|
|
270
|
+
: null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
it.each([false, true])(
|
|
274
|
+
'restores the opener after StrictMode effect replay with initialFocusRef = %s',
|
|
275
|
+
(withInitialFocus) => {
|
|
276
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
277
|
+
opener.focus();
|
|
278
|
+
const initialFocusRef = withInitialFocus ? { current: null } : undefined;
|
|
279
|
+
const view = render(
|
|
280
|
+
createElement(StrictMode, null, createElement(Dialog, { initialFocusRef })),
|
|
281
|
+
);
|
|
282
|
+
expect(document.activeElement?.id).toBe(withInitialFocus ? 'initial' : 'first');
|
|
283
|
+
|
|
284
|
+
view.unmount();
|
|
285
|
+
expect(document.activeElement).toBe(document.body);
|
|
286
|
+
act(() => {
|
|
287
|
+
vi.runOnlyPendingTimers();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
expect(document.activeElement).toBe(opener);
|
|
291
|
+
},
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
it.each(['initialFocusRef', 'containerRef'] as const)(
|
|
295
|
+
'keeps the opener when %s changes while open under StrictMode',
|
|
296
|
+
(changedRef) => {
|
|
297
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
298
|
+
opener.focus();
|
|
299
|
+
const view = render(
|
|
300
|
+
createElement(StrictMode, null, createElement(Dialog, { [changedRef]: { current: null } })),
|
|
301
|
+
);
|
|
302
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
303
|
+
|
|
304
|
+
view.rerender(
|
|
305
|
+
createElement(StrictMode, null, createElement(Dialog, { [changedRef]: { current: null } })),
|
|
306
|
+
);
|
|
307
|
+
act(() => {
|
|
308
|
+
vi.runOnlyPendingTimers();
|
|
309
|
+
});
|
|
310
|
+
expect(document.activeElement?.id).toBe(
|
|
311
|
+
changedRef === 'initialFocusRef' ? 'initial' : 'first',
|
|
312
|
+
);
|
|
313
|
+
expect(focus).not.toHaveBeenCalled();
|
|
314
|
+
|
|
315
|
+
view.unmount();
|
|
316
|
+
act(() => {
|
|
317
|
+
vi.runOnlyPendingTimers();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(document.activeElement).toBe(opener);
|
|
321
|
+
},
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
it('captures a different opener for a separate opening under StrictMode', () => {
|
|
325
|
+
const triggers = mount('<button>first opener</button><button>second opener</button>');
|
|
326
|
+
const first = triggers.firstElementChild as HTMLButtonElement;
|
|
327
|
+
const second = triggers.lastElementChild as HTMLButtonElement;
|
|
328
|
+
first.focus();
|
|
329
|
+
const view = render(createElement(StrictMode, null, createElement(Dialog)));
|
|
330
|
+
|
|
331
|
+
view.rerender(createElement(StrictMode, null, createElement(Dialog, { active: false })));
|
|
332
|
+
act(() => {
|
|
333
|
+
vi.runOnlyPendingTimers();
|
|
334
|
+
});
|
|
335
|
+
expect(document.activeElement).toBe(first);
|
|
336
|
+
|
|
337
|
+
second.focus();
|
|
338
|
+
view.rerender(createElement(StrictMode, null, createElement(Dialog)));
|
|
339
|
+
expect(document.activeElement?.id).toBe('first');
|
|
340
|
+
view.rerender(createElement(StrictMode, null, createElement(Dialog, { active: false })));
|
|
341
|
+
act(() => {
|
|
342
|
+
vi.runOnlyPendingTimers();
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
expect(document.activeElement).toBe(second);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it.each([false, true])(
|
|
349
|
+
'does not reuse a previous opener after autoFocus with fallback = %s',
|
|
350
|
+
(withFallback) => {
|
|
351
|
+
const triggers = mount('<button>first opener</button><button>second opener</button>');
|
|
352
|
+
const first = triggers.firstElementChild as HTMLButtonElement;
|
|
353
|
+
const second = triggers.lastElementChild as HTMLButtonElement;
|
|
354
|
+
const fallback = mount('<h1 tabindex="-1">fallback</h1>').firstElementChild as HTMLElement;
|
|
355
|
+
|
|
356
|
+
function AutoFocusDialog({ active, autoFocus }: { active: boolean; autoFocus: boolean }) {
|
|
357
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
358
|
+
useFocusTrap({
|
|
359
|
+
active,
|
|
360
|
+
containerRef: ref,
|
|
361
|
+
returnFocus: withFallback ? () => fallback : undefined,
|
|
362
|
+
});
|
|
363
|
+
return active ? createElement('div', { ref }, createElement('input', { autoFocus })) : null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
first.focus();
|
|
367
|
+
const view = render(createElement(AutoFocusDialog, { active: true, autoFocus: false }));
|
|
368
|
+
view.rerender(createElement(AutoFocusDialog, { active: false, autoFocus: false }));
|
|
369
|
+
act(() => {
|
|
370
|
+
vi.runOnlyPendingTimers();
|
|
371
|
+
});
|
|
372
|
+
expect(document.activeElement).toBe(first);
|
|
373
|
+
|
|
374
|
+
second.focus();
|
|
375
|
+
view.rerender(createElement(AutoFocusDialog, { active: true, autoFocus: true }));
|
|
376
|
+
expect(document.activeElement).toBe(view.container.querySelector('input'));
|
|
377
|
+
view.unmount();
|
|
378
|
+
act(() => {
|
|
379
|
+
vi.runOnlyPendingTimers();
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
expect(document.activeElement).toBe(withFallback ? fallback : document.body);
|
|
383
|
+
},
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
it('captures fresh focus after a declined restore under StrictMode', () => {
|
|
387
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
388
|
+
opener.focus();
|
|
389
|
+
const host = mount('<button>inside</button>');
|
|
390
|
+
const ref = containerRef(host);
|
|
391
|
+
let restoreOpener = false;
|
|
392
|
+
const view = renderHook(
|
|
393
|
+
({ active }) =>
|
|
394
|
+
useFocusTrap({ active, containerRef: ref, restoreFocus: () => restoreOpener }),
|
|
395
|
+
{
|
|
396
|
+
initialProps: { active: true },
|
|
397
|
+
wrapper: ({ children }) => createElement(StrictMode, null, children),
|
|
398
|
+
},
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
view.rerender({ active: false });
|
|
402
|
+
act(() => {
|
|
403
|
+
vi.runOnlyPendingTimers();
|
|
404
|
+
});
|
|
405
|
+
expect(document.activeElement).toBe(host.firstElementChild);
|
|
406
|
+
|
|
407
|
+
restoreOpener = true;
|
|
408
|
+
view.rerender({ active: true });
|
|
409
|
+
view.unmount();
|
|
410
|
+
host.remove();
|
|
411
|
+
act(() => {
|
|
412
|
+
vi.runOnlyPendingTimers();
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
expect(document.activeElement).toBe(document.body);
|
|
416
|
+
});
|
|
417
|
+
|
|
251
418
|
it('moves focus to the first control when the overlay opens', () => {
|
|
252
419
|
const host = mount('<button id="first">first</button><button id="second">second</button>');
|
|
253
420
|
|
|
@@ -461,25 +628,39 @@ describe('useFocusTrap', () => {
|
|
|
461
628
|
expect(document.activeElement).toBe(fallback);
|
|
462
629
|
});
|
|
463
630
|
|
|
464
|
-
it(
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
631
|
+
it.each([false, true])(
|
|
632
|
+
'cancels the pending restore when reopened before the next task with outside focus = %s',
|
|
633
|
+
(withOutsideFocus) => {
|
|
634
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
635
|
+
opener.focus();
|
|
636
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
637
|
+
const host = mount('<button>inside</button>');
|
|
638
|
+
const ref = containerRef(host);
|
|
639
|
+
const view = renderHook(({ active }) => useFocusTrap({ active, containerRef: ref }), {
|
|
640
|
+
initialProps: { active: true },
|
|
641
|
+
});
|
|
473
642
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
643
|
+
view.rerender({ active: false });
|
|
644
|
+
if (withOutsideFocus) {
|
|
645
|
+
const outside = mount('<button>outside</button>').firstElementChild as HTMLButtonElement;
|
|
646
|
+
outside.focus();
|
|
647
|
+
}
|
|
648
|
+
view.rerender({ active: true });
|
|
649
|
+
act(() => {
|
|
650
|
+
vi.runOnlyPendingTimers();
|
|
651
|
+
});
|
|
479
652
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
653
|
+
expect(focus).not.toHaveBeenCalled();
|
|
654
|
+
expect(document.activeElement).toBe(host.firstElementChild);
|
|
655
|
+
|
|
656
|
+
view.unmount();
|
|
657
|
+
host.remove();
|
|
658
|
+
act(() => {
|
|
659
|
+
vi.runOnlyPendingTimers();
|
|
660
|
+
});
|
|
661
|
+
expect(document.activeElement).toBe(opener);
|
|
662
|
+
},
|
|
663
|
+
);
|
|
483
664
|
|
|
484
665
|
it('prefers the requested initial focus target', () => {
|
|
485
666
|
const host = mount('<button id="first">first</button><button id="wanted">wanted</button>');
|
package/src/use-focus-trap.ts
CHANGED
|
@@ -116,6 +116,17 @@ export function applyFocusTrapKey(
|
|
|
116
116
|
return 'wrapped';
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
function containsFocus(container: object | null): boolean {
|
|
120
|
+
for (
|
|
121
|
+
let element: Element | null = document.activeElement;
|
|
122
|
+
element !== null;
|
|
123
|
+
element = element.parentElement
|
|
124
|
+
) {
|
|
125
|
+
if (element === container) return true;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
119
130
|
function hasOutsideFocus(container: object | null): boolean {
|
|
120
131
|
const active = document.activeElement;
|
|
121
132
|
if (
|
|
@@ -126,10 +137,7 @@ function hasOutsideFocus(container: object | null): boolean {
|
|
|
126
137
|
) {
|
|
127
138
|
return false;
|
|
128
139
|
}
|
|
129
|
-
|
|
130
|
-
if (element === container) return false;
|
|
131
|
-
}
|
|
132
|
-
return true;
|
|
140
|
+
return !containsFocus(container);
|
|
133
141
|
}
|
|
134
142
|
|
|
135
143
|
function isConnected(target: FocusTarget | null | undefined): target is FocusTarget {
|
|
@@ -148,6 +156,7 @@ export function useFocusTrap({
|
|
|
148
156
|
const onEscapeRef = useRef(onEscape);
|
|
149
157
|
const restoreFocusRef = useRef(restoreFocus);
|
|
150
158
|
const returnFocusRef = useRef(returnFocus);
|
|
159
|
+
const openerRef = useRef<FocusTarget | null | undefined>(undefined);
|
|
151
160
|
const restoreTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
152
161
|
|
|
153
162
|
useEffect(() => {
|
|
@@ -161,13 +170,19 @@ export function useFocusTrap({
|
|
|
161
170
|
|
|
162
171
|
useEffect(() => {
|
|
163
172
|
if (!hasDocument() || !active) return;
|
|
173
|
+
const container = hostElement(containerRef);
|
|
174
|
+
if (
|
|
175
|
+
openerRef.current === undefined ||
|
|
176
|
+
(restoreTimerRef.current === undefined && !containsFocus(container))
|
|
177
|
+
) {
|
|
178
|
+
openerRef.current = activeFocusTarget();
|
|
179
|
+
}
|
|
164
180
|
if (restoreTimerRef.current !== undefined) {
|
|
165
181
|
clearTimeout(restoreTimerRef.current);
|
|
166
182
|
restoreTimerRef.current = undefined;
|
|
167
183
|
}
|
|
168
184
|
|
|
169
|
-
const previouslyFocused =
|
|
170
|
-
const container = hostElement(containerRef);
|
|
185
|
+
const previouslyFocused = openerRef.current;
|
|
171
186
|
focusOverlayEntry(asFocusContainer(containerRef), asFocusTarget(initialFocusRef));
|
|
172
187
|
|
|
173
188
|
return () => {
|
|
@@ -175,6 +190,7 @@ export function useFocusTrap({
|
|
|
175
190
|
// the next task so the browser does not reject focus on a still-inert trigger.
|
|
176
191
|
restoreTimerRef.current = setTimeout(() => {
|
|
177
192
|
restoreTimerRef.current = undefined;
|
|
193
|
+
openerRef.current = undefined;
|
|
178
194
|
if (hasOutsideFocus(container)) return;
|
|
179
195
|
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
180
196
|
const target =
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { act, cleanup, renderHook } from '@testing-library/react';
|
|
3
|
-
import type
|
|
3
|
+
import { createElement, StrictMode, type RefObject } from 'react';
|
|
4
4
|
import type { View } from 'react-native';
|
|
5
5
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
6
|
|
|
@@ -94,6 +94,23 @@ describe('useOverlayA11y on native', () => {
|
|
|
94
94
|
expect(setAccessibilityFocus).toHaveBeenCalledWith(3);
|
|
95
95
|
});
|
|
96
96
|
|
|
97
|
+
it('keeps the explicit trigger after StrictMode effect replay', () => {
|
|
98
|
+
findNodeHandle.mockImplementation((target) =>
|
|
99
|
+
target === CONTAINER ? 7 : target === TRIGGER ? 3 : null,
|
|
100
|
+
);
|
|
101
|
+
const containerRef = ref(CONTAINER);
|
|
102
|
+
const triggerRef = ref(TRIGGER);
|
|
103
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef, triggerRef }), {
|
|
104
|
+
wrapper: ({ children }) => createElement(StrictMode, null, children),
|
|
105
|
+
});
|
|
106
|
+
act(flushInteractions);
|
|
107
|
+
setAccessibilityFocus.mockReset();
|
|
108
|
+
|
|
109
|
+
view.unmount();
|
|
110
|
+
|
|
111
|
+
expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(3);
|
|
112
|
+
});
|
|
113
|
+
|
|
97
114
|
it('prefers a trigger handle the caller already resolved', () => {
|
|
98
115
|
findNodeHandle.mockReturnValue(null);
|
|
99
116
|
|