@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
@@ -1,9 +1,11 @@
1
1
  import { useUniqueId } from "@charcuterie/logic"
2
2
  import type { IntentName } from "@charcuterie/tokens"
3
3
  import type { ReactNode } from "react"
4
+ import { useCallback, useState } from "react"
4
5
 
6
+ import { useListMotion } from "../Motion/useListMotion.ts"
5
7
  import { toClassName } from "../toClassName.ts"
6
- import { GroupedListRow } from "./GroupedListRow.tsx"
8
+ import { GroupedListRows } from "./GroupedListRows.tsx"
7
9
  import { STICKY_HEADING_CLASS } from "./groupedListStickyHeading.ts"
8
10
 
9
11
  export type GroupedListHeadingLevel = 2 | 3 | 4 | 5 | 6
@@ -58,13 +60,20 @@ export type GroupedListItem = {
58
60
  * it lasts, usually by clearing it on a timer.
59
61
  *
60
62
  * **To pulse a row that is already highlighted again, change its
61
- * `key`.** A new key is a new row, and a new row starts the pulse
62
- * from the beginning; `` `${id}-${sequence}` `` is the usual shape.
63
+ * `highlightKey`**, not its `key`. Rows enter, leave, and slide
64
+ * with motion, so a new `key` is a new row: the old one would leave
65
+ * while a copy of it entered. A new `highlightKey` starts only the
66
+ * ring's pulse from the beginning; a scan count is the usual value.
63
67
  *
64
68
  * Under `prefers-reduced-motion` and on ePaper the ring does not
65
69
  * pulse; it is simply there.
66
70
  */
67
71
  isHighlighted?: boolean
72
+ /**
73
+ * Changing it restarts the highlight ring's pulse. See
74
+ * `isHighlighted`.
75
+ */
76
+ highlightKey?: number | string
68
77
  /**
69
78
  * `true` draws the row receded — a sunken surface and a quieter
70
79
  * label — for an entry that exists but is switched off. It stays
@@ -245,6 +254,35 @@ export const GroupedList = ({
245
254
  (group) => group.items.length > 0,
246
255
  )
247
256
 
257
+ // Groups move like their rows do. A group an action creates (the
258
+ // first scan of a day) enters with its first row, and a group an
259
+ // action empties leaves with its last row, instead of either one
260
+ // appearing or vanishing in a frame.
261
+ //
262
+ // The row keys are the layout key: a row added to or removed from
263
+ // one group changes its height, and the groups below it slide to
264
+ // their new places with it.
265
+ //
266
+ // A row that finishes leaving closes its gap one render after its
267
+ // key went away, so the rows report it, and that report is part of
268
+ // the layout key too.
269
+ const [rowExitCount, setRowExitCount] = useState(0)
270
+
271
+ const onRowExitEnd = useCallback(() => {
272
+ setRowExitCount((count) => count + 1)
273
+ }, [])
274
+
275
+ const { entries: groupEntries } = useListMotion({
276
+ getKey: (group: GroupedListGroup) => group.key,
277
+ items: shownGroups,
278
+ layoutKey: JSON.stringify([
279
+ shownGroups.map((group) =>
280
+ group.items.map((item) => item.key),
281
+ ),
282
+ rowExitCount,
283
+ ]),
284
+ })
285
+
248
286
  const Heading = HEADING_TAG[headingLevel]
249
287
 
250
288
  return (
@@ -255,58 +293,64 @@ export const GroupedList = ({
255
293
  className,
256
294
  )}
257
295
  >
258
- {shownGroups.length === 0
296
+ {groupEntries.length === 0
259
297
  ? (emptyState ?? null)
260
- : shownGroups.map((group, index) => {
261
- const headingId = `${baseId}-heading-${index}`
298
+ : groupEntries.map(
299
+ (
300
+ { item: group, key, motionProps },
301
+ position,
302
+ ) => {
303
+ // By place in what is drawn, so a leaving group and the
304
+ // group now at its old index never share an id.
305
+ const headingId = `${baseId}-heading-${position}`
262
306
 
263
- return (
264
- <div
265
- className={toClassName(
266
- "flex flex-col",
267
- isHeadingSticky ? "gap-0" : "gap-2",
268
- )}
269
- key={group.key}
270
- >
307
+ return (
271
308
  <div
309
+ {...motionProps}
272
310
  className={toClassName(
273
- "flex flex-wrap items-baseline gap-x-3 gap-y-0.5",
274
- isHeadingSticky && STICKY_HEADING_CLASS,
311
+ "flex flex-col",
312
+ isHeadingSticky ? "gap-0" : "gap-2",
313
+ motionProps.className,
275
314
  )}
315
+ key={key}
276
316
  >
277
- <Heading
317
+ <div
278
318
  className={toClassName(
279
- "m-0 min-w-0 wrap-anywhere font-semibold text-content-primary",
280
- size === "sm" ? "text-sm" : "text-md",
319
+ "flex flex-wrap items-baseline gap-x-3 gap-y-0.5",
320
+ isHeadingSticky &&
321
+ STICKY_HEADING_CLASS,
281
322
  )}
282
- id={headingId}
283
323
  >
284
- {group.heading}
285
- </Heading>
324
+ <Heading
325
+ className={toClassName(
326
+ "m-0 min-w-0 wrap-anywhere font-semibold text-content-primary",
327
+ size === "sm"
328
+ ? "text-sm"
329
+ : "text-md",
330
+ )}
331
+ id={headingId}
332
+ >
333
+ {group.heading}
334
+ </Heading>
286
335
 
287
- {group.summary ? (
288
- <span className="min-w-0 wrap-anywhere text-content-secondary text-sm">
289
- {group.summary}
290
- </span>
291
- ) : null}
292
- </div>
336
+ {group.summary ? (
337
+ <span className="min-w-0 wrap-anywhere text-content-secondary text-sm">
338
+ {group.summary}
339
+ </span>
340
+ ) : null}
341
+ </div>
293
342
 
294
- <ul
295
- aria-labelledby={headingId}
296
- className="m-0 flex list-none flex-col divide-y divide-border-subtle overflow-hidden rounded-md border border-border-subtle bg-surface-raised p-0"
297
- >
298
- {group.items.map((item) => (
299
- <GroupedListRow
300
- isTextTruncated={isTextTruncated}
301
- item={item}
302
- key={item.key}
303
- size={size}
304
- />
305
- ))}
306
- </ul>
307
- </div>
308
- )
309
- })}
343
+ <GroupedListRows
344
+ headingId={headingId}
345
+ onExitEnd={onRowExitEnd}
346
+ isTextTruncated={isTextTruncated}
347
+ items={group.items}
348
+ size={size}
349
+ />
350
+ </div>
351
+ )
352
+ },
353
+ )}
310
354
  </div>
311
355
  )
312
356
  }
@@ -2,6 +2,7 @@ import type { IntentName } from "@charcuterie/tokens"
2
2
  import type { ReactNode } from "react"
3
3
 
4
4
  import { useIsTextClipped } from "../Badge/useIsTextClipped.ts"
5
+ import type { ListMotionProps } from "../Motion/useListMotion.ts"
5
6
  import { toClassName } from "../toClassName.ts"
6
7
  import type {
7
8
  GroupedListItem,
@@ -11,6 +12,8 @@ import type {
11
12
  export type GroupedListRowProps = {
12
13
  isTextTruncated: boolean
13
14
  item: GroupedListItem
15
+ /** From `useListMotion`, so an added row enters and a removed one leaves. */
16
+ motionProps?: ListMotionProps
14
17
  size: GroupedListSize
15
18
  }
16
19
 
@@ -97,6 +100,7 @@ const SELECTABLE_ROW_CLASS =
97
100
  export const GroupedListRow = ({
98
101
  isTextTruncated,
99
102
  item,
103
+ motionProps,
100
104
  size,
101
105
  }: GroupedListRowProps): ReactNode => {
102
106
  const [labelRef, clippedLabel] = useIsTextClipped()
@@ -125,12 +129,14 @@ export const GroupedListRow = ({
125
129
 
126
130
  return (
127
131
  <li
132
+ {...motionProps}
128
133
  className={toClassName(
129
134
  "flex flex-wrap items-center gap-x-3 gap-y-2",
130
135
  ROW_SIZE_CLASS[size],
131
136
  isSelectable && SELECTABLE_ROW_CLASS,
132
137
  isHighlighted && "relative",
133
138
  isMuted && "bg-surface-sunken",
139
+ motionProps?.className,
134
140
  )}
135
141
  >
136
142
  {isSelectable ? (
@@ -231,6 +237,8 @@ export const GroupedListRow = ({
231
237
  {isHighlighted ? (
232
238
  <span
233
239
  aria-hidden
240
+ // A new key is a new ring, and a new ring pulses again.
241
+ key={item.highlightKey}
234
242
  className={toClassName(
235
243
  "charcuterie-highlight pointer-events-none absolute inset-0 inset-ring-2",
236
244
  HIGHLIGHT_RING_CLASS[highlightIntent],
@@ -0,0 +1,51 @@
1
+ import type { ReactNode } from "react"
2
+
3
+ import { useListMotion } from "../Motion/useListMotion.ts"
4
+ import type {
5
+ GroupedListItem,
6
+ GroupedListSize,
7
+ } from "./GroupedList.tsx"
8
+ import { GroupedListRow } from "./GroupedListRow.tsx"
9
+
10
+ /**
11
+ * One group's rows, in their own component so each group can hold the
12
+ * `useListMotion` hook: an added row enters, a removed one leaves, and
13
+ * the rest slide, which a `map` over groups cannot do inline.
14
+ */
15
+ export const GroupedListRows = ({
16
+ headingId,
17
+ isTextTruncated,
18
+ items,
19
+ onExitEnd,
20
+ size,
21
+ }: {
22
+ headingId: string
23
+ isTextTruncated: boolean
24
+ /** A row finished leaving; the groups below are about to move. */
25
+ onExitEnd: () => void
26
+ items: readonly GroupedListItem[]
27
+ size: GroupedListSize
28
+ }): ReactNode => {
29
+ const { entries } = useListMotion({
30
+ getKey: (item: GroupedListItem) => item.key,
31
+ items,
32
+ onExitEnd,
33
+ })
34
+
35
+ return (
36
+ <ul
37
+ aria-labelledby={headingId}
38
+ className="m-0 flex list-none flex-col divide-y divide-border-subtle overflow-hidden rounded-md border border-border-subtle bg-surface-raised p-0"
39
+ >
40
+ {entries.map(({ item, key, motionProps }) => (
41
+ <GroupedListRow
42
+ isTextTruncated={isTextTruncated}
43
+ item={item}
44
+ key={key}
45
+ motionProps={motionProps}
46
+ size={size}
47
+ />
48
+ ))}
49
+ </ul>
50
+ )
51
+ }
@@ -122,6 +122,9 @@ export const Listbox = ({
122
122
  context,
123
123
  floatingStyles,
124
124
  getFloatingProps,
125
+ isExiting,
126
+ isMounted,
127
+ motionClassName,
125
128
  setFloating,
126
129
  triggerId,
127
130
  } = useAnchoredOverlay({
@@ -204,10 +207,12 @@ export const Listbox = ({
204
207
  <>
205
208
  {clonedTrigger}
206
209
 
207
- {isVisible ? (
210
+ {isMounted ? (
208
211
  <FloatingPortal>
209
212
  <FloatingFocusManager
210
213
  context={context}
214
+ // A leaving panel hands focus back at once.
215
+ disabled={isExiting}
211
216
  initialFocus={-1}
212
217
  modal={false}
213
218
  >
@@ -223,6 +228,7 @@ export const Listbox = ({
223
228
  // middleware (`maxHeightPx`), written as an inline
224
229
  // style; this scrolls within it.
225
230
  "z-[var(--layer-modal)] flex min-w-48 flex-col gap-0.5 overflow-auto p-1",
231
+ motionClassName,
226
232
  className,
227
233
  )}
228
234
  onKeyDown={(keyEvent) => {
@@ -267,6 +273,8 @@ export const Listbox = ({
267
273
  runTypeahead(keyEvent.key)
268
274
  }
269
275
  }}
276
+ aria-hidden={isExiting ? true : undefined}
277
+ inert={isExiting ? true : undefined}
270
278
  ref={setFloating}
271
279
  // Duplicated from `getFloatingProps()` on purpose, and
272
280
  // the two cannot differ — `useRole(context, { role:
package/src/Menu/Menu.tsx CHANGED
@@ -296,6 +296,9 @@ export const Menu = ({
296
296
  context,
297
297
  floatingStyles,
298
298
  getFloatingProps,
299
+ isExiting,
300
+ isMounted,
301
+ motionClassName,
299
302
  setFloating,
300
303
  triggerId,
301
304
  } = useAnchoredOverlay({
@@ -444,10 +447,12 @@ export const Menu = ({
444
447
  <>
445
448
  {clonedTrigger}
446
449
 
447
- {isVisible ? (
450
+ {isMounted ? (
448
451
  <FloatingPortal>
449
452
  <FloatingFocusManager
450
453
  context={context}
454
+ // A leaving panel hands focus back at once.
455
+ disabled={isExiting}
451
456
  // `false`, so Tab leaves the menu and lands on the next
452
457
  // control in the page — which is what closes it, through
453
458
  // `useDismiss`. A trapped menu is a dialog.
@@ -478,6 +483,7 @@ export const Menu = ({
478
483
  // written straight onto the element by the `size`
479
484
  // middleware; this scrolls inside it.
480
485
  "charcuterie-scrollbar z-[var(--layer-modal)] flex min-w-48 flex-col gap-0.5 overflow-y-auto p-1",
486
+ motionClassName,
481
487
  className,
482
488
  )}
483
489
  onKeyDown={(keyEvent) => {
@@ -501,6 +507,8 @@ export const Menu = ({
501
507
  command()
502
508
  }
503
509
  }}
510
+ aria-hidden={isExiting ? true : undefined}
511
+ inert={isExiting ? true : undefined}
504
512
  ref={setFloating}
505
513
  // Duplicated from `getFloatingProps()` on purpose, and
506
514
  // the two cannot differ — `useRole(context, { role:
@@ -12,6 +12,12 @@ export type ModalProps = {
12
12
  * it the focus manager takes the first tabbable element — the
13
13
  * Close button on anything with chrome. */
14
14
  initialFocus?: RefObject<HTMLElement | null>
15
+ /**
16
+ * `false` only for a caller that animates the panel itself, as
17
+ * `TableViewport` does when it grows a table out of the page. Every
18
+ * other panel keeps the library's enter and exit motion.
19
+ */
20
+ hasMotion?: boolean
15
21
  /** Escape and a backdrop press both close. */
16
22
  isDismissable?: boolean
17
23
  isVisible: boolean
@@ -63,6 +69,7 @@ export const Modal = ({
63
69
  "aria-labelledby": ariaLabelledBy,
64
70
  children,
65
71
  className,
72
+ hasMotion = true,
66
73
  initialFocus,
67
74
  isDismissable = true,
68
75
  isVisible,
@@ -73,6 +80,7 @@ export const Modal = ({
73
80
  aria-label={ariaLabel}
74
81
  aria-labelledby={ariaLabelledBy}
75
82
  className={toClassName(MODAL_BOX_CLASS, className)}
83
+ hasMotion={hasMotion}
76
84
  initialFocus={initialFocus}
77
85
  isDismissable={isDismissable}
78
86
  isVisible={isVisible}
@@ -0,0 +1,66 @@
1
+ import type { ReactNode } from "react"
2
+ import { useState } from "react"
3
+ import { createRoot } from "react-dom/client"
4
+ import { expect, userEvent, waitFor } from "storybook/test"
5
+ import { test } from "vitest"
6
+
7
+ import { useLastPresentValue } from "./useLastPresentValue.ts"
8
+
9
+ const mount = (node: ReactNode) => {
10
+ const host = document.createElement("div")
11
+
12
+ document.body.append(host)
13
+
14
+ createRoot(host).render(node)
15
+
16
+ return host
17
+ }
18
+
19
+ const Selection = (): ReactNode => {
20
+ const [selected, setSelected] = useState<string | null>(
21
+ null,
22
+ )
23
+
24
+ const shown = useLastPresentValue(selected)
25
+
26
+ return (
27
+ <div>
28
+ <button
29
+ onClick={() => {
30
+ setSelected((previous) =>
31
+ previous === null ? "First" : null,
32
+ )
33
+ }}
34
+ type="button"
35
+ >
36
+ Toggle
37
+ </button>
38
+
39
+ <output>{shown ?? "none"}</output>
40
+ </div>
41
+ )
42
+ }
43
+
44
+ test("the last present value is held after the value empties", async () => {
45
+ const host = mount(<Selection />)
46
+
47
+ const button = await waitFor(() => {
48
+ const found = host.querySelector("button")
49
+
50
+ expect(found).not.toBeNull()
51
+
52
+ return found as HTMLButtonElement
53
+ })
54
+
55
+ const output = host.querySelector("output") as HTMLElement
56
+
57
+ expect(output.textContent).toBe("none")
58
+
59
+ await userEvent.click(button)
60
+
61
+ expect(output.textContent).toBe("First")
62
+
63
+ await userEvent.click(button)
64
+
65
+ expect(output.textContent).toBe("First")
66
+ })
@@ -0,0 +1,38 @@
1
+ import { useState } from "react"
2
+
3
+ /**
4
+ * The value, or — once it becomes `null` or `undefined` — the last
5
+ * value it had. For an overlay whose content comes from a selection:
6
+ *
7
+ * ```tsx
8
+ * const editingCard = cards.find((card) => card.id === editingId)
9
+ * const shownCard = useLastPresentValue(editingCard)
10
+ *
11
+ * <Dialog isVisible={editingCard !== undefined} heading={`Edit ${shownCard?.name}`}>
12
+ * {shownCard ? <CardForm card={shownCard} /> : null}
13
+ * </Dialog>
14
+ * ```
15
+ *
16
+ * Closing clears the selection in the same render that starts the
17
+ * dialog's exit, so without this the dialog spends its exit animation
18
+ * empty — it shrinks to a heading as it fades, which reads as a glitch
19
+ * rather than as closing. Visibility still follows the live value; only
20
+ * the content is held.
21
+ */
22
+ export const useLastPresentValue = <Value>(
23
+ value: Value | null | undefined,
24
+ ): Value | undefined => {
25
+ const [lastPresent, setLastPresent] = useState<
26
+ Value | undefined
27
+ >(value ?? undefined)
28
+
29
+ if (
30
+ value !== null &&
31
+ value !== undefined &&
32
+ !Object.is(value, lastPresent)
33
+ ) {
34
+ setLastPresent(value)
35
+ }
36
+
37
+ return value ?? lastPresent
38
+ }