@charcuterie/ui 4.27.3 → 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/Accordion/AccordionSection.d.ts.map +1 -1
- package/dist/Accordion/AccordionSection.js +43 -4
- package/dist/Accordion/AccordionSection.js.map +1 -1
- package/dist/Combobox/Combobox.d.ts.map +1 -1
- package/dist/Combobox/Combobox.js +3 -3
- package/dist/Combobox/Combobox.js.map +1 -1
- package/dist/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/DatePicker/DatePicker.js +3 -3
- package/dist/DatePicker/DatePicker.js.map +1 -1
- package/dist/Dialog/Dialog.d.ts +7 -1
- package/dist/Dialog/Dialog.d.ts.map +1 -1
- package/dist/Dialog/Dialog.js +2 -2
- package/dist/Dialog/Dialog.js.map +1 -1
- package/dist/GroupedList/GroupedList.d.ts +9 -2
- package/dist/GroupedList/GroupedList.d.ts.map +1 -1
- package/dist/GroupedList/GroupedList.js +39 -5
- package/dist/GroupedList/GroupedList.js.map +1 -1
- package/dist/GroupedList/GroupedListRow.d.ts +4 -1
- package/dist/GroupedList/GroupedListRow.d.ts.map +1 -1
- package/dist/GroupedList/GroupedListRow.js +3 -3
- package/dist/GroupedList/GroupedListRow.js.map +1 -1
- package/dist/GroupedList/GroupedListRows.d.ts +16 -0
- package/dist/GroupedList/GroupedListRows.d.ts.map +1 -0
- package/dist/GroupedList/GroupedListRows.js +17 -0
- package/dist/GroupedList/GroupedListRows.js.map +1 -0
- package/dist/Listbox/Listbox.d.ts.map +1 -1
- package/dist/Listbox/Listbox.js +6 -4
- package/dist/Listbox/Listbox.js.map +1 -1
- package/dist/Menu/Menu.d.ts.map +1 -1
- package/dist/Menu/Menu.js +6 -4
- package/dist/Menu/Menu.js.map +1 -1
- package/dist/Modal/Modal.d.ts +7 -1
- package/dist/Modal/Modal.d.ts.map +1 -1
- package/dist/Modal/Modal.js +1 -1
- package/dist/Modal/Modal.js.map +1 -1
- package/dist/Motion/useLastPresentValue.d.ts +21 -0
- package/dist/Motion/useLastPresentValue.d.ts.map +1 -0
- package/dist/Motion/useLastPresentValue.js +30 -0
- package/dist/Motion/useLastPresentValue.js.map +1 -0
- package/dist/Motion/useListMotion.d.ts +111 -0
- package/dist/Motion/useListMotion.d.ts.map +1 -0
- package/dist/Motion/useListMotion.js +358 -0
- package/dist/Motion/useListMotion.js.map +1 -0
- package/dist/Motion/usePresence.d.ts +81 -0
- package/dist/Motion/usePresence.d.ts.map +1 -0
- package/dist/Motion/usePresence.js +131 -0
- package/dist/Motion/usePresence.js.map +1 -0
- package/dist/Motion/useValueChangeMotion.d.ts +52 -0
- package/dist/Motion/useValueChangeMotion.d.ts.map +1 -0
- package/dist/Motion/useValueChangeMotion.js +87 -0
- package/dist/Motion/useValueChangeMotion.js.map +1 -0
- package/dist/Overlay/OverlayPanel.d.ts +18 -1
- package/dist/Overlay/OverlayPanel.d.ts.map +1 -1
- package/dist/Overlay/OverlayPanel.js +83 -11
- package/dist/Overlay/OverlayPanel.js.map +1 -1
- package/dist/Overlay/SharedBackdrop.d.ts +3 -0
- package/dist/Overlay/SharedBackdrop.d.ts.map +1 -1
- package/dist/Overlay/SharedBackdrop.js +8 -2
- package/dist/Overlay/SharedBackdrop.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/dist/Overlay/useAnchoredOverlay.d.ts +17 -3
- package/dist/Overlay/useAnchoredOverlay.d.ts.map +1 -1
- package/dist/Overlay/useAnchoredOverlay.js +23 -5
- package/dist/Overlay/useAnchoredOverlay.js.map +1 -1
- package/dist/Popover/Popover.d.ts.map +1 -1
- package/dist/Popover/Popover.js +4 -2
- package/dist/Popover/Popover.js.map +1 -1
- package/dist/ReorderList/ReorderList.d.ts +6 -0
- package/dist/ReorderList/ReorderList.d.ts.map +1 -1
- package/dist/ReorderList/ReorderList.js +23 -6
- package/dist/ReorderList/ReorderList.js.map +1 -1
- package/dist/TableViewport/TableViewport.d.ts.map +1 -1
- package/dist/TableViewport/TableViewport.js +9 -1
- package/dist/TableViewport/TableViewport.js.map +1 -1
- package/dist/Tooltip/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip/Tooltip.js +16 -13
- package/dist/Tooltip/Tooltip.js.map +1 -1
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/styles.css +191 -1
- package/package.json +6 -6
- package/src/Accordion/AccordionSection.tsx +63 -4
- package/src/Combobox/Combobox.tsx +8 -2
- package/src/DatePicker/DatePicker.tsx +7 -1
- package/src/Dialog/Dialog.tsx +8 -0
- package/src/GroupedList/GroupedList.mdx +3 -2
- package/src/GroupedList/GroupedList.test.tsx +283 -0
- package/src/GroupedList/GroupedList.tsx +88 -44
- package/src/GroupedList/GroupedListRow.tsx +8 -0
- package/src/GroupedList/GroupedListRows.tsx +51 -0
- package/src/Listbox/Listbox.tsx +9 -1
- package/src/Menu/Menu.tsx +9 -1
- package/src/Modal/Modal.tsx +8 -0
- package/src/Motion/useLastPresentValue.test.tsx +66 -0
- package/src/Motion/useLastPresentValue.ts +38 -0
- package/src/Motion/useListMotion.test.tsx +527 -0
- package/src/Motion/useListMotion.ts +674 -0
- package/src/Motion/usePresence.test.tsx +223 -0
- package/src/Motion/usePresence.ts +252 -0
- package/src/Motion/useValueChangeMotion.test.tsx +131 -0
- package/src/Motion/useValueChangeMotion.ts +110 -0
- package/src/Overlay/OverlayPanel.server.test.ts +30 -0
- package/src/Overlay/OverlayPanel.tsx +146 -14
- package/src/Overlay/SharedBackdrop.tsx +17 -2
- package/src/Overlay/focusHistory.ts +152 -0
- package/src/Overlay/overlayReturnFocus.storyHelpers.tsx +242 -0
- package/src/Overlay/overlayReturnFocus.test.tsx +255 -0
- package/src/Overlay/useAnchoredOverlay.ts +56 -4
- package/src/Popover/Popover.tsx +9 -1
- package/src/ReorderList/ReorderList.test.tsx +76 -0
- package/src/ReorderList/ReorderList.tsx +86 -48
- package/src/TableViewport/TableViewport.tsx +4 -0
- package/src/Tooltip/Tooltip.tsx +32 -4
- package/src/index.ts +30 -0
- package/src/styles.css +191 -1
|
@@ -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
|
+
})
|
|
@@ -8,15 +8,28 @@ import {
|
|
|
8
8
|
useDismiss,
|
|
9
9
|
useFloating,
|
|
10
10
|
useInteractions,
|
|
11
|
+
useMergeRefs,
|
|
11
12
|
useRole,
|
|
12
13
|
} from "@floating-ui/react"
|
|
13
14
|
import type { ReactNode, RefObject } from "react"
|
|
14
|
-
import {
|
|
15
|
-
|
|
15
|
+
import {
|
|
16
|
+
useCallback,
|
|
17
|
+
useEffect,
|
|
18
|
+
useLayoutEffect,
|
|
19
|
+
useMemo,
|
|
20
|
+
useRef,
|
|
21
|
+
useState,
|
|
22
|
+
} from "react"
|
|
23
|
+
import {
|
|
24
|
+
toOverlayMotionClass,
|
|
25
|
+
usePresence,
|
|
26
|
+
} from "../Motion/usePresence.ts"
|
|
27
|
+
import { toClassName } from "../toClassName.ts"
|
|
16
28
|
import {
|
|
17
29
|
AnchoredOverlayScopeContext,
|
|
18
30
|
useAnchoredOverlayScope,
|
|
19
31
|
} from "./AnchoredOverlayScope.ts"
|
|
32
|
+
import { findReturnFocusTarget } from "./focusHistory.ts"
|
|
20
33
|
import { lockScrollBehind } from "./lockScrollBehind.ts"
|
|
21
34
|
import { useOverlayStack } from "./OverlayStack.tsx"
|
|
22
35
|
import { SharedBackdrop } from "./SharedBackdrop.tsx"
|
|
@@ -56,6 +69,17 @@ import { SharedBackdrop } from "./SharedBackdrop.tsx"
|
|
|
56
69
|
* `aria-label` / `aria-labelledby` must be present. Missing both is a
|
|
57
70
|
* dev-time warning, not a throw — the lesson `Spinner`, `Popover`, and
|
|
58
71
|
* `Modal` each learned, applied without breaking a render.
|
|
72
|
+
*
|
|
73
|
+
* ### It opens and closes with motion
|
|
74
|
+
*
|
|
75
|
+
* The panel scales and fades in, and on close it stays in the page
|
|
76
|
+
* until its exit animation ends (`usePresence`). While it leaves it is
|
|
77
|
+
* already closed to everything but the eye: it leaves the stack at
|
|
78
|
+
* once, so the panel under it is live again; its focus manager is
|
|
79
|
+
* disabled, which returns focus to the trigger immediately; and it is
|
|
80
|
+
* `inert`, `aria-hidden`, and click-through. Under reduced motion and
|
|
81
|
+
* in the automated suites the exit has no length, so it is removed in
|
|
82
|
+
* the same commit exactly as before.
|
|
59
83
|
*/
|
|
60
84
|
export type OverlayPanelProps = {
|
|
61
85
|
"aria-label"?: string
|
|
@@ -73,6 +97,12 @@ export type OverlayPanelProps = {
|
|
|
73
97
|
* manager wins, which reads as the app ignoring you.
|
|
74
98
|
*/
|
|
75
99
|
initialFocus?: RefObject<HTMLElement | null>
|
|
100
|
+
/**
|
|
101
|
+
* `false` only for a caller that animates the panel itself, as
|
|
102
|
+
* `TableViewport` does when it grows a table out of the page. Every
|
|
103
|
+
* other panel keeps the library's enter and exit motion.
|
|
104
|
+
*/
|
|
105
|
+
hasMotion?: boolean
|
|
76
106
|
/** Escape and an outside press both close. */
|
|
77
107
|
isDismissable?: boolean
|
|
78
108
|
isVisible: boolean
|
|
@@ -85,6 +115,7 @@ export const OverlayPanel = ({
|
|
|
85
115
|
"aria-labelledby": ariaLabelledBy,
|
|
86
116
|
children,
|
|
87
117
|
className,
|
|
118
|
+
hasMotion = true,
|
|
88
119
|
initialFocus,
|
|
89
120
|
isDismissable = true,
|
|
90
121
|
isVisible,
|
|
@@ -103,7 +134,61 @@ export const OverlayPanel = ({
|
|
|
103
134
|
onCloseRef.current()
|
|
104
135
|
}, [onCloseRef])
|
|
105
136
|
|
|
106
|
-
const
|
|
137
|
+
const { isExiting, isMounted, presenceRef } =
|
|
138
|
+
usePresence(isVisible)
|
|
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
|
+
|
|
190
|
+
const isTop =
|
|
191
|
+
!isExiting && (!stack.isProvided || stack.topId === id)
|
|
107
192
|
const anchors = useAnchoredOverlayScope()
|
|
108
193
|
const hasOpenAnchor = anchors.topId !== null
|
|
109
194
|
|
|
@@ -168,10 +253,34 @@ export const OverlayPanel = ({
|
|
|
168
253
|
* `--layer-modal` it paints. `0` when unprovided — a lone `Modal`
|
|
169
254
|
* is the only thing open.
|
|
170
255
|
*/
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
256
|
+
const registeredIndex = stack.orderedIds.indexOf(id)
|
|
257
|
+
|
|
258
|
+
// A closing panel has already left the stack, so its own index is
|
|
259
|
+
// gone. It keeps the layer it had, or the panel it is leaving would
|
|
260
|
+
// be drawn under the one it was covering for the length of its exit.
|
|
261
|
+
// State with a render-phase update, not a ref written during render,
|
|
262
|
+
// so a render React throws away cannot leave a layer behind.
|
|
263
|
+
const [lastStackIndex, setLastStackIndex] = useState(0)
|
|
264
|
+
|
|
265
|
+
if (
|
|
266
|
+
registeredIndex >= 0 &&
|
|
267
|
+
registeredIndex !== lastStackIndex
|
|
268
|
+
) {
|
|
269
|
+
setLastStackIndex(registeredIndex)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const stackIndex =
|
|
273
|
+
registeredIndex >= 0
|
|
274
|
+
? registeredIndex
|
|
275
|
+
: isExiting
|
|
276
|
+
? lastStackIndex
|
|
277
|
+
: 0
|
|
278
|
+
|
|
279
|
+
const setPanelRef = useMergeRefs([
|
|
280
|
+
refs.setFloating,
|
|
281
|
+
presenceRef,
|
|
282
|
+
panelElementRef,
|
|
283
|
+
])
|
|
175
284
|
|
|
176
285
|
useEffect(() => {
|
|
177
286
|
if (!isVisible) {
|
|
@@ -188,9 +297,13 @@ export const OverlayPanel = ({
|
|
|
188
297
|
// Ref-counted through `lockScrollBehind`'s module scope, so N
|
|
189
298
|
// stacked panels lock the page once and free it only when the last
|
|
190
299
|
// closes — with or without a provider.
|
|
300
|
+
//
|
|
301
|
+
// Held while the panel is mounted, not only while it is visible, so
|
|
302
|
+
// the page's scrollbar does not come back, and shift the page
|
|
303
|
+
// sideways, while the panel is still fading out over it.
|
|
191
304
|
useEffect(
|
|
192
|
-
() => (
|
|
193
|
-
[
|
|
305
|
+
() => (isMounted ? lockScrollBehind() : undefined),
|
|
306
|
+
[isMounted],
|
|
194
307
|
)
|
|
195
308
|
|
|
196
309
|
useEffect(() => {
|
|
@@ -205,7 +318,7 @@ export const OverlayPanel = ({
|
|
|
205
318
|
}
|
|
206
319
|
}, [ariaLabel, ariaLabelledBy, isVisible])
|
|
207
320
|
|
|
208
|
-
if (!
|
|
321
|
+
if (!isMounted) {
|
|
209
322
|
return null
|
|
210
323
|
}
|
|
211
324
|
|
|
@@ -214,12 +327,17 @@ export const OverlayPanel = ({
|
|
|
214
327
|
{/* A self-hosting `Modal` renders its own scrim; under a
|
|
215
328
|
provider the shared one already stands behind every panel. */}
|
|
216
329
|
{stack.isProvided ? null : (
|
|
217
|
-
<SharedBackdrop isVisible />
|
|
330
|
+
<SharedBackdrop isVisible={isVisible} />
|
|
218
331
|
)}
|
|
219
332
|
|
|
220
333
|
<FloatingPortal>
|
|
221
334
|
<div
|
|
222
|
-
className=
|
|
335
|
+
className={toClassName(
|
|
336
|
+
"fixed inset-0 flex items-center justify-center",
|
|
337
|
+
// The centring layer covers the viewport. While the panel
|
|
338
|
+
// leaves, a press must reach the page under it.
|
|
339
|
+
isExiting && "pointer-events-none",
|
|
340
|
+
)}
|
|
223
341
|
/*
|
|
224
342
|
* ⚠️ Cascading, not one shared layer.
|
|
225
343
|
*
|
|
@@ -248,6 +366,7 @@ export const OverlayPanel = ({
|
|
|
248
366
|
// anyway, and two live traps fight over one caret.
|
|
249
367
|
disabled={!isTop}
|
|
250
368
|
modal
|
|
369
|
+
returnFocus={returnFocus}
|
|
251
370
|
>
|
|
252
371
|
<div
|
|
253
372
|
{...getFloatingProps()}
|
|
@@ -260,13 +379,26 @@ export const OverlayPanel = ({
|
|
|
260
379
|
"aria-label": ariaLabel,
|
|
261
380
|
"aria-labelledby": ariaLabelledBy,
|
|
262
381
|
}}
|
|
263
|
-
|
|
382
|
+
aria-hidden={isExiting ? true : undefined}
|
|
383
|
+
className={toClassName(
|
|
384
|
+
className,
|
|
385
|
+
hasMotion &&
|
|
386
|
+
toOverlayMotionClass(isExiting),
|
|
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=""
|
|
264
396
|
// The fallback the plan calls for: `inert` on every panel
|
|
265
397
|
// but the top, so a lower modal is unreachable across the
|
|
266
398
|
// portal boundary even where `FloatingFocusManager`'s
|
|
267
399
|
// outside-hiding does not reach a sibling portal.
|
|
268
400
|
inert={isTop ? undefined : true}
|
|
269
|
-
ref={
|
|
401
|
+
ref={setPanelRef}
|
|
270
402
|
role={role}
|
|
271
403
|
>
|
|
272
404
|
<AnchoredOverlayScopeContext.Provider
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { FloatingPortal } from "@floating-ui/react"
|
|
2
2
|
import type { ReactNode } from "react"
|
|
3
|
+
import {
|
|
4
|
+
toFadeMotionClass,
|
|
5
|
+
usePresence,
|
|
6
|
+
} from "../Motion/usePresence.ts"
|
|
7
|
+
import { toClassName } from "../toClassName.ts"
|
|
3
8
|
|
|
4
9
|
/**
|
|
5
10
|
* The scrim, and there is exactly one of it however many backdrop
|
|
@@ -19,13 +24,19 @@ import type { ReactNode } from "react"
|
|
|
19
24
|
* — a real bug a same-z-plus-DOM-order scheme has, because a
|
|
20
25
|
* later-mounted scrim then covers the panel. Stacking *between* panels
|
|
21
26
|
* still falls out of append order at the one `--layer-modal`.
|
|
27
|
+
*
|
|
28
|
+
* It fades in with the first panel and out with the last, and during
|
|
29
|
+
* the fade out it is click-through like the panel leaving with it.
|
|
22
30
|
*/
|
|
23
31
|
export const SharedBackdrop = ({
|
|
24
32
|
isVisible,
|
|
25
33
|
}: {
|
|
26
34
|
isVisible: boolean
|
|
27
35
|
}): ReactNode => {
|
|
28
|
-
|
|
36
|
+
const { isExiting, isMounted, presenceRef } =
|
|
37
|
+
usePresence<HTMLDivElement>(isVisible)
|
|
38
|
+
|
|
39
|
+
if (!isMounted) {
|
|
29
40
|
return null
|
|
30
41
|
}
|
|
31
42
|
|
|
@@ -33,7 +44,11 @@ export const SharedBackdrop = ({
|
|
|
33
44
|
<FloatingPortal>
|
|
34
45
|
<div
|
|
35
46
|
aria-hidden="true"
|
|
36
|
-
className=
|
|
47
|
+
className={toClassName(
|
|
48
|
+
"fixed inset-0 z-[var(--layer-overlay)] bg-scrim",
|
|
49
|
+
toFadeMotionClass(isExiting),
|
|
50
|
+
)}
|
|
51
|
+
ref={presenceRef}
|
|
37
52
|
/>
|
|
38
53
|
</FloatingPortal>
|
|
39
54
|
)
|
|
@@ -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
|
+
}
|