@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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charcuterie/ui",
3
- "version": "4.27.3",
3
+ "version": "4.28.1",
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",
@@ -138,7 +138,7 @@
138
138
  "@codemirror/state": "^6",
139
139
  "@codemirror/view": "^6",
140
140
  "@lezer/common": "^1",
141
- "@storybook/react": "^10.3.6",
141
+ "@storybook/react": "^10.6.2",
142
142
  "@tiptap/core": "^3.31.4",
143
143
  "@tiptap/extension-image": "^3.31.4",
144
144
  "@tiptap/extension-text-align": "^3.31.4",
@@ -152,13 +152,13 @@
152
152
  "react": "^19.2.8",
153
153
  "react-dom": "^19.2.8",
154
154
  "react-router": "^8.3.0",
155
- "storybook": "^10.3.6",
155
+ "storybook": "^10.6.2",
156
156
  "tailwindcss": "^4.3.0",
157
157
  "typescript": "^6.0.3",
158
- "vitest": "^4.1.8",
158
+ "vitest": "^4.1.12",
159
159
  "axe-core": "4.12.1",
160
- "@vitest/browser": "^4.1.8",
161
- "@storybook/addon-docs": "^10.3.6"
160
+ "@vitest/browser": "^4.1.12",
161
+ "@storybook/addon-docs": "^10.6.2"
162
162
  },
163
163
  "repository": {
164
164
  "type": "git",
@@ -1,7 +1,8 @@
1
1
  import type { ReactNode } from "react"
2
- import { useEffect } from "react"
2
+ import { useEffect, useState } from "react"
3
3
 
4
4
  import { FOCUS_RING_CLASS } from "../intentStyles.ts"
5
+ import { usePresence } from "../Motion/usePresence.ts"
5
6
  import { toClassName } from "../toClassName.ts"
6
7
  import type {
7
8
  AccordionHeadingLevel,
@@ -63,6 +64,38 @@ export const AccordionSection = ({
63
64
 
64
65
  const Heading = HEADING_TAG[headingLevel]
65
66
 
67
+ const {
68
+ isExiting: isCollapsing,
69
+ isMounted: isShown,
70
+ presenceRef,
71
+ } = usePresence<HTMLDivElement>(isExpanded)
72
+
73
+ // Latched on the first change and never cleared. Before it, the
74
+ // section is in the state the page loaded with, and that should not
75
+ // move; after it, collapsing back to that state still animates.
76
+ const [isInitiallyExpanded] = useState(isExpanded)
77
+
78
+ const [hasToggled, setHasToggled] = useState(false)
79
+
80
+ if (!hasToggled && isExpanded !== isInitiallyExpanded) {
81
+ setHasToggled(true)
82
+ }
83
+
84
+ // The open class comes off when the open animation ends. Left on, it
85
+ // would replay each time an ancestor goes from hidden to shown (a
86
+ // tab switched back to), and its `overflow: clip` would keep
87
+ // clipping the open section's content.
88
+ const [isOpenSettled, setIsOpenSettled] = useState(false)
89
+
90
+ const [isLastExpanded, setIsLastExpanded] =
91
+ useState(isExpanded)
92
+
93
+ if (isLastExpanded !== isExpanded) {
94
+ setIsLastExpanded(isExpanded)
95
+
96
+ setIsOpenSettled(false)
97
+ }
98
+
66
99
  useEffect(() => {
67
100
  if (isDisabled) {
68
101
  return
@@ -141,13 +174,39 @@ export const AccordionSection = ({
141
174
  {/* biome-ignore lint/a11y/useSemanticElements: the semantic element for `group` is `<fieldset>`, which is a form-control grouping — it drags `<legend>` semantics and form-reset behaviour onto an accordion panel that contains prose. */}
142
175
  <div
143
176
  aria-labelledby={id}
144
- className="px-4 pt-1 pb-4 text-content-secondary text-sm"
177
+ // The height animates open and shut (`styles.css`). The
178
+ // padding lives on the inner box, because a padded box
179
+ // collapsed to `height: 0` is still as tall as its padding.
180
+ // No class until the section is first toggled, so a section
181
+ // that starts open does not animate open on page load.
182
+ className={toClassName(
183
+ "text-content-secondary text-sm",
184
+ hasToggled &&
185
+ (isCollapsing
186
+ ? "charcuterie-disclosure-exit"
187
+ : !isOpenSettled &&
188
+ "charcuterie-disclosure-enter"),
189
+ )}
145
190
  // `hidden` rather than unmounting, matching `Tabs`. A panel
146
191
  // that unmounts loses a scroll position, a partially typed
147
192
  // form, and any subscription its content opened — and the
148
193
  // fleet's log panes are exactly that.
149
- hidden={!isExpanded}
194
+ //
195
+ // A collapsing panel stays un-hidden for the length of its
196
+ // close animation, and is `inert` while it does.
197
+ hidden={!isShown}
150
198
  id={panelId}
199
+ inert={isCollapsing ? true : undefined}
200
+ onAnimationEnd={(event) => {
201
+ if (
202
+ event.target === event.currentTarget &&
203
+ event.animationName ===
204
+ "charcuterie-disclosure-in"
205
+ ) {
206
+ setIsOpenSettled(true)
207
+ }
208
+ }}
209
+ ref={presenceRef}
151
210
  // `group`, not `region`. A `group` is explicitly "a set of
152
211
  // UI objects **not** intended to be included in a page
153
212
  // summary", so it carries `aria-labelledby` and the
@@ -156,7 +215,7 @@ export const AccordionSection = ({
156
215
  // is reaching for and does not have a role for.
157
216
  role="group"
158
217
  >
159
- {item.content}
218
+ <div className="px-4 pt-1 pb-4">{item.content}</div>
160
219
  </div>
161
220
  </div>
162
221
  )
@@ -244,6 +244,9 @@ export const Combobox = ({
244
244
  context,
245
245
  floatingStyles,
246
246
  getFloatingProps,
247
+ isExiting,
248
+ isMounted,
249
+ motionClassName,
247
250
  setFloating,
248
251
  triggerId,
249
252
  } = useAnchoredOverlay({
@@ -886,13 +889,13 @@ export const Combobox = ({
886
889
 
887
890
  {clonedTrigger}
888
891
 
889
- {isVisible ? (
892
+ {isMounted ? (
890
893
  <FloatingPortal>
891
894
  <FloatingFocusManager
892
895
  context={context}
893
896
  // Attached mode keeps focus in the consumer's input — the
894
897
  // panel must never grab it.
895
- disabled={isAttached}
898
+ disabled={isAttached || isExiting}
896
899
  initialFocus={inputElement}
897
900
  modal={false}
898
901
  >
@@ -901,8 +904,11 @@ export const Combobox = ({
901
904
  className={toClassName(
902
905
  PANEL_SURFACE_CLASS,
903
906
  "z-[var(--layer-modal)] flex min-w-64 flex-col overflow-hidden",
907
+ motionClassName,
904
908
  className,
905
909
  )}
910
+ aria-hidden={isExiting ? true : undefined}
911
+ inert={isExiting ? true : undefined}
906
912
  ref={setFloating}
907
913
  style={floatingStyles}
908
914
  >
@@ -333,6 +333,9 @@ export const DatePicker = ({
333
333
  context,
334
334
  floatingStyles,
335
335
  getFloatingProps,
336
+ isExiting,
337
+ isMounted,
338
+ motionClassName,
336
339
  setFloating,
337
340
  } = useAnchoredOverlay({
338
341
  anchorRef: anchorElement,
@@ -709,7 +712,7 @@ export const DatePicker = ({
709
712
  {echoText}
710
713
  </p>
711
714
 
712
- {isVisible ? (
715
+ {isMounted ? (
713
716
  <FloatingPortal>
714
717
  <FloatingFocusManager
715
718
  context={context}
@@ -736,8 +739,11 @@ export const DatePicker = ({
736
739
  // phone.
737
740
  "@container z-[var(--layer-modal)] flex flex-col gap-3 overflow-hidden p-3 text-content-primary",
738
741
  isRange ? "w-[41rem]" : "w-80",
742
+ motionClassName,
739
743
  )}
740
744
  id={panelId}
745
+ aria-hidden={isExiting ? true : undefined}
746
+ inert={isExiting ? true : undefined}
741
747
  ref={setFloating}
742
748
  role="dialog"
743
749
  style={floatingStyles}
@@ -34,6 +34,12 @@ export type DialogProps = {
34
34
  * the case: every keystroke landed on "Close".
35
35
  */
36
36
  initialFocus?: RefObject<HTMLElement | null>
37
+ /**
38
+ * `false` only for a caller that animates the panel itself, as
39
+ * `TableViewport` does when it grows a table out of the page. Every
40
+ * other panel keeps the library's enter and exit motion.
41
+ */
42
+ hasMotion?: boolean
37
43
  isDismissable?: boolean
38
44
  isVisible: boolean
39
45
  /**
@@ -91,6 +97,7 @@ export const Dialog = ({
91
97
  className,
92
98
  footer,
93
99
  heading,
100
+ hasMotion = true,
94
101
  headingLevel = 2,
95
102
  initialFocus,
96
103
  isDismissable = true,
@@ -106,6 +113,7 @@ export const Dialog = ({
106
113
  <Modal
107
114
  aria-labelledby={headingId}
108
115
  className={toClassName(SIZE_CLASS[size], className)}
116
+ hasMotion={hasMotion}
109
117
  initialFocus={initialFocus}
110
118
  isDismissable={isDismissable}
111
119
  isVisible={isVisible}
@@ -118,8 +118,9 @@ wide list and drops under the text in a narrow one.
118
118
  - `isHighlighted` rings the row and pulses the ring twice — "this one just changed", after a
119
119
  scan or a save. `highlightIntent` picks the colour (`accent` by default). The ring stays
120
120
  while the flag does, so the app decides how long it lasts, usually with a timer. **To pulse
121
- an already-highlighted row again, change its `key`**: a new key is a new row, and a new row
122
- starts the pulse from the beginning. Under `prefers-reduced-motion` and on ePaper the ring
121
+ an already-highlighted row again, change its `highlightKey`**, not its `key`. Rows enter,
122
+ leave, and slide by key, so a new `key` would make the row leave while a copy of it
123
+ entered; a new `highlightKey` restarts only the ring's pulse. Under `prefers-reduced-motion` and on ePaper the ring
123
124
  does not pulse; it is simply there. The pulse lives in `@charcuterie/ui/styles.css`, which
124
125
  every app already imports.
125
126
 
@@ -2,6 +2,8 @@ import {
2
2
  composeStories,
3
3
  composeStory,
4
4
  } from "@storybook/react"
5
+ import { flushSync } from "react-dom"
6
+ import { createRoot } from "react-dom/client"
5
7
  import {
6
8
  expect,
7
9
  userEvent,
@@ -15,6 +17,8 @@ import { expectNoAxeViolations } from "../expectNoAxeViolations.testHelpers.ts"
15
17
  import { mountStory } from "../mountStory.testHelpers.ts"
16
18
  import { expectAgentDrivable } from "../testing/index.ts"
17
19
  import meta, * as stories from "./GroupedList.stories.tsx"
20
+ import type { GroupedListGroup } from "./GroupedList.tsx"
21
+ import { GroupedList } from "./GroupedList.tsx"
18
22
 
19
23
  const {
20
24
  AllStates,
@@ -779,3 +783,282 @@ test("every row state passes axe in both schemes", async () => {
779
783
  await expectNoAxeViolations(canvasElement)
780
784
  }
781
785
  })
786
+
787
+ test("a new highlightKey pulses the ring again without replacing the row", async () => {
788
+ const host = document.createElement("div")
789
+
790
+ document.body.append(host)
791
+
792
+ const root = createRoot(host)
793
+
794
+ const toGroups = (
795
+ highlightKey: number,
796
+ ): GroupedListGroup[] => [
797
+ {
798
+ heading: "Chores",
799
+ items: [
800
+ {
801
+ highlightKey,
802
+ isHighlighted: true,
803
+ key: "dishes",
804
+ label: "Dishes",
805
+ },
806
+ ],
807
+ key: "chores",
808
+ },
809
+ ]
810
+
811
+ try {
812
+ flushSync(() => {
813
+ root.render(<GroupedList groups={toGroups(1)} />)
814
+ })
815
+
816
+ const row = host.querySelector("li") as HTMLElement
817
+
818
+ const ring = row.querySelector(".charcuterie-highlight")
819
+
820
+ expect(ring).not.toBeNull()
821
+
822
+ flushSync(() => {
823
+ root.render(<GroupedList groups={toGroups(2)} />)
824
+ })
825
+
826
+ // The same row, which neither left nor entered…
827
+ expect(host.querySelector("li")).toBe(row)
828
+ expect(row).not.toHaveClass("charcuterie-item-enter")
829
+
830
+ // …wearing a new ring, which starts its pulse from the beginning.
831
+ const nextRing = row.querySelector(
832
+ ".charcuterie-highlight",
833
+ )
834
+
835
+ expect(nextRing).not.toBeNull()
836
+ expect(nextRing).not.toBe(ring)
837
+ } finally {
838
+ root.unmount()
839
+
840
+ host.remove()
841
+ }
842
+ })
843
+
844
+ test("a group an action creates enters, and a group it empties leaves with its last row", async () => {
845
+ // About the motion, so the suite's freeze is lifted for this test.
846
+ const frozenStyle = document.getElementById(
847
+ "charcuterie-freeze-motion",
848
+ )
849
+
850
+ frozenStyle?.remove()
851
+
852
+ const host = document.createElement("div")
853
+
854
+ // Long enough that a loaded host cannot finish the exit first.
855
+ host.style.setProperty("--duration-fast", "600ms")
856
+ host.style.setProperty("--duration-normal", "600ms")
857
+
858
+ document.body.append(host)
859
+
860
+ const root = createRoot(host)
861
+
862
+ const chores: GroupedListGroup = {
863
+ heading: "Chores",
864
+ items: [{ key: "dishes", label: "Dishes" }],
865
+ key: "chores",
866
+ }
867
+
868
+ const reading: GroupedListGroup = {
869
+ heading: "Reading",
870
+ items: [{ key: "book", label: "Book" }],
871
+ key: "reading",
872
+ }
873
+
874
+ const groupOf = (heading: string) =>
875
+ [...host.querySelectorAll("h3")]
876
+ .find((element) => element.textContent === heading)
877
+ ?.closest("[data-motion-key]")
878
+
879
+ try {
880
+ flushSync(() => {
881
+ root.render(<GroupedList groups={[chores]} />)
882
+ })
883
+
884
+ // On the first render, nothing moves.
885
+ expect(groupOf("Chores")?.className).not.toContain(
886
+ "charcuterie-item-enter",
887
+ )
888
+
889
+ flushSync(() => {
890
+ root.render(
891
+ <GroupedList groups={[chores, reading]} />,
892
+ )
893
+ })
894
+
895
+ expect(groupOf("Reading")).toHaveClass(
896
+ "charcuterie-item-enter",
897
+ )
898
+
899
+ flushSync(() => {
900
+ root.render(
901
+ <GroupedList
902
+ groups={[chores, { ...reading, items: [] }]}
903
+ />,
904
+ )
905
+ })
906
+
907
+ // Still drawn, with its last row, while it leaves…
908
+ const leaving = groupOf("Reading")
909
+
910
+ expect(leaving).toHaveClass("charcuterie-item-exit")
911
+ expect(leaving).toHaveAttribute("inert")
912
+ expect(leaving?.textContent).toContain("Book")
913
+
914
+ // …and gone once it has.
915
+ await waitFor(
916
+ () => {
917
+ expect(groupOf("Reading")).toBeUndefined()
918
+ },
919
+ { timeout: 5_000 },
920
+ )
921
+ } finally {
922
+ root.unmount()
923
+
924
+ host.remove()
925
+
926
+ if (frozenStyle !== null) {
927
+ document.head.append(frozenStyle)
928
+ }
929
+ }
930
+ })
931
+
932
+ const mountUnfrozen = () => {
933
+ const frozenStyle = document.getElementById(
934
+ "charcuterie-freeze-motion",
935
+ )
936
+
937
+ frozenStyle?.remove()
938
+
939
+ const host = document.createElement("div")
940
+
941
+ host.style.setProperty("--duration-fast", "600ms")
942
+ host.style.setProperty("--duration-normal", "600ms")
943
+
944
+ document.body.append(host)
945
+
946
+ const root = createRoot(host)
947
+
948
+ return {
949
+ host,
950
+ render: (groups: GroupedListGroup[]) => {
951
+ flushSync(() => {
952
+ root.render(<GroupedList groups={groups} />)
953
+ })
954
+ },
955
+ unmount: () => {
956
+ root.unmount()
957
+
958
+ host.remove()
959
+
960
+ if (frozenStyle !== null) {
961
+ document.head.append(frozenStyle)
962
+ }
963
+ },
964
+ }
965
+ }
966
+
967
+ const toGroup = (key: string, rowKeys: string[]) => ({
968
+ heading: key,
969
+ items: rowKeys.map((rowKey) => ({
970
+ key: rowKey,
971
+ label: rowKey,
972
+ })),
973
+ key,
974
+ })
975
+
976
+ const topOf = (host: HTMLElement, key: string) =>
977
+ (
978
+ host.querySelector(
979
+ `[data-motion-key="${key}"]`,
980
+ ) as HTMLElement
981
+ ).getBoundingClientRect().top
982
+
983
+ test("a row whose group moves in the same commit starts its slide where it was shown", () => {
984
+ const { host, render, unmount } = mountUnfrozen()
985
+
986
+ try {
987
+ render([
988
+ toGroup("A", ["a1", "a2"]),
989
+ toGroup("B", ["b1", "b2"]),
990
+ ])
991
+
992
+ const before = topOf(host, "b1")
993
+
994
+ // B moves above A, and gains a row above b1, in one commit.
995
+ render([
996
+ toGroup("B", ["b0", "b1", "b2"]),
997
+ toGroup("A", ["a1", "a2"]),
998
+ ])
999
+
1000
+ // The row's slide and its group's slide add up to no jump at all.
1001
+ expect(
1002
+ Math.abs(topOf(host, "b1") - before),
1003
+ ).toBeLessThan(2)
1004
+ } finally {
1005
+ unmount()
1006
+ }
1007
+ })
1008
+
1009
+ test("a group below one that gains a row slides down instead of jumping", () => {
1010
+ const { host, render, unmount } = mountUnfrozen()
1011
+
1012
+ try {
1013
+ render([toGroup("A", ["a1"]), toGroup("B", ["b1"])])
1014
+
1015
+ const before = topOf(host, "B")
1016
+
1017
+ render([
1018
+ toGroup("A", ["a1", "a2"]),
1019
+ toGroup("B", ["b1"]),
1020
+ ])
1021
+
1022
+ expect(
1023
+ Math.abs(topOf(host, "B") - before),
1024
+ ).toBeLessThan(2)
1025
+ } finally {
1026
+ unmount()
1027
+ }
1028
+ })
1029
+
1030
+ test("a group below one whose row has finished leaving slides up instead of jumping", async () => {
1031
+ const { host, render, unmount } = mountUnfrozen()
1032
+
1033
+ try {
1034
+ render([
1035
+ toGroup("A", ["a1", "a2"]),
1036
+ toGroup("B", ["b1"]),
1037
+ ])
1038
+
1039
+ const leaving = host.querySelector(
1040
+ '[data-motion-key="a2"]',
1041
+ ) as HTMLElement
1042
+
1043
+ render([toGroup("A", ["a1"]), toGroup("B", ["b1"])])
1044
+
1045
+ const group = host.querySelector(
1046
+ '[data-motion-key="B"]',
1047
+ ) as HTMLElement
1048
+
1049
+ // The leaving row still holds its space, so nothing has moved yet.
1050
+ expect(group.style.transition).toBe("")
1051
+
1052
+ await waitFor(
1053
+ () => {
1054
+ expect(leaving.isConnected).toBe(false)
1055
+ },
1056
+ { timeout: 5_000 },
1057
+ )
1058
+
1059
+ // Its gap closed, and the group below is sliding up into it.
1060
+ expect(group.style.transition).toContain("translate")
1061
+ } finally {
1062
+ unmount()
1063
+ }
1064
+ })