@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
@@ -0,0 +1,71 @@
1
+ import { useCallback, useState } from "react";
2
+ /**
3
+ * Whether the photo at `src` has failed, and the two props an `<img>`
4
+ * needs so that every failure is noticed.
5
+ *
6
+ * Shared by `Avatar` and `PortraitTiles`, whose whole promise about a
7
+ * photo is the same: it is never the only thing, so a 404, an expired
8
+ * signed URL, or a wrong content type lands on the initials rather than
9
+ * on a broken-image glyph.
10
+ *
11
+ * ### The failed URL, not a boolean
12
+ *
13
+ * A `hasFailed` boolean has to be *reset* when `src` changes, which is
14
+ * an effect syncing a prop into state — the shape this library refuses
15
+ * ([decision](../../../docs/decisions/2026-07-29-logic-hooks-are-uncontrolled.md)).
16
+ * Storing the URL that failed makes the comparison derive the answer:
17
+ * a new URL gets its own attempt, and a re-render never resurrects a
18
+ * dead one. `PortraitTiles` kept a boolean, so a tile whose photo
19
+ * failed showed initials for the rest of its life, even after the
20
+ * caller passed a working URL.
21
+ *
22
+ * ### `onError` alone misses a failure that happened first
23
+ *
24
+ * React attaches `onError` before it sets `src` on an image it creates,
25
+ * so a client render never misses the event. A **server-rendered**
26
+ * image is different. The browser starts loading it while it parses
27
+ * the HTML, and it can fail before `hydrateRoot` attaches the listener.
28
+ * React does not set `src` again when it hydrates an image, so the
29
+ * `error` event is never repeated, and the broken glyph stays.
30
+ *
31
+ * The ref closes that gap. When React attaches it, an image that is
32
+ * already `complete` has already answered, so `decode()` is asked for
33
+ * the answer again: it rejects for a broken image and resolves for a
34
+ * loaded one. An image still loading is not `complete`, and its answer
35
+ * arrives through `onError` as usual.
36
+ *
37
+ * ⚠️ `decode()` **also rejects when a newer `src` supersedes it**, with
38
+ * the same `EncodingError` a broken image gets. So a rejection counts
39
+ * only while this ref is still the one attached — React runs the
40
+ * cleanup below when the ref detaches or `src` changes — and only
41
+ * while the element still shows that `src` and has finished with it.
42
+ * Without the guard, a picture that was swapped out mid-decode was
43
+ * marked dead, and coming back to it showed the initials.
44
+ */
45
+ export const useImageFailure = (src) => {
46
+ const [failedSrc, setFailedSrc] = useState(undefined);
47
+ const onError = useCallback(() => {
48
+ setFailedSrc(src);
49
+ }, [src]);
50
+ const ref = useCallback((image) => {
51
+ if (image === null || !image.complete) {
52
+ return;
53
+ }
54
+ let isAttached = true;
55
+ image.decode().catch(() => {
56
+ if (isAttached &&
57
+ image.complete &&
58
+ image.getAttribute("src") === src) {
59
+ setFailedSrc(src);
60
+ }
61
+ });
62
+ return () => {
63
+ isAttached = false;
64
+ };
65
+ }, [src]);
66
+ return {
67
+ hasFailed: src !== undefined && failedSrc === src,
68
+ imageProps: { onError, ref },
69
+ };
70
+ };
71
+ //# sourceMappingURL=useImageFailure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useImageFailure.js","sourceRoot":"","sources":["../src/useImageFailure.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,GAAuB,EAOvB,EAAE;IACF,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAExC,SAAS,CAAC,CAAA;IAEZ,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,EAAE;QAC/B,YAAY,CAAC,GAAG,CAAC,CAAA;IACnB,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;IAET,MAAM,GAAG,GAAG,WAAW,CACrB,CAAC,KAA8B,EAAE,EAAE;QACjC,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;YACtC,OAAM;QACR,CAAC;QAED,IAAI,UAAU,GAAG,IAAI,CAAA;QAErB,KAAK,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE;YACxB,IACE,UAAU;gBACV,KAAK,CAAC,QAAQ;gBACd,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,GAAG,EACjC,CAAC;gBACD,YAAY,CAAC,GAAG,CAAC,CAAA;YACnB,CAAC;QACH,CAAC,CAAC,CAAA;QAEF,OAAO,GAAG,EAAE;YACV,UAAU,GAAG,KAAK,CAAA;QACpB,CAAC,CAAA;IACH,CAAC,EACD,CAAC,GAAG,CAAC,CACN,CAAA;IAED,OAAO;QACL,SAAS,EAAE,GAAG,KAAK,SAAS,IAAI,SAAS,KAAK,GAAG;QACjD,UAAU,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE;KAC7B,CAAA;AACH,CAAC,CAAA"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Resolve once every `<img>` under `root` has **finished**: loaded, or
3
+ * failed.
4
+ *
5
+ * This is the readiness signal for a test about an image, and it has
6
+ * no clock of its own. A `waitFor` around "the broken photo is gone"
7
+ * puts Testing Library's 1 s default on the **network**: the browser
8
+ * has to ask the dev server for the file and hear back 404 before
9
+ * `onError` can run. On a quiet machine that is a few milliseconds.
10
+ * On a shared CI runner, with four browser windows and the whole
11
+ * suite going through one Vite server, it is whenever that server
12
+ * gets to it — `Avatar > a photo that fails falls back to the
13
+ * initials` failed in the ultrawide window on the first run of #427
14
+ * with the `<img>` still in place after 1.37 s, and passed on the
15
+ * re-run. `PortraitTiles` had already hit the same wall, and had
16
+ * been given five seconds instead of a signal.
17
+ *
18
+ * So the wait is on the browser's own answer instead: the `load` or
19
+ * `error` event, or `complete` when that answer arrived before the
20
+ * test looked. `complete` is `true` for a **broken** image as well as
21
+ * a loaded one, which is the property this relies on.
22
+ *
23
+ * The listeners are added after the component's own, so a component
24
+ * that handles `onError` has already run its handler by the time this
25
+ * resolves. What is left is the component's own re-render, which a
26
+ * plain `waitFor` covers — that is page work, not network.
27
+ *
28
+ * The only budget left is the test's own timeout. An image that never
29
+ * finishes, such as a `loading="lazy"` one off-screen, holds the test
30
+ * until then; no story in this library has one.
31
+ */
32
+ export declare const waitForImagesToFinish: (root: ParentNode) => Promise<void>;
33
+ //# sourceMappingURL=waitForImages.testHelpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitForImages.testHelpers.d.ts","sourceRoot":"","sources":["../src/waitForImages.testHelpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,qBAAqB,GAChC,MAAM,UAAU,KACf,OAAO,CAAC,IAAI,CAMd,CAAA"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Resolve once every `<img>` under `root` has **finished**: loaded, or
3
+ * failed.
4
+ *
5
+ * This is the readiness signal for a test about an image, and it has
6
+ * no clock of its own. A `waitFor` around "the broken photo is gone"
7
+ * puts Testing Library's 1 s default on the **network**: the browser
8
+ * has to ask the dev server for the file and hear back 404 before
9
+ * `onError` can run. On a quiet machine that is a few milliseconds.
10
+ * On a shared CI runner, with four browser windows and the whole
11
+ * suite going through one Vite server, it is whenever that server
12
+ * gets to it — `Avatar > a photo that fails falls back to the
13
+ * initials` failed in the ultrawide window on the first run of #427
14
+ * with the `<img>` still in place after 1.37 s, and passed on the
15
+ * re-run. `PortraitTiles` had already hit the same wall, and had
16
+ * been given five seconds instead of a signal.
17
+ *
18
+ * So the wait is on the browser's own answer instead: the `load` or
19
+ * `error` event, or `complete` when that answer arrived before the
20
+ * test looked. `complete` is `true` for a **broken** image as well as
21
+ * a loaded one, which is the property this relies on.
22
+ *
23
+ * The listeners are added after the component's own, so a component
24
+ * that handles `onError` has already run its handler by the time this
25
+ * resolves. What is left is the component's own re-render, which a
26
+ * plain `waitFor` covers — that is page work, not network.
27
+ *
28
+ * The only budget left is the test's own timeout. An image that never
29
+ * finishes, such as a `loading="lazy"` one off-screen, holds the test
30
+ * until then; no story in this library has one.
31
+ */
32
+ export const waitForImagesToFinish = async (root) => {
33
+ await Promise.all(Array.from(root.querySelectorAll("img"), (image) => waitForImageToFinish(image)));
34
+ };
35
+ const waitForImageToFinish = (image) => new Promise((resolve) => {
36
+ if (image.complete) {
37
+ resolve();
38
+ return;
39
+ }
40
+ const finish = () => {
41
+ image.removeEventListener("error", finish);
42
+ image.removeEventListener("load", finish);
43
+ resolve();
44
+ };
45
+ image.addEventListener("error", finish);
46
+ image.addEventListener("load", finish);
47
+ });
48
+ //# sourceMappingURL=waitForImages.testHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitForImages.testHelpers.js","sourceRoot":"","sources":["../src/waitForImages.testHelpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EACxC,IAAgB,EACD,EAAE;IACjB,MAAM,OAAO,CAAC,GAAG,CACf,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CACjD,oBAAoB,CAAC,KAAK,CAAC,CAC5B,CACF,CAAA;AACH,CAAC,CAAA;AAED,MAAM,oBAAoB,GAAG,CAC3B,KAAuB,EACR,EAAE,CACjB,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;IACtB,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;QACnB,OAAO,EAAE,CAAA;QAET,OAAM;IACR,CAAC;IAED,MAAM,MAAM,GAAG,GAAG,EAAE;QAClB,KAAK,CAAC,mBAAmB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;QAC1C,KAAK,CAAC,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAEzC,OAAO,EAAE,CAAA;IACX,CAAC,CAAA;IAED,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IACvC,KAAK,CAAC,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;AACxC,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charcuterie/ui",
3
- "version": "4.28.1",
3
+ "version": "4.28.3",
4
4
  "description": "The Charcuterie components. Tokens for look, @charcuterie/logic for state, ARIA that agents can actually drive.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,11 +1,15 @@
1
1
  import { composeStories } from "@storybook/react"
2
+ import { hydrateRoot } from "react-dom/client"
3
+ import { renderToString } from "react-dom/server"
2
4
  import { expect, waitFor } from "storybook/test"
3
5
  import { test } from "vitest"
4
6
 
5
7
  import { expectNoAxeViolations } from "../expectNoAxeViolations.testHelpers.ts"
6
8
  import { mountStory } from "../mountStory.testHelpers.ts"
7
9
  import { expectAgentDrivable } from "../testing/index.ts"
10
+ import { waitForImagesToFinish } from "../waitForImages.testHelpers.ts"
8
11
  import * as stories from "./Avatar.stories.tsx"
12
+ import { Avatar } from "./Avatar.tsx"
9
13
 
10
14
  const { AllStates, AllVariants, Playground, InAList } =
11
15
  composeStories(stories)
@@ -79,13 +83,20 @@ test("an unassigned avatar renders nothing at all", async () => {
79
83
  * because nobody writes the `onError` until they see it.
80
84
  */
81
85
  test("a photo that fails falls back to the initials", async () => {
82
- const { canvas } = await mountStory(AllStates)
86
+ const { canvas, canvasElement } =
87
+ await mountStory(AllStates)
83
88
 
84
89
  const broken = expectAgentDrivable(canvas, {
85
90
  name: "Kit Sandoval",
86
91
  role: "img",
87
92
  })
88
93
 
94
+ // Wait for the browser's answer about each photo, not for a clock:
95
+ // the 404 comes back from the dev server whenever it gets to it,
96
+ // and on a shared CI runner that was later than `waitFor`'s 1 s.
97
+ // What is left after it is the component's own re-render.
98
+ await waitForImagesToFinish(canvasElement)
99
+
89
100
  await waitFor(async () => {
90
101
  await expect(broken.querySelector("img")).toBeNull()
91
102
  })
@@ -109,6 +120,48 @@ test("a photo that fails falls back to the initials", async () => {
109
120
  )
110
121
  })
111
122
 
123
+ /**
124
+ * The same promise for a server render, where `onError` alone cannot
125
+ * keep it. The browser loads a server-rendered photo while it parses
126
+ * the HTML, so a 404 can land before `hydrateRoot` attaches the
127
+ * listener, and React does not set `src` again to repeat it. Without
128
+ * the check `useImageFailure` makes when React attaches its ref, the
129
+ * broken-image glyph stays for good.
130
+ */
131
+ test("a photo that failed before hydration still falls back to the initials", async () => {
132
+ const avatar = (
133
+ <Avatar
134
+ imageUrl="/charcuterie-missing-portrait.png"
135
+ name="Kit Sandoval"
136
+ />
137
+ )
138
+
139
+ const host = document.createElement("div")
140
+
141
+ document.body.append(host)
142
+
143
+ host.innerHTML = renderToString(avatar)
144
+
145
+ // The photo has failed, and nothing was listening yet.
146
+ await waitForImagesToFinish(host)
147
+
148
+ await expect(host.querySelector("img")).not.toBeNull()
149
+
150
+ const root = hydrateRoot(host, avatar)
151
+
152
+ try {
153
+ await waitFor(async () => {
154
+ await expect(host.querySelector("img")).toBeNull()
155
+ })
156
+
157
+ await expect(host).toHaveTextContent("KS")
158
+ } finally {
159
+ root.unmount()
160
+
161
+ host.remove()
162
+ }
163
+ })
164
+
112
165
  /**
113
166
  * The colour is a pure function of the key, so the same person is
114
167
  * the same colour in every appearance, on every machine, and in a
@@ -4,11 +4,11 @@ import type {
4
4
  ComponentPropsWithRef,
5
5
  ReactNode,
6
6
  } from "react"
7
- import { useState } from "react"
8
7
 
9
8
  import { CATEGORICAL_APPEARANCE_CLASS } from "../categoricalStyles.ts"
10
9
  import type { IntentAppearance } from "../intentStyles.ts"
11
10
  import { toClassName } from "../toClassName.ts"
11
+ import { useImageFailure } from "../useImageFailure.ts"
12
12
 
13
13
  export type AvatarSize = keyof typeof AVATAR_SIZE_CLASS
14
14
 
@@ -203,22 +203,12 @@ export const Avatar = ({
203
203
  ...spanProps
204
204
  }: AvatarProps): ReactNode => {
205
205
  /**
206
- * The URL that failed, rather than a `hasFailed` boolean.
207
- *
208
- * A boolean has to be *reset* when `imageUrl` changes, which is an
209
- * effect syncing a prop into state — the shape this library
210
- * refuses everywhere else
211
- * ([decision](../../../docs/decisions/2026-07-29-logic-hooks-are-uncontrolled.md)).
212
- * Storing the URL makes the comparison below derive the answer, so
213
- * a second photo gets its own attempt and a re-render never
214
- * resurrects a dead one.
206
+ * Keyed by the URL that failed, so a second photo gets its own
207
+ * attempt, and it also notices a photo that failed before a
208
+ * server render hydrated — see `useImageFailure`.
215
209
  */
216
- const [failedImageUrl, setFailedImageUrl] = useState<
217
- string | undefined
218
- >(undefined)
219
-
220
- const hasImageError =
221
- imageUrl !== undefined && failedImageUrl === imageUrl
210
+ const { hasFailed: hasImageError, imageProps } =
211
+ useImageFailure(imageUrl)
222
212
 
223
213
  const trimmedName = name?.trim()
224
214
 
@@ -253,11 +243,9 @@ export const Avatar = ({
253
243
  // accessible name, and a second copy on the image is how an
254
244
  // avatar gets announced twice.
255
245
  <img
246
+ {...imageProps}
256
247
  alt=""
257
248
  className="size-full object-cover"
258
- onError={() => {
259
- setFailedImageUrl(imageUrl)
260
- }}
261
249
  src={imageUrl}
262
250
  />
263
251
  ) : (