@charcuterie/ui 4.28.0 → 4.28.2

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.
@@ -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
+ })
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * Ten repos hand-roll their page chrome and **no two of them cap
6
6
  * the column at the same width**: `max-w-5xl` (board-games,
7
- * portly), `max-w-6xl` and `max-w-3xl` mixed inside one app
7
+ * sibling-app), `max-w-6xl` and `max-w-3xl` mixed inside one app
8
8
  * (mux-magic), `max-w-7xl` (gallery-downloader, spoolbuddy),
9
9
  * `max-w-[90rem]` (mail-sifter), `80rem` (points-market), a raw
10
10
  * `1400px` in plain CSS (plex-channels), a JS-computed one
@@ -372,7 +372,7 @@ test("the barrel is the only place components are re-exported", async () => {
372
372
  // components: +1 → 29. The picker family adds `Listbox` (single-select)
373
373
  // and `Combobox` (searchable/virtualized): +2 → 31.
374
374
  // `Swatch` — a colour as content with a required name, the one
375
- // colour the system does not own — comes out of `portly-controllers`,
375
+ // colour the system does not own — comes out of `sibling-app`,
376
376
  // the fleet's newest consumer: +1 -> 32.
377
377
  //
378
378
  // The boolean-input family fills the gap `mux-magic`'s `BooleanField`