@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,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
+ })
@@ -15,14 +15,25 @@ import {
15
15
  useDismiss,
16
16
  useFloating,
17
17
  useInteractions,
18
+ useMergeRefs,
18
19
  useRole,
19
20
  } from "@floating-ui/react"
20
- import type { ReactElement, RefObject } from "react"
21
+ import type {
22
+ CSSProperties,
23
+ ReactElement,
24
+ RefObject,
25
+ } from "react"
21
26
  import {
22
27
  createElement,
28
+ useCallback,
23
29
  useContext,
24
30
  useLayoutEffect,
25
31
  } from "react"
32
+ import {
33
+ toOverlayMotionClass,
34
+ toTransformOrigin,
35
+ usePresence,
36
+ } from "../Motion/usePresence.ts"
26
37
  import { AnchoredOverlayScopeContext } from "./AnchoredOverlayScope.ts"
27
38
 
28
39
  /**
@@ -69,6 +80,17 @@ import { AnchoredOverlayScopeContext } from "./AnchoredOverlayScope.ts"
69
80
  * Whether the panel goes in a `FloatingPortal`, the top layer, or in
70
81
  * place is the component's call, which is what lets the same hook back
71
82
  * a portalled `Listbox` and (for now) a top-layer `Popover`.
83
+ *
84
+ * ### Presence, so the panel can leave with motion
85
+ *
86
+ * A component renders its panel while `isMounted`, not while
87
+ * `isVisible`: the two differ only while `isExiting`, the length of
88
+ * the exit animation. During that time the component makes the panel
89
+ * `inert` and `aria-hidden` and disables any focus manager on it, so
90
+ * it is closed to everything but the eye. `motionClassName` is the
91
+ * enter or exit class, and `floatingStyles` already carries the
92
+ * `transform-origin` that makes the panel grow out of its trigger,
93
+ * flipped with the placement.
72
94
  */
73
95
  export type AnchoredOverlayRole =
74
96
  | "dialog"
@@ -243,7 +265,12 @@ export const useAnchoredOverlay = ({
243
265
 
244
266
  const isAnchored = anchorRef !== undefined
245
267
 
246
- const { context, floatingStyles, refs } = useFloating({
268
+ const {
269
+ context,
270
+ floatingStyles,
271
+ placement: resolvedPlacement,
272
+ refs,
273
+ } = useFloating({
247
274
  // Read-only. Charcuterie remains the sole owner; floating-ui is
248
275
  // told what is true and never decides it.
249
276
  open: isVisible,
@@ -375,12 +402,37 @@ export const useAnchoredOverlay = ({
375
402
 
376
403
  const clonedTrigger = isAnchored ? null : clonedChild
377
404
 
405
+ const { isExiting, isMounted, presenceRef } =
406
+ usePresence(isVisible)
407
+
408
+ const mergedFloatingRef = useMergeRefs([
409
+ refs.setFloating,
410
+ presenceRef,
411
+ ])
412
+
413
+ // Re-typed as the plain callback `refs.setFloating` was, so the
414
+ // hook's inferred return type names nothing from React's internals.
415
+ const setFloating = useCallback(
416
+ (node: HTMLElement | null) => {
417
+ mergedFloatingRef?.(node)
418
+ },
419
+ [mergedFloatingRef],
420
+ )
421
+
422
+ const panelStyles: CSSProperties = {
423
+ ...floatingStyles,
424
+ transformOrigin: toTransformOrigin(resolvedPlacement),
425
+ }
426
+
378
427
  return {
379
428
  clonedTrigger,
380
429
  context,
381
- floatingStyles,
430
+ floatingStyles: panelStyles,
382
431
  getFloatingProps,
383
- setFloating: refs.setFloating,
432
+ isExiting,
433
+ isMounted,
434
+ motionClassName: toOverlayMotionClass(isExiting),
435
+ setFloating,
384
436
  triggerId,
385
437
  }
386
438
  }
@@ -78,6 +78,9 @@ export const Popover = ({
78
78
  context,
79
79
  floatingStyles,
80
80
  getFloatingProps,
81
+ isExiting,
82
+ isMounted,
83
+ motionClassName,
81
84
  setFloating,
82
85
  } = useAnchoredOverlay({
83
86
  isVisible,
@@ -92,10 +95,12 @@ export const Popover = ({
92
95
  <>
93
96
  {clonedTrigger}
94
97
 
95
- {isVisible ? (
98
+ {isMounted ? (
96
99
  <FloatingPortal>
97
100
  <FloatingFocusManager
98
101
  context={context}
102
+ // A leaving panel hands focus back at once.
103
+ disabled={isExiting}
99
104
  modal={false}
100
105
  >
101
106
  <div
@@ -104,8 +109,11 @@ export const Popover = ({
104
109
  className={toClassName(
105
110
  PANEL_SURFACE_CLASS,
106
111
  "z-[var(--layer-modal)] max-w-xs p-3 text-content-primary text-sm",
112
+ motionClassName,
107
113
  className,
108
114
  )}
115
+ aria-hidden={isExiting ? true : undefined}
116
+ inert={isExiting ? true : undefined}
109
117
  ref={setFloating}
110
118
  // Duplicated from `getFloatingProps()` on purpose, and
111
119
  // the two cannot differ — `useRole(context, { role:
@@ -1,4 +1,6 @@
1
1
  import { composeStories } from "@storybook/react"
2
+ import { flushSync } from "react-dom"
3
+ import { createRoot } from "react-dom/client"
2
4
  import {
3
5
  expect,
4
6
  userEvent,
@@ -9,6 +11,8 @@ import { test } from "vitest"
9
11
  import { expectNoAxeViolations } from "../expectNoAxeViolations.testHelpers.ts"
10
12
  import { mountStory } from "../mountStory.testHelpers.ts"
11
13
  import * as stories from "./ReorderList.stories.tsx"
14
+ import type { ReorderListItem } from "./ReorderList.tsx"
15
+ import { ReorderList } from "./ReorderList.tsx"
12
16
 
13
17
  const { Playground, SingleItem } = composeStories(stories)
14
18
 
@@ -188,3 +192,75 @@ test("has no axe violations", async () => {
188
192
 
189
193
  await expectNoAxeViolations(canvasElement)
190
194
  })
195
+
196
+ test("a leaving row keeps its position and reports both ends, so its move buttons stay disabled", () => {
197
+ // About the motion, so the suite's freeze is lifted for this test.
198
+ const frozenStyle = document.getElementById(
199
+ "charcuterie-freeze-motion",
200
+ )
201
+
202
+ frozenStyle?.remove()
203
+
204
+ const host = document.createElement("div")
205
+
206
+ host.style.setProperty("--duration-fast", "600ms")
207
+ host.style.setProperty("--duration-normal", "600ms")
208
+
209
+ document.body.append(host)
210
+
211
+ const root = createRoot(host)
212
+
213
+ const toList = (items: ReorderListItem[]) => (
214
+ <ReorderList
215
+ elementType="ul"
216
+ items={items}
217
+ label="Rows"
218
+ onReorder={() => {}}
219
+ renderItem={({
220
+ isExiting,
221
+ isFirst,
222
+ isLast,
223
+ item,
224
+ position,
225
+ }) =>
226
+ `${item.label}:${String(position)}:${String(isExiting)}:${String(isFirst)}:${String(isLast)}`
227
+ }
228
+ />
229
+ )
230
+
231
+ const rows = [
232
+ { key: "a", label: "A" },
233
+ { key: "b", label: "B" },
234
+ { key: "c", label: "C" },
235
+ ]
236
+
237
+ try {
238
+ flushSync(() => {
239
+ root.render(toList(rows))
240
+ })
241
+
242
+ flushSync(() => {
243
+ root.render(
244
+ toList(rows.filter((row) => row.key !== "b")),
245
+ )
246
+ })
247
+
248
+ expect(
249
+ [...host.querySelectorAll("li")].map(
250
+ (row) => row.textContent,
251
+ ),
252
+ ).toEqual([
253
+ "A:1:false:true:false",
254
+ "B:2:true:true:true",
255
+ "C:2:false:false:true",
256
+ ])
257
+ } finally {
258
+ root.unmount()
259
+
260
+ host.remove()
261
+
262
+ if (frozenStyle !== null) {
263
+ document.head.append(frozenStyle)
264
+ }
265
+ }
266
+ })