@charcuterie/ui 4.28.1 → 4.28.3

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 (53) hide show
  1. package/dist/Avatar/Avatar.d.ts.map +1 -1
  2. package/dist/Avatar/Avatar.js +6 -15
  3. package/dist/Avatar/Avatar.js.map +1 -1
  4. package/dist/Board/useBoardDrag.d.ts.map +1 -1
  5. package/dist/Board/useBoardDrag.js +101 -11
  6. package/dist/Board/useBoardDrag.js.map +1 -1
  7. package/dist/CopyButton/copyText.d.ts +1 -1
  8. package/dist/CopyButton/copyText.js +1 -1
  9. package/dist/PortraitTiles/PortraitAvatar.d.ts +5 -0
  10. package/dist/PortraitTiles/PortraitAvatar.d.ts.map +1 -1
  11. package/dist/PortraitTiles/PortraitAvatar.js +9 -6
  12. package/dist/PortraitTiles/PortraitAvatar.js.map +1 -1
  13. package/dist/Shell/contentWidth.d.ts +1 -1
  14. package/dist/Shell/contentWidth.js +1 -1
  15. package/dist/markdownEditorCodeMirror/finishParsing.testHelpers.d.ts +29 -0
  16. package/dist/markdownEditorCodeMirror/finishParsing.testHelpers.d.ts.map +1 -0
  17. package/dist/markdownEditorCodeMirror/finishParsing.testHelpers.js +38 -0
  18. package/dist/markdownEditorCodeMirror/finishParsing.testHelpers.js.map +1 -0
  19. package/dist/motion.testHelpers.d.ts +35 -0
  20. package/dist/motion.testHelpers.d.ts.map +1 -0
  21. package/dist/motion.testHelpers.js +44 -0
  22. package/dist/motion.testHelpers.js.map +1 -0
  23. package/dist/useImageFailure.d.ts +52 -0
  24. package/dist/useImageFailure.d.ts.map +1 -0
  25. package/dist/useImageFailure.js +71 -0
  26. package/dist/useImageFailure.js.map +1 -0
  27. package/dist/waitForImages.testHelpers.d.ts +33 -0
  28. package/dist/waitForImages.testHelpers.d.ts.map +1 -0
  29. package/dist/waitForImages.testHelpers.js +48 -0
  30. package/dist/waitForImages.testHelpers.js.map +1 -0
  31. package/package.json +1 -1
  32. package/src/Avatar/Avatar.test.tsx +54 -1
  33. package/src/Avatar/Avatar.tsx +7 -19
  34. package/src/Board/useBoardDrag.test.tsx +395 -0
  35. package/src/Board/useBoardDrag.ts +129 -16
  36. package/src/CopyButton/copyText.ts +1 -1
  37. package/src/GroupedList/GroupedList.test.tsx +22 -17
  38. package/src/Main/Main.mdx +1 -1
  39. package/src/Motion/useListMotion.test.tsx +21 -15
  40. package/src/Motion/usePresence.test.tsx +76 -24
  41. package/src/Motion/useValueChangeMotion.test.tsx +12 -9
  42. package/src/Nav/Nav.mdx +1 -1
  43. package/src/Nav/Nav.stories.tsx +1 -1
  44. package/src/Overlay/overlayReturnFocus.test.tsx +18 -20
  45. package/src/PortraitTiles/PortraitAvatar.tsx +10 -5
  46. package/src/PortraitTiles/PortraitTiles.test.tsx +219 -17
  47. package/src/Shell/contentWidth.ts +1 -1
  48. package/src/markdownEditorCodeMirror/MarkdownView.test.tsx +32 -11
  49. package/src/markdownEditorCodeMirror/finishParsing.testHelpers.ts +47 -0
  50. package/src/motion.testHelpers.ts +46 -0
  51. package/src/sourceRules.test.ts +1 -1
  52. package/src/useImageFailure.ts +93 -0
  53. package/src/waitForImages.testHelpers.ts +61 -0
@@ -5,6 +5,10 @@ import { createRoot } from "react-dom/client"
5
5
  import { expect, userEvent, waitFor } from "storybook/test"
6
6
  import { afterEach, beforeEach, test } from "vitest"
7
7
 
8
+ import {
9
+ finishMotion,
10
+ HELD_MOTION,
11
+ } from "../motion.testHelpers.ts"
8
12
  import { useListMotion } from "./useListMotion.ts"
9
13
 
10
14
  /**
@@ -33,15 +37,6 @@ afterEach(() => {
33
37
  }
34
38
  })
35
39
 
36
- /**
37
- * Long enough that a loaded test host cannot finish an exit or a
38
- * slide between two lines of a test. The tokens are inherited, so
39
- * setting them on the host slows every motion inside it.
40
- */
41
- const SLOW_MOTION = "600ms"
42
-
43
- const MOTION_TIMEOUT = { timeout: 5_000 }
44
-
45
40
  const unfreeze = () => {
46
41
  frozenStyle?.remove()
47
42
  }
@@ -181,7 +176,10 @@ const mount = (node: ReactNode) => {
181
176
  "--duration-normal",
182
177
  "--duration-slow",
183
178
  ]) {
184
- host.style.setProperty(token, SLOW_MOTION)
179
+ // Held, so no motion can end between two lines of a test however
180
+ // long a click takes; see `HELD_MOTION`. The tokens are inherited,
181
+ // so setting them on the host holds every motion inside it.
182
+ host.style.setProperty(token, HELD_MOTION)
185
183
  }
186
184
 
187
185
  document.body.append(host)
@@ -247,9 +245,11 @@ test("a removed item stays, inert, at its index until its exit ends", async () =
247
245
  expect(alpha).toHaveAttribute("aria-hidden", "true")
248
246
  expect(host.querySelector("li")).toBe(alpha)
249
247
 
248
+ finishMotion(host)
249
+
250
250
  await waitFor(() => {
251
251
  expect(alpha.isConnected).toBe(false)
252
- }, MOTION_TIMEOUT)
252
+ })
253
253
  })
254
254
 
255
255
  test("with motion frozen, a removed item is gone in the same commit", async () => {
@@ -285,9 +285,11 @@ test("a reorder slides each moved item from where it was", async () => {
285
285
  // element sits away from its laid-out place.
286
286
  expect(alpha.style.transition).toContain("translate")
287
287
 
288
+ finishMotion(host)
289
+
288
290
  await waitFor(() => {
289
291
  expect(alpha.style.transition).toBe("")
290
- }, MOTION_TIMEOUT)
292
+ })
291
293
  })
292
294
 
293
295
  test("an added item loses its enter class when it has entered, so showing the list again does not replay it", async () => {
@@ -311,9 +313,11 @@ test("an added item loses its enter class when it has entered, so showing the li
311
313
  return found as HTMLElement
312
314
  })
313
315
 
316
+ finishMotion(host)
317
+
314
318
  await waitFor(() => {
315
319
  expect(delta).not.toHaveClass("charcuterie-item-enter")
316
- }, MOTION_TIMEOUT)
320
+ })
317
321
 
318
322
  // Hidden and shown again, with a style flush at each step, which is
319
323
  // what restarts a CSS animation still named on the element.
@@ -444,9 +448,11 @@ test("two items removed one after the other keep their order while they leave",
444
448
  "Delta",
445
449
  ])
446
450
 
451
+ finishMotion(host)
452
+
447
453
  await waitFor(() => {
448
454
  expect(shownOrder(host)).toEqual(["Alpha", "Delta"])
449
- }, MOTION_TIMEOUT)
455
+ })
450
456
  })
451
457
 
452
458
  test("a leaving item that a reorder moves slides from where it was, instead of jumping", async () => {
@@ -503,7 +509,7 @@ test("an item that moves while it is still entering slides from where it is show
503
509
 
504
510
  const topBefore = delta.getBoundingClientRect().top
505
511
 
506
- // Still entering (the motion is 600ms here), and moved to the top.
512
+ // Still entering (the motion is held here), and moved to the top.
507
513
  await userEvent.click(getButton(host, "Reverse"))
508
514
 
509
515
  expect(shownOrder(host)[0]).toBe("Delta")
@@ -5,6 +5,10 @@ import { afterEach, beforeEach, test } from "vitest"
5
5
  import * as accordionStories from "../Accordion/Accordion.stories.tsx"
6
6
  import * as menuStories from "../Menu/Menu.stories.tsx"
7
7
  import * as modalStories from "../Modal/Modal.stories.tsx"
8
+ import {
9
+ finishMotion,
10
+ HELD_MOTION,
11
+ } from "../motion.testHelpers.ts"
8
12
  import { mountStory } from "../mountStory.testHelpers.ts"
9
13
  import { getAnimationLengthMs } from "./usePresence.ts"
10
14
 
@@ -23,26 +27,56 @@ const { Playground: AccordionPlayground } = composeStories(
23
27
  * is what keeps every other overlay test synchronous. These tests are
24
28
  * about the motion itself, so they lift the freeze for their own
25
29
  * duration and put it back after.
30
+ *
31
+ * And they **hold** it: every duration token is an hour on `<body>`,
32
+ * which the canvas and the portalled overlays both inherit from, so
33
+ * no enter or exit can end between two lines of a test however long
34
+ * a click takes (see `HELD_MOTION`). A test that needs one over ends
35
+ * it with `finishMotion`.
26
36
  */
27
37
  const FREEZE_ID = "charcuterie-freeze-motion"
28
38
 
39
+ const DURATION_TOKENS = [
40
+ "--duration-fast",
41
+ "--duration-normal",
42
+ "--duration-slow",
43
+ ]
44
+
29
45
  let frozenStyle: HTMLElement | null = null
30
46
 
31
47
  beforeEach(() => {
32
48
  frozenStyle = document.getElementById(FREEZE_ID)
33
49
 
34
50
  frozenStyle?.remove()
51
+
52
+ for (const token of DURATION_TOKENS) {
53
+ document.body.style.setProperty(token, HELD_MOTION)
54
+ }
35
55
  })
36
56
 
37
57
  afterEach(() => {
58
+ for (const token of DURATION_TOKENS) {
59
+ document.body.style.removeProperty(token)
60
+ }
61
+
38
62
  if (frozenStyle !== null) {
39
63
  document.head.append(frozenStyle)
40
64
  }
41
65
  })
42
66
 
43
- const SLOW_EXIT = "1500ms"
44
-
45
- const MOTION_TIMEOUT = { timeout: 5_000 }
67
+ /**
68
+ * Two animation frames: whatever the last event scheduled, React has
69
+ * committed. A barrier tied to the browser's own frames, as in
70
+ * `mountStory`, rather than a millisecond count.
71
+ */
72
+ const settleFrames = () =>
73
+ new Promise<void>((resolve) => {
74
+ globalThis.requestAnimationFrame(() => {
75
+ globalThis.requestAnimationFrame(() => {
76
+ resolve()
77
+ })
78
+ })
79
+ })
46
80
 
47
81
  test("the animation length is the longest delay plus duration, and zero with no animation", () => {
48
82
  const element = document.createElement("div")
@@ -76,10 +110,6 @@ test("a closing modal stays in the page, closed to everything but the eye, until
76
110
 
77
111
  expect(modal).toHaveClass("charcuterie-overlay-enter")
78
112
 
79
- // Long enough that a loaded host cannot finish the exit between two
80
- // lines of this test. The inline longhand beats the class's.
81
- modal.style.animationDuration = SLOW_EXIT
82
-
83
113
  await userEvent.keyboard("{Escape}")
84
114
 
85
115
  // Already gone to a role query, the keyboard, and the pointer…
@@ -95,9 +125,11 @@ test("a closing modal stays in the page, closed to everything but the eye, until
95
125
  // But still painted, until the exit animation finishes.
96
126
  expect(modal.isConnected).toBe(true)
97
127
 
128
+ finishMotion(modal)
129
+
98
130
  await waitFor(() => {
99
131
  expect(modal.isConnected).toBe(false)
100
- }, MOTION_TIMEOUT)
132
+ })
101
133
  })
102
134
 
103
135
  test("a modal re-opened during its exit stays open", async () => {
@@ -111,8 +143,6 @@ test("a modal re-opened during its exit stays open", async () => {
111
143
 
112
144
  const modal = body.getByRole("dialog")
113
145
 
114
- modal.style.animationDuration = SLOW_EXIT
115
-
116
146
  await userEvent.keyboard("{Escape}")
117
147
 
118
148
  expect(modal).toHaveClass("charcuterie-overlay-exit")
@@ -127,21 +157,41 @@ test("a modal re-opened during its exit stays open", async () => {
127
157
 
128
158
  expect(modal).toHaveClass("charcuterie-overlay-enter")
129
159
 
130
- await new Promise((resolve) => {
131
- globalThis.setTimeout(resolve, 400)
160
+ // The re-open's own enter ends, which is the `animationend` an
161
+ // abandoned exit listener would mistake for its own. The listener
162
+ // here is added after the hook's, so it runs after any handler the
163
+ // hook still has; two frames then let React commit what it did.
164
+ const enterEnded = new Promise((resolve) => {
165
+ modal.addEventListener("animationend", resolve, {
166
+ once: true,
167
+ })
132
168
  })
133
169
 
134
- expect(modal.isConnected).toBe(true)
170
+ finishMotion(modal)
171
+
172
+ await enterEnded
135
173
 
136
- // Closed again, and gone, so its focus manager has let go of the
137
- // page before the next test mounts into it.
138
- modal.style.animationDuration = ""
174
+ await settleFrames()
139
175
 
176
+ expect(modal.isConnected).toBe(true)
177
+
178
+ // Closed again. This exit has to run in full: an abandoned exit
179
+ // listener that heard the enter end has already marked the modal
180
+ // done, and it would vanish here at once, with no exit at all.
181
+ // (`isMounted` is `isVisible || isShown`, so while it was open the
182
+ // assertion above could not see that.)
140
183
  await userEvent.keyboard("{Escape}")
141
184
 
185
+ expect(modal.isConnected).toBe(true)
186
+ expect(modal).toHaveClass("charcuterie-overlay-exit")
187
+
188
+ // And gone, so its focus manager has let go of the page before the
189
+ // next test mounts into it.
190
+ finishMotion(modal)
191
+
142
192
  await waitFor(() => {
143
193
  expect(modal.isConnected).toBe(false)
144
- }, MOTION_TIMEOUT)
194
+ })
145
195
  })
146
196
 
147
197
  test("an anchored menu grows out of its trigger and leaves with motion", async () => {
@@ -154,16 +204,16 @@ test("an anchored menu grows out of its trigger and leaves with motion", async (
154
204
  expect(menu).toHaveClass("charcuterie-overlay-enter")
155
205
  expect(menu.style.transformOrigin).not.toBe("")
156
206
 
157
- menu.style.animationDuration = SLOW_EXIT
158
-
159
207
  await userEvent.keyboard("{Escape}")
160
208
 
161
209
  expect(body.queryByRole("menu")).toBeNull()
162
210
  expect(menu).toHaveClass("charcuterie-overlay-exit")
163
211
 
212
+ finishMotion(menu)
213
+
164
214
  await waitFor(() => {
165
215
  expect(menu.isConnected).toBe(false)
166
- }, MOTION_TIMEOUT)
216
+ })
167
217
  })
168
218
 
169
219
  test("an accordion section stays shown while it collapses, then hides", async () => {
@@ -182,8 +232,6 @@ test("an accordion section stays shown while it collapses, then hides", async ()
182
232
  expect(panel).not.toBeNull()
183
233
  expect(panel).not.toHaveAttribute("hidden")
184
234
 
185
- panel?.style.setProperty("animation-duration", SLOW_EXIT)
186
-
187
235
  await userEvent.click(firstHeader)
188
236
 
189
237
  // `interpolate-size` is Chromium's, which is what this suite runs.
@@ -191,9 +239,11 @@ test("an accordion section stays shown while it collapses, then hides", async ()
191
239
  expect(panel).not.toHaveAttribute("hidden")
192
240
  expect(panel).toHaveAttribute("inert")
193
241
 
242
+ finishMotion(panel as HTMLElement)
243
+
194
244
  await waitFor(() => {
195
245
  expect(panel).toHaveAttribute("hidden")
196
- }, MOTION_TIMEOUT)
246
+ })
197
247
  })
198
248
 
199
249
  test("an accordion section drops its open class when it has opened", async () => {
@@ -213,11 +263,13 @@ test("an accordion section drops its open class when it has opened", async () =>
213
263
 
214
264
  // Off once it has opened, so showing an ancestor again does not
215
265
  // replay it, and its `overflow: clip` no longer clips the content.
266
+ finishMotion(panel)
267
+
216
268
  await waitFor(() => {
217
269
  expect(panel).not.toHaveClass(
218
270
  "charcuterie-disclosure-enter",
219
271
  )
220
- }, MOTION_TIMEOUT)
272
+ })
221
273
 
222
274
  expect(panel).not.toHaveAttribute("hidden")
223
275
  })
@@ -5,6 +5,10 @@ import { createRoot } from "react-dom/client"
5
5
  import { expect, userEvent, waitFor } from "storybook/test"
6
6
  import { afterEach, beforeEach, test } from "vitest"
7
7
 
8
+ import {
9
+ finishMotion,
10
+ HELD_MOTION,
11
+ } from "../motion.testHelpers.ts"
8
12
  import { useValueChangeMotion } from "./useValueChangeMotion.ts"
9
13
 
10
14
  /**
@@ -39,9 +43,9 @@ afterEach(() => {
39
43
  const mount = (node: ReactNode) => {
40
44
  const host = document.createElement("div")
41
45
 
42
- // Long enough that a loaded host cannot finish a pop between two
43
- // lines of a test.
44
- host.style.setProperty("--duration-slow", "600ms")
46
+ // Held, so no pop can end between two lines of a test however long
47
+ // a click takes; see `HELD_MOTION`.
48
+ host.style.setProperty("--duration-slow", HELD_MOTION)
45
49
 
46
50
  document.body.append(host)
47
51
 
@@ -122,10 +126,9 @@ test("the pop class comes off when the pop ends", async () => {
122
126
 
123
127
  expect(output).toHaveClass("charcuterie-value-changed")
124
128
 
125
- await waitFor(
126
- () => {
127
- expect(output.className).toBe("")
128
- },
129
- { timeout: 5_000 },
130
- )
129
+ finishMotion(output)
130
+
131
+ await waitFor(() => {
132
+ expect(output.className).toBe("")
133
+ })
131
134
  })
package/src/Nav/Nav.mdx CHANGED
@@ -159,7 +159,7 @@ reason `IconButton` requires `label`.
159
159
 
160
160
  | Layout | For |
161
161
  | --- | --- |
162
- | `bar` | three to six destinations, and a grid that wants every pixel — ai-usage, folio, mux-magic, portly-controllers. Folds itself; needs no `useNavLayout`. |
162
+ | `bar` | three to six destinations, and a grid that wants every pixel — ai-usage, folio, mux-magic, sibling-app. Folds itself; needs no `useNavLayout`. |
163
163
  | `rail` / `railIcons` | seven or more, or a pipeline you read down — Docket, mail-sifter, QueuePilot |
164
164
  | `bottom` | three or four you switch between constantly, in the Narrow View only |
165
165
  | `menu` | the Narrow View of a `rail` app |
@@ -152,7 +152,7 @@ type Story = StoryObj<typeof Nav>
152
152
 
153
153
  /**
154
154
  * A header row, which is what an app with a handful of destinations
155
- * wants: ai-usage, folio, mux-magic, portly-controllers.
155
+ * wants: ai-usage, folio, mux-magic, sibling-app.
156
156
  */
157
157
  export const Playground: Story = {
158
158
  parameters: playgroundParameters,
@@ -3,7 +3,7 @@ import type { Root } from "react-dom/client"
3
3
  import { createRoot } from "react-dom/client"
4
4
  import { expect, userEvent, waitFor } from "storybook/test"
5
5
  import { afterEach, beforeEach, test } from "vitest"
6
-
6
+ import { finishMotion } from "../motion.testHelpers.ts"
7
7
  import {
8
8
  DeleteRowWithConfirm,
9
9
  MenuThenModal,
@@ -128,13 +128,13 @@ test("a modal opened from a menu item returns focus to the menu's trigger, once
128
128
  })
129
129
 
130
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
- )
131
+ // is gone from the page. Ended here rather than waited for: the
132
+ // test is about focus after the exit, not about how long it takes.
133
+ finishMotion(document.body)
134
+
135
+ await waitFor(() => {
136
+ expect(item.isConnected).toBe(false)
137
+ })
138
138
 
139
139
  await userEvent.keyboard("{Escape}")
140
140
 
@@ -165,12 +165,11 @@ test("a modal that replaced another returns focus to the first one's opener", as
165
165
  })
166
166
 
167
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
- )
168
+ finishMotion(document.body)
169
+
170
+ await waitFor(() => {
171
+ expect(next.isConnected).toBe(false)
172
+ })
174
173
 
175
174
  await userEvent.keyboard("{Escape}")
176
175
 
@@ -208,12 +207,11 @@ test("a modal that opened while nothing had focus leaves focus where it was when
208
207
 
209
208
  await userEvent.keyboard("{Escape}")
210
209
 
211
- await waitFor(
212
- () => {
213
- expect(getButton("Inside timer")).toBeUndefined()
214
- },
215
- { timeout: 5_000 },
216
- )
210
+ finishMotion(document.body)
211
+
212
+ await waitFor(() => {
213
+ expect(getButton("Inside timer")).toBeUndefined()
214
+ })
217
215
 
218
216
  expect(search).not.toHaveFocus()
219
217
  })
@@ -1,5 +1,6 @@
1
1
  import type { ReactNode } from "react"
2
- import { useState } from "react"
2
+
3
+ import { useImageFailure } from "../useImageFailure.ts"
3
4
 
4
5
  /**
5
6
  * The face, and the one piece of this component that holds state.
@@ -11,6 +12,11 @@ import { useState } from "react"
11
12
  * network stripped — so the fallback the caller already supplied for
12
13
  * "no picture" covers "no picture *today*" as well.
13
14
  *
15
+ * The failure is keyed by URL, so a tile whose photo failed tries
16
+ * again when the caller passes a new one, and a photo that failed
17
+ * before a server render hydrated is noticed too — see
18
+ * `useImageFailure`.
19
+ *
14
20
  * Its own component because the state is per portrait. Hoisting it
15
21
  * to a `Record<string, boolean>` in the set would re-render every
16
22
  * tile whenever any one image failed.
@@ -24,7 +30,8 @@ export const PortraitAvatar = ({
24
30
  imageSrc: string | undefined
25
31
  initials: ReactNode
26
32
  }): ReactNode => {
27
- const [isImageBroken, setIsImageBroken] = useState(false)
33
+ const { hasFailed: isImageBroken, imageProps } =
34
+ useImageFailure(imageSrc)
28
35
 
29
36
  if (imageSrc === undefined || isImageBroken) {
30
37
  return (
@@ -36,13 +43,11 @@ export const PortraitAvatar = ({
36
43
 
37
44
  return (
38
45
  <img
46
+ {...imageProps}
39
47
  // The name is beside it, inside the same tile, so an alt here
40
48
  // is the name announced twice.
41
49
  alt=""
42
50
  className={className}
43
- onError={() => {
44
- setIsImageBroken(true)
45
- }}
46
51
  src={imageSrc}
47
52
  />
48
53
  )