@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.
- package/dist/Accordion/AccordionSection.d.ts.map +1 -1
- package/dist/Accordion/AccordionSection.js +43 -4
- package/dist/Accordion/AccordionSection.js.map +1 -1
- package/dist/Combobox/Combobox.d.ts.map +1 -1
- package/dist/Combobox/Combobox.js +3 -3
- package/dist/Combobox/Combobox.js.map +1 -1
- package/dist/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/DatePicker/DatePicker.js +3 -3
- package/dist/DatePicker/DatePicker.js.map +1 -1
- package/dist/Dialog/Dialog.d.ts +7 -1
- package/dist/Dialog/Dialog.d.ts.map +1 -1
- package/dist/Dialog/Dialog.js +2 -2
- package/dist/Dialog/Dialog.js.map +1 -1
- package/dist/GroupedList/GroupedList.d.ts +9 -2
- package/dist/GroupedList/GroupedList.d.ts.map +1 -1
- package/dist/GroupedList/GroupedList.js +39 -5
- package/dist/GroupedList/GroupedList.js.map +1 -1
- package/dist/GroupedList/GroupedListRow.d.ts +4 -1
- package/dist/GroupedList/GroupedListRow.d.ts.map +1 -1
- package/dist/GroupedList/GroupedListRow.js +3 -3
- package/dist/GroupedList/GroupedListRow.js.map +1 -1
- package/dist/GroupedList/GroupedListRows.d.ts +16 -0
- package/dist/GroupedList/GroupedListRows.d.ts.map +1 -0
- package/dist/GroupedList/GroupedListRows.js +17 -0
- package/dist/GroupedList/GroupedListRows.js.map +1 -0
- package/dist/Listbox/Listbox.d.ts.map +1 -1
- package/dist/Listbox/Listbox.js +6 -4
- package/dist/Listbox/Listbox.js.map +1 -1
- package/dist/Menu/Menu.d.ts.map +1 -1
- package/dist/Menu/Menu.js +6 -4
- package/dist/Menu/Menu.js.map +1 -1
- package/dist/Modal/Modal.d.ts +7 -1
- package/dist/Modal/Modal.d.ts.map +1 -1
- package/dist/Modal/Modal.js +1 -1
- package/dist/Modal/Modal.js.map +1 -1
- package/dist/Motion/useLastPresentValue.d.ts +21 -0
- package/dist/Motion/useLastPresentValue.d.ts.map +1 -0
- package/dist/Motion/useLastPresentValue.js +30 -0
- package/dist/Motion/useLastPresentValue.js.map +1 -0
- package/dist/Motion/useListMotion.d.ts +111 -0
- package/dist/Motion/useListMotion.d.ts.map +1 -0
- package/dist/Motion/useListMotion.js +358 -0
- package/dist/Motion/useListMotion.js.map +1 -0
- package/dist/Motion/usePresence.d.ts +81 -0
- package/dist/Motion/usePresence.d.ts.map +1 -0
- package/dist/Motion/usePresence.js +131 -0
- package/dist/Motion/usePresence.js.map +1 -0
- package/dist/Motion/useValueChangeMotion.d.ts +52 -0
- package/dist/Motion/useValueChangeMotion.d.ts.map +1 -0
- package/dist/Motion/useValueChangeMotion.js +87 -0
- package/dist/Motion/useValueChangeMotion.js.map +1 -0
- package/dist/Overlay/OverlayPanel.d.ts +18 -1
- package/dist/Overlay/OverlayPanel.d.ts.map +1 -1
- package/dist/Overlay/OverlayPanel.js +83 -11
- package/dist/Overlay/OverlayPanel.js.map +1 -1
- package/dist/Overlay/SharedBackdrop.d.ts +3 -0
- package/dist/Overlay/SharedBackdrop.d.ts.map +1 -1
- package/dist/Overlay/SharedBackdrop.js +8 -2
- package/dist/Overlay/SharedBackdrop.js.map +1 -1
- package/dist/Overlay/focusHistory.d.ts +16 -0
- package/dist/Overlay/focusHistory.d.ts.map +1 -0
- package/dist/Overlay/focusHistory.js +110 -0
- package/dist/Overlay/focusHistory.js.map +1 -0
- package/dist/Overlay/useAnchoredOverlay.d.ts +17 -3
- package/dist/Overlay/useAnchoredOverlay.d.ts.map +1 -1
- package/dist/Overlay/useAnchoredOverlay.js +23 -5
- package/dist/Overlay/useAnchoredOverlay.js.map +1 -1
- package/dist/Popover/Popover.d.ts.map +1 -1
- package/dist/Popover/Popover.js +4 -2
- package/dist/Popover/Popover.js.map +1 -1
- package/dist/ReorderList/ReorderList.d.ts +6 -0
- package/dist/ReorderList/ReorderList.d.ts.map +1 -1
- package/dist/ReorderList/ReorderList.js +23 -6
- package/dist/ReorderList/ReorderList.js.map +1 -1
- package/dist/TableViewport/TableViewport.d.ts.map +1 -1
- package/dist/TableViewport/TableViewport.js +9 -1
- package/dist/TableViewport/TableViewport.js.map +1 -1
- package/dist/Tooltip/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip/Tooltip.js +16 -13
- package/dist/Tooltip/Tooltip.js.map +1 -1
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/styles.css +191 -1
- package/package.json +6 -6
- package/src/Accordion/AccordionSection.tsx +63 -4
- package/src/Combobox/Combobox.tsx +8 -2
- package/src/DatePicker/DatePicker.tsx +7 -1
- package/src/Dialog/Dialog.tsx +8 -0
- package/src/GroupedList/GroupedList.mdx +3 -2
- package/src/GroupedList/GroupedList.test.tsx +283 -0
- package/src/GroupedList/GroupedList.tsx +88 -44
- package/src/GroupedList/GroupedListRow.tsx +8 -0
- package/src/GroupedList/GroupedListRows.tsx +51 -0
- package/src/Listbox/Listbox.tsx +9 -1
- package/src/Menu/Menu.tsx +9 -1
- package/src/Modal/Modal.tsx +8 -0
- package/src/Motion/useLastPresentValue.test.tsx +66 -0
- package/src/Motion/useLastPresentValue.ts +38 -0
- package/src/Motion/useListMotion.test.tsx +527 -0
- package/src/Motion/useListMotion.ts +674 -0
- package/src/Motion/usePresence.test.tsx +223 -0
- package/src/Motion/usePresence.ts +252 -0
- package/src/Motion/useValueChangeMotion.test.tsx +131 -0
- package/src/Motion/useValueChangeMotion.ts +110 -0
- package/src/Overlay/OverlayPanel.server.test.ts +30 -0
- package/src/Overlay/OverlayPanel.tsx +146 -14
- package/src/Overlay/SharedBackdrop.tsx +17 -2
- package/src/Overlay/focusHistory.ts +152 -0
- package/src/Overlay/overlayReturnFocus.storyHelpers.tsx +242 -0
- package/src/Overlay/overlayReturnFocus.test.tsx +255 -0
- package/src/Overlay/useAnchoredOverlay.ts +56 -4
- package/src/Popover/Popover.tsx +9 -1
- package/src/ReorderList/ReorderList.test.tsx +76 -0
- package/src/ReorderList/ReorderList.tsx +86 -48
- package/src/TableViewport/TableViewport.tsx +4 -0
- package/src/Tooltip/Tooltip.tsx +32 -4
- package/src/index.ts +30 -0
- package/src/styles.css +191 -1
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { useLatestRef } from "@charcuterie/logic";
|
|
2
|
+
import { useCallback, useLayoutEffect, useRef, useState, } from "react";
|
|
3
|
+
import { getAnimationLengthMs, getTransitionLengthMs, } from "./usePresence.js";
|
|
4
|
+
const ENTER_CLASS = "charcuterie-item-enter";
|
|
5
|
+
const ENTER_ANIMATION = "charcuterie-item-in";
|
|
6
|
+
const EXIT_CLASS = "charcuterie-item-exit";
|
|
7
|
+
const SLIDE_TRANSITION = "translate var(--duration-normal) var(--easing-standard)";
|
|
8
|
+
/** As in `usePresence`: a margin past the computed length, not a
|
|
9
|
+
* design duration. */
|
|
10
|
+
const SAFETY_MARGIN_MS = 50;
|
|
11
|
+
/**
|
|
12
|
+
* Where `element` sits inside its parent, the list.
|
|
13
|
+
*
|
|
14
|
+
* Inside the parent's own box, whatever the parent's CSS. `offsetTop`
|
|
15
|
+
* is measured from the nearest positioned ancestor, which is often
|
|
16
|
+
* outside the list: in a `GroupedList`, a row's offset would then
|
|
17
|
+
* include its group's, and a group and its rows moving in one commit
|
|
18
|
+
* would slide each row by the group's move twice, once in the row and
|
|
19
|
+
* once in the group. Offsets ignore `translate`, so a slide in flight
|
|
20
|
+
* does not change what is measured.
|
|
21
|
+
*/
|
|
22
|
+
const readPosition = (element) => {
|
|
23
|
+
const parent = element.parentElement;
|
|
24
|
+
if (parent === null ||
|
|
25
|
+
element.offsetParent === parent ||
|
|
26
|
+
parent.offsetParent !== element.offsetParent) {
|
|
27
|
+
return {
|
|
28
|
+
left: element.offsetLeft,
|
|
29
|
+
top: element.offsetTop,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
left: element.offsetLeft - parent.offsetLeft,
|
|
34
|
+
top: element.offsetTop - parent.offsetTop,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export const useListMotion = ({ getKey, items, layoutKey = "", onExitEnd, }) => {
|
|
38
|
+
// Compared by key, never by array identity. Callers routinely build
|
|
39
|
+
// the array during render (`groupPurchases(recent)`), so an identity
|
|
40
|
+
// check would see a "change" on every render — and this block sets
|
|
41
|
+
// state during render, so that would re-render forever.
|
|
42
|
+
const keySignature = JSON.stringify(items.map(getKey));
|
|
43
|
+
const [previousKeySignature, setPreviousKeySignature] = useState(keySignature);
|
|
44
|
+
// The items as of the last commit, so a removed item leaves showing
|
|
45
|
+
// its latest content rather than what it held when the keys last
|
|
46
|
+
// changed.
|
|
47
|
+
const committedItemsRef = useRef(items);
|
|
48
|
+
useLayoutEffect(() => {
|
|
49
|
+
committedItemsRef.current = items;
|
|
50
|
+
});
|
|
51
|
+
// The keys the last commit showed, in order, leaving items included.
|
|
52
|
+
const shownKeysRef = useRef(items.map(getKey));
|
|
53
|
+
const [exiting, setExiting] = useState([]);
|
|
54
|
+
// Keys that arrived after the first render. They keep the enter
|
|
55
|
+
// class while they are mounted; it only animates once, on mount.
|
|
56
|
+
const [enteredKeys, setEnteredKeys] = useState(() => new Set());
|
|
57
|
+
if (keySignature !== previousKeySignature) {
|
|
58
|
+
// Read during render on purpose: the last committed list is the
|
|
59
|
+
// one the user is looking at, and so the one items leave from.
|
|
60
|
+
const previousItems = committedItemsRef.current;
|
|
61
|
+
const currentKeys = new Set(items.map(getKey));
|
|
62
|
+
const previousKeys = new Set(previousItems.map(getKey));
|
|
63
|
+
const removed = previousItems
|
|
64
|
+
.map((item, index) => ({
|
|
65
|
+
index,
|
|
66
|
+
item,
|
|
67
|
+
key: getKey(item),
|
|
68
|
+
}))
|
|
69
|
+
.filter((one) => !currentKeys.has(one.key));
|
|
70
|
+
const added = items
|
|
71
|
+
.map(getKey)
|
|
72
|
+
.filter((key) => !previousKeys.has(key));
|
|
73
|
+
setPreviousKeySignature(keySignature);
|
|
74
|
+
if (removed.length > 0 || exiting.length > 0) {
|
|
75
|
+
setExiting((previous) => [
|
|
76
|
+
// A key that came back stops leaving.
|
|
77
|
+
...previous.filter((one) => !currentKeys.has(one.key)),
|
|
78
|
+
...removed.filter((one) => !previous.some((exitingOne) => exitingOne.key === one.key)),
|
|
79
|
+
]);
|
|
80
|
+
}
|
|
81
|
+
if (added.length > 0) {
|
|
82
|
+
setEnteredKeys((previous) => new Set([...previous, ...added]));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const elementsRef = useRef(new Map());
|
|
86
|
+
const refCallbacksRef = useRef(new Map());
|
|
87
|
+
const getRef = useCallback((key) => {
|
|
88
|
+
const existing = refCallbacksRef.current.get(key);
|
|
89
|
+
if (existing) {
|
|
90
|
+
return existing;
|
|
91
|
+
}
|
|
92
|
+
let stopListening;
|
|
93
|
+
const callback = (node) => {
|
|
94
|
+
stopListening?.();
|
|
95
|
+
stopListening = undefined;
|
|
96
|
+
if (node === null) {
|
|
97
|
+
elementsRef.current.delete(key);
|
|
98
|
+
// Dropped on detach, so a list whose keys keep changing does
|
|
99
|
+
// not keep a callback for every key it ever showed. A key
|
|
100
|
+
// that comes back gets a new one. Only this callback's own
|
|
101
|
+
// entry: React detaches an old callback after a new one for
|
|
102
|
+
// the same key may already be in the map.
|
|
103
|
+
if (refCallbacksRef.current.get(key) === callback) {
|
|
104
|
+
refCallbacksRef.current.delete(key);
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
elementsRef.current.set(key, node);
|
|
109
|
+
// StrictMode detaches and re-attaches the same callback; put it
|
|
110
|
+
// back so the next render hands React the same identity.
|
|
111
|
+
if (!refCallbacksRef.current.has(key)) {
|
|
112
|
+
refCallbacksRef.current.set(key, callback);
|
|
113
|
+
}
|
|
114
|
+
const onAnimationEnd = (event) => {
|
|
115
|
+
if (event.target === node &&
|
|
116
|
+
event.animationName === ENTER_ANIMATION) {
|
|
117
|
+
setEnteredKeys((previous) => {
|
|
118
|
+
if (!previous.has(key)) {
|
|
119
|
+
return previous;
|
|
120
|
+
}
|
|
121
|
+
const next = new Set(previous);
|
|
122
|
+
next.delete(key);
|
|
123
|
+
return next;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
node.addEventListener("animationend", onAnimationEnd);
|
|
128
|
+
stopListening = () => {
|
|
129
|
+
node.removeEventListener("animationend", onAnimationEnd);
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
refCallbacksRef.current.set(key, callback);
|
|
133
|
+
return callback;
|
|
134
|
+
}, []);
|
|
135
|
+
const exitingKeys = new Set(exiting.map((one) => one.key));
|
|
136
|
+
// Each leaving item stays where the user last saw it: after the item
|
|
137
|
+
// that was shown just before it, whether that one stayed, left too,
|
|
138
|
+
// or was added in front of it since. A stored index would not hold:
|
|
139
|
+
// in a newest-first list capped at three, `[A, B, C]` becoming
|
|
140
|
+
// `[X, A, B]` would put the leaving C between A and B.
|
|
141
|
+
const itemKeys = items.map(getKey);
|
|
142
|
+
const itemKeySet = new Set(itemKeys);
|
|
143
|
+
const exitingByKey = new Map(exiting.map((one) => [one.key, one]));
|
|
144
|
+
// Leaving items, by the key of the staying item they follow. `null`
|
|
145
|
+
// holds the ones that were shown before every staying item.
|
|
146
|
+
const exitingAfter = new Map();
|
|
147
|
+
const placedKeys = new Set();
|
|
148
|
+
let lastStayingKey = null;
|
|
149
|
+
for (const key of shownKeysRef.current) {
|
|
150
|
+
const one = exitingByKey.get(key);
|
|
151
|
+
if (one !== undefined) {
|
|
152
|
+
exitingAfter.set(lastStayingKey, [
|
|
153
|
+
...(exitingAfter.get(lastStayingKey) ?? []),
|
|
154
|
+
one,
|
|
155
|
+
]);
|
|
156
|
+
placedKeys.add(key);
|
|
157
|
+
}
|
|
158
|
+
else if (itemKeySet.has(key)) {
|
|
159
|
+
lastStayingKey = key;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// A leaving item the last commit did not show (removed in the same
|
|
163
|
+
// batch that would have shown it) goes at the end.
|
|
164
|
+
const unplaced = exiting.filter((one) => !placedKeys.has(one.key));
|
|
165
|
+
const toExitingEntry = (one) => ({
|
|
166
|
+
index: one.index,
|
|
167
|
+
isExiting: true,
|
|
168
|
+
item: one.item,
|
|
169
|
+
key: one.key,
|
|
170
|
+
motionProps: {
|
|
171
|
+
"aria-hidden": true,
|
|
172
|
+
className: EXIT_CLASS,
|
|
173
|
+
"data-motion-key": one.key,
|
|
174
|
+
inert: true,
|
|
175
|
+
ref: getRef(one.key),
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
const entries = [
|
|
179
|
+
...(exitingAfter.get(null) ?? []).map(toExitingEntry),
|
|
180
|
+
...items.flatMap((item, index) => {
|
|
181
|
+
const key = itemKeys[index] ?? getKey(item);
|
|
182
|
+
return [
|
|
183
|
+
{
|
|
184
|
+
index,
|
|
185
|
+
isExiting: false,
|
|
186
|
+
item,
|
|
187
|
+
key,
|
|
188
|
+
motionProps: {
|
|
189
|
+
className: enteredKeys.has(key)
|
|
190
|
+
? ENTER_CLASS
|
|
191
|
+
: undefined,
|
|
192
|
+
"data-motion-key": key,
|
|
193
|
+
ref: getRef(key),
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
...(exitingAfter.get(key) ?? []).map(toExitingEntry),
|
|
197
|
+
];
|
|
198
|
+
}),
|
|
199
|
+
...unplaced.map(toExitingEntry),
|
|
200
|
+
];
|
|
201
|
+
// The order this commit shows, leaving items included, which is
|
|
202
|
+
// where the next change places what leaves.
|
|
203
|
+
useLayoutEffect(() => {
|
|
204
|
+
shownKeysRef.current = entries.map((entry) => entry.key);
|
|
205
|
+
});
|
|
206
|
+
const onExitEndRef = useLatestRef(onExitEnd);
|
|
207
|
+
const finishExit = useCallback((key) => {
|
|
208
|
+
setExiting((previous) => previous.filter((one) => one.key !== key));
|
|
209
|
+
onExitEndRef.current?.();
|
|
210
|
+
setEnteredKeys((previous) => {
|
|
211
|
+
if (!previous.has(key)) {
|
|
212
|
+
return previous;
|
|
213
|
+
}
|
|
214
|
+
const next = new Set(previous);
|
|
215
|
+
next.delete(key);
|
|
216
|
+
return next;
|
|
217
|
+
});
|
|
218
|
+
}, [onExitEndRef]);
|
|
219
|
+
const watchersRef = useRef(new Map());
|
|
220
|
+
const exitingSignature = [...exitingKeys]
|
|
221
|
+
.sort()
|
|
222
|
+
.join("\n");
|
|
223
|
+
// Watch each leaving element for the end of its exit.
|
|
224
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the signature is the set of leaving keys, which is what this effect depends on.
|
|
225
|
+
useLayoutEffect(() => {
|
|
226
|
+
const watchers = watchersRef.current;
|
|
227
|
+
for (const [key, stop] of watchers) {
|
|
228
|
+
if (!exitingKeys.has(key)) {
|
|
229
|
+
stop();
|
|
230
|
+
watchers.delete(key);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
for (const key of exitingKeys) {
|
|
234
|
+
if (watchers.has(key)) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const element = elementsRef.current.get(key);
|
|
238
|
+
const lengthMs = element === undefined
|
|
239
|
+
? 0
|
|
240
|
+
: getAnimationLengthMs(element);
|
|
241
|
+
if (element === undefined || lengthMs === 0) {
|
|
242
|
+
finishExit(key);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const onAnimationEnd = (event) => {
|
|
246
|
+
if (event.target === element) {
|
|
247
|
+
finishExit(key);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
element.addEventListener("animationend", onAnimationEnd);
|
|
251
|
+
const timer = globalThis.setTimeout(() => {
|
|
252
|
+
finishExit(key);
|
|
253
|
+
}, lengthMs + SAFETY_MARGIN_MS);
|
|
254
|
+
watchers.set(key, () => {
|
|
255
|
+
element.removeEventListener("animationend", onAnimationEnd);
|
|
256
|
+
globalThis.clearTimeout(timer);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}, [exitingSignature, finishExit]);
|
|
260
|
+
useLayoutEffect(() => () => {
|
|
261
|
+
for (const stop of watchersRef.current.values()) {
|
|
262
|
+
stop();
|
|
263
|
+
}
|
|
264
|
+
watchersRef.current.clear();
|
|
265
|
+
}, []);
|
|
266
|
+
// What slides the items: the order of keys, plus the caller's
|
|
267
|
+
// `layoutKey`.
|
|
268
|
+
const orderSignature = JSON.stringify([
|
|
269
|
+
entries.map((entry) => entry.key),
|
|
270
|
+
layoutKey,
|
|
271
|
+
]);
|
|
272
|
+
// The order the DOM shows now: the last committed one.
|
|
273
|
+
const committedOrderRef = useRef(orderSignature);
|
|
274
|
+
const firstPositionsRef = useRef(null);
|
|
275
|
+
// The listener of the slide each element is running, if any.
|
|
276
|
+
const slideStopsRef = useRef(new Map());
|
|
277
|
+
// "First", read during the render that changes the order, while the
|
|
278
|
+
// DOM still holds the commit the user is looking at. Read any
|
|
279
|
+
// earlier and a row that grew in between, with no key change, would
|
|
280
|
+
// send every row below it sliding from where it used to be. This is
|
|
281
|
+
// `useFlipList`'s reason too; reading layout here has no effect on
|
|
282
|
+
// the tree, and a repeated render reads the same unchanged DOM.
|
|
283
|
+
if (committedOrderRef.current !== orderSignature) {
|
|
284
|
+
const positions = new Map();
|
|
285
|
+
for (const [key, element] of elementsRef.current) {
|
|
286
|
+
positions.set(key, readPosition(element));
|
|
287
|
+
}
|
|
288
|
+
firstPositionsRef.current = positions;
|
|
289
|
+
}
|
|
290
|
+
// Slide every item that the change moved. Positions are offsets
|
|
291
|
+
// inside the list's own box, so scrolling the page between two
|
|
292
|
+
// renders is not mistaken for a move.
|
|
293
|
+
useLayoutEffect(() => {
|
|
294
|
+
const previousPositions = firstPositionsRef.current;
|
|
295
|
+
// Consumed either way: a stale set of positions is worse than
|
|
296
|
+
// none.
|
|
297
|
+
firstPositionsRef.current = null;
|
|
298
|
+
if (committedOrderRef.current === orderSignature) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
committedOrderRef.current = orderSignature;
|
|
302
|
+
if (previousPositions === null) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
for (const [key, element] of elementsRef.current) {
|
|
306
|
+
const before = previousPositions.get(key);
|
|
307
|
+
const after = readPosition(element);
|
|
308
|
+
// Leaving items slide too: one that a reorder moves would
|
|
309
|
+
// otherwise jump, then fade where it never was. Their exit
|
|
310
|
+
// animates `opacity` and `scale`, never `translate`.
|
|
311
|
+
if (before === undefined) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const deltaLeft = before.left - after.left;
|
|
315
|
+
const deltaTop = before.top - after.top;
|
|
316
|
+
if (deltaLeft === 0 && deltaTop === 0) {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
// A slide that starts while the last one runs takes over from
|
|
320
|
+
// it. The last one's listener goes first, without touching the
|
|
321
|
+
// style: if it ran on the cancel this slide causes, it would
|
|
322
|
+
// clear the `transition` this slide is about to set, and a
|
|
323
|
+
// browser that follows the spec would then stop this slide dead.
|
|
324
|
+
slideStopsRef.current.get(element)?.();
|
|
325
|
+
element.style.transition = "none";
|
|
326
|
+
element.style.translate = `${deltaLeft}px ${deltaTop}px`;
|
|
327
|
+
// Commit the inverted position before transitioning out of it,
|
|
328
|
+
// or the browser coalesces both writes and nothing moves.
|
|
329
|
+
element.getBoundingClientRect();
|
|
330
|
+
element.style.transition = SLIDE_TRANSITION;
|
|
331
|
+
element.style.translate = "";
|
|
332
|
+
// Reduced motion and the frozen suites make the slide 0s long,
|
|
333
|
+
// and a 0s transition never starts, so it would never end and
|
|
334
|
+
// never clear what it wrote.
|
|
335
|
+
if (getTransitionLengthMs(element) === 0) {
|
|
336
|
+
element.style.transition = "";
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const stop = () => {
|
|
340
|
+
element.removeEventListener("transitionend", clear);
|
|
341
|
+
element.removeEventListener("transitioncancel", clear);
|
|
342
|
+
slideStopsRef.current.delete(element);
|
|
343
|
+
};
|
|
344
|
+
const clear = (event) => {
|
|
345
|
+
if (event.target !== element) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
element.style.transition = "";
|
|
349
|
+
stop();
|
|
350
|
+
};
|
|
351
|
+
element.addEventListener("transitionend", clear);
|
|
352
|
+
element.addEventListener("transitioncancel", clear);
|
|
353
|
+
slideStopsRef.current.set(element, stop);
|
|
354
|
+
}
|
|
355
|
+
}, [orderSignature]);
|
|
356
|
+
return { entries };
|
|
357
|
+
};
|
|
358
|
+
//# sourceMappingURL=useListMotion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useListMotion.js","sourceRoot":"","sources":["../../src/Motion/useListMotion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEjD,OAAO,EACL,WAAW,EACX,eAAe,EACf,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAA;AAEd,OAAO,EACL,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,kBAAkB,CAAA;AA6HzB,MAAM,WAAW,GAAG,wBAAwB,CAAA;AAE5C,MAAM,eAAe,GAAG,qBAAqB,CAAA;AAE7C,MAAM,UAAU,GAAG,uBAAuB,CAAA;AAE1C,MAAM,gBAAgB,GACpB,yDAAyD,CAAA;AAE3D;sBACsB;AACtB,MAAM,gBAAgB,GAAG,EAAE,CAAA;AAE3B;;;;;;;;;;GAUG;AACH,MAAM,YAAY,GAAG,CAAC,OAAoB,EAAY,EAAE;IACtD,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAA;IAEpC,IACE,MAAM,KAAK,IAAI;QACf,OAAO,CAAC,YAAY,KAAK,MAAM;QAC/B,MAAM,CAAC,YAAY,KAAK,OAAO,CAAC,YAAY,EAC5C,CAAC;QACD,OAAO;YACL,IAAI,EAAE,OAAO,CAAC,UAAU;YACxB,GAAG,EAAE,OAAO,CAAC,SAAS;SACvB,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AAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;YACvC,CAAC,CAAA;YAED,MAAM,KAAK,GAAG,CAAC,KAAsB,EAAE,EAAE;gBACvC,IAAI,KAAK,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;oBAC7B,OAAM;gBACR,CAAC;gBAED,OAAO,CAAC,KAAK,CAAC,UAAU,GAAG,EAAE,CAAA;gBAE7B,IAAI,EAAE,CAAA;YACR,CAAC,CAAA;YAED,OAAO,CAAC,gBAAgB,CAAC,eAAe,EAAE,KAAK,CAAC,CAAA;YAEhD,OAAO,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAA;YAEnD,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;QAC1C,CAAC;IACH,CAAC,EAAE,CAAC,cAAc,CAAC,CAAC,CAAA;IAEpB,OAAO,EAAE,OAAO,EAAE,CAAA;AACpB,CAAC,CAAA"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { RefObject } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Keeps an element in the page until its exit animation has run, so
|
|
4
|
+
* a closing overlay leaves the screen with motion instead of
|
|
5
|
+
* vanishing in one frame.
|
|
6
|
+
*
|
|
7
|
+
* ### Why the length comes from the computed style
|
|
8
|
+
*
|
|
9
|
+
* The exit's length is **read from the element**, never passed in.
|
|
10
|
+
* The duration is a token (`--duration-fast`), and three different
|
|
11
|
+
* renderings set it to three different things: the designed motion,
|
|
12
|
+
* `prefers-reduced-motion` (where `styles.css` sets `animation: none`),
|
|
13
|
+
* and the automated suites' `FREEZE_MOTION_CSS` (`0s`). A number
|
|
14
|
+
* written here would hold a closed panel in the page for that long
|
|
15
|
+
* under all three, which is a dead overlay the user cannot see but a
|
|
16
|
+
* test still finds. Reading `getComputedStyle` in a layout effect
|
|
17
|
+
* means a motionless exit finishes **in the same commit** — before
|
|
18
|
+
* paint, and inside the test's `act()` — so nothing that asserts "it
|
|
19
|
+
* closed" has to learn to wait.
|
|
20
|
+
*
|
|
21
|
+
* A DOM with no CSS engine (jsdom, happy-dom) reports an empty
|
|
22
|
+
* `animation-name`, which reads as no animation and unmounts at once
|
|
23
|
+
* for the same reason.
|
|
24
|
+
*
|
|
25
|
+
* ### What "exiting" must mean to the caller
|
|
26
|
+
*
|
|
27
|
+
* While `isExiting` is true the element is still in the page but is
|
|
28
|
+
* **closed** in every sense except paint. The caller makes it `inert`
|
|
29
|
+
* and `aria-hidden`, and the exit class sets `pointer-events: none`,
|
|
30
|
+
* so a role query, the keyboard, and the pointer all behave as though
|
|
31
|
+
* it has gone. Only the eye sees it leave.
|
|
32
|
+
*
|
|
33
|
+
* ### Re-opening mid-exit
|
|
34
|
+
*
|
|
35
|
+
* Re-opening during the exit simply swaps the exit class back for the
|
|
36
|
+
* enter class. Changing `animation-name` restarts the animation, and
|
|
37
|
+
* the listener for the abandoned exit is removed by the effect's
|
|
38
|
+
* cleanup, so it can never unmount the re-opened element.
|
|
39
|
+
*/
|
|
40
|
+
export type UsePresenceResult<Element extends HTMLElement = HTMLElement> = {
|
|
41
|
+
/** Mounted, but closed: make it `inert` and give it the exit class. */
|
|
42
|
+
isExiting: boolean;
|
|
43
|
+
/** Whether to render the element at all. */
|
|
44
|
+
isMounted: boolean;
|
|
45
|
+
/** Attach to the element whose exit animation ends the presence. */
|
|
46
|
+
presenceRef: RefObject<Element | null>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The longest `delay + duration` across every animation on the
|
|
50
|
+
* element, or `0` when it has none. CSS repeats the shorter of the two
|
|
51
|
+
* lists to match the longer, so the delays are indexed the same way.
|
|
52
|
+
*/
|
|
53
|
+
export declare const getAnimationLengthMs: (element: Element) => number;
|
|
54
|
+
/**
|
|
55
|
+
* The longest transition on `element`, delay included, in
|
|
56
|
+
* milliseconds. Zero under reduced motion, in the frozen suites, and
|
|
57
|
+
* in a DOM with no CSS engine — the same three cases as
|
|
58
|
+
* `getAnimationLengthMs`.
|
|
59
|
+
*/
|
|
60
|
+
export declare const getTransitionLengthMs: (element: Element) => number;
|
|
61
|
+
export declare const usePresence: <Element extends HTMLElement = HTMLElement>(isVisible: boolean) => UsePresenceResult<Element>;
|
|
62
|
+
/**
|
|
63
|
+
* The enter and exit classes for a panel that scales from its anchor:
|
|
64
|
+
* a modal box, a popover, a menu, a listbox. Defined in `styles.css`
|
|
65
|
+
* on the `--duration-*` and `--easing-*` tokens, and switched off
|
|
66
|
+
* under `prefers-reduced-motion`.
|
|
67
|
+
*/
|
|
68
|
+
export declare const toOverlayMotionClass: (isExiting: boolean) => string;
|
|
69
|
+
/** The same, for things that only fade: the scrim and a tooltip. */
|
|
70
|
+
export declare const toFadeMotionClass: (isExiting: boolean) => string;
|
|
71
|
+
/**
|
|
72
|
+
* Where an anchored panel grows from: the edge that faces its
|
|
73
|
+
* trigger. A menu below its button scales down out of the button, and
|
|
74
|
+
* one flipped above it scales up out of it.
|
|
75
|
+
*
|
|
76
|
+
* Only the side is used, centred on the cross axis. The alignment half
|
|
77
|
+
* of a placement (`-start`, `-end`) is logical and flips under RTL,
|
|
78
|
+
* and a 4% scale is too small for the cross-axis origin to be seen.
|
|
79
|
+
*/
|
|
80
|
+
export declare const toTransformOrigin: (placement: string) => string;
|
|
81
|
+
//# sourceMappingURL=usePresence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePresence.d.ts","sourceRoot":"","sources":["../../src/Motion/usePresence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAGtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,MAAM,iBAAiB,CAC3B,OAAO,SAAS,WAAW,GAAG,WAAW,IACvC;IACF,uEAAuE;IACvE,SAAS,EAAE,OAAO,CAAA;IAClB,4CAA4C;IAC5C,SAAS,EAAE,OAAO,CAAA;IAClB,oEAAoE;IACpE,WAAW,EAAE,SAAS,CAAC,OAAO,GAAG,IAAI,CAAC,CAAA;CACvC,CAAA;AAsBD;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAC/B,SAAS,OAAO,KACf,MA0BF,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAChC,SAAS,OAAO,KACf,MAoBF,CAAA;AAED,eAAO,MAAM,WAAW,GACtB,OAAO,SAAS,WAAW,GAAG,WAAW,EAEzC,WAAW,OAAO,KACjB,iBAAiB,CAAC,OAAO,CAiE3B,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAC/B,WAAW,OAAO,KACjB,MAG8B,CAAA;AAEjC,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,GAC5B,WAAW,OAAO,KACjB,MAG2B,CAAA;AAE9B;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,GAC5B,WAAW,MAAM,KAChB,MAWF,CAAA"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* If `animationend` never arrives — the tab is hidden, or a consumer
|
|
4
|
+
* class replaced the animation with one that never ends — the
|
|
5
|
+
* element is removed anyway. This is a safety margin past the
|
|
6
|
+
* element's own computed length, not a design duration.
|
|
7
|
+
*/
|
|
8
|
+
const EXIT_SAFETY_MARGIN_MS = 50;
|
|
9
|
+
const toMilliseconds = (value) => {
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
const amount = Number.parseFloat(trimmed);
|
|
12
|
+
if (Number.isNaN(amount)) {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
return trimmed.endsWith("ms") ? amount : amount * 1000;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The longest `delay + duration` across every animation on the
|
|
19
|
+
* element, or `0` when it has none. CSS repeats the shorter of the two
|
|
20
|
+
* lists to match the longer, so the delays are indexed the same way.
|
|
21
|
+
*/
|
|
22
|
+
export const getAnimationLengthMs = (element) => {
|
|
23
|
+
const style = globalThis.getComputedStyle(element);
|
|
24
|
+
const names = style.animationName.split(",");
|
|
25
|
+
const durations = style.animationDuration.split(",");
|
|
26
|
+
const delays = style.animationDelay.split(",");
|
|
27
|
+
return names.reduce((longest, name, index) => {
|
|
28
|
+
if (name.trim() === "none" || name.trim() === "") {
|
|
29
|
+
return longest;
|
|
30
|
+
}
|
|
31
|
+
const duration = toMilliseconds(durations[index % durations.length] ?? "0s");
|
|
32
|
+
const delay = toMilliseconds(delays[index % delays.length] ?? "0s");
|
|
33
|
+
return duration === 0
|
|
34
|
+
? longest
|
|
35
|
+
: Math.max(longest, duration + Math.max(delay, 0));
|
|
36
|
+
}, 0);
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The longest transition on `element`, delay included, in
|
|
40
|
+
* milliseconds. Zero under reduced motion, in the frozen suites, and
|
|
41
|
+
* in a DOM with no CSS engine — the same three cases as
|
|
42
|
+
* `getAnimationLengthMs`.
|
|
43
|
+
*/
|
|
44
|
+
export const getTransitionLengthMs = (element) => {
|
|
45
|
+
const style = globalThis.getComputedStyle(element);
|
|
46
|
+
const durations = (style.transitionDuration ?? "").split(",");
|
|
47
|
+
const delays = (style.transitionDelay ?? "").split(",");
|
|
48
|
+
return durations.reduce((longest, value, index) => {
|
|
49
|
+
const duration = toMilliseconds(value);
|
|
50
|
+
const delay = toMilliseconds(delays[index % delays.length] ?? "0s");
|
|
51
|
+
return duration === 0
|
|
52
|
+
? longest
|
|
53
|
+
: Math.max(longest, duration + Math.max(delay, 0));
|
|
54
|
+
}, 0);
|
|
55
|
+
};
|
|
56
|
+
export const usePresence = (isVisible) => {
|
|
57
|
+
// Whether the element is still on screen. It lags `isVisible` only
|
|
58
|
+
// on the way out, by exactly the length of the exit animation.
|
|
59
|
+
const [isShown, setIsShown] = useState(isVisible);
|
|
60
|
+
const presenceRef = useRef(null);
|
|
61
|
+
const isExiting = !isVisible && isShown;
|
|
62
|
+
useLayoutEffect(() => {
|
|
63
|
+
if (isVisible) {
|
|
64
|
+
setIsShown(true);
|
|
65
|
+
}
|
|
66
|
+
}, [isVisible]);
|
|
67
|
+
useLayoutEffect(() => {
|
|
68
|
+
if (!isExiting) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const element = presenceRef.current;
|
|
72
|
+
const lengthMs = element === null ? 0 : getAnimationLengthMs(element);
|
|
73
|
+
if (element === null || lengthMs === 0) {
|
|
74
|
+
setIsShown(false);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const finish = () => {
|
|
78
|
+
setIsShown(false);
|
|
79
|
+
};
|
|
80
|
+
const onAnimationEnd = (event) => {
|
|
81
|
+
// A child's own animation (a spinner inside a dialog) bubbles
|
|
82
|
+
// here too, and must not end the parent's exit.
|
|
83
|
+
if (event.target === element) {
|
|
84
|
+
finish();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
element.addEventListener("animationend", onAnimationEnd);
|
|
88
|
+
const timer = globalThis.setTimeout(finish, lengthMs + EXIT_SAFETY_MARGIN_MS);
|
|
89
|
+
return () => {
|
|
90
|
+
element.removeEventListener("animationend", onAnimationEnd);
|
|
91
|
+
globalThis.clearTimeout(timer);
|
|
92
|
+
};
|
|
93
|
+
}, [isExiting]);
|
|
94
|
+
return {
|
|
95
|
+
isExiting,
|
|
96
|
+
isMounted: isVisible || isShown,
|
|
97
|
+
presenceRef,
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* The enter and exit classes for a panel that scales from its anchor:
|
|
102
|
+
* a modal box, a popover, a menu, a listbox. Defined in `styles.css`
|
|
103
|
+
* on the `--duration-*` and `--easing-*` tokens, and switched off
|
|
104
|
+
* under `prefers-reduced-motion`.
|
|
105
|
+
*/
|
|
106
|
+
export const toOverlayMotionClass = (isExiting) => isExiting
|
|
107
|
+
? "charcuterie-overlay-exit"
|
|
108
|
+
: "charcuterie-overlay-enter";
|
|
109
|
+
/** The same, for things that only fade: the scrim and a tooltip. */
|
|
110
|
+
export const toFadeMotionClass = (isExiting) => isExiting
|
|
111
|
+
? "charcuterie-fade-exit"
|
|
112
|
+
: "charcuterie-fade-enter";
|
|
113
|
+
/**
|
|
114
|
+
* Where an anchored panel grows from: the edge that faces its
|
|
115
|
+
* trigger. A menu below its button scales down out of the button, and
|
|
116
|
+
* one flipped above it scales up out of it.
|
|
117
|
+
*
|
|
118
|
+
* Only the side is used, centred on the cross axis. The alignment half
|
|
119
|
+
* of a placement (`-start`, `-end`) is logical and flips under RTL,
|
|
120
|
+
* and a 4% scale is too small for the cross-axis origin to be seen.
|
|
121
|
+
*/
|
|
122
|
+
export const toTransformOrigin = (placement) => {
|
|
123
|
+
const [side] = placement.split("-");
|
|
124
|
+
return ({
|
|
125
|
+
bottom: "center top",
|
|
126
|
+
left: "right center",
|
|
127
|
+
right: "left center",
|
|
128
|
+
top: "center bottom",
|
|
129
|
+
}[side ?? "bottom"] ?? "center top");
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=usePresence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePresence.js","sourceRoot":"","sources":["../../src/Motion/usePresence.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAmDzD;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,EAAE,CAAA;AAEhC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAU,EAAE;IAC/C,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;IAE5B,MAAM,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAA;IAEzC,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QACzB,OAAO,CAAC,CAAA;IACV,CAAC;IAED,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC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AC,MAAM,iBAAiB,GAAG,CAC/B,SAAkB,EACV,EAAE,CACV,SAAS;IACP,CAAC,CAAC,uBAAuB;IACzB,CAAC,CAAC,wBAAwB,CAAA;AAE9B;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,SAAiB,EACT,EAAE;IACV,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEnC,OAAO,CACL;QACE,MAAM,EAAE,YAAY;QACpB,IAAI,EAAE,cAAc;QACpB,KAAK,EAAE,aAAa;QACpB,GAAG,EAAE,eAAe;KACrB,CAAC,IAAI,IAAI,QAAQ,CAAC,IAAI,YAAY,CACpC,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { AnimationEvent } from "react";
|
|
2
|
+
export type ValueChangeMotionProps = {
|
|
3
|
+
/** The pop class while the pop runs; `undefined` otherwise. */
|
|
4
|
+
className: string | undefined;
|
|
5
|
+
onAnimationEnd: (event: AnimationEvent<HTMLElement>) => void;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Props that make an element pop briefly each time `value` changes: a
|
|
9
|
+
* balance after a purchase, a count after an add, a status after a
|
|
10
|
+
* save. They show the user which part of the screen their action
|
|
11
|
+
* changed.
|
|
12
|
+
*
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const changeMotion = useValueChangeMotion(balance.spendable)
|
|
15
|
+
*
|
|
16
|
+
* <div
|
|
17
|
+
* {...changeMotion}
|
|
18
|
+
* className={toClassName("inline-block text-3xl tabular-nums", changeMotion.className)}
|
|
19
|
+
* >
|
|
20
|
+
* {formatPoints(balance.spendable)}
|
|
21
|
+
* </div>
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* ### What counts as a change
|
|
25
|
+
*
|
|
26
|
+
* The first value does not pop, and neither does the first real value
|
|
27
|
+
* after `null` or `undefined` — that is data arriving, not an action
|
|
28
|
+
* changing it. Values are compared with `Object.is`, so pass the
|
|
29
|
+
* number or string that is shown, not a fresh object.
|
|
30
|
+
*
|
|
31
|
+
* ### Why the class comes off, and why there are two
|
|
32
|
+
*
|
|
33
|
+
* The class is removed when its animation ends. An element that goes
|
|
34
|
+
* from `display: none` back to shown restarts its CSS animation, so a
|
|
35
|
+
* class left on would pop again each time a hidden tab is shown, for
|
|
36
|
+
* a change made long ago.
|
|
37
|
+
*
|
|
38
|
+
* A CSS animation restarts only when its name changes. A change that
|
|
39
|
+
* arrives while the last pop is still running switches to a second
|
|
40
|
+
* class, with identical keyframes under another name, so two
|
|
41
|
+
* purchases in a row pop twice.
|
|
42
|
+
*
|
|
43
|
+
* ### On the element
|
|
44
|
+
*
|
|
45
|
+
* Spread the props onto the element that shows the value. If it has
|
|
46
|
+
* its own `onAnimationEnd`, call both. The pop animates the `scale`
|
|
47
|
+
* property, which does nothing on a plain inline element: give the
|
|
48
|
+
* element `inline-block` or `block` layout. Under reduced motion the
|
|
49
|
+
* class has no animation and the new value is simply shown.
|
|
50
|
+
*/
|
|
51
|
+
export declare const useValueChangeMotion: (value: unknown) => ValueChangeMotionProps;
|
|
52
|
+
//# sourceMappingURL=useValueChangeMotion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useValueChangeMotion.d.ts","sourceRoot":"","sources":["../../src/Motion/useValueChangeMotion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAA;AAQ3C,MAAM,MAAM,sBAAsB,GAAG;IACnC,+DAA+D;IAC/D,SAAS,EAAE,MAAM,GAAG,SAAS,CAAA;IAC7B,cAAc,EAAE,CACd,KAAK,EAAE,cAAc,CAAC,WAAW,CAAC,KAC/B,IAAI,CAAA;CACV,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,eAAO,MAAM,oBAAoB,GAC/B,OAAO,OAAO,KACb,sBA+CF,CAAA"}
|