@charcuterie/ui 4.28.0 → 4.28.1
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/dist/Overlay/OverlayPanel.d.ts.map +1 -1
- package/dist/Overlay/OverlayPanel.js +45 -3
- package/dist/Overlay/OverlayPanel.js.map +1 -1
- package/dist/Overlay/focusHistory.d.ts +16 -0
- package/dist/Overlay/focusHistory.d.ts.map +1 -0
- package/dist/Overlay/focusHistory.js +110 -0
- package/dist/Overlay/focusHistory.js.map +1 -0
- package/package.json +6 -6
- package/src/Overlay/OverlayPanel.server.test.ts +30 -0
- package/src/Overlay/OverlayPanel.tsx +69 -1
- package/src/Overlay/focusHistory.ts +152 -0
- package/src/Overlay/overlayReturnFocus.storyHelpers.tsx +242 -0
- package/src/Overlay/overlayReturnFocus.test.tsx +255 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OverlayPanel.d.ts","sourceRoot":"","sources":["../../src/Overlay/OverlayPanel.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"OverlayPanel.d.ts","sourceRoot":"","sources":["../../src/Overlay/OverlayPanel.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAuBjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,EAAE,SAAS,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;IAC5C;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,8CAA8C;IAC9C,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,SAAS,EAAE,OAAO,CAAA;IAClB,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,IAAI,CAAC,EAAE,aAAa,GAAG,QAAQ,CAAA;CAChC,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,wJAW1B,iBAAiB,KAAG,SAmStB,CAAA"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useLatestRef, useUniqueId, } from "@charcuterie/logic";
|
|
3
3
|
import { FloatingFocusManager, FloatingPortal, useDismiss, useFloating, useInteractions, useMergeRefs, useRole, } from "@floating-ui/react";
|
|
4
|
-
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from "react";
|
|
5
5
|
import { toOverlayMotionClass, usePresence, } from "../Motion/usePresence.js";
|
|
6
6
|
import { toClassName } from "../toClassName.js";
|
|
7
7
|
import { AnchoredOverlayScopeContext, useAnchoredOverlayScope, } from "./AnchoredOverlayScope.js";
|
|
8
|
+
import { findReturnFocusTarget } from "./focusHistory.js";
|
|
8
9
|
import { lockScrollBehind } from "./lockScrollBehind.js";
|
|
9
10
|
import { useOverlayStack } from "./OverlayStack.js";
|
|
10
11
|
import { SharedBackdrop } from "./SharedBackdrop.js";
|
|
@@ -18,6 +19,46 @@ export const OverlayPanel = ({ "aria-label": ariaLabel, "aria-labelledby": ariaL
|
|
|
18
19
|
onCloseRef.current();
|
|
19
20
|
}, [onCloseRef]);
|
|
20
21
|
const { isExiting, isMounted, presenceRef } = usePresence(isVisible);
|
|
22
|
+
// Where focus goes when the panel closes: the element that had focus
|
|
23
|
+
// when it opened, or, if that has gone, the most recently focused
|
|
24
|
+
// element that is still there (`findReturnFocusTarget`).
|
|
25
|
+
//
|
|
26
|
+
// The focus manager's own memory is not enough. When a panel stacked
|
|
27
|
+
// on this one closes, this one's manager is enabled again and
|
|
28
|
+
// remembers whatever had focus then, which is a button INSIDE this
|
|
29
|
+
// panel. Unmounted at once, that button was gone and floating-ui fell
|
|
30
|
+
// back further; kept in the page for the exit motion, it is still
|
|
31
|
+
// there, but inert, so focus landed nowhere.
|
|
32
|
+
//
|
|
33
|
+
// Recorded in a layout effect, which never runs on the server, and
|
|
34
|
+
// which runs before the focus manager moves focus inside: it does
|
|
35
|
+
// that in a microtask.
|
|
36
|
+
const openerRef = useRef(null);
|
|
37
|
+
const panelElementRef = useRef(null);
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
if (!isVisible) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const focused = document.activeElement;
|
|
43
|
+
openerRef.current =
|
|
44
|
+
focused instanceof HTMLElement ? focused : null;
|
|
45
|
+
}, [isVisible]);
|
|
46
|
+
useLayoutEffect(() => {
|
|
47
|
+
if (!isMounted) {
|
|
48
|
+
openerRef.current = null;
|
|
49
|
+
}
|
|
50
|
+
}, [isMounted]);
|
|
51
|
+
// The manager reads `returnFocus.current` as the panel closes, so a
|
|
52
|
+
// getter decides then, with the page as it is at that moment, rather
|
|
53
|
+
// than in an earlier render.
|
|
54
|
+
const returnFocus = useMemo(() => ({
|
|
55
|
+
get current() {
|
|
56
|
+
return findReturnFocusTarget({
|
|
57
|
+
opener: openerRef.current,
|
|
58
|
+
panel: panelElementRef.current,
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
}), []);
|
|
21
62
|
const isTop = !isExiting && (!stack.isProvided || stack.topId === id);
|
|
22
63
|
const anchors = useAnchoredOverlayScope();
|
|
23
64
|
const hasOpenAnchor = anchors.topId !== null;
|
|
@@ -94,6 +135,7 @@ export const OverlayPanel = ({ "aria-label": ariaLabel, "aria-labelledby": ariaL
|
|
|
94
135
|
const setPanelRef = useMergeRefs([
|
|
95
136
|
refs.setFloating,
|
|
96
137
|
presenceRef,
|
|
138
|
+
panelElementRef,
|
|
97
139
|
]);
|
|
98
140
|
useEffect(() => {
|
|
99
141
|
if (!isVisible) {
|
|
@@ -149,9 +191,9 @@ export const OverlayPanel = ({ "aria-label": ariaLabel, "aria-labelledby": ariaL
|
|
|
149
191
|
initialFocus: initialFocus ?? undefined,
|
|
150
192
|
// Only the top traps focus; a lower panel is `inert`
|
|
151
193
|
// anyway, and two live traps fight over one caret.
|
|
152
|
-
disabled: !isTop, modal: true, children: _jsx("div", { ...getFloatingProps(), "aria-label": ariaLabel,
|
|
194
|
+
disabled: !isTop, modal: true, returnFocus: returnFocus, children: _jsx("div", { ...getFloatingProps(), "aria-label": ariaLabel,
|
|
153
195
|
"aria-labelledby": ariaLabelledBy, "aria-hidden": isExiting ? true : undefined, className: toClassName(className, hasMotion &&
|
|
154
|
-
toOverlayMotionClass(isExiting)),
|
|
196
|
+
toOverlayMotionClass(isExiting)), "data-overlay-exiting": isExiting ? "" : undefined, "data-overlay-panel": "",
|
|
155
197
|
// The fallback the plan calls for: `inert` on every panel
|
|
156
198
|
// but the top, so a lower modal is unreachable across the
|
|
157
199
|
// portal boundary even where `FloatingFocusManager`'s
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OverlayPanel.js","sourceRoot":"","sources":["../../src/Overlay/OverlayPanel.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,YAAY,EACZ,WAAW,GACZ,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,oBAAoB,EACpB,cAAc,EACd,UAAU,EACV,WAAW,EACX,eAAe,EACf,YAAY,EACZ,OAAO,GACR,MAAM,oBAAoB,CAAA;AAE3B,OAAO,
|
|
1
|
+
{"version":3,"file":"OverlayPanel.js","sourceRoot":"","sources":["../../src/Overlay/OverlayPanel.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,YAAY,EACZ,WAAW,GACZ,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,oBAAoB,EACpB,cA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CAAC,CACzB,KAAC,cAAc,IAAC,SAAS,EAAE,SAAS,GAAI,CACzC,EAED,KAAC,cAAc,cACb,cACE,SAAS,EAAE,WAAW,CACpB,gDAAgD;oBAChD,0DAA0D;oBAC1D,gDAAgD;oBAChD,SAAS,IAAI,qBAAqB,CACnC;oBACD;;;;;;;;;;;;;uBAaG;oBACH,KAAK,EAAE;wBACL,MAAM,EAAE,6BAA6B,UAAU,GAAG;qBACnD,YAED,KAAC,oBAAoB,IACnB,OAAO,EAAE,OAAO;wBAChB,yDAAyD;wBACzD,0DAA0D;wBAC1D,OAAO;wBACP,YAAY,EAAE,YAAY,IAAI,SAAS;wBACvC,qDAAqD;wBACrD,mDAAmD;wBACnD,QAAQ,EAAE,CAAC,KAAK,EAChB,KAAK,QACL,WAAW,EAAE,WAAW,YAExB,iBACM,gBAAgB,EAAE,EAOpB,YAAY,EAAE,SAAS;4BACvB,iBAAiB,EAAE,cAAc,iBAEtB,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,EACzC,SAAS,EAAE,WAAW,CACpB,SAAS,EACT,SAAS;gCACP,oBAAoB,CAAC,SAAS,CAAC,CAClC,0BAMC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,wBAET,EAAE;4BACrB,0DAA0D;4BAC1D,0DAA0D;4BAC1D,sDAAsD;4BACtD,kDAAkD;4BAClD,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,EAC/B,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,IAAI,YAEV,KAAC,2BAA2B,CAAC,QAAQ,IACnC,KAAK,EAAE,OAAO,YAEb,QAAQ,GAC4B,GACnC,GACe,GACnB,GACS,IAChB,CACJ,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a closing panel sends focus: the element that opened it if
|
|
3
|
+
* that still can take focus; otherwise, if there was an opener, the
|
|
4
|
+
* most recently focused element that can and is not a text field.
|
|
5
|
+
*
|
|
6
|
+
* A panel that opened while nothing had focus returns nothing, so
|
|
7
|
+
* focus stays where it was, as before. The history is only for an
|
|
8
|
+
* opener that has gone, not for a panel that never had one: sending
|
|
9
|
+
* focus to a field the user left minutes ago would open the keyboard
|
|
10
|
+
* on a phone.
|
|
11
|
+
*/
|
|
12
|
+
export declare const findReturnFocusTarget: ({ opener, panel, }: {
|
|
13
|
+
opener: HTMLElement | null;
|
|
14
|
+
panel: HTMLElement | null;
|
|
15
|
+
}) => HTMLElement | null;
|
|
16
|
+
//# sourceMappingURL=focusHistory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focusHistory.d.ts","sourceRoot":"","sources":["../../src/Overlay/focusHistory.ts"],"names":[],"mappings":"AA0GA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,GAAI,oBAGnC;IACD,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;IAC1B,KAAK,EAAE,WAAW,GAAG,IAAI,CAAA;CAC1B,KAAG,WAAW,GAAG,IA4BjB,CAAA"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The elements the user has focused, newest last, so a closing panel
|
|
3
|
+
* can find somewhere real to send focus when its opener has gone.
|
|
4
|
+
*
|
|
5
|
+
* floating-ui keeps a list like this, but it is private, and a panel
|
|
6
|
+
* that passes its own `returnFocus` target no longer falls back to it.
|
|
7
|
+
*
|
|
8
|
+
* Weak references, so a removed element is not kept alive by the list.
|
|
9
|
+
*/
|
|
10
|
+
const LIMIT = 20;
|
|
11
|
+
let history = [];
|
|
12
|
+
const remember = (event) => {
|
|
13
|
+
const { target } = event;
|
|
14
|
+
if (!(target instanceof HTMLElement) ||
|
|
15
|
+
target === target.ownerDocument.body) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
history = [
|
|
19
|
+
...history.filter((entry) => {
|
|
20
|
+
const element = entry.deref();
|
|
21
|
+
return element !== undefined && element !== target;
|
|
22
|
+
}),
|
|
23
|
+
new WeakRef(target),
|
|
24
|
+
].slice(-LIMIT);
|
|
25
|
+
};
|
|
26
|
+
// Installed when the module first loads in a browser, so an element
|
|
27
|
+
// focused before any panel ever opened, such as a menu's trigger, is
|
|
28
|
+
// already in the list. Nothing runs on the server.
|
|
29
|
+
if (typeof document !== "undefined") {
|
|
30
|
+
document.addEventListener("focusin", remember, true);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether focus can land on `element` once `panel` has closed: it is
|
|
34
|
+
* still in the page, enabled, and rendered; it is outside the closing
|
|
35
|
+
* panel and outside any panel that is itself leaving; and it is not
|
|
36
|
+
* inside anything else that is `inert`, such as a menu that is still
|
|
37
|
+
* leaving.
|
|
38
|
+
*
|
|
39
|
+
* An `inert` overlay panel does not rule an element out: a lower panel
|
|
40
|
+
* that the closing one covered is still `inert` at the moment this is
|
|
41
|
+
* read, and is turned back on in the same commit.
|
|
42
|
+
*/
|
|
43
|
+
const isReturnable = (element, panel) => {
|
|
44
|
+
if (element?.isConnected !== true ||
|
|
45
|
+
element === element.ownerDocument.body ||
|
|
46
|
+
(panel?.contains(element) ?? false) ||
|
|
47
|
+
element.closest("[data-overlay-exiting]") !== null ||
|
|
48
|
+
element.matches(":disabled")) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (typeof element.checkVisibility === "function" &&
|
|
52
|
+
!element.checkVisibility()) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const inertAncestor = element.closest("[inert]");
|
|
56
|
+
return (inertAncestor === null ||
|
|
57
|
+
inertAncestor.hasAttribute("data-overlay-panel"));
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Input types that do not bring up an on-screen keyboard.
|
|
61
|
+
*/
|
|
62
|
+
const KEYLESS_INPUT_TYPES = new Set([
|
|
63
|
+
"button",
|
|
64
|
+
"checkbox",
|
|
65
|
+
"color",
|
|
66
|
+
"file",
|
|
67
|
+
"image",
|
|
68
|
+
"radio",
|
|
69
|
+
"range",
|
|
70
|
+
"reset",
|
|
71
|
+
"submit",
|
|
72
|
+
]);
|
|
73
|
+
/**
|
|
74
|
+
* Whether focusing `element` brings up the keyboard on a phone. The
|
|
75
|
+
* history fallback skips these: the user left that field, and did not
|
|
76
|
+
* ask to type in it again.
|
|
77
|
+
*/
|
|
78
|
+
const isTextEntry = (element) => element instanceof HTMLTextAreaElement ||
|
|
79
|
+
element.isContentEditable ||
|
|
80
|
+
(element instanceof HTMLInputElement &&
|
|
81
|
+
!KEYLESS_INPUT_TYPES.has(element.type));
|
|
82
|
+
/**
|
|
83
|
+
* Where a closing panel sends focus: the element that opened it if
|
|
84
|
+
* that still can take focus; otherwise, if there was an opener, the
|
|
85
|
+
* most recently focused element that can and is not a text field.
|
|
86
|
+
*
|
|
87
|
+
* A panel that opened while nothing had focus returns nothing, so
|
|
88
|
+
* focus stays where it was, as before. The history is only for an
|
|
89
|
+
* opener that has gone, not for a panel that never had one: sending
|
|
90
|
+
* focus to a field the user left minutes ago would open the keyboard
|
|
91
|
+
* on a phone.
|
|
92
|
+
*/
|
|
93
|
+
export const findReturnFocusTarget = ({ opener, panel, }) => {
|
|
94
|
+
if (opener === null ||
|
|
95
|
+
opener === opener.ownerDocument.body) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
if (isReturnable(opener, panel)) {
|
|
99
|
+
return opener;
|
|
100
|
+
}
|
|
101
|
+
for (let index = history.length - 1; index >= 0; index -= 1) {
|
|
102
|
+
const element = history[index]?.deref();
|
|
103
|
+
if (isReturnable(element, panel) &&
|
|
104
|
+
!isTextEntry(element)) {
|
|
105
|
+
return element;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
};
|
|
110
|
+
//# sourceMappingURL=focusHistory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"foc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AE,CAAA;QAEvC,IACE,YAAY,CAAC,OAAO,EAAE,KAAK,CAAC;YAC5B,CAAC,WAAW,CAAC,OAAO,CAAC,EACrB,CAAC;YACD,OAAO,OAAO,CAAA;QAChB,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charcuterie/ui",
|
|
3
|
-
"version": "4.28.
|
|
3
|
+
"version": "4.28.1",
|
|
4
4
|
"description": "The Charcuterie components. Tokens for look, @charcuterie/logic for state, ARIA that agents can actually drive.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"@codemirror/state": "^6",
|
|
139
139
|
"@codemirror/view": "^6",
|
|
140
140
|
"@lezer/common": "^1",
|
|
141
|
-
"@storybook/react": "^10.
|
|
141
|
+
"@storybook/react": "^10.6.2",
|
|
142
142
|
"@tiptap/core": "^3.31.4",
|
|
143
143
|
"@tiptap/extension-image": "^3.31.4",
|
|
144
144
|
"@tiptap/extension-text-align": "^3.31.4",
|
|
@@ -152,13 +152,13 @@
|
|
|
152
152
|
"react": "^19.2.8",
|
|
153
153
|
"react-dom": "^19.2.8",
|
|
154
154
|
"react-router": "^8.3.0",
|
|
155
|
-
"storybook": "^10.
|
|
155
|
+
"storybook": "^10.6.2",
|
|
156
156
|
"tailwindcss": "^4.3.0",
|
|
157
157
|
"typescript": "^6.0.3",
|
|
158
|
-
"vitest": "^4.1.
|
|
158
|
+
"vitest": "^4.1.12",
|
|
159
159
|
"axe-core": "4.12.1",
|
|
160
|
-
"@vitest/browser": "^4.1.
|
|
161
|
-
"@storybook/addon-docs": "^10.
|
|
160
|
+
"@vitest/browser": "^4.1.12",
|
|
161
|
+
"@storybook/addon-docs": "^10.6.2"
|
|
162
162
|
},
|
|
163
163
|
"repository": {
|
|
164
164
|
"type": "git",
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ComponentProps } from "react"
|
|
2
|
+
import { createElement } from "react"
|
|
3
|
+
import { renderToString } from "react-dom/server"
|
|
4
|
+
import { expect, test } from "vitest"
|
|
5
|
+
|
|
6
|
+
import { Modal } from "../Modal/Modal.tsx"
|
|
7
|
+
|
|
8
|
+
// Node has no `document` and no `HTMLElement`. A route can render a
|
|
9
|
+
// dialog that is already open, so the panel must not touch either
|
|
10
|
+
// while it renders.
|
|
11
|
+
test("an open Modal renders on the server", () => {
|
|
12
|
+
const props: Omit<
|
|
13
|
+
ComponentProps<typeof Modal>,
|
|
14
|
+
"children"
|
|
15
|
+
> = {
|
|
16
|
+
"aria-label": "Server",
|
|
17
|
+
isVisible: true,
|
|
18
|
+
onClose: () => {},
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
expect(() =>
|
|
22
|
+
renderToString(
|
|
23
|
+
createElement(
|
|
24
|
+
Modal,
|
|
25
|
+
props as ComponentProps<typeof Modal>,
|
|
26
|
+
"Body",
|
|
27
|
+
),
|
|
28
|
+
),
|
|
29
|
+
).not.toThrow()
|
|
30
|
+
})
|
|
@@ -12,7 +12,14 @@ import {
|
|
|
12
12
|
useRole,
|
|
13
13
|
} from "@floating-ui/react"
|
|
14
14
|
import type { ReactNode, RefObject } from "react"
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
useCallback,
|
|
17
|
+
useEffect,
|
|
18
|
+
useLayoutEffect,
|
|
19
|
+
useMemo,
|
|
20
|
+
useRef,
|
|
21
|
+
useState,
|
|
22
|
+
} from "react"
|
|
16
23
|
import {
|
|
17
24
|
toOverlayMotionClass,
|
|
18
25
|
usePresence,
|
|
@@ -22,6 +29,7 @@ import {
|
|
|
22
29
|
AnchoredOverlayScopeContext,
|
|
23
30
|
useAnchoredOverlayScope,
|
|
24
31
|
} from "./AnchoredOverlayScope.ts"
|
|
32
|
+
import { findReturnFocusTarget } from "./focusHistory.ts"
|
|
25
33
|
import { lockScrollBehind } from "./lockScrollBehind.ts"
|
|
26
34
|
import { useOverlayStack } from "./OverlayStack.tsx"
|
|
27
35
|
import { SharedBackdrop } from "./SharedBackdrop.tsx"
|
|
@@ -129,6 +137,56 @@ export const OverlayPanel = ({
|
|
|
129
137
|
const { isExiting, isMounted, presenceRef } =
|
|
130
138
|
usePresence(isVisible)
|
|
131
139
|
|
|
140
|
+
// Where focus goes when the panel closes: the element that had focus
|
|
141
|
+
// when it opened, or, if that has gone, the most recently focused
|
|
142
|
+
// element that is still there (`findReturnFocusTarget`).
|
|
143
|
+
//
|
|
144
|
+
// The focus manager's own memory is not enough. When a panel stacked
|
|
145
|
+
// on this one closes, this one's manager is enabled again and
|
|
146
|
+
// remembers whatever had focus then, which is a button INSIDE this
|
|
147
|
+
// panel. Unmounted at once, that button was gone and floating-ui fell
|
|
148
|
+
// back further; kept in the page for the exit motion, it is still
|
|
149
|
+
// there, but inert, so focus landed nowhere.
|
|
150
|
+
//
|
|
151
|
+
// Recorded in a layout effect, which never runs on the server, and
|
|
152
|
+
// which runs before the focus manager moves focus inside: it does
|
|
153
|
+
// that in a microtask.
|
|
154
|
+
const openerRef = useRef<HTMLElement | null>(null)
|
|
155
|
+
|
|
156
|
+
const panelElementRef = useRef<HTMLElement | null>(null)
|
|
157
|
+
|
|
158
|
+
useLayoutEffect(() => {
|
|
159
|
+
if (!isVisible) {
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const focused = document.activeElement
|
|
164
|
+
|
|
165
|
+
openerRef.current =
|
|
166
|
+
focused instanceof HTMLElement ? focused : null
|
|
167
|
+
}, [isVisible])
|
|
168
|
+
|
|
169
|
+
useLayoutEffect(() => {
|
|
170
|
+
if (!isMounted) {
|
|
171
|
+
openerRef.current = null
|
|
172
|
+
}
|
|
173
|
+
}, [isMounted])
|
|
174
|
+
|
|
175
|
+
// The manager reads `returnFocus.current` as the panel closes, so a
|
|
176
|
+
// getter decides then, with the page as it is at that moment, rather
|
|
177
|
+
// than in an earlier render.
|
|
178
|
+
const returnFocus = useMemo(
|
|
179
|
+
() => ({
|
|
180
|
+
get current() {
|
|
181
|
+
return findReturnFocusTarget({
|
|
182
|
+
opener: openerRef.current,
|
|
183
|
+
panel: panelElementRef.current,
|
|
184
|
+
})
|
|
185
|
+
},
|
|
186
|
+
}),
|
|
187
|
+
[],
|
|
188
|
+
)
|
|
189
|
+
|
|
132
190
|
const isTop =
|
|
133
191
|
!isExiting && (!stack.isProvided || stack.topId === id)
|
|
134
192
|
const anchors = useAnchoredOverlayScope()
|
|
@@ -221,6 +279,7 @@ export const OverlayPanel = ({
|
|
|
221
279
|
const setPanelRef = useMergeRefs([
|
|
222
280
|
refs.setFloating,
|
|
223
281
|
presenceRef,
|
|
282
|
+
panelElementRef,
|
|
224
283
|
])
|
|
225
284
|
|
|
226
285
|
useEffect(() => {
|
|
@@ -307,6 +366,7 @@ export const OverlayPanel = ({
|
|
|
307
366
|
// anyway, and two live traps fight over one caret.
|
|
308
367
|
disabled={!isTop}
|
|
309
368
|
modal
|
|
369
|
+
returnFocus={returnFocus}
|
|
310
370
|
>
|
|
311
371
|
<div
|
|
312
372
|
{...getFloatingProps()}
|
|
@@ -325,6 +385,14 @@ export const OverlayPanel = ({
|
|
|
325
385
|
hasMotion &&
|
|
326
386
|
toOverlayMotionClass(isExiting),
|
|
327
387
|
)}
|
|
388
|
+
// Both read by `findReturnFocusTarget`: a closing panel
|
|
389
|
+
// does not send focus into another that is leaving, and
|
|
390
|
+
// an `inert` overlay panel, unlike other `inert` content,
|
|
391
|
+
// can still receive it once it is turned back on.
|
|
392
|
+
data-overlay-exiting={
|
|
393
|
+
isExiting ? "" : undefined
|
|
394
|
+
}
|
|
395
|
+
data-overlay-panel=""
|
|
328
396
|
// The fallback the plan calls for: `inert` on every panel
|
|
329
397
|
// but the top, so a lower modal is unreachable across the
|
|
330
398
|
// portal boundary even where `FloatingFocusManager`'s
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The elements the user has focused, newest last, so a closing panel
|
|
3
|
+
* can find somewhere real to send focus when its opener has gone.
|
|
4
|
+
*
|
|
5
|
+
* floating-ui keeps a list like this, but it is private, and a panel
|
|
6
|
+
* that passes its own `returnFocus` target no longer falls back to it.
|
|
7
|
+
*
|
|
8
|
+
* Weak references, so a removed element is not kept alive by the list.
|
|
9
|
+
*/
|
|
10
|
+
const LIMIT = 20
|
|
11
|
+
|
|
12
|
+
let history: WeakRef<HTMLElement>[] = []
|
|
13
|
+
|
|
14
|
+
const remember = (event: FocusEvent) => {
|
|
15
|
+
const { target } = event
|
|
16
|
+
|
|
17
|
+
if (
|
|
18
|
+
!(target instanceof HTMLElement) ||
|
|
19
|
+
target === target.ownerDocument.body
|
|
20
|
+
) {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
history = [
|
|
25
|
+
...history.filter((entry) => {
|
|
26
|
+
const element = entry.deref()
|
|
27
|
+
|
|
28
|
+
return element !== undefined && element !== target
|
|
29
|
+
}),
|
|
30
|
+
new WeakRef(target),
|
|
31
|
+
].slice(-LIMIT)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Installed when the module first loads in a browser, so an element
|
|
35
|
+
// focused before any panel ever opened, such as a menu's trigger, is
|
|
36
|
+
// already in the list. Nothing runs on the server.
|
|
37
|
+
if (typeof document !== "undefined") {
|
|
38
|
+
document.addEventListener("focusin", remember, true)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether focus can land on `element` once `panel` has closed: it is
|
|
43
|
+
* still in the page, enabled, and rendered; it is outside the closing
|
|
44
|
+
* panel and outside any panel that is itself leaving; and it is not
|
|
45
|
+
* inside anything else that is `inert`, such as a menu that is still
|
|
46
|
+
* leaving.
|
|
47
|
+
*
|
|
48
|
+
* An `inert` overlay panel does not rule an element out: a lower panel
|
|
49
|
+
* that the closing one covered is still `inert` at the moment this is
|
|
50
|
+
* read, and is turned back on in the same commit.
|
|
51
|
+
*/
|
|
52
|
+
const isReturnable = (
|
|
53
|
+
element: HTMLElement | null | undefined,
|
|
54
|
+
panel: HTMLElement | null,
|
|
55
|
+
): element is HTMLElement => {
|
|
56
|
+
if (
|
|
57
|
+
element?.isConnected !== true ||
|
|
58
|
+
element === element.ownerDocument.body ||
|
|
59
|
+
(panel?.contains(element) ?? false) ||
|
|
60
|
+
element.closest("[data-overlay-exiting]") !== null ||
|
|
61
|
+
element.matches(":disabled")
|
|
62
|
+
) {
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (
|
|
67
|
+
typeof element.checkVisibility === "function" &&
|
|
68
|
+
!element.checkVisibility()
|
|
69
|
+
) {
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const inertAncestor = element.closest("[inert]")
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
inertAncestor === null ||
|
|
77
|
+
inertAncestor.hasAttribute("data-overlay-panel")
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Input types that do not bring up an on-screen keyboard.
|
|
83
|
+
*/
|
|
84
|
+
const KEYLESS_INPUT_TYPES = new Set([
|
|
85
|
+
"button",
|
|
86
|
+
"checkbox",
|
|
87
|
+
"color",
|
|
88
|
+
"file",
|
|
89
|
+
"image",
|
|
90
|
+
"radio",
|
|
91
|
+
"range",
|
|
92
|
+
"reset",
|
|
93
|
+
"submit",
|
|
94
|
+
])
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Whether focusing `element` brings up the keyboard on a phone. The
|
|
98
|
+
* history fallback skips these: the user left that field, and did not
|
|
99
|
+
* ask to type in it again.
|
|
100
|
+
*/
|
|
101
|
+
const isTextEntry = (element: HTMLElement): boolean =>
|
|
102
|
+
element instanceof HTMLTextAreaElement ||
|
|
103
|
+
element.isContentEditable ||
|
|
104
|
+
(element instanceof HTMLInputElement &&
|
|
105
|
+
!KEYLESS_INPUT_TYPES.has(element.type))
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Where a closing panel sends focus: the element that opened it if
|
|
109
|
+
* that still can take focus; otherwise, if there was an opener, the
|
|
110
|
+
* most recently focused element that can and is not a text field.
|
|
111
|
+
*
|
|
112
|
+
* A panel that opened while nothing had focus returns nothing, so
|
|
113
|
+
* focus stays where it was, as before. The history is only for an
|
|
114
|
+
* opener that has gone, not for a panel that never had one: sending
|
|
115
|
+
* focus to a field the user left minutes ago would open the keyboard
|
|
116
|
+
* on a phone.
|
|
117
|
+
*/
|
|
118
|
+
export const findReturnFocusTarget = ({
|
|
119
|
+
opener,
|
|
120
|
+
panel,
|
|
121
|
+
}: {
|
|
122
|
+
opener: HTMLElement | null
|
|
123
|
+
panel: HTMLElement | null
|
|
124
|
+
}): HTMLElement | null => {
|
|
125
|
+
if (
|
|
126
|
+
opener === null ||
|
|
127
|
+
opener === opener.ownerDocument.body
|
|
128
|
+
) {
|
|
129
|
+
return null
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (isReturnable(opener, panel)) {
|
|
133
|
+
return opener
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
for (
|
|
137
|
+
let index = history.length - 1;
|
|
138
|
+
index >= 0;
|
|
139
|
+
index -= 1
|
|
140
|
+
) {
|
|
141
|
+
const element = history[index]?.deref()
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
isReturnable(element, panel) &&
|
|
145
|
+
!isTextEntry(element)
|
|
146
|
+
) {
|
|
147
|
+
return element
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return null
|
|
152
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
import { useState } from "react"
|
|
3
|
+
|
|
4
|
+
import { Menu } from "../Menu/Menu.tsx"
|
|
5
|
+
import { Modal } from "../Modal/Modal.tsx"
|
|
6
|
+
import { OverlayStackProvider } from "./OverlayStack.tsx"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The page shapes `overlayReturnFocus.test.tsx` opens and closes.
|
|
10
|
+
*
|
|
11
|
+
* `.storyHelpers.tsx` rather than `.tsx` because `react/no-multi-comp`
|
|
12
|
+
* is switched off for that suffix, and it is excluded from the
|
|
13
|
+
* package build so none of this ships.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const StackedModals = (): ReactNode => {
|
|
17
|
+
const [isOuterVisible, setIsOuterVisible] =
|
|
18
|
+
useState(false)
|
|
19
|
+
|
|
20
|
+
const [isInnerVisible, setIsInnerVisible] =
|
|
21
|
+
useState(false)
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<OverlayStackProvider>
|
|
25
|
+
<button
|
|
26
|
+
onClick={() => {
|
|
27
|
+
setIsOuterVisible(true)
|
|
28
|
+
}}
|
|
29
|
+
type="button"
|
|
30
|
+
>
|
|
31
|
+
Open outer
|
|
32
|
+
</button>
|
|
33
|
+
|
|
34
|
+
<Modal
|
|
35
|
+
aria-label="Outer"
|
|
36
|
+
isVisible={isOuterVisible}
|
|
37
|
+
onClose={() => {
|
|
38
|
+
setIsOuterVisible(false)
|
|
39
|
+
}}
|
|
40
|
+
>
|
|
41
|
+
<button
|
|
42
|
+
onClick={() => {
|
|
43
|
+
setIsInnerVisible(true)
|
|
44
|
+
}}
|
|
45
|
+
type="button"
|
|
46
|
+
>
|
|
47
|
+
Open inner
|
|
48
|
+
</button>
|
|
49
|
+
</Modal>
|
|
50
|
+
|
|
51
|
+
<Modal
|
|
52
|
+
aria-label="Inner"
|
|
53
|
+
isVisible={isInnerVisible}
|
|
54
|
+
onClose={() => {
|
|
55
|
+
setIsInnerVisible(false)
|
|
56
|
+
}}
|
|
57
|
+
>
|
|
58
|
+
<button type="button">Inside inner</button>
|
|
59
|
+
</Modal>
|
|
60
|
+
</OverlayStackProvider>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const MenuThenModal = (): ReactNode => {
|
|
65
|
+
const [isMenuVisible, setIsMenuVisible] = useState(false)
|
|
66
|
+
|
|
67
|
+
const [isModalVisible, setIsModalVisible] =
|
|
68
|
+
useState(false)
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<>
|
|
72
|
+
<Menu
|
|
73
|
+
isVisible={isMenuVisible}
|
|
74
|
+
items={[
|
|
75
|
+
{
|
|
76
|
+
key: "rename",
|
|
77
|
+
label: "Rename",
|
|
78
|
+
onSelect: () => {
|
|
79
|
+
setIsModalVisible(true)
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]}
|
|
83
|
+
onDismiss={() => {
|
|
84
|
+
setIsMenuVisible(false)
|
|
85
|
+
}}
|
|
86
|
+
trigger={
|
|
87
|
+
<button
|
|
88
|
+
onClick={() => {
|
|
89
|
+
setIsMenuVisible((isVisible) => !isVisible)
|
|
90
|
+
}}
|
|
91
|
+
type="button"
|
|
92
|
+
>
|
|
93
|
+
Actions
|
|
94
|
+
</button>
|
|
95
|
+
}
|
|
96
|
+
/>
|
|
97
|
+
|
|
98
|
+
<Modal
|
|
99
|
+
aria-label="Rename"
|
|
100
|
+
isVisible={isModalVisible}
|
|
101
|
+
onClose={() => {
|
|
102
|
+
setIsModalVisible(false)
|
|
103
|
+
}}
|
|
104
|
+
>
|
|
105
|
+
<button type="button">Inside rename</button>
|
|
106
|
+
</Modal>
|
|
107
|
+
</>
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const SwappedModals = (): ReactNode => {
|
|
112
|
+
const [isFirstVisible, setIsFirstVisible] =
|
|
113
|
+
useState(false)
|
|
114
|
+
|
|
115
|
+
const [isSecondVisible, setIsSecondVisible] =
|
|
116
|
+
useState(false)
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<OverlayStackProvider>
|
|
120
|
+
<button
|
|
121
|
+
onClick={() => {
|
|
122
|
+
setIsFirstVisible(true)
|
|
123
|
+
}}
|
|
124
|
+
type="button"
|
|
125
|
+
>
|
|
126
|
+
Open first
|
|
127
|
+
</button>
|
|
128
|
+
|
|
129
|
+
<Modal
|
|
130
|
+
aria-label="First"
|
|
131
|
+
isVisible={isFirstVisible}
|
|
132
|
+
onClose={() => {
|
|
133
|
+
setIsFirstVisible(false)
|
|
134
|
+
}}
|
|
135
|
+
>
|
|
136
|
+
<button
|
|
137
|
+
onClick={() => {
|
|
138
|
+
setIsFirstVisible(false)
|
|
139
|
+
|
|
140
|
+
setIsSecondVisible(true)
|
|
141
|
+
}}
|
|
142
|
+
type="button"
|
|
143
|
+
>
|
|
144
|
+
Next
|
|
145
|
+
</button>
|
|
146
|
+
</Modal>
|
|
147
|
+
|
|
148
|
+
<Modal
|
|
149
|
+
aria-label="Second"
|
|
150
|
+
isVisible={isSecondVisible}
|
|
151
|
+
onClose={() => {
|
|
152
|
+
setIsSecondVisible(false)
|
|
153
|
+
}}
|
|
154
|
+
>
|
|
155
|
+
<button type="button">Inside second</button>
|
|
156
|
+
</Modal>
|
|
157
|
+
</OverlayStackProvider>
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A modal that opens on its own, with nothing focused, as a timeout
|
|
163
|
+
* warning does. A field the user focused earlier sits beside it.
|
|
164
|
+
*/
|
|
165
|
+
export const ModalWithoutOpener = (): ReactNode => {
|
|
166
|
+
const [isVisible, setIsVisible] = useState(false)
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<>
|
|
170
|
+
<input aria-label="Search" type="search" />
|
|
171
|
+
|
|
172
|
+
<button
|
|
173
|
+
onClick={() => {
|
|
174
|
+
setIsVisible(true)
|
|
175
|
+
}}
|
|
176
|
+
type="button"
|
|
177
|
+
>
|
|
178
|
+
Open from a timer
|
|
179
|
+
</button>
|
|
180
|
+
|
|
181
|
+
<Modal
|
|
182
|
+
aria-label="Timer"
|
|
183
|
+
isVisible={isVisible}
|
|
184
|
+
onClose={() => {
|
|
185
|
+
setIsVisible(false)
|
|
186
|
+
}}
|
|
187
|
+
>
|
|
188
|
+
<button type="button">Inside timer</button>
|
|
189
|
+
</Modal>
|
|
190
|
+
</>
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A row whose Delete button opens a confirm modal, and whose Confirm
|
|
196
|
+
* removes the row, and the button with it. A search field the user
|
|
197
|
+
* typed in earlier sits above.
|
|
198
|
+
*/
|
|
199
|
+
export const DeleteRowWithConfirm = (): ReactNode => {
|
|
200
|
+
const [hasRow, setHasRow] = useState(true)
|
|
201
|
+
|
|
202
|
+
const [isConfirmVisible, setIsConfirmVisible] =
|
|
203
|
+
useState(false)
|
|
204
|
+
|
|
205
|
+
return (
|
|
206
|
+
<>
|
|
207
|
+
<input aria-label="Filter" type="search" />
|
|
208
|
+
|
|
209
|
+
<button type="button">Sort</button>
|
|
210
|
+
|
|
211
|
+
{hasRow ? (
|
|
212
|
+
<button
|
|
213
|
+
onClick={() => {
|
|
214
|
+
setIsConfirmVisible(true)
|
|
215
|
+
}}
|
|
216
|
+
type="button"
|
|
217
|
+
>
|
|
218
|
+
Delete row
|
|
219
|
+
</button>
|
|
220
|
+
) : null}
|
|
221
|
+
|
|
222
|
+
<Modal
|
|
223
|
+
aria-label="Confirm"
|
|
224
|
+
isVisible={isConfirmVisible}
|
|
225
|
+
onClose={() => {
|
|
226
|
+
setIsConfirmVisible(false)
|
|
227
|
+
}}
|
|
228
|
+
>
|
|
229
|
+
<button
|
|
230
|
+
onClick={() => {
|
|
231
|
+
setHasRow(false)
|
|
232
|
+
|
|
233
|
+
setIsConfirmVisible(false)
|
|
234
|
+
}}
|
|
235
|
+
type="button"
|
|
236
|
+
>
|
|
237
|
+
Confirm
|
|
238
|
+
</button>
|
|
239
|
+
</Modal>
|
|
240
|
+
</>
|
|
241
|
+
)
|
|
242
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
import type { Root } from "react-dom/client"
|
|
3
|
+
import { createRoot } from "react-dom/client"
|
|
4
|
+
import { expect, userEvent, waitFor } from "storybook/test"
|
|
5
|
+
import { afterEach, beforeEach, test } from "vitest"
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
DeleteRowWithConfirm,
|
|
9
|
+
MenuThenModal,
|
|
10
|
+
ModalWithoutOpener,
|
|
11
|
+
StackedModals,
|
|
12
|
+
SwappedModals,
|
|
13
|
+
} from "./overlayReturnFocus.storyHelpers.tsx"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* About focus during the exit motion, so the suite's motion freeze is
|
|
17
|
+
* lifted: frozen, a closed panel unmounts in the same commit, which is
|
|
18
|
+
* the path that already worked.
|
|
19
|
+
*/
|
|
20
|
+
const FREEZE_ID = "charcuterie-freeze-motion"
|
|
21
|
+
|
|
22
|
+
let frozenStyle: HTMLElement | null = null
|
|
23
|
+
|
|
24
|
+
const mounted: { host: HTMLElement; root: Root }[] = []
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
frozenStyle = document.getElementById(FREEZE_ID)
|
|
28
|
+
|
|
29
|
+
frozenStyle?.remove()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
for (const { host, root } of mounted.splice(0)) {
|
|
34
|
+
root.unmount()
|
|
35
|
+
|
|
36
|
+
host.remove()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (frozenStyle !== null && !frozenStyle.isConnected) {
|
|
40
|
+
document.head.append(frozenStyle)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const mount = (node: ReactNode) => {
|
|
45
|
+
const host = document.createElement("div")
|
|
46
|
+
|
|
47
|
+
document.body.append(host)
|
|
48
|
+
|
|
49
|
+
const root = createRoot(host)
|
|
50
|
+
|
|
51
|
+
root.render(node)
|
|
52
|
+
|
|
53
|
+
mounted.push({ host, root })
|
|
54
|
+
|
|
55
|
+
return host
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const getButton = (name: string) =>
|
|
59
|
+
[...document.querySelectorAll("button")].find(
|
|
60
|
+
(button) => button.textContent === name,
|
|
61
|
+
) as HTMLButtonElement
|
|
62
|
+
|
|
63
|
+
test("closing a modal that a stacked modal covered returns focus to the button that opened it", async () => {
|
|
64
|
+
mount(<StackedModals />)
|
|
65
|
+
|
|
66
|
+
await waitFor(() => {
|
|
67
|
+
expect(getButton("Open outer")).toBeDefined()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const outerTrigger = getButton("Open outer")
|
|
71
|
+
|
|
72
|
+
await userEvent.click(outerTrigger)
|
|
73
|
+
|
|
74
|
+
await waitFor(() => {
|
|
75
|
+
expect(getButton("Open inner")).toBeDefined()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
await userEvent.click(getButton("Open inner"))
|
|
79
|
+
|
|
80
|
+
await waitFor(() => {
|
|
81
|
+
expect(getButton("Inside inner")).toBeDefined()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
// The inner closes, and focus goes back into the outer. (It goes to
|
|
85
|
+
// the outer's first button, which here is also the one that opened
|
|
86
|
+
// the inner: when the outer's focus manager is turned back on, it
|
|
87
|
+
// runs its initial focus again.)
|
|
88
|
+
await userEvent.keyboard("{Escape}")
|
|
89
|
+
|
|
90
|
+
await waitFor(() => {
|
|
91
|
+
expect(getButton("Open inner")).toHaveFocus()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// The outer closes. Its focus manager was re-enabled when the inner
|
|
95
|
+
// left, but focus still goes back to the button that opened the
|
|
96
|
+
// OUTER, not to a button inside the outer, which is inert while it
|
|
97
|
+
// leaves and cannot hold focus.
|
|
98
|
+
await userEvent.keyboard("{Escape}")
|
|
99
|
+
|
|
100
|
+
await waitFor(() => {
|
|
101
|
+
expect(outerTrigger).toHaveFocus()
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test("a modal opened from a menu item returns focus to the menu's trigger, once the item has gone", async () => {
|
|
106
|
+
mount(<MenuThenModal />)
|
|
107
|
+
|
|
108
|
+
await waitFor(() => {
|
|
109
|
+
expect(getButton("Actions")).toBeDefined()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
await userEvent.click(getButton("Actions"))
|
|
113
|
+
|
|
114
|
+
const item = await waitFor(() => {
|
|
115
|
+
const found = document.querySelector<HTMLElement>(
|
|
116
|
+
'[role="menuitem"]',
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
expect(found).not.toBeNull()
|
|
120
|
+
|
|
121
|
+
return found as HTMLElement
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
await userEvent.click(item)
|
|
125
|
+
|
|
126
|
+
await waitFor(() => {
|
|
127
|
+
expect(getButton("Inside rename")).toHaveFocus()
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
// The menu has finished leaving, so the item that opened the modal
|
|
131
|
+
// is gone from the page.
|
|
132
|
+
await waitFor(
|
|
133
|
+
() => {
|
|
134
|
+
expect(item.isConnected).toBe(false)
|
|
135
|
+
},
|
|
136
|
+
{ timeout: 5_000 },
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
await userEvent.keyboard("{Escape}")
|
|
140
|
+
|
|
141
|
+
await waitFor(() => {
|
|
142
|
+
expect(getButton("Actions")).toHaveFocus()
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test("a modal that replaced another returns focus to the first one's opener", async () => {
|
|
147
|
+
mount(<SwappedModals />)
|
|
148
|
+
|
|
149
|
+
await waitFor(() => {
|
|
150
|
+
expect(getButton("Open first")).toBeDefined()
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
await userEvent.click(getButton("Open first"))
|
|
154
|
+
|
|
155
|
+
await waitFor(() => {
|
|
156
|
+
expect(getButton("Next")).toHaveFocus()
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const next = getButton("Next")
|
|
160
|
+
|
|
161
|
+
await userEvent.click(next)
|
|
162
|
+
|
|
163
|
+
await waitFor(() => {
|
|
164
|
+
expect(getButton("Inside second")).toHaveFocus()
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
// The first modal has finished leaving, and "Next" with it.
|
|
168
|
+
await waitFor(
|
|
169
|
+
() => {
|
|
170
|
+
expect(next.isConnected).toBe(false)
|
|
171
|
+
},
|
|
172
|
+
{ timeout: 5_000 },
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
await userEvent.keyboard("{Escape}")
|
|
176
|
+
|
|
177
|
+
await waitFor(() => {
|
|
178
|
+
expect(getButton("Open first")).toHaveFocus()
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
test("a modal that opened while nothing had focus leaves focus where it was when it closes", async () => {
|
|
183
|
+
mount(<ModalWithoutOpener />)
|
|
184
|
+
|
|
185
|
+
const search = await waitFor(() => {
|
|
186
|
+
const found = document.querySelector<HTMLInputElement>(
|
|
187
|
+
'input[aria-label="Search"]',
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
expect(found).not.toBeNull()
|
|
191
|
+
|
|
192
|
+
return found as HTMLInputElement
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
// Focused once, then left, so it is in the focus history.
|
|
196
|
+
search.focus()
|
|
197
|
+
|
|
198
|
+
search.blur()
|
|
199
|
+
|
|
200
|
+
expect(document.activeElement).toBe(document.body)
|
|
201
|
+
|
|
202
|
+
// Opened the way a timer opens it, without moving focus first.
|
|
203
|
+
getButton("Open from a timer").click()
|
|
204
|
+
|
|
205
|
+
await waitFor(() => {
|
|
206
|
+
expect(getButton("Inside timer")).toHaveFocus()
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
await userEvent.keyboard("{Escape}")
|
|
210
|
+
|
|
211
|
+
await waitFor(
|
|
212
|
+
() => {
|
|
213
|
+
expect(getButton("Inside timer")).toBeUndefined()
|
|
214
|
+
},
|
|
215
|
+
{ timeout: 5_000 },
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
expect(search).not.toHaveFocus()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test("when the opener is removed, focus does not go back to a text field the user left", async () => {
|
|
222
|
+
mount(<DeleteRowWithConfirm />)
|
|
223
|
+
|
|
224
|
+
const filter = await waitFor(() => {
|
|
225
|
+
const found = document.querySelector<HTMLInputElement>(
|
|
226
|
+
'input[aria-label="Filter"]',
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
expect(found).not.toBeNull()
|
|
230
|
+
|
|
231
|
+
return found as HTMLInputElement
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
await userEvent.click(getButton("Sort"))
|
|
235
|
+
|
|
236
|
+
await userEvent.click(filter)
|
|
237
|
+
|
|
238
|
+
await userEvent.keyboard("ab")
|
|
239
|
+
|
|
240
|
+
await userEvent.click(getButton("Delete row"))
|
|
241
|
+
|
|
242
|
+
await waitFor(() => {
|
|
243
|
+
expect(getButton("Confirm")).toHaveFocus()
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
await userEvent.click(getButton("Confirm"))
|
|
247
|
+
|
|
248
|
+
// The row and its Delete button are gone. The newest button the
|
|
249
|
+
// user pressed that is still there takes focus, not the field.
|
|
250
|
+
await waitFor(() => {
|
|
251
|
+
expect(getButton("Sort")).toHaveFocus()
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
expect(filter).not.toHaveFocus()
|
|
255
|
+
})
|