@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.
Files changed (120) hide show
  1. package/dist/Accordion/AccordionSection.d.ts.map +1 -1
  2. package/dist/Accordion/AccordionSection.js +43 -4
  3. package/dist/Accordion/AccordionSection.js.map +1 -1
  4. package/dist/Combobox/Combobox.d.ts.map +1 -1
  5. package/dist/Combobox/Combobox.js +3 -3
  6. package/dist/Combobox/Combobox.js.map +1 -1
  7. package/dist/DatePicker/DatePicker.d.ts.map +1 -1
  8. package/dist/DatePicker/DatePicker.js +3 -3
  9. package/dist/DatePicker/DatePicker.js.map +1 -1
  10. package/dist/Dialog/Dialog.d.ts +7 -1
  11. package/dist/Dialog/Dialog.d.ts.map +1 -1
  12. package/dist/Dialog/Dialog.js +2 -2
  13. package/dist/Dialog/Dialog.js.map +1 -1
  14. package/dist/GroupedList/GroupedList.d.ts +9 -2
  15. package/dist/GroupedList/GroupedList.d.ts.map +1 -1
  16. package/dist/GroupedList/GroupedList.js +39 -5
  17. package/dist/GroupedList/GroupedList.js.map +1 -1
  18. package/dist/GroupedList/GroupedListRow.d.ts +4 -1
  19. package/dist/GroupedList/GroupedListRow.d.ts.map +1 -1
  20. package/dist/GroupedList/GroupedListRow.js +3 -3
  21. package/dist/GroupedList/GroupedListRow.js.map +1 -1
  22. package/dist/GroupedList/GroupedListRows.d.ts +16 -0
  23. package/dist/GroupedList/GroupedListRows.d.ts.map +1 -0
  24. package/dist/GroupedList/GroupedListRows.js +17 -0
  25. package/dist/GroupedList/GroupedListRows.js.map +1 -0
  26. package/dist/Listbox/Listbox.d.ts.map +1 -1
  27. package/dist/Listbox/Listbox.js +6 -4
  28. package/dist/Listbox/Listbox.js.map +1 -1
  29. package/dist/Menu/Menu.d.ts.map +1 -1
  30. package/dist/Menu/Menu.js +6 -4
  31. package/dist/Menu/Menu.js.map +1 -1
  32. package/dist/Modal/Modal.d.ts +7 -1
  33. package/dist/Modal/Modal.d.ts.map +1 -1
  34. package/dist/Modal/Modal.js +1 -1
  35. package/dist/Modal/Modal.js.map +1 -1
  36. package/dist/Motion/useLastPresentValue.d.ts +21 -0
  37. package/dist/Motion/useLastPresentValue.d.ts.map +1 -0
  38. package/dist/Motion/useLastPresentValue.js +30 -0
  39. package/dist/Motion/useLastPresentValue.js.map +1 -0
  40. package/dist/Motion/useListMotion.d.ts +111 -0
  41. package/dist/Motion/useListMotion.d.ts.map +1 -0
  42. package/dist/Motion/useListMotion.js +358 -0
  43. package/dist/Motion/useListMotion.js.map +1 -0
  44. package/dist/Motion/usePresence.d.ts +81 -0
  45. package/dist/Motion/usePresence.d.ts.map +1 -0
  46. package/dist/Motion/usePresence.js +131 -0
  47. package/dist/Motion/usePresence.js.map +1 -0
  48. package/dist/Motion/useValueChangeMotion.d.ts +52 -0
  49. package/dist/Motion/useValueChangeMotion.d.ts.map +1 -0
  50. package/dist/Motion/useValueChangeMotion.js +87 -0
  51. package/dist/Motion/useValueChangeMotion.js.map +1 -0
  52. package/dist/Overlay/OverlayPanel.d.ts +18 -1
  53. package/dist/Overlay/OverlayPanel.d.ts.map +1 -1
  54. package/dist/Overlay/OverlayPanel.js +83 -11
  55. package/dist/Overlay/OverlayPanel.js.map +1 -1
  56. package/dist/Overlay/SharedBackdrop.d.ts +3 -0
  57. package/dist/Overlay/SharedBackdrop.d.ts.map +1 -1
  58. package/dist/Overlay/SharedBackdrop.js +8 -2
  59. package/dist/Overlay/SharedBackdrop.js.map +1 -1
  60. package/dist/Overlay/focusHistory.d.ts +16 -0
  61. package/dist/Overlay/focusHistory.d.ts.map +1 -0
  62. package/dist/Overlay/focusHistory.js +110 -0
  63. package/dist/Overlay/focusHistory.js.map +1 -0
  64. package/dist/Overlay/useAnchoredOverlay.d.ts +17 -3
  65. package/dist/Overlay/useAnchoredOverlay.d.ts.map +1 -1
  66. package/dist/Overlay/useAnchoredOverlay.js +23 -5
  67. package/dist/Overlay/useAnchoredOverlay.js.map +1 -1
  68. package/dist/Popover/Popover.d.ts.map +1 -1
  69. package/dist/Popover/Popover.js +4 -2
  70. package/dist/Popover/Popover.js.map +1 -1
  71. package/dist/ReorderList/ReorderList.d.ts +6 -0
  72. package/dist/ReorderList/ReorderList.d.ts.map +1 -1
  73. package/dist/ReorderList/ReorderList.js +23 -6
  74. package/dist/ReorderList/ReorderList.js.map +1 -1
  75. package/dist/TableViewport/TableViewport.d.ts.map +1 -1
  76. package/dist/TableViewport/TableViewport.js +9 -1
  77. package/dist/TableViewport/TableViewport.js.map +1 -1
  78. package/dist/Tooltip/Tooltip.d.ts.map +1 -1
  79. package/dist/Tooltip/Tooltip.js +16 -13
  80. package/dist/Tooltip/Tooltip.js.map +1 -1
  81. package/dist/index.d.ts +22 -0
  82. package/dist/index.d.ts.map +1 -1
  83. package/dist/index.js +12 -0
  84. package/dist/index.js.map +1 -1
  85. package/dist/styles.css +191 -1
  86. package/package.json +6 -6
  87. package/src/Accordion/AccordionSection.tsx +63 -4
  88. package/src/Combobox/Combobox.tsx +8 -2
  89. package/src/DatePicker/DatePicker.tsx +7 -1
  90. package/src/Dialog/Dialog.tsx +8 -0
  91. package/src/GroupedList/GroupedList.mdx +3 -2
  92. package/src/GroupedList/GroupedList.test.tsx +283 -0
  93. package/src/GroupedList/GroupedList.tsx +88 -44
  94. package/src/GroupedList/GroupedListRow.tsx +8 -0
  95. package/src/GroupedList/GroupedListRows.tsx +51 -0
  96. package/src/Listbox/Listbox.tsx +9 -1
  97. package/src/Menu/Menu.tsx +9 -1
  98. package/src/Modal/Modal.tsx +8 -0
  99. package/src/Motion/useLastPresentValue.test.tsx +66 -0
  100. package/src/Motion/useLastPresentValue.ts +38 -0
  101. package/src/Motion/useListMotion.test.tsx +527 -0
  102. package/src/Motion/useListMotion.ts +674 -0
  103. package/src/Motion/usePresence.test.tsx +223 -0
  104. package/src/Motion/usePresence.ts +252 -0
  105. package/src/Motion/useValueChangeMotion.test.tsx +131 -0
  106. package/src/Motion/useValueChangeMotion.ts +110 -0
  107. package/src/Overlay/OverlayPanel.server.test.ts +30 -0
  108. package/src/Overlay/OverlayPanel.tsx +146 -14
  109. package/src/Overlay/SharedBackdrop.tsx +17 -2
  110. package/src/Overlay/focusHistory.ts +152 -0
  111. package/src/Overlay/overlayReturnFocus.storyHelpers.tsx +242 -0
  112. package/src/Overlay/overlayReturnFocus.test.tsx +255 -0
  113. package/src/Overlay/useAnchoredOverlay.ts +56 -4
  114. package/src/Popover/Popover.tsx +9 -1
  115. package/src/ReorderList/ReorderList.test.tsx +76 -0
  116. package/src/ReorderList/ReorderList.tsx +86 -48
  117. package/src/TableViewport/TableViewport.tsx +4 -0
  118. package/src/Tooltip/Tooltip.tsx +32 -4
  119. package/src/index.ts +30 -0
  120. 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 { useCallback, useEffect } from "react"
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 isTop = !stack.isProvided || stack.topId === id
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 stackIndex = Math.max(
172
- stack.orderedIds.indexOf(id),
173
- 0,
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
- () => (isVisible ? lockScrollBehind() : undefined),
193
- [isVisible],
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 (!isVisible) {
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="fixed inset-0 flex items-center justify-center"
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
- className={className}
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={refs.setFloating}
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
- if (!isVisible) {
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="fixed inset-0 z-[var(--layer-overlay)] bg-scrim"
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
+ }