@baukit/a11y-core 0.10.28 → 0.10.30
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 +35 -4
- 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 +217 -5
- package/src/use-focus-trap.ts +43 -2
- package/src/use-overlay-a11y.test.ts +219 -3
- 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,CAqDnC"}
|
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,12 +67,36 @@ export function applyFocusTrapKey(container, event) {
|
|
|
68
67
|
event.preventDefault();
|
|
69
68
|
return 'wrapped';
|
|
70
69
|
}
|
|
71
|
-
|
|
70
|
+
function hasOutsideFocus(container) {
|
|
71
|
+
const active = document.activeElement;
|
|
72
|
+
if (active === null ||
|
|
73
|
+
active === document.body ||
|
|
74
|
+
!active.isConnected ||
|
|
75
|
+
active.closest('[inert], [hidden], [aria-hidden="true"]') !== null) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
for (let element = active; element !== null; element = element.parentElement) {
|
|
79
|
+
if (element === container)
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
function isConnected(target) {
|
|
85
|
+
return target != null && 'isConnected' in target && target.isConnected === true;
|
|
86
|
+
}
|
|
87
|
+
/** Contains web Tab order inside an overlay and restores focus when needed on close. */
|
|
88
|
+
export function useFocusTrap({ active, containerRef, initialFocusRef, onEscape, restoreFocus, returnFocus, }) {
|
|
72
89
|
const onEscapeRef = useRef(onEscape);
|
|
90
|
+
const restoreFocusRef = useRef(restoreFocus);
|
|
91
|
+
const returnFocusRef = useRef(returnFocus);
|
|
73
92
|
const restoreTimerRef = useRef(undefined);
|
|
74
93
|
useEffect(() => {
|
|
75
94
|
onEscapeRef.current = onEscape;
|
|
76
95
|
}, [onEscape]);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
restoreFocusRef.current = restoreFocus;
|
|
98
|
+
returnFocusRef.current = returnFocus;
|
|
99
|
+
}, [restoreFocus, returnFocus]);
|
|
77
100
|
useEffect(() => {
|
|
78
101
|
if (!hasDocument() || !active)
|
|
79
102
|
return;
|
|
@@ -82,13 +105,21 @@ export function useFocusTrap({ active, containerRef, initialFocusRef, onEscape,
|
|
|
82
105
|
restoreTimerRef.current = undefined;
|
|
83
106
|
}
|
|
84
107
|
const previouslyFocused = activeFocusTarget();
|
|
108
|
+
const container = hostElement(containerRef);
|
|
85
109
|
focusOverlayEntry(asFocusContainer(containerRef), asFocusTarget(initialFocusRef));
|
|
86
110
|
return () => {
|
|
87
111
|
// useInert releases outside siblings in a sibling effect cleanup. Restore on
|
|
88
112
|
// the next task so the browser does not reject focus on a still-inert trigger.
|
|
89
113
|
restoreTimerRef.current = setTimeout(() => {
|
|
90
114
|
restoreTimerRef.current = undefined;
|
|
91
|
-
|
|
115
|
+
if (hasOutsideFocus(container))
|
|
116
|
+
return;
|
|
117
|
+
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
118
|
+
const target = restoreOpener && isConnected(previouslyFocused)
|
|
119
|
+
? previouslyFocused
|
|
120
|
+
: returnFocusRef.current?.();
|
|
121
|
+
if (isConnected(target))
|
|
122
|
+
target.focus({ preventScroll: true });
|
|
92
123
|
}, 0);
|
|
93
124
|
};
|
|
94
125
|
}, [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,CAAC,aAAa,CAAC,KAAK,MAAM;QAC9C,CAAC,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAC/B,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,SAAyB;IACzD,OAAO,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,kBAAkB,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AACxF,CAAC;AAED,y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G,EAAE;gBACxC,eAAe,CAAC,OAAO,GAAG,SAAS,CAAC;gBACpC,IAAI,eAAe,CAAC,SAAS,CAAC;oBAAE,OAAO;gBACvC,MAAM,aAAa,GAAG,eAAe,CAAC,OAAO,EAAE,EAAE,IAAI,IAAI,CAAC;gBAC1D,MAAM,MAAM,GACV,aAAa,IAAI,WAAW,CAAC,iBAAiB,CAAC;oBAC7C,CAAC,CAAC,iBAAiB;oBACnB,CAAC,CAAC,cAAc,CAAC,OAAO,EAAE,EAAE,CAAC;gBACjC,IAAI,WAAW,CAAC,MAAM,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"}
|
|
@@ -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,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Ref } 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;AAC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Y,GAChB,YAAY,IAAI,gBAAgB,CAAC,EAAE,OAAO,EAAE,cAAc,CAAC,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;YACpF,IAAI,YAAY,KAAK,IAAI;gBAAE,iBAAiB,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC;QACnF,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,UAAU,CAAC,CAAC,CAAC;IAElE,OAAO;QACL,eAAe,EACb,QAAQ,CAAC,EAAE,KAAK,KAAK,IAAI,MAAM;YAC7B,CAAC,CAAC,EAAE,2BAA2B,EAAE,IAAI,EAAE,yBAAyB,EAAE,qBAAqB,EAAE;YACzF,CAAC,CAAC,EAAE;QACR,cAAc;KACf,CAAC;AACJ,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.30",
|
|
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
3
|
import { createElement, useRef } from 'react';
|
|
4
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
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
|
|
|
@@ -250,7 +256,7 @@ describe('useFocusTrap', () => {
|
|
|
250
256
|
expect(document.activeElement?.id).toBe('first');
|
|
251
257
|
});
|
|
252
258
|
|
|
253
|
-
it('restores focus to the opener on the next task',
|
|
259
|
+
it('restores focus to the opener on the next task', () => {
|
|
254
260
|
const opener = mount('<button id="opener">open</button>')
|
|
255
261
|
.firstElementChild as HTMLButtonElement;
|
|
256
262
|
opener.focus();
|
|
@@ -263,12 +269,218 @@ describe('useFocusTrap', () => {
|
|
|
263
269
|
// The restore is deliberately deferred so useInert can release the trigger.
|
|
264
270
|
expect(document.activeElement?.id).toBe('inside');
|
|
265
271
|
|
|
266
|
-
|
|
267
|
-
|
|
272
|
+
act(() => {
|
|
273
|
+
vi.runOnlyPendingTimers();
|
|
268
274
|
});
|
|
269
275
|
expect(document.activeElement?.id).toBe('opener');
|
|
270
276
|
});
|
|
271
277
|
|
|
278
|
+
it('keeps focus moved outside after closing', () => {
|
|
279
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
280
|
+
opener.focus();
|
|
281
|
+
const host = mount('<button>inside</button>');
|
|
282
|
+
const destination = mount('<h1 tabindex="-1">new route</h1>').firstElementChild as HTMLElement;
|
|
283
|
+
const restoreFocus = vi.fn(() => true);
|
|
284
|
+
const returnFocus = vi.fn(() => opener);
|
|
285
|
+
const view = renderHook(() =>
|
|
286
|
+
useFocusTrap({ active: true, containerRef: containerRef(host), restoreFocus, returnFocus }),
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
view.unmount();
|
|
290
|
+
destination.focus();
|
|
291
|
+
act(() => {
|
|
292
|
+
vi.runOnlyPendingTimers();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
expect(document.activeElement).toBe(destination);
|
|
296
|
+
expect(restoreFocus).not.toHaveBeenCalled();
|
|
297
|
+
expect(returnFocus).not.toHaveBeenCalled();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it.each(['body', 'container', 'removed container'])(
|
|
301
|
+
'restores when focus is on %s',
|
|
302
|
+
(location) => {
|
|
303
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
304
|
+
opener.focus();
|
|
305
|
+
const host = mount('<button>inside</button>');
|
|
306
|
+
host.tabIndex = -1;
|
|
307
|
+
const ref = containerRef(host);
|
|
308
|
+
const view = renderHook(() => useFocusTrap({ active: true, containerRef: ref }));
|
|
309
|
+
|
|
310
|
+
view.unmount();
|
|
311
|
+
ref.current = null;
|
|
312
|
+
if (location === 'body') (document.activeElement as HTMLElement).blur();
|
|
313
|
+
if (location === 'container') host.focus();
|
|
314
|
+
if (location === 'removed container') host.remove();
|
|
315
|
+
expect(document.activeElement).toBe(location === 'container' ? host : document.body);
|
|
316
|
+
act(() => {
|
|
317
|
+
vi.runOnlyPendingTimers();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(document.activeElement).toBe(opener);
|
|
321
|
+
},
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
it.each(['inert', 'hidden', 'aria-hidden'])(
|
|
325
|
+
'restores when outside focus is in a %s subtree',
|
|
326
|
+
(attribute) => {
|
|
327
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
328
|
+
opener.focus();
|
|
329
|
+
const host = mount('<button>inside</button>');
|
|
330
|
+
const outside = mount('<button>outside</button>');
|
|
331
|
+
const destination = outside.firstElementChild as HTMLButtonElement;
|
|
332
|
+
const view = renderHook(() =>
|
|
333
|
+
useFocusTrap({ active: true, containerRef: containerRef(host) }),
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
view.unmount();
|
|
337
|
+
destination.focus();
|
|
338
|
+
outside.setAttribute(attribute, 'true');
|
|
339
|
+
expect(document.activeElement).toBe(destination);
|
|
340
|
+
act(() => {
|
|
341
|
+
vi.runOnlyPendingTimers();
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
expect(document.activeElement).toBe(opener);
|
|
345
|
+
},
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
it('does not focus a disconnected opener', () => {
|
|
349
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
350
|
+
opener.focus();
|
|
351
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
352
|
+
const host = mount('<button>inside</button>');
|
|
353
|
+
const view = renderHook(() => useFocusTrap({ active: true, containerRef: containerRef(host) }));
|
|
354
|
+
|
|
355
|
+
view.unmount();
|
|
356
|
+
opener.remove();
|
|
357
|
+
act(() => {
|
|
358
|
+
vi.runOnlyPendingTimers();
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
expect(focus).not.toHaveBeenCalled();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('resolves a fallback at restore time when the opener was disconnected', () => {
|
|
365
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
366
|
+
opener.focus();
|
|
367
|
+
const host = mount('<button>inside</button>');
|
|
368
|
+
let fallback: HTMLElement | null = null;
|
|
369
|
+
const returnFocus = vi.fn(() => fallback);
|
|
370
|
+
const view = renderHook(() =>
|
|
371
|
+
useFocusTrap({ active: true, containerRef: containerRef(host), returnFocus }),
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
view.unmount();
|
|
375
|
+
opener.remove();
|
|
376
|
+
fallback = mount('<h1 tabindex="-1">fallback</h1>').firstElementChild as HTMLElement;
|
|
377
|
+
act(() => {
|
|
378
|
+
vi.runOnlyPendingTimers();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
expect(returnFocus).toHaveBeenCalledTimes(1);
|
|
382
|
+
expect(document.activeElement).toBe(fallback);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it('does not focus a disconnected fallback', () => {
|
|
386
|
+
const host = mount('<button>inside</button>');
|
|
387
|
+
const fallback = document.createElement('button');
|
|
388
|
+
const focus = vi.spyOn(fallback, 'focus');
|
|
389
|
+
const view = renderHook(() =>
|
|
390
|
+
useFocusTrap({
|
|
391
|
+
active: true,
|
|
392
|
+
containerRef: containerRef(host),
|
|
393
|
+
restoreFocus: () => false,
|
|
394
|
+
returnFocus: () => fallback,
|
|
395
|
+
}),
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
view.unmount();
|
|
399
|
+
act(() => {
|
|
400
|
+
vi.runOnlyPendingTimers();
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
expect(focus).not.toHaveBeenCalled();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('does nothing when restoration is declined without a fallback', () => {
|
|
407
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
408
|
+
opener.focus();
|
|
409
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
410
|
+
const host = mount('<button>inside</button>');
|
|
411
|
+
const restoreFocus = vi.fn(() => false);
|
|
412
|
+
const view = renderHook(() =>
|
|
413
|
+
useFocusTrap({ active: true, containerRef: containerRef(host), restoreFocus }),
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
view.unmount();
|
|
417
|
+
host.remove();
|
|
418
|
+
act(() => {
|
|
419
|
+
vi.runOnlyPendingTimers();
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
expect(restoreFocus).toHaveBeenCalledTimes(1);
|
|
423
|
+
expect(focus).not.toHaveBeenCalled();
|
|
424
|
+
expect(document.activeElement).toBe(document.body);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it('uses the latest restoration callbacks without re-entering the trap', () => {
|
|
428
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
429
|
+
opener.focus();
|
|
430
|
+
const host = mount('<button>first</button><button>second</button>');
|
|
431
|
+
const ref = containerRef(host);
|
|
432
|
+
const fallback = mount('<h1 tabindex="-1">fallback</h1>').firstElementChild as HTMLElement;
|
|
433
|
+
const firstRestore = vi.fn(() => true);
|
|
434
|
+
const firstReturn = vi.fn<() => FocusTarget | null>(() => null);
|
|
435
|
+
const view = renderHook(
|
|
436
|
+
({
|
|
437
|
+
restoreFocus,
|
|
438
|
+
returnFocus,
|
|
439
|
+
}: {
|
|
440
|
+
restoreFocus: () => boolean;
|
|
441
|
+
returnFocus: () => FocusTarget | null;
|
|
442
|
+
}) => useFocusTrap({ active: true, containerRef: ref, restoreFocus, returnFocus }),
|
|
443
|
+
{ initialProps: { restoreFocus: firstRestore, returnFocus: firstReturn } },
|
|
444
|
+
);
|
|
445
|
+
const second = host.lastElementChild as HTMLButtonElement;
|
|
446
|
+
second.focus();
|
|
447
|
+
const latestRestore = vi.fn(() => false);
|
|
448
|
+
const latestReturn = vi.fn<() => FocusTarget | null>(() => fallback);
|
|
449
|
+
|
|
450
|
+
view.rerender({ restoreFocus: latestRestore, returnFocus: latestReturn });
|
|
451
|
+
expect(document.activeElement).toBe(second);
|
|
452
|
+
view.unmount();
|
|
453
|
+
act(() => {
|
|
454
|
+
vi.runOnlyPendingTimers();
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
expect(firstRestore).not.toHaveBeenCalled();
|
|
458
|
+
expect(firstReturn).not.toHaveBeenCalled();
|
|
459
|
+
expect(latestRestore).toHaveBeenCalledTimes(1);
|
|
460
|
+
expect(latestReturn).toHaveBeenCalledTimes(1);
|
|
461
|
+
expect(document.activeElement).toBe(fallback);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('cancels the pending restore when reopened before the next task', () => {
|
|
465
|
+
const opener = mount('<button>open</button>').firstElementChild as HTMLButtonElement;
|
|
466
|
+
opener.focus();
|
|
467
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
468
|
+
const host = mount('<button>inside</button>');
|
|
469
|
+
const ref = containerRef(host);
|
|
470
|
+
const view = renderHook(({ active }) => useFocusTrap({ active, containerRef: ref }), {
|
|
471
|
+
initialProps: { active: true },
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
view.rerender({ active: false });
|
|
475
|
+
view.rerender({ active: true });
|
|
476
|
+
act(() => {
|
|
477
|
+
vi.runOnlyPendingTimers();
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
expect(focus).not.toHaveBeenCalled();
|
|
481
|
+
expect(document.activeElement).toBe(host.firstElementChild);
|
|
482
|
+
});
|
|
483
|
+
|
|
272
484
|
it('prefers the requested initial focus target', () => {
|
|
273
485
|
const host = mount('<button id="first">first</button><button id="wanted">wanted</button>');
|
|
274
486
|
|
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,19 +116,49 @@ export function applyFocusTrapKey(
|
|
|
112
116
|
return 'wrapped';
|
|
113
117
|
}
|
|
114
118
|
|
|
119
|
+
function hasOutsideFocus(container: object | null): boolean {
|
|
120
|
+
const active = document.activeElement;
|
|
121
|
+
if (
|
|
122
|
+
active === null ||
|
|
123
|
+
active === document.body ||
|
|
124
|
+
!active.isConnected ||
|
|
125
|
+
active.closest('[inert], [hidden], [aria-hidden="true"]') !== null
|
|
126
|
+
) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
for (let element: Element | null = active; element !== null; element = element.parentElement) {
|
|
130
|
+
if (element === container) return false;
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isConnected(target: FocusTarget | null | undefined): target is FocusTarget {
|
|
136
|
+
return target != null && 'isConnected' in target && target.isConnected === true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Contains web Tab order inside an overlay and restores focus when needed on close. */
|
|
115
140
|
export function useFocusTrap({
|
|
116
141
|
active,
|
|
117
142
|
containerRef,
|
|
118
143
|
initialFocusRef,
|
|
119
144
|
onEscape,
|
|
145
|
+
restoreFocus,
|
|
146
|
+
returnFocus,
|
|
120
147
|
}: FocusTrapOptions): FocusTrapProps {
|
|
121
148
|
const onEscapeRef = useRef(onEscape);
|
|
149
|
+
const restoreFocusRef = useRef(restoreFocus);
|
|
150
|
+
const returnFocusRef = useRef(returnFocus);
|
|
122
151
|
const restoreTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
123
152
|
|
|
124
153
|
useEffect(() => {
|
|
125
154
|
onEscapeRef.current = onEscape;
|
|
126
155
|
}, [onEscape]);
|
|
127
156
|
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
restoreFocusRef.current = restoreFocus;
|
|
159
|
+
returnFocusRef.current = returnFocus;
|
|
160
|
+
}, [restoreFocus, returnFocus]);
|
|
161
|
+
|
|
128
162
|
useEffect(() => {
|
|
129
163
|
if (!hasDocument() || !active) return;
|
|
130
164
|
if (restoreTimerRef.current !== undefined) {
|
|
@@ -133,6 +167,7 @@ export function useFocusTrap({
|
|
|
133
167
|
}
|
|
134
168
|
|
|
135
169
|
const previouslyFocused = activeFocusTarget();
|
|
170
|
+
const container = hostElement(containerRef);
|
|
136
171
|
focusOverlayEntry(asFocusContainer(containerRef), asFocusTarget(initialFocusRef));
|
|
137
172
|
|
|
138
173
|
return () => {
|
|
@@ -140,7 +175,13 @@ export function useFocusTrap({
|
|
|
140
175
|
// the next task so the browser does not reject focus on a still-inert trigger.
|
|
141
176
|
restoreTimerRef.current = setTimeout(() => {
|
|
142
177
|
restoreTimerRef.current = undefined;
|
|
143
|
-
|
|
178
|
+
if (hasOutsideFocus(container)) return;
|
|
179
|
+
const restoreOpener = restoreFocusRef.current?.() ?? true;
|
|
180
|
+
const target =
|
|
181
|
+
restoreOpener && isConnected(previouslyFocused)
|
|
182
|
+
? previouslyFocused
|
|
183
|
+
: returnFocusRef.current?.();
|
|
184
|
+
if (isConnected(target)) target.focus({ preventScroll: true });
|
|
144
185
|
}, 0);
|
|
145
186
|
};
|
|
146
187
|
}, [active, containerRef, initialFocusRef]);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { act, cleanup, renderHook } from '@testing-library/react';
|
|
3
3
|
import type { RefObject } from 'react';
|
|
4
|
-
import {
|
|
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();
|
|
@@ -112,6 +119,96 @@ describe('useOverlayA11y on native', () => {
|
|
|
112
119
|
expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
113
120
|
});
|
|
114
121
|
|
|
122
|
+
it('resolves the fallback when the trigger unmounts while open', () => {
|
|
123
|
+
const fallback = { focus: vi.fn() };
|
|
124
|
+
findNodeHandle.mockImplementation((target) =>
|
|
125
|
+
target === TRIGGER ? 3 : target === fallback ? 9 : null,
|
|
126
|
+
);
|
|
127
|
+
const triggerRef = ref(TRIGGER);
|
|
128
|
+
const view = renderHook(() =>
|
|
129
|
+
useOverlayA11y({
|
|
130
|
+
active: true,
|
|
131
|
+
containerRef: ref(CONTAINER),
|
|
132
|
+
triggerRef,
|
|
133
|
+
returnFocus: () => fallback,
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
triggerRef.current = null;
|
|
138
|
+
view.unmount();
|
|
139
|
+
|
|
140
|
+
expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(9);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it.each([false, true])('honors declined restoration with fallback = %s', (withFallback) => {
|
|
144
|
+
const fallback = { focus: vi.fn() };
|
|
145
|
+
findNodeHandle.mockImplementation((target) =>
|
|
146
|
+
target === TRIGGER ? 3 : target === fallback ? 9 : null,
|
|
147
|
+
);
|
|
148
|
+
const view = renderHook(() =>
|
|
149
|
+
useOverlayA11y({
|
|
150
|
+
active: true,
|
|
151
|
+
containerRef: ref(CONTAINER),
|
|
152
|
+
triggerRef: ref(TRIGGER),
|
|
153
|
+
restoreFocus: () => false,
|
|
154
|
+
returnFocus: withFallback ? () => fallback : undefined,
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
view.unmount();
|
|
159
|
+
|
|
160
|
+
if (withFallback) expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(9);
|
|
161
|
+
else expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('reads the latest restoration callbacks without repeating native entry focus', () => {
|
|
165
|
+
const fallback = { focus: vi.fn() };
|
|
166
|
+
findNodeHandle.mockImplementation((target) =>
|
|
167
|
+
target === CONTAINER ? 7 : target === TRIGGER ? 3 : target === fallback ? 9 : null,
|
|
168
|
+
);
|
|
169
|
+
const containerRef = ref(CONTAINER);
|
|
170
|
+
const triggerRef = ref(TRIGGER);
|
|
171
|
+
const view = renderHook(
|
|
172
|
+
({ restoreFocus }) =>
|
|
173
|
+
useOverlayA11y({
|
|
174
|
+
active: true,
|
|
175
|
+
containerRef,
|
|
176
|
+
triggerRef,
|
|
177
|
+
restoreFocus,
|
|
178
|
+
returnFocus: () => fallback,
|
|
179
|
+
}),
|
|
180
|
+
{ initialProps: { restoreFocus: () => true } },
|
|
181
|
+
);
|
|
182
|
+
act(flushInteractions);
|
|
183
|
+
setAccessibilityFocus.mockReset();
|
|
184
|
+
|
|
185
|
+
view.rerender({ restoreFocus: () => false });
|
|
186
|
+
expect(pendingInteractions).toHaveLength(0);
|
|
187
|
+
expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
188
|
+
view.unmount();
|
|
189
|
+
|
|
190
|
+
expect(setAccessibilityFocus).toHaveBeenCalledExactlyOnceWith(9);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('honors an opt-out supplied in the render that closes the native overlay', () => {
|
|
194
|
+
findNodeHandle.mockReturnValue(3);
|
|
195
|
+
const containerRef = ref(CONTAINER);
|
|
196
|
+
const view = renderHook(
|
|
197
|
+
({ active }) =>
|
|
198
|
+
useOverlayA11y({
|
|
199
|
+
active,
|
|
200
|
+
containerRef,
|
|
201
|
+
triggerHandle: 3,
|
|
202
|
+
restoreFocus: () => active,
|
|
203
|
+
}),
|
|
204
|
+
{ initialProps: { active: true } },
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
view.rerender({ active: false });
|
|
208
|
+
|
|
209
|
+
expect(setAccessibilityFocus).not.toHaveBeenCalled();
|
|
210
|
+
});
|
|
211
|
+
|
|
115
212
|
it('survives a host node that disappears between layout and focus', () => {
|
|
116
213
|
findNodeHandle.mockImplementation(() => {
|
|
117
214
|
throw new Error('view unmounted');
|
|
@@ -174,6 +271,125 @@ describe('useOverlayA11y on native', () => {
|
|
|
174
271
|
});
|
|
175
272
|
|
|
176
273
|
describe('useOverlayA11y on web', () => {
|
|
274
|
+
function mountOverlay() {
|
|
275
|
+
platform.OS = 'web';
|
|
276
|
+
document.body.innerHTML = `
|
|
277
|
+
<button id="opener">open</button>
|
|
278
|
+
<div id="overlay"><button id="inside">inside</button></div>
|
|
279
|
+
<div id="outside"><h1 id="destination" tabindex="-1">destination</h1></div>
|
|
280
|
+
`;
|
|
281
|
+
const opener = document.querySelector<HTMLButtonElement>('#opener');
|
|
282
|
+
const overlay = document.querySelector<HTMLElement>('#overlay');
|
|
283
|
+
const outside = document.querySelector<HTMLElement>('#outside');
|
|
284
|
+
const destination = document.querySelector<HTMLElement>('#destination');
|
|
285
|
+
if (!opener || !overlay || !outside || !destination) throw new Error('Missing overlay fixture');
|
|
286
|
+
opener.focus();
|
|
287
|
+
return { opener, overlay, outside, destination };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
it('keeps outside focus moved after useInert releases the background', () => {
|
|
291
|
+
const { overlay, destination } = mountOverlay();
|
|
292
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef: ref(overlay) }));
|
|
293
|
+
|
|
294
|
+
view.unmount();
|
|
295
|
+
destination.focus();
|
|
296
|
+
act(() => {
|
|
297
|
+
vi.runOnlyPendingTimers();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
expect(document.activeElement).toBe(destination);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it.each(['body', 'closing container'])(
|
|
304
|
+
'restores focus from %s after releasing the inert opener',
|
|
305
|
+
(location) => {
|
|
306
|
+
const { opener, overlay } = mountOverlay();
|
|
307
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef: ref(overlay) }));
|
|
308
|
+
expect(opener.hasAttribute('inert')).toBe(true);
|
|
309
|
+
|
|
310
|
+
view.unmount();
|
|
311
|
+
if (location === 'body') overlay.remove();
|
|
312
|
+
expect(opener.hasAttribute('inert')).toBe(false);
|
|
313
|
+
act(() => {
|
|
314
|
+
vi.runOnlyPendingTimers();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
expect(document.activeElement).toBe(opener);
|
|
318
|
+
},
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
it.each(['inert', 'hidden', 'aria-hidden'])(
|
|
322
|
+
'restores when the outside destination becomes %s',
|
|
323
|
+
(attribute) => {
|
|
324
|
+
const { opener, overlay, outside, destination } = mountOverlay();
|
|
325
|
+
const view = renderHook(() => useOverlayA11y({ active: true, containerRef: ref(overlay) }));
|
|
326
|
+
|
|
327
|
+
view.unmount();
|
|
328
|
+
destination.focus();
|
|
329
|
+
outside.setAttribute(attribute, 'true');
|
|
330
|
+
expect(document.activeElement).toBe(destination);
|
|
331
|
+
act(() => {
|
|
332
|
+
vi.runOnlyPendingTimers();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
expect(document.activeElement).toBe(opener);
|
|
336
|
+
},
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
it('uses the fallback when the opener disconnects', () => {
|
|
340
|
+
const { opener, overlay, destination } = mountOverlay();
|
|
341
|
+
const view = renderHook(() =>
|
|
342
|
+
useOverlayA11y({
|
|
343
|
+
active: true,
|
|
344
|
+
containerRef: ref(overlay),
|
|
345
|
+
returnFocus: () => destination,
|
|
346
|
+
}),
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
view.unmount();
|
|
350
|
+
opener.remove();
|
|
351
|
+
act(() => {
|
|
352
|
+
vi.runOnlyPendingTimers();
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
expect(document.activeElement).toBe(destination);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('does nothing when restoration is declined', () => {
|
|
359
|
+
const { opener, overlay } = mountOverlay();
|
|
360
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
361
|
+
const view = renderHook(() =>
|
|
362
|
+
useOverlayA11y({ active: true, containerRef: ref(overlay), restoreFocus: () => false }),
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
view.unmount();
|
|
366
|
+
overlay.remove();
|
|
367
|
+
act(() => {
|
|
368
|
+
vi.runOnlyPendingTimers();
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
expect(focus).not.toHaveBeenCalled();
|
|
372
|
+
expect(document.activeElement).toBe(document.body);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('cancels restoration when reopened before the next task', () => {
|
|
376
|
+
const { opener, overlay } = mountOverlay();
|
|
377
|
+
const containerRef = ref(overlay);
|
|
378
|
+
const focus = vi.spyOn(opener, 'focus');
|
|
379
|
+
const view = renderHook(({ active }) => useOverlayA11y({ active, containerRef }), {
|
|
380
|
+
initialProps: { active: true },
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
view.rerender({ active: false });
|
|
384
|
+
view.rerender({ active: true });
|
|
385
|
+
act(() => {
|
|
386
|
+
vi.runOnlyPendingTimers();
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
expect(focus).not.toHaveBeenCalled();
|
|
390
|
+
expect(document.activeElement).toBe(overlay.firstElementChild);
|
|
391
|
+
});
|
|
392
|
+
|
|
177
393
|
it('traps focus in the real overlay and leaves the background props empty', () => {
|
|
178
394
|
platform.OS = 'web';
|
|
179
395
|
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
|
|