@baukit/a11y-core 0.10.29 → 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.
- package/README.md +30 -5
- package/dist/use-focus-trap.d.ts +6 -2
- package/dist/use-focus-trap.d.ts.map +1 -1
- package/dist/use-focus-trap.js +45 -5
- package/dist/use-focus-trap.js.map +1 -1
- package/dist/use-overlay-a11y.d.ts +3 -3
- package/dist/use-overlay-a11y.d.ts.map +1 -1
- package/dist/use-overlay-a11y.js +21 -9
- package/dist/use-overlay-a11y.js.map +1 -1
- package/package.json +1 -1
- package/src/use-focus-trap.test.ts +399 -6
- package/src/use-focus-trap.ts +60 -3
- package/src/use-overlay-a11y.test.ts +237 -4
- package/src/use-overlay-a11y.ts +24 -9
package/README.md
CHANGED
|
@@ -39,14 +39,39 @@ const { backgroundProps, containerProps } = useOverlayA11y({
|
|
|
39
39
|
});
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
On web it moves focus into the overlay, contains Tab and Shift+Tab, calls `onEscape`, makes
|
|
43
|
-
everything outside `inertContainerRef` inert
|
|
44
|
-
|
|
42
|
+
On web it moves focus into the overlay, contains Tab and Shift+Tab, calls `onEscape`, and makes
|
|
43
|
+
everything outside `inertContainerRef` inert. On close it checks focus on the next task so
|
|
44
|
+
`useInert` can release the background first. It keeps focus on a connected element outside the
|
|
45
|
+
closing container unless that element is in an `inert`, `hidden`, or `aria-hidden="true"` subtree.
|
|
46
|
+
Otherwise it restores the opener if the opener is still connected. Reopening before the task
|
|
47
|
+
runs cancels the pending restore.
|
|
48
|
+
|
|
49
|
+
Both `useFocusTrap` and `useOverlayA11y` accept two optional callbacks:
|
|
50
|
+
|
|
51
|
+
- `restoreFocus?: () => boolean` decides whether to restore the opener. It defaults to true.
|
|
52
|
+
- `returnFocus?: () => FocusTarget | null` supplies a fallback when the opener is disconnected
|
|
53
|
+
or `restoreFocus` returns false. A `FocusTarget` has a `focus()` method, so an element or native
|
|
54
|
+
view can be returned directly. Web only focuses a connected fallback.
|
|
55
|
+
|
|
56
|
+
The callbacks run when restoration is needed, after the outside-focus check on web. Changes to
|
|
57
|
+
either callback do not restart the trap. To leave focus alone, pass `restoreFocus: () => false`
|
|
58
|
+
without a fallback. To return to a route heading when an action removes the opener:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
useFocusTrap({
|
|
62
|
+
active: visible,
|
|
63
|
+
containerRef: panelRef,
|
|
64
|
+
restoreFocus: () => !navigatingRef.current,
|
|
65
|
+
returnFocus: () => headingRef.current,
|
|
66
|
+
});
|
|
67
|
+
```
|
|
45
68
|
|
|
46
69
|
On native it waits before requesting accessibility focus, so no focus lands before layout. On
|
|
47
70
|
close it returns focus to `triggerRef`. Native cannot discover what previously held accessibility
|
|
48
|
-
focus, so the caller owns that ref.
|
|
49
|
-
|
|
71
|
+
focus, so the caller owns that ref. The hook resolves the ref again on close and uses
|
|
72
|
+
`returnFocus` if the trigger has unmounted or `restoreFocus` returns false. Native cannot inspect
|
|
73
|
+
current accessibility focus, so it cannot apply the web outside-focus check. Pass `triggerHandle`
|
|
74
|
+
instead when the node tag is already resolved; the caller must keep that tag valid until close.
|
|
50
75
|
|
|
51
76
|
The wait defaults to `InteractionManager.runAfterInteractions`, which React Native 0.86 deprecates
|
|
52
77
|
without offering a replacement for "after the overlay presented". Products with a real
|
package/dist/use-focus-trap.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ export interface FocusTrapOptions {
|
|
|
13
13
|
containerRef: HostRef;
|
|
14
14
|
initialFocusRef?: HostRef | undefined;
|
|
15
15
|
onEscape?: (() => void) | undefined;
|
|
16
|
+
/** Whether to restore the opener. Defaults to true; false still allows `returnFocus`. */
|
|
17
|
+
restoreFocus?: (() => boolean) | undefined;
|
|
18
|
+
/** Fallback when the opener is gone or `restoreFocus` returns false. */
|
|
19
|
+
returnFocus?: (() => FocusTarget | null) | undefined;
|
|
16
20
|
}
|
|
17
21
|
export interface FocusTrapProps {
|
|
18
22
|
onKeyDown: (event: FocusTrapKeyEvent) => void;
|
|
@@ -20,7 +24,6 @@ export interface FocusTrapProps {
|
|
|
20
24
|
export declare function focusableElements(container: FocusContainer): FocusableElement[];
|
|
21
25
|
/** Moves focus to the far end when Tab leaves the trap, reporting whether it wrapped. */
|
|
22
26
|
export declare function wrapFocusAtBoundary(focusable: readonly FocusTarget[], activeElement: unknown, movingBackward: boolean): boolean;
|
|
23
|
-
/** Contains web Tab order inside an overlay and restores focus to its opener. */
|
|
24
27
|
/** Focuses the requested target, else the first focusable child, else the container. */
|
|
25
28
|
export declare function focusOverlayEntry(container: FocusContainer | null, requestedInitial: FocusTarget | null): void;
|
|
26
29
|
/**
|
|
@@ -28,5 +31,6 @@ export declare function focusOverlayEntry(container: FocusContainer | null, requ
|
|
|
28
31
|
* should close, `wrapped` when focus moved, and `ignored` otherwise.
|
|
29
32
|
*/
|
|
30
33
|
export declare function applyFocusTrapKey(container: FocusContainer | null, event: FocusTrapKeyEvent): 'escape' | 'wrapped' | 'ignored';
|
|
31
|
-
|
|
34
|
+
/** Contains web Tab order inside an overlay and restores focus when needed on close. */
|
|
35
|
+
export declare function useFocusTrap({ active, containerRef, initialFocusRef, onEscape, restoreFocus, returnFocus, }: FocusTrapOptions): FocusTrapProps;
|
|
32
36
|
//# sourceMappingURL=use-focus-trap.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-focus-trap.d.ts","sourceRoot":"","sources":["../src/use-focus-trap.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
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;QAA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ACZ,EAAE,gBAAgB,GAAG,cAAc,CA6DnC"}
|
package/dist/use-focus-trap.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
-
import { activeFocusTarget, asFocusContainer, asFocusTarget, } from './dom-boundary.js';
|
|
2
|
+
import { activeFocusTarget, asFocusContainer, asFocusTarget, hostElement, } from './dom-boundary.js';
|
|
3
3
|
import { hasDocument } from './platform.js';
|
|
4
4
|
const FOCUSABLE_SELECTOR = [
|
|
5
5
|
'a[href]',
|
|
@@ -36,7 +36,6 @@ export function wrapFocusAtBoundary(focusable, activeElement, movingBackward) {
|
|
|
36
36
|
focusable[movingBackward ? focusable.length - 1 : 0]?.focus({ preventScroll: true });
|
|
37
37
|
return true;
|
|
38
38
|
}
|
|
39
|
-
/** Contains web Tab order inside an overlay and restores focus to its opener. */
|
|
40
39
|
/** Focuses the requested target, else the first focusable child, else the container. */
|
|
41
40
|
export function focusOverlayEntry(container, requestedInitial) {
|
|
42
41
|
const firstFocusable = container ? focusableElements(container)[0] : undefined;
|
|
@@ -68,27 +67,68 @@ export function applyFocusTrapKey(container, event) {
|
|
|
68
67
|
event.preventDefault();
|
|
69
68
|
return 'wrapped';
|
|
70
69
|
}
|
|
71
|
-
|
|
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
|
+
}
|
|
77
|
+
function hasOutsideFocus(container) {
|
|
78
|
+
const active = document.activeElement;
|
|
79
|
+
if (active === null ||
|
|
80
|
+
active === document.body ||
|
|
81
|
+
!active.isConnected ||
|
|
82
|
+
active.closest('[inert], [hidden], [aria-hidden="true"]') !== null) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return !containsFocus(container);
|
|
86
|
+
}
|
|
87
|
+
function isConnected(target) {
|
|
88
|
+
return target != null && 'isConnected' in target && target.isConnected === true;
|
|
89
|
+
}
|
|
90
|
+
/** Contains web Tab order inside an overlay and restores focus when needed on close. */
|
|
91
|
+
export function useFocusTrap({ active, containerRef, initialFocusRef, onEscape, restoreFocus, returnFocus, }) {
|
|
72
92
|
const onEscapeRef = useRef(onEscape);
|
|
93
|
+
const restoreFocusRef = useRef(restoreFocus);
|
|
94
|
+
const returnFocusRef = useRef(returnFocus);
|
|
95
|
+
const openerRef = useRef(undefined);
|
|
73
96
|
const restoreTimerRef = useRef(undefined);
|
|
74
97
|
useEffect(() => {
|
|
75
98
|
onEscapeRef.current = onEscape;
|
|
76
99
|
}, [onEscape]);
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
restoreFocusRef.current = restoreFocus;
|
|
102
|
+
returnFocusRef.current = returnFocus;
|
|
103
|
+
}, [restoreFocus, returnFocus]);
|
|
77
104
|
useEffect(() => {
|
|
78
105
|
if (!hasDocument() || !active)
|
|
79
106
|
return;
|
|
107
|
+
const container = hostElement(containerRef);
|
|
108
|
+
if (openerRef.current === undefined ||
|
|
109
|
+
(restoreTimerRef.current === undefined && !containsFocus(container))) {
|
|
110
|
+
openerRef.current = activeFocusTarget();
|
|
111
|
+
}
|
|
80
112
|
if (restoreTimerRef.current !== undefined) {
|
|
81
113
|
clearTimeout(restoreTimerRef.current);
|
|
82
114
|
restoreTimerRef.current = undefined;
|
|
83
115
|
}
|
|
84
|
-
const previouslyFocused =
|
|
116
|
+
const previouslyFocused = openerRef.current;
|
|
85
117
|
focusOverlayEntry(asFocusContainer(containerRef), asFocusTarget(initialFocusRef));
|
|
86
118
|
return () => {
|
|
87
119
|
// useInert releases outside siblings in a sibling effect cleanup. Restore on
|
|
88
120
|
// the next task so the browser does not reject focus on a still-inert trigger.
|
|
89
121
|
restoreTimerRef.current = setTimeout(() => {
|
|
90
122
|
restoreTimerRef.current = undefined;
|
|
91
|
-
|
|
123
|
+
openerRef.current = undefined;
|
|
124
|
+
if (hasOutsideFocus(container))
|
|
125
|
+
return;
|
|
126
|
+
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
127
|
+
const target = restoreOpener && isConnected(previouslyFocused)
|
|
128
|
+
? previouslyFocused
|
|
129
|
+
: returnFocusRef.current?.();
|
|
130
|
+
if (isConnected(target))
|
|
131
|
+
target.focus({ preventScroll: true });
|
|
92
132
|
}, 0);
|
|
93
133
|
};
|
|
94
134
|
}, [active, containerRef, initialFocusRef]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-focus-trap.js","sourceRoot":"","sources":["../src/use-focus-trap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEvD,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,
|
|
1
|
+
{"version":3,"file":"use-focus-trap.js","sourceRoot":"","sources":["../src/use-focus-trap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEvD,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,WAAW,GAKZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,MAAM,kBAAkB,GAAG;IACzB,SAAS;IACT,wBAAwB;IACxB,uBAAuB;IACvB,wBAAwB;IACxB,0BAA0B;IAC1B,6CAA6C;IAC7C,eAAe;IACf,+CAA+C;IAC/C,4CAA4C;IAC5C,iCAAiC;CAClC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAuBZ,SAAS,WAAW,CAAC,OAAyB;IAC5C,OAAO,CACL,OAAO,CAAC,QAAQ,KAAK,IAAI;QACzB,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,IAAI;QACzC,OAAO,CAAC,YAAY,CAAC,eAAe,CAAC,KAAK,MAAM;QAChD,OAAO,CAAC,YAAY,CA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AC,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"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type RefObject } from 'react';
|
|
2
2
|
import { type View, type ViewProps } from 'react-native';
|
|
3
3
|
import type { HostRef } from './dom-boundary.js';
|
|
4
|
-
import { type FocusTrapProps } from './use-focus-trap.js';
|
|
4
|
+
import { type FocusTrapOptions, type FocusTrapProps } from './use-focus-trap.js';
|
|
5
5
|
/**
|
|
6
6
|
* Runs `task` once the overlay has laid out and returns a canceller. Products
|
|
7
7
|
* that already have a presentation event should pass their own.
|
|
@@ -9,7 +9,7 @@ import { type FocusTrapProps } from './use-focus-trap.js';
|
|
|
9
9
|
export type DeferFocus = (task: () => void) => {
|
|
10
10
|
cancel: () => void;
|
|
11
11
|
};
|
|
12
|
-
export interface OverlayA11yOptions {
|
|
12
|
+
export interface OverlayA11yOptions extends Pick<FocusTrapOptions, 'restoreFocus' | 'returnFocus'> {
|
|
13
13
|
active: boolean;
|
|
14
14
|
containerRef: RefObject<View | null>;
|
|
15
15
|
/** Overrides how the native focus request waits for layout. */
|
|
@@ -32,5 +32,5 @@ export interface OverlayA11yResult {
|
|
|
32
32
|
* Escape, and makes the background inert. Native moves accessibility focus into
|
|
33
33
|
* the overlay after layout and returns it to the trigger on close.
|
|
34
34
|
*/
|
|
35
|
-
export declare function useOverlayA11y({ active, containerRef, deferFocus, inertContainerRef, initialFocusRef, onEscape, triggerHandle, triggerRef, }: OverlayA11yOptions): OverlayA11yResult;
|
|
35
|
+
export declare function useOverlayA11y({ active, containerRef, deferFocus, inertContainerRef, initialFocusRef, onEscape, restoreFocus, returnFocus, triggerHandle, triggerRef, }: OverlayA11yOptions): OverlayA11yResult;
|
|
36
36
|
//# sourceMappingURL=use-overlay-a11y.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-overlay-a11y.d.ts","sourceRoot":"","sources":["../src/use-overlay-a11y.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"use-overlay-a11y.d.ts","sourceRoot":"","sources":["../src/use-overlay-a11y.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC3E,OAAO,EAIL,KAAK,IAAI,EACT,KAAK,SAAS,EACf,MAAM,cAAc,CAAC;AAEtB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAgB,KAAK,gBAAgB,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG/F;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,MAAM,IAAI,KAAK;IAAE,MAAM,EAAE,MAAM,IAAI,CAAA;CAAE,CAAC;AAEtE,MAAM,WAAW,kBAAmB,SAAQ,IAAI,CAAC,gBAAgB,EAAE,cAAc,GAAG,aAAa,CAAC;IAChG,MAAM,EAAE,OAAO,CAAC;IAChB,YAAY,EAAE,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IACrC,+DAA+D;IAC/D,UAAU,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IACpC,6EAA6E;IAC7E,iBAAiB,CAAC,EAAE,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;IACvD,eAAe,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACtC,QAAQ,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;IACpC,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAC1C,UAAU,CAAC,EAAE,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;CACjD;AAED,MAAM,MAAM,sBAAsB,GAAG,IAAI,CACvC,SAAS,EACT,6BAA6B,GAAG,2BAA2B,CAC5D,CAAC;AAEF,MAAM,WAAW,iBAAiB;IAChC,eAAe,EAAE,sBAAsB,CAAC;IACxC,cAAc,EAAE,cAAc,CAAC;CAChC;AASD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAC7B,MAAM,EACN,YAAY,EACZ,UAAuC,EACvC,iBAAiB,EACjB,eAAe,EACf,QAAQ,EACR,YAAY,EACZ,WAAW,EACX,aAAa,EACb,UAAU,GACX,EAAE,kBAAkB,GAAG,iBAAiB,CA0CxC"}
|
package/dist/use-overlay-a11y.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from 'react';
|
|
2
2
|
import { AccessibilityInfo, InteractionManager, Platform, } from 'react-native';
|
|
3
3
|
import { nativeNodeHandle } from './native-focus.js';
|
|
4
4
|
import { useFocusTrap } from './use-focus-trap.js';
|
|
@@ -14,14 +14,25 @@ InteractionManager.runAfterInteractions(task);
|
|
|
14
14
|
* Escape, and makes the background inert. Native moves accessibility focus into
|
|
15
15
|
* the overlay after layout and returns it to the trigger on close.
|
|
16
16
|
*/
|
|
17
|
-
export function useOverlayA11y({ active, containerRef, deferFocus = deferUntilInteractionsDone, inertContainerRef, initialFocusRef, onEscape, triggerHandle, triggerRef, }) {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
17
|
+
export function useOverlayA11y({ active, containerRef, deferFocus = deferUntilInteractionsDone, inertContainerRef, initialFocusRef, onEscape, restoreFocus, returnFocus, triggerHandle, triggerRef, }) {
|
|
18
|
+
const restoreFocusRef = useRef(restoreFocus);
|
|
19
|
+
const returnFocusRef = useRef(returnFocus);
|
|
20
|
+
useLayoutEffect(() => {
|
|
21
|
+
restoreFocusRef.current = restoreFocus;
|
|
22
|
+
returnFocusRef.current = returnFocus;
|
|
23
|
+
}, [restoreFocus, returnFocus]);
|
|
24
|
+
const containerProps = useFocusTrap({
|
|
25
|
+
active,
|
|
26
|
+
containerRef,
|
|
27
|
+
initialFocusRef,
|
|
28
|
+
onEscape,
|
|
29
|
+
restoreFocus,
|
|
30
|
+
returnFocus,
|
|
31
|
+
});
|
|
20
32
|
useInert(inertContainerRef ?? containerRef, active);
|
|
21
33
|
useEffect(() => {
|
|
22
34
|
if (Platform.OS === 'web' || !active)
|
|
23
35
|
return;
|
|
24
|
-
triggerHandleRef.current = triggerHandle ?? nativeNodeHandle(triggerRef);
|
|
25
36
|
const task = deferFocus(() => {
|
|
26
37
|
const containerHandle = nativeNodeHandle(containerRef);
|
|
27
38
|
if (containerHandle !== null)
|
|
@@ -29,10 +40,11 @@ export function useOverlayA11y({ active, containerRef, deferFocus = deferUntilIn
|
|
|
29
40
|
});
|
|
30
41
|
return () => {
|
|
31
42
|
task.cancel();
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
44
|
+
const openerHandle = restoreOpener ? (triggerHandle ?? nativeNodeHandle(triggerRef)) : null;
|
|
45
|
+
const targetHandle = openerHandle ?? nativeNodeHandle({ current: returnFocusRef.current?.() ?? null });
|
|
46
|
+
if (targetHandle !== null)
|
|
47
|
+
AccessibilityInfo.setAccessibilityFocus(targetHandle);
|
|
36
48
|
};
|
|
37
49
|
}, [active, containerRef, deferFocus, triggerHandle, triggerRef]);
|
|
38
50
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-overlay-a11y.js","sourceRoot":"","sources":["../src/use-overlay-a11y.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"use-overlay-a11y.js","sourceRoot":"","sources":["../src/use-overlay-a11y.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;AAC3E,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,QAAQ,GAGT,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,YAAY,EAA8C,MAAM,qBAAqB,CAAC;AAC/F,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAgC1C,gFAAgF;AAChF,+EAA+E;AAC/E,8EAA8E;AAC9E,MAAM,0BAA0B,GAAe,CAAC,IAAI,EAAE,EAAE;AACtD,4DAA4D;AAC5D,kBAAkB,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;AAEhD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,EAC7B,MAAM,EACN,YAAY,EACZ,UAAU,GAAG,0BAA0B,EACvC,iBAAiB,EACjB,eAAe,EACf,QAAQ,EACR,YAAY,EACZ,WAAW,EACX,aAAa,EACb,UAAU,GACS;IACnB,MAAM,eAAe,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAC7C,MAAM,cAAc,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAC3C,eAAe,CAAC,GAAG,EAAE;QACnB,eAAe,CAAC,OAAO,GAAG,YAAY,CAAC;QACvC,cAAc,CAAC,OAAO,GAAG,WAAW,CAAC;IACvC,CAAC,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC,CAAC;IAChC,MAAM,cAAc,GAAG,YAAY,CAAC;QAClC,MAAM;QACN,YAAY;QACZ,eAAe;QACf,QAAQ;QACR,YAAY;QACZ,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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,7 +1,7 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { act, cleanup, render, renderHook } from '@testing-library/react';
|
|
3
|
-
import { createElement, useRef } from 'react';
|
|
4
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { createElement, StrictMode, useRef, type RefObject } from 'react';
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
5
|
|
|
6
6
|
import type { FocusContainer, FocusTarget, HostRef } from './dom-boundary.js';
|
|
7
7
|
import {
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
useFocusTrap,
|
|
12
12
|
wrapFocusAtBoundary,
|
|
13
13
|
type FocusTrapKeyEvent,
|
|
14
|
-
} from './
|
|
14
|
+
} from './web.js';
|
|
15
15
|
|
|
16
16
|
function mount(html: string): HTMLElement {
|
|
17
17
|
const host = document.createElement('div');
|
|
@@ -48,8 +48,14 @@ function keyEvent(key: string, extras: { shiftKey?: boolean; target?: unknown }
|
|
|
48
48
|
return event;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
vi.useFakeTimers();
|
|
53
|
+
});
|
|
54
|
+
|
|
51
55
|
afterEach(() => {
|
|
52
56
|
cleanup();
|
|
57
|
+
vi.clearAllTimers();
|
|
58
|
+
vi.useRealTimers();
|
|
53
59
|
document.body.innerHTML = '';
|
|
54
60
|
});
|
|
55
61
|
|
|
@@ -242,6 +248,173 @@ describe('applyFocusTrapKey', () => {
|
|
|
242
248
|
});
|
|
243
249
|
|
|
244
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
|
+
|
|
245
418
|
it('moves focus to the first control when the overlay opens', () => {
|
|
246
419
|
const host = mount('<button id="first">first</button><button id="second">second</button>');
|
|
247
420
|
|
|
@@ -250,7 +423,7 @@ describe('useFocusTrap', () => {
|
|
|
250
423
|
expect(document.activeElement?.id).toBe('first');
|
|
251
424
|
});
|
|
252
425
|
|
|
253
|
-
it('restores focus to the opener on the next task',
|
|
426
|
+
it('restores focus to the opener on the next task', () => {
|
|
254
427
|
const opener = mount('<button id="opener">open</button>')
|
|
255
428
|
.firstElementChild as HTMLButtonElement;
|
|
256
429
|
opener.focus();
|
|
@@ -263,12 +436,232 @@ describe('useFocusTrap', () => {
|
|
|
263
436
|
// The restore is deliberately deferred so useInert can release the trigger.
|
|
264
437
|
expect(document.activeElement?.id).toBe('inside');
|
|
265
438
|
|
|
266
|
-
|
|
267
|
-
|
|
439
|
+
act(() => {
|
|
440
|
+
vi.runOnlyPendingTimers();
|
|
268
441
|
});
|
|
269
442
|
expect(document.activeElement?.id).toBe('opener');
|
|
270
443
|
});
|
|
271
444
|
|
|
445
|
+
it('keeps focus moved outside after closing', () => {
|
|
446
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
447
|
+
opener.focus();
|
|
448
|
+
const host = mount('<button>inside</button>');
|
|
449
|
+
const destination = mount('<h1 tabindex="-1">new route</h1>').firstElementChild as HTMLElement;
|
|
450
|
+
const restoreFocus = vi.fn(() => true);
|
|
451
|
+
const returnFocus = vi.fn(() => opener);
|
|
452
|
+
const view = renderHook(() =>
|
|
453
|
+
useFocusTrap({ active: true, containerRef: containerRef(host), restoreFocus, returnFocus }),
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
view.unmount();
|
|
457
|
+
destination.focus();
|
|
458
|
+
act(() => {
|
|
459
|
+
vi.runOnlyPendingTimers();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
expect(document.activeElement).toBe(destination);
|
|
463
|
+
expect(restoreFocus).not.toHaveBeenCalled();
|
|
464
|
+
expect(returnFocus).not.toHaveBeenCalled();
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it.each(['body', 'container', 'removed container'])(
|
|
468
|
+
'restores when focus is on %s',
|
|
469
|
+
(location) => {
|
|
470
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
471
|
+
opener.focus();
|
|
472
|
+
const host = mount('<button>inside</button>');
|
|
473
|
+
host.tabIndex = -1;
|
|
474
|
+
const ref = containerRef(host);
|
|
475
|
+
const view = renderHook(() => useFocusTrap({ active: true, containerRef: ref }));
|
|
476
|
+
|
|
477
|
+
view.unmount();
|
|
478
|
+
ref.current = null;
|
|
479
|
+
if (location === 'body') (document.activeElement as HTMLElement).blur();
|
|
480
|
+
if (location === 'container') host.focus();
|
|
481
|
+
if (location === 'removed container') host.remove();
|
|
482
|
+
expect(document.activeElement).toBe(location === 'container' ? host : document.body);
|
|
483
|
+
act(() => {
|
|
484
|
+
vi.runOnlyPendingTimers();
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
expect(document.activeElement).toBe(opener);
|
|
488
|
+
},
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
it.each(['inert', 'hidden', 'aria-hidden'])(
|
|
492
|
+
'restores when outside focus is in a %s subtree',
|
|
493
|
+
(attribute) => {
|
|
494
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
495
|
+
opener.focus();
|
|
496
|
+
const host = mount('<button>inside</button>');
|
|
497
|
+
const outside = mount('<button>outside</button>');
|
|
498
|
+
const destination = outside.firstElementChild as HTMLButtonElement;
|
|
499
|
+
const view = renderHook(() =>
|
|
500
|
+
useFocusTrap({ active: true, containerRef: containerRef(host) }),
|
|
501
|
+
);
|
|
502
|
+
|
|
503
|
+
view.unmount();
|
|
504
|
+
destination.focus();
|
|
505
|
+
outside.setAttribute(attribute, 'true');
|
|
506
|
+
expect(document.activeElement).toBe(destination);
|
|
507
|
+
act(() => {
|
|
508
|
+
vi.runOnlyPendingTimers();
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
expect(document.activeElement).toBe(opener);
|
|
512
|
+
},
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
it('does not focus a disconnected opener', () => {
|
|
516
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
517
|
+
opener.focus();
|
|
518
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
519
|
+
const host = mount('<button>inside</button>');
|
|
520
|
+
const view = renderHook(() => useFocusTrap({ active: true, containerRef: containerRef(host) }));
|
|
521
|
+
|
|
522
|
+
view.unmount();
|
|
523
|
+
opener.remove();
|
|
524
|
+
act(() => {
|
|
525
|
+
vi.runOnlyPendingTimers();
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
expect(focus).not.toHaveBeenCalled();
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it('resolves a fallback at restore time when the opener was disconnected', () => {
|
|
532
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
533
|
+
opener.focus();
|
|
534
|
+
const host = mount('<button>inside</button>');
|
|
535
|
+
let fallback: HTMLElement | null = null;
|
|
536
|
+
const returnFocus = vi.fn(() => fallback);
|
|
537
|
+
const view = renderHook(() =>
|
|
538
|
+
useFocusTrap({ active: true, containerRef: containerRef(host), returnFocus }),
|
|
539
|
+
);
|
|
540
|
+
|
|
541
|
+
view.unmount();
|
|
542
|
+
opener.remove();
|
|
543
|
+
fallback = mount('<h1 tabindex="-1">fallback</h1>').firstElementChild as HTMLElement;
|
|
544
|
+
act(() => {
|
|
545
|
+
vi.runOnlyPendingTimers();
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
expect(returnFocus).toHaveBeenCalledTimes(1);
|
|
549
|
+
expect(document.activeElement).toBe(fallback);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it('does not focus a disconnected fallback', () => {
|
|
553
|
+
const host = mount('<button>inside</button>');
|
|
554
|
+
const fallback = document.createElement('button');
|
|
555
|
+
const focus = vi.spyOn(fallback, 'focus');
|
|
556
|
+
const view = renderHook(() =>
|
|
557
|
+
useFocusTrap({
|
|
558
|
+
active: true,
|
|
559
|
+
containerRef: containerRef(host),
|
|
560
|
+
restoreFocus: () => false,
|
|
561
|
+
returnFocus: () => fallback,
|
|
562
|
+
}),
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
view.unmount();
|
|
566
|
+
act(() => {
|
|
567
|
+
vi.runOnlyPendingTimers();
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
expect(focus).not.toHaveBeenCalled();
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it('does nothing when restoration is declined without a fallback', () => {
|
|
574
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
575
|
+
opener.focus();
|
|
576
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
577
|
+
const host = mount('<button>inside</button>');
|
|
578
|
+
const restoreFocus = vi.fn(() => false);
|
|
579
|
+
const view = renderHook(() =>
|
|
580
|
+
useFocusTrap({ active: true, containerRef: containerRef(host), restoreFocus }),
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
view.unmount();
|
|
584
|
+
host.remove();
|
|
585
|
+
act(() => {
|
|
586
|
+
vi.runOnlyPendingTimers();
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
expect(restoreFocus).toHaveBeenCalledTimes(1);
|
|
590
|
+
expect(focus).not.toHaveBeenCalled();
|
|
591
|
+
expect(document.activeElement).toBe(document.body);
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it('uses the latest restoration callbacks without re-entering the trap', () => {
|
|
595
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
596
|
+
opener.focus();
|
|
597
|
+
const host = mount('<button>first</button><button>second</button>');
|
|
598
|
+
const ref = containerRef(host);
|
|
599
|
+
const fallback = mount('<h1 tabindex="-1">fallback</h1>').firstElementChild as HTMLElement;
|
|
600
|
+
const firstRestore = vi.fn(() => true);
|
|
601
|
+
const firstReturn = vi.fn<() => FocusTarget | null>(() => null);
|
|
602
|
+
const view = renderHook(
|
|
603
|
+
({
|
|
604
|
+
restoreFocus,
|
|
605
|
+
returnFocus,
|
|
606
|
+
}: {
|
|
607
|
+
restoreFocus: () => boolean;
|
|
608
|
+
returnFocus: () => FocusTarget | null;
|
|
609
|
+
}) => useFocusTrap({ active: true, containerRef: ref, restoreFocus, returnFocus }),
|
|
610
|
+
{ initialProps: { restoreFocus: firstRestore, returnFocus: firstReturn } },
|
|
611
|
+
);
|
|
612
|
+
const second = host.lastElementChild as HTMLButtonElement;
|
|
613
|
+
second.focus();
|
|
614
|
+
const latestRestore = vi.fn(() => false);
|
|
615
|
+
const latestReturn = vi.fn<() => FocusTarget | null>(() => fallback);
|
|
616
|
+
|
|
617
|
+
view.rerender({ restoreFocus: latestRestore, returnFocus: latestReturn });
|
|
618
|
+
expect(document.activeElement).toBe(second);
|
|
619
|
+
view.unmount();
|
|
620
|
+
act(() => {
|
|
621
|
+
vi.runOnlyPendingTimers();
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
expect(firstRestore).not.toHaveBeenCalled();
|
|
625
|
+
expect(firstReturn).not.toHaveBeenCalled();
|
|
626
|
+
expect(latestRestore).toHaveBeenCalledTimes(1);
|
|
627
|
+
expect(latestReturn).toHaveBeenCalledTimes(1);
|
|
628
|
+
expect(document.activeElement).toBe(fallback);
|
|
629
|
+
});
|
|
630
|
+
|
|
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
|
+
});
|
|
642
|
+
|
|
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
|
+
});
|
|
652
|
+
|
|
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
|
+
);
|
|
664
|
+
|
|
272
665
|
it('prefers the requested initial focus target', () => {
|
|
273
666
|
const host = mount('<button id="first">first</button><button id="wanted">wanted</button>');
|
|
274
667
|
|
package/src/use-focus-trap.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
activeFocusTarget,
|
|
5
5
|
asFocusContainer,
|
|
6
6
|
asFocusTarget,
|
|
7
|
+
hostElement,
|
|
7
8
|
type FocusContainer,
|
|
8
9
|
type FocusableElement,
|
|
9
10
|
type FocusTarget,
|
|
@@ -35,6 +36,10 @@ export interface FocusTrapOptions {
|
|
|
35
36
|
containerRef: HostRef;
|
|
36
37
|
initialFocusRef?: HostRef | undefined;
|
|
37
38
|
onEscape?: (() => void) | undefined;
|
|
39
|
+
/** Whether to restore the opener. Defaults to true; false still allows `returnFocus`. */
|
|
40
|
+
restoreFocus?: (() => boolean) | undefined;
|
|
41
|
+
/** Fallback when the opener is gone or `restoreFocus` returns false. */
|
|
42
|
+
returnFocus?: (() => FocusTarget | null) | undefined;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
export interface FocusTrapProps {
|
|
@@ -71,7 +76,6 @@ export function wrapFocusAtBoundary(
|
|
|
71
76
|
return true;
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
/** Contains web Tab order inside an overlay and restores focus to its opener. */
|
|
75
79
|
/** Focuses the requested target, else the first focusable child, else the container. */
|
|
76
80
|
export function focusOverlayEntry(
|
|
77
81
|
container: FocusContainer | null,
|
|
@@ -112,27 +116,73 @@ export function applyFocusTrapKey(
|
|
|
112
116
|
return 'wrapped';
|
|
113
117
|
}
|
|
114
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
|
+
|
|
130
|
+
function hasOutsideFocus(container: object | null): boolean {
|
|
131
|
+
const active = document.activeElement;
|
|
132
|
+
if (
|
|
133
|
+
active === null ||
|
|
134
|
+
active === document.body ||
|
|
135
|
+
!active.isConnected ||
|
|
136
|
+
active.closest('[inert], [hidden], [aria-hidden="true"]') !== null
|
|
137
|
+
) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
return !containsFocus(container);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function isConnected(target: FocusTarget | null | undefined): target is FocusTarget {
|
|
144
|
+
return target != null && 'isConnected' in target && target.isConnected === true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Contains web Tab order inside an overlay and restores focus when needed on close. */
|
|
115
148
|
export function useFocusTrap({
|
|
116
149
|
active,
|
|
117
150
|
containerRef,
|
|
118
151
|
initialFocusRef,
|
|
119
152
|
onEscape,
|
|
153
|
+
restoreFocus,
|
|
154
|
+
returnFocus,
|
|
120
155
|
}: FocusTrapOptions): FocusTrapProps {
|
|
121
156
|
const onEscapeRef = useRef(onEscape);
|
|
157
|
+
const restoreFocusRef = useRef(restoreFocus);
|
|
158
|
+
const returnFocusRef = useRef(returnFocus);
|
|
159
|
+
const openerRef = useRef<FocusTarget | null | undefined>(undefined);
|
|
122
160
|
const restoreTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
123
161
|
|
|
124
162
|
useEffect(() => {
|
|
125
163
|
onEscapeRef.current = onEscape;
|
|
126
164
|
}, [onEscape]);
|
|
127
165
|
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
restoreFocusRef.current = restoreFocus;
|
|
168
|
+
returnFocusRef.current = returnFocus;
|
|
169
|
+
}, [restoreFocus, returnFocus]);
|
|
170
|
+
|
|
128
171
|
useEffect(() => {
|
|
129
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
|
+
}
|
|
130
180
|
if (restoreTimerRef.current !== undefined) {
|
|
131
181
|
clearTimeout(restoreTimerRef.current);
|
|
132
182
|
restoreTimerRef.current = undefined;
|
|
133
183
|
}
|
|
134
184
|
|
|
135
|
-
const previouslyFocused =
|
|
185
|
+
const previouslyFocused = openerRef.current;
|
|
136
186
|
focusOverlayEntry(asFocusContainer(containerRef), asFocusTarget(initialFocusRef));
|
|
137
187
|
|
|
138
188
|
return () => {
|
|
@@ -140,7 +190,14 @@ export function useFocusTrap({
|
|
|
140
190
|
// the next task so the browser does not reject focus on a still-inert trigger.
|
|
141
191
|
restoreTimerRef.current = setTimeout(() => {
|
|
142
192
|
restoreTimerRef.current = undefined;
|
|
143
|
-
|
|
193
|
+
openerRef.current = undefined;
|
|
194
|
+
if (hasOutsideFocus(container)) return;
|
|
195
|
+
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
196
|
+
const target =
|
|
197
|
+
restoreOpener && isConnected(previouslyFocused)
|
|
198
|
+
? previouslyFocused
|
|
199
|
+
: returnFocusRef.current?.();
|
|
200
|
+
if (isConnected(target)) target.focus({ preventScroll: true });
|
|
144
201
|
}, 0);
|
|
145
202
|
};
|
|
146
203
|
}, [active, containerRef, initialFocusRef]);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { act, cleanup, renderHook } from '@testing-library/react';
|
|
3
|
-
import type
|
|
4
|
-
import {
|
|
3
|
+
import { createElement, StrictMode, type RefObject } from 'react';
|
|
4
|
+
import type { View } from 'react-native';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
6
|
|
|
6
7
|
const platform = { OS: 'ios' as string };
|
|
7
8
|
const setAccessibilityFocus = vi.fn<(tag: number) => void>();
|
|
@@ -32,8 +33,8 @@ import { useOverlayA11y } from './use-overlay-a11y.js';
|
|
|
32
33
|
const CONTAINER = { id: 'container' };
|
|
33
34
|
const TRIGGER = { id: 'trigger' };
|
|
34
35
|
|
|
35
|
-
function ref(current: unknown): RefObject<
|
|
36
|
-
return { current } as
|
|
36
|
+
function ref(current: unknown): RefObject<View | null> {
|
|
37
|
+
return { current } as RefObject<View | null>;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
function flushInteractions(): void {
|
|
@@ -41,8 +42,14 @@ function flushInteractions(): void {
|
|
|
41
42
|
for (const task of tasks) task();
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.useFakeTimers();
|
|
47
|
+
});
|
|
48
|
+
|
|
44
49
|
afterEach(() => {
|
|
45
50
|
cleanup();
|
|
51
|
+
vi.clearAllTimers();
|
|
52
|
+
vi.useRealTimers();
|
|
46
53
|
setAccessibilityFocus.mockReset();
|
|
47
54
|
findNodeHandle.mockReset();
|
|
48
55
|
cancel.mockReset();
|
|
@@ -87,6 +94,23 @@ describe('useOverlayA11y on native', () => {
|
|
|
87
94
|
expect(setAccessibilityFocus).toHaveBeenCalledWith(3);
|
|
88
95
|
});
|
|
89
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
|
+
|
|
90
114
|
it('prefers a trigger handle the caller already resolved', () => {
|
|
91
115
|
findNodeHandle.mockReturnValue(null);
|
|
92
116
|
|
|
@@ -112,6 +136,96 @@ describe('useOverlayA11y on native', () => {
|
|
|
112
136
|
expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
113
137
|
});
|
|
114
138
|
|
|
139
|
+
it('resolves the fallback when the trigger unmounts while open', () => {
|
|
140
|
+
const fallback = { focus: vi.fn() };
|
|
141
|
+
findNodeHandle.mockImplementation((target) =>
|
|
142
|
+
target === TRIGGER ? 3 : target === fallback ? 9 : null,
|
|
143
|
+
);
|
|
144
|
+
const triggerRef = ref(TRIGGER);
|
|
145
|
+
const view = renderHook(() =>
|
|
146
|
+
useOverlayA11y({
|
|
147
|
+
active: true,
|
|
148
|
+
containerRef: ref(CONTAINER),
|
|
149
|
+
triggerRef,
|
|
150
|
+
returnFocus: () => fallback,
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
triggerRef.current = null;
|
|
155
|
+
view.unmount();
|
|
156
|
+
|
|
157
|
+
expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(9);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it.each([false, true])('honors declined restoration with fallback = %s', (withFallback) => {
|
|
161
|
+
const fallback = { focus: vi.fn() };
|
|
162
|
+
findNodeHandle.mockImplementation((target) =>
|
|
163
|
+
target === TRIGGER ? 3 : target === fallback ? 9 : null,
|
|
164
|
+
);
|
|
165
|
+
const view = renderHook(() =>
|
|
166
|
+
useOverlayA11y({
|
|
167
|
+
active: true,
|
|
168
|
+
containerRef: ref(CONTAINER),
|
|
169
|
+
triggerRef: ref(TRIGGER),
|
|
170
|
+
restoreFocus: () => false,
|
|
171
|
+
returnFocus: withFallback ? () => fallback : undefined,
|
|
172
|
+
}),
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
view.unmount();
|
|
176
|
+
|
|
177
|
+
if (withFallback) expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(9);
|
|
178
|
+
else expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('reads the latest restoration callbacks without repeating native entry focus', () => {
|
|
182
|
+
const fallback = { focus: vi.fn() };
|
|
183
|
+
findNodeHandle.mockImplementation((target) =>
|
|
184
|
+
target === CONTAINER ? 7 : target === TRIGGER ? 3 : target === fallback ? 9 : null,
|
|
185
|
+
);
|
|
186
|
+
const containerRef = ref(CONTAINER);
|
|
187
|
+
const triggerRef = ref(TRIGGER);
|
|
188
|
+
const view = renderHook(
|
|
189
|
+
({ restoreFocus }) =>
|
|
190
|
+
useOverlayA11y({
|
|
191
|
+
active: true,
|
|
192
|
+
containerRef,
|
|
193
|
+
triggerRef,
|
|
194
|
+
restoreFocus,
|
|
195
|
+
returnFocus: () => fallback,
|
|
196
|
+
}),
|
|
197
|
+
{ initialProps: { restoreFocus: () => true } },
|
|
198
|
+
);
|
|
199
|
+
act(flushInteractions);
|
|
200
|
+
setAccessibilityFocus.mockReset();
|
|
201
|
+
|
|
202
|
+
view.rerender({ restoreFocus: () => false });
|
|
203
|
+
expect(pendingInteractions).toHaveLength(0);
|
|
204
|
+
expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
205
|
+
view.unmount();
|
|
206
|
+
|
|
207
|
+
expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(9);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('honors an opt-out supplied in the render that closes the native overlay', () => {
|
|
211
|
+
findNodeHandle.mockReturnValue(3);
|
|
212
|
+
const containerRef = ref(CONTAINER);
|
|
213
|
+
const view = renderHook(
|
|
214
|
+
({ active }) =>
|
|
215
|
+
useOverlayA11y({
|
|
216
|
+
active,
|
|
217
|
+
containerRef,
|
|
218
|
+
triggerHandle: 3,
|
|
219
|
+
restoreFocus: () => active,
|
|
220
|
+
}),
|
|
221
|
+
{ initialProps: { active: true } },
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
view.rerender({ active: false });
|
|
225
|
+
|
|
226
|
+
expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
|
|
115
229
|
it('survives a host node that disappears between layout and focus', () => {
|
|
116
230
|
findNodeHandle.mockImplementation(() => {
|
|
117
231
|
throw new Error('view unmounted');
|
|
@@ -174,6 +288,125 @@ describe('useOverlayA11y on native', () => {
|
|
|
174
288
|
});
|
|
175
289
|
|
|
176
290
|
describe('useOverlayA11y on web', () => {
|
|
291
|
+
function mountOverlay() {
|
|
292
|
+
platform.OS = 'web';
|
|
293
|
+
document.body.innerHTML = `
|
|
294
|
+
<button id="opener">open</button>
|
|
295
|
+
<div id="overlay"><button id="inside">inside</button></div>
|
|
296
|
+
<div id="outside"><h1 id="destination" tabindex="-1">destination</h1></div>
|
|
297
|
+
`;
|
|
298
|
+
const opener = document.querySelector<HTMLButtonElement>('#opener');
|
|
299
|
+
const overlay = document.querySelector<HTMLElement>('#overlay');
|
|
300
|
+
const outside = document.querySelector<HTMLElement>('#outside');
|
|
301
|
+
const destination = document.querySelector<HTMLElement>('#destination');
|
|
302
|
+
if (!opener || !overlay || !outside || !destination) throw new Error('Missing overlay fixture');
|
|
303
|
+
opener.focus();
|
|
304
|
+
return { opener, overlay, outside, destination };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
it('keeps outside focus moved after useInert releases the background', () => {
|
|
308
|
+
const { overlay, destination } = mountOverlay();
|
|
309
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef: ref(overlay) }));
|
|
310
|
+
|
|
311
|
+
view.unmount();
|
|
312
|
+
destination.focus();
|
|
313
|
+
act(() => {
|
|
314
|
+
vi.runOnlyPendingTimers();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
expect(document.activeElement).toBe(destination);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it.each(['body', 'closing container'])(
|
|
321
|
+
'restores focus from %s after releasing the inert opener',
|
|
322
|
+
(location) => {
|
|
323
|
+
const { opener, overlay } = mountOverlay();
|
|
324
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef: ref(overlay) }));
|
|
325
|
+
expect(opener.hasAttribute('inert')).toBe(true);
|
|
326
|
+
|
|
327
|
+
view.unmount();
|
|
328
|
+
if (location === 'body') overlay.remove();
|
|
329
|
+
expect(opener.hasAttribute('inert')).toBe(false);
|
|
330
|
+
act(() => {
|
|
331
|
+
vi.runOnlyPendingTimers();
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
expect(document.activeElement).toBe(opener);
|
|
335
|
+
},
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
it.each(['inert', 'hidden', 'aria-hidden'])(
|
|
339
|
+
'restores when the outside destination becomes %s',
|
|
340
|
+
(attribute) => {
|
|
341
|
+
const { opener, overlay, outside, destination } = mountOverlay();
|
|
342
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef: ref(overlay) }));
|
|
343
|
+
|
|
344
|
+
view.unmount();
|
|
345
|
+
destination.focus();
|
|
346
|
+
outside.setAttribute(attribute, 'true');
|
|
347
|
+
expect(document.activeElement).toBe(destination);
|
|
348
|
+
act(() => {
|
|
349
|
+
vi.runOnlyPendingTimers();
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
expect(document.activeElement).toBe(opener);
|
|
353
|
+
},
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
it('uses the fallback when the opener disconnects', () => {
|
|
357
|
+
const { opener, overlay, destination } = mountOverlay();
|
|
358
|
+
const view = renderHook(() =>
|
|
359
|
+
useOverlayA11y({
|
|
360
|
+
active: true,
|
|
361
|
+
containerRef: ref(overlay),
|
|
362
|
+
returnFocus: () => destination,
|
|
363
|
+
}),
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
view.unmount();
|
|
367
|
+
opener.remove();
|
|
368
|
+
act(() => {
|
|
369
|
+
vi.runOnlyPendingTimers();
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
expect(document.activeElement).toBe(destination);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('does nothing when restoration is declined', () => {
|
|
376
|
+
const { opener, overlay } = mountOverlay();
|
|
377
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
378
|
+
const view = renderHook(() =>
|
|
379
|
+
useOverlayA11y({ active: true, containerRef: ref(overlay), restoreFocus: () => false }),
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
view.unmount();
|
|
383
|
+
overlay.remove();
|
|
384
|
+
act(() => {
|
|
385
|
+
vi.runOnlyPendingTimers();
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
expect(focus).not.toHaveBeenCalled();
|
|
389
|
+
expect(document.activeElement).toBe(document.body);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it('cancels restoration when reopened before the next task', () => {
|
|
393
|
+
const { opener, overlay } = mountOverlay();
|
|
394
|
+
const containerRef = ref(overlay);
|
|
395
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
396
|
+
const view = renderHook(({ active }) => useOverlayA11y({ active, containerRef }), {
|
|
397
|
+
initialProps: { active: true },
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
view.rerender({ active: false });
|
|
401
|
+
view.rerender({ active: true });
|
|
402
|
+
act(() => {
|
|
403
|
+
vi.runOnlyPendingTimers();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
expect(focus).not.toHaveBeenCalled();
|
|
407
|
+
expect(document.activeElement).toBe(overlay.firstElementChild);
|
|
408
|
+
});
|
|
409
|
+
|
|
177
410
|
it('traps focus in the real overlay and leaves the background props empty', () => {
|
|
178
411
|
platform.OS = 'web';
|
|
179
412
|
document.body.innerHTML = `
|
package/src/use-overlay-a11y.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef, type RefObject } from 'react';
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, type RefObject } from 'react';
|
|
2
2
|
import {
|
|
3
3
|
AccessibilityInfo,
|
|
4
4
|
InteractionManager,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
|
|
10
10
|
import type { HostRef } from './dom-boundary.js';
|
|
11
11
|
import { nativeNodeHandle } from './native-focus.js';
|
|
12
|
-
import { useFocusTrap, type FocusTrapProps } from './use-focus-trap.js';
|
|
12
|
+
import { useFocusTrap, type FocusTrapOptions, type FocusTrapProps } from './use-focus-trap.js';
|
|
13
13
|
import { useInert } from './use-inert.js';
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -18,7 +18,7 @@ import { useInert } from './use-inert.js';
|
|
|
18
18
|
*/
|
|
19
19
|
export type DeferFocus = (task: () => void) => { cancel: () => void };
|
|
20
20
|
|
|
21
|
-
export interface OverlayA11yOptions {
|
|
21
|
+
export interface OverlayA11yOptions extends Pick<FocusTrapOptions, 'restoreFocus' | 'returnFocus'> {
|
|
22
22
|
active: boolean;
|
|
23
23
|
containerRef: RefObject<View | null>;
|
|
24
24
|
/** Overrides how the native focus request waits for layout. */
|
|
@@ -61,17 +61,30 @@ export function useOverlayA11y({
|
|
|
61
61
|
inertContainerRef,
|
|
62
62
|
initialFocusRef,
|
|
63
63
|
onEscape,
|
|
64
|
+
restoreFocus,
|
|
65
|
+
returnFocus,
|
|
64
66
|
triggerHandle,
|
|
65
67
|
triggerRef,
|
|
66
68
|
}: OverlayA11yOptions): OverlayA11yResult {
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
+
const restoreFocusRef = useRef(restoreFocus);
|
|
70
|
+
const returnFocusRef = useRef(returnFocus);
|
|
71
|
+
useLayoutEffect(() => {
|
|
72
|
+
restoreFocusRef.current = restoreFocus;
|
|
73
|
+
returnFocusRef.current = returnFocus;
|
|
74
|
+
}, [restoreFocus, returnFocus]);
|
|
75
|
+
const containerProps = useFocusTrap({
|
|
76
|
+
active,
|
|
77
|
+
containerRef,
|
|
78
|
+
initialFocusRef,
|
|
79
|
+
onEscape,
|
|
80
|
+
restoreFocus,
|
|
81
|
+
returnFocus,
|
|
82
|
+
});
|
|
69
83
|
useInert(inertContainerRef ?? containerRef, active);
|
|
70
84
|
|
|
71
85
|
useEffect(() => {
|
|
72
86
|
if (Platform.OS === 'web' || !active) return;
|
|
73
87
|
|
|
74
|
-
triggerHandleRef.current = triggerHandle ?? nativeNodeHandle(triggerRef);
|
|
75
88
|
const task = deferFocus(() => {
|
|
76
89
|
const containerHandle = nativeNodeHandle(containerRef);
|
|
77
90
|
if (containerHandle !== null) AccessibilityInfo.setAccessibilityFocus(containerHandle);
|
|
@@ -79,9 +92,11 @@ export function useOverlayA11y({
|
|
|
79
92
|
|
|
80
93
|
return () => {
|
|
81
94
|
task.cancel();
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
95
|
+
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
96
|
+
const openerHandle = restoreOpener ? (triggerHandle ?? nativeNodeHandle(triggerRef)) : null;
|
|
97
|
+
const targetHandle =
|
|
98
|
+
openerHandle ?? nativeNodeHandle({ current: returnFocusRef.current?.() ?? null });
|
|
99
|
+
if (targetHandle !== null) AccessibilityInfo.setAccessibilityFocus(targetHandle);
|
|
85
100
|
};
|
|
86
101
|
}, [active, containerRef, deferFocus, triggerHandle, triggerRef]);
|
|
87
102
|
|