@gridui/motion 0.0.0-stage → 1.0.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 (40) hide show
  1. package/README.md +257 -2
  2. package/dist/chunk-MMMTUJVS.js +106 -0
  3. package/dist/chunk-MMMTUJVS.js.map +7 -0
  4. package/dist/index.d.ts +25 -0
  5. package/dist/index.js +682 -0
  6. package/dist/index.js.map +7 -0
  7. package/dist/layout-motion/layout-motion.d.ts +20 -0
  8. package/dist/motion-config/context.d.ts +2 -0
  9. package/dist/motion-config/motion-config.d.ts +2 -0
  10. package/dist/motion-config/types.d.ts +27 -0
  11. package/dist/motion-config/validate-transition.d.ts +2 -0
  12. package/dist/motion-layout-group/context.d.ts +1 -0
  13. package/dist/motion-layout-group/motion-layout-group.d.ts +6 -0
  14. package/dist/motion-reveal/motion-reveal.d.ts +9 -0
  15. package/dist/motion-scope/motion-scope.d.ts +6 -0
  16. package/dist/motion-sequence/controller.d.ts +37 -0
  17. package/dist/motion-sequence/use-motion-sequence.d.ts +4 -0
  18. package/dist/motion-size/motion-size.d.ts +8 -0
  19. package/dist/policy.d.ts +4 -0
  20. package/dist/policy.js +15 -0
  21. package/dist/policy.js.map +7 -0
  22. package/dist/presence/presence.d.ts +9 -0
  23. package/dist/shared/dom-policy.d.ts +13 -0
  24. package/dist/shared/outcome.d.ts +1 -0
  25. package/dist/shared/resolve-transition.d.ts +9 -0
  26. package/dist/shared/state.d.ts +6 -0
  27. package/dist/shared/use-motion-policy.d.ts +10 -0
  28. package/dist/shared/use-motion-style.d.ts +3 -0
  29. package/dist/spring-value/controller.d.ts +26 -0
  30. package/dist/spring-value/use-spring-value.d.ts +3 -0
  31. package/docs/catalog.json +233 -0
  32. package/docs/reference/README.md +10 -0
  33. package/docs/reference/layout-motion.md +116 -0
  34. package/docs/reference/motion-config.md +46 -0
  35. package/docs/reference/motion-layout-group.md +24 -0
  36. package/docs/reference/motion-reveal.md +110 -0
  37. package/docs/reference/motion-scope.md +60 -0
  38. package/docs/reference/motion-size.md +64 -0
  39. package/docs/reference/presence.md +42 -0
  40. package/package.json +50 -3
package/dist/index.js ADDED
@@ -0,0 +1,682 @@
1
+ import {
2
+ MotionContext,
3
+ observeMotionPolicy,
4
+ readMotionDuration,
5
+ readMotionPolicy,
6
+ useMotionPolicy,
7
+ useMotionStyle
8
+ } from "./chunk-MMMTUJVS.js";
9
+
10
+ // src/motion-config/motion-config.tsx
11
+ import { useContext, useMemo } from "react";
12
+
13
+ // src/motion-config/validate-transition.ts
14
+ var validateTransition = (transition) => {
15
+ if (transition.type === "spring") {
16
+ for (const value of [transition.stiffness, transition.damping, transition.mass]) {
17
+ if (value !== void 0 && (!Number.isFinite(value) || value <= 0)) {
18
+ throw new RangeError("Motion spring parameters must be finite and positive.");
19
+ }
20
+ }
21
+ } else if (transition.duration !== void 0 && (!Number.isFinite(transition.duration) || transition.duration < 0)) {
22
+ throw new RangeError("Motion duration must be finite and nonnegative.");
23
+ }
24
+ };
25
+
26
+ // src/motion-config/motion-config.tsx
27
+ import { jsx } from "react/jsx-runtime";
28
+ var MotionConfig = ({ children, tempo, reducedMotion, disabled, transition }) => {
29
+ const parent = useContext(MotionContext);
30
+ if (tempo !== void 0 && (!Number.isFinite(tempo) || tempo <= 0)) {
31
+ throw new RangeError("MotionConfig tempo must be finite and positive.");
32
+ }
33
+ if (transition) {
34
+ validateTransition(transition);
35
+ }
36
+ const value = useMemo(() => ({
37
+ tempo: tempo ?? parent.tempo,
38
+ reducedMotion: parent.reducedMotion === "always" ? "always" : reducedMotion ?? parent.reducedMotion,
39
+ disabled: parent.disabled || disabled === true,
40
+ transition: transition ?? parent.transition
41
+ }), [parent, tempo, reducedMotion, disabled, transition]);
42
+ return /* @__PURE__ */ jsx(MotionContext.Provider, { value, children });
43
+ };
44
+
45
+ // src/motion-layout-group/context.ts
46
+ import { createContext } from "react";
47
+ var LayoutGroupContext = createContext(false);
48
+
49
+ // src/presence/presence.tsx
50
+ import { AnimatePresence, LayoutGroup } from "motion/react";
51
+ import { useContext as useContext2, useId } from "react";
52
+ import { jsx as jsx2 } from "react/jsx-runtime";
53
+ var Presence = ({ children, mode = "sync", initial = false, onExitComplete }) => {
54
+ const id = useId();
55
+ const grouped = useContext2(LayoutGroupContext);
56
+ const presence = /* @__PURE__ */ jsx2(AnimatePresence, { mode, initial, ...onExitComplete ? { onExitComplete } : {}, children });
57
+ return grouped ? presence : /* @__PURE__ */ jsx2(LayoutGroup, { id, children: presence });
58
+ };
59
+
60
+ // src/layout-motion/layout-motion.tsx
61
+ import { animate as animateValue, motion, useMotionValue, usePresence } from "motion/react";
62
+ import { useImperativeHandle, useEffect, useEffectEvent, useLayoutEffect, useMemo as useMemo2, useRef, useState } from "react";
63
+
64
+ // src/shared/resolve-transition.ts
65
+ var fallbackTransition = { duration: 180, ease: [0.2, 0.8, 0.2, 1] };
66
+ var readThemeTransition = (element) => {
67
+ const style = getComputedStyle(element);
68
+ const duration = readMotionDuration(element);
69
+ const rawEase = style.getPropertyValue("--gridui-motion-ease-out").trim();
70
+ const values = /^cubic-bezier\(([^)]+)\)$/.exec(rawEase)?.[1]?.split(",").map(Number);
71
+ let ease = fallbackTransition.ease;
72
+ if (values?.length === 4 && values.every(Number.isFinite) && values[0] >= 0 && values[0] <= 1 && values[2] >= 0 && values[2] <= 1) {
73
+ ease = [values[0], values[1], values[2], values[3]];
74
+ } else if (rawEase === "linear") {
75
+ ease = "linear";
76
+ } else if (rawEase === "ease-in") {
77
+ ease = "easeIn";
78
+ } else if (rawEase === "ease-out") {
79
+ ease = "easeOut";
80
+ } else if (rawEase === "ease-in-out") {
81
+ ease = "easeInOut";
82
+ }
83
+ return { duration, ease };
84
+ };
85
+ var resolveTransition = (transition, theme, tempo, immediate) => {
86
+ if (transition) {
87
+ validateTransition(transition);
88
+ }
89
+ if (immediate) {
90
+ return { type: "tween", duration: 0 };
91
+ }
92
+ if (transition?.type === "spring") {
93
+ return {
94
+ type: "spring",
95
+ stiffness: (transition.stiffness ?? 170) / (tempo * tempo),
96
+ damping: (transition.damping ?? 26) / tempo,
97
+ mass: transition.mass ?? 1
98
+ };
99
+ }
100
+ return {
101
+ type: "tween",
102
+ duration: (transition?.duration ?? theme.duration) * tempo / 1e3,
103
+ ease: transition?.ease ?? theme.ease
104
+ };
105
+ };
106
+
107
+ // src/layout-motion/layout-motion.tsx
108
+ import { jsx as jsx3 } from "react/jsx-runtime";
109
+ var LayoutMotion = ({
110
+ layout = true,
111
+ layoutId,
112
+ layoutScroll,
113
+ layoutRoot,
114
+ enter,
115
+ animate: animate2,
116
+ exit,
117
+ transition,
118
+ exitTransition,
119
+ ref,
120
+ inert,
121
+ style,
122
+ onAnimationComplete,
123
+ "aria-hidden": ariaHidden,
124
+ ...props
125
+ }) => {
126
+ const policy = useMotionPolicy();
127
+ const [present, safeToRemove] = usePresence();
128
+ const node = useRef(null);
129
+ const running = useRef([]);
130
+ const [theme, setTheme] = useState(fallbackTransition);
131
+ useImperativeHandle(ref, () => node.current, []);
132
+ useLayoutEffect(() => {
133
+ const next = readThemeTransition(node.current);
134
+ setTheme((previous) => next.duration === previous.duration && String(next.ease) === String(previous.ease) ? previous : next);
135
+ }, [style, props.className, policy.transition, transition]);
136
+ const opacity = animate2?.opacity ?? 1;
137
+ const x = animate2?.x ?? 0;
138
+ const y = animate2?.y ?? 0;
139
+ const scale = animate2?.scale ?? 1;
140
+ const opacityValue = useMotionValue(opacity);
141
+ const xValue = useMotionValue(x);
142
+ const yValue = useMotionValue(y);
143
+ const scaleValue = useMotionValue(scale);
144
+ const values = useMemo2(
145
+ () => ({ opacity: opacityValue, x: xValue, y: yValue, scale: scaleValue }),
146
+ [opacityValue, xValue, yValue, scaleValue]
147
+ );
148
+ const exitOpacity = exit?.opacity;
149
+ const exitX = exit?.x;
150
+ const exitY = exit?.y;
151
+ const exitScale = exit?.scale;
152
+ const hasExit = exit !== void 0;
153
+ const settings = useEffectEvent(() => ({
154
+ timing: resolveTransition(
155
+ (!present ? exitTransition : void 0) ?? transition ?? policy.transition,
156
+ readThemeTransition(node.current),
157
+ 1,
158
+ policy.immediate
159
+ ),
160
+ immediate: policy.immediate,
161
+ tempo: policy.tempo
162
+ }));
163
+ const complete = useEffectEvent(() => onAnimationComplete?.());
164
+ useEffect(() => {
165
+ const { timing, immediate, tempo } = settings();
166
+ const target = present ? { opacity, x, y, scale } : {
167
+ opacity: exitOpacity,
168
+ x: exitX,
169
+ y: exitY,
170
+ scale: exitScale
171
+ };
172
+ let active = true;
173
+ const playback = [];
174
+ for (const key of ["opacity", "x", "y", "scale"]) {
175
+ const next = target[key];
176
+ if (next === void 0 || next === values[key].get()) {
177
+ continue;
178
+ }
179
+ if (immediate) {
180
+ values[key].jump(next);
181
+ continue;
182
+ }
183
+ const animation = animateValue(values[key], next, timing);
184
+ animation.speed = 1 / tempo;
185
+ playback.push(animation);
186
+ }
187
+ running.current = playback;
188
+ if (playback.length) {
189
+ void Promise.all(playback).then(() => {
190
+ if (!active) {
191
+ return;
192
+ }
193
+ running.current = [];
194
+ complete();
195
+ if (!present) {
196
+ safeToRemove();
197
+ }
198
+ });
199
+ } else if (!present) {
200
+ safeToRemove();
201
+ }
202
+ return () => {
203
+ active = false;
204
+ for (const animation of playback) {
205
+ animation.stop();
206
+ }
207
+ running.current = [];
208
+ };
209
+ }, [present, hasExit, safeToRemove, values, opacity, x, y, scale, exitOpacity, exitX, exitY, exitScale]);
210
+ useLayoutEffect(() => {
211
+ for (const animation of running.current) {
212
+ if (policy.immediate) {
213
+ animation.complete();
214
+ } else {
215
+ animation.speed = 1 / policy.tempo;
216
+ }
217
+ }
218
+ }, [policy.immediate, policy.tempo]);
219
+ return /* @__PURE__ */ jsx3(
220
+ motion.div,
221
+ {
222
+ ...props,
223
+ ref: node,
224
+ style: { ...style, ...values },
225
+ inert: !present || inert || void 0,
226
+ "aria-hidden": !present ? true : ariaHidden,
227
+ "data-motion-present": present ? "true" : "false",
228
+ layout: policy.immediate ? false : layout,
229
+ ...layoutScroll === void 0 ? {} : { layoutScroll },
230
+ ...layoutRoot === void 0 ? {} : { layoutRoot },
231
+ ...policy.immediate ? { transformTemplate: () => {
232
+ return x === 0 && y === 0 && scale === 1 ? "none" : `translate(${x}px, ${y}px) scale(${scale})`;
233
+ } } : {},
234
+ ...layoutId === void 0 ? {} : { layoutId },
235
+ initial: policy.immediate ? false : enter ?? false,
236
+ transition: resolveTransition(transition ?? policy.transition, theme, policy.tempo, policy.immediate)
237
+ }
238
+ );
239
+ };
240
+
241
+ // src/motion-scope/motion-scope.tsx
242
+ import { Slot } from "@radix-ui/react-slot";
243
+ import { jsx as jsx4 } from "react/jsx-runtime";
244
+ var MotionScope = ({ asChild = false, style, ...props }) => {
245
+ const motionStyle = useMotionStyle(style);
246
+ const Component = asChild ? Slot : "div";
247
+ return /* @__PURE__ */ jsx4(Component, { ...props, style: motionStyle });
248
+ };
249
+
250
+ // src/motion-layout-group/motion-layout-group.tsx
251
+ import { LayoutGroup as LayoutGroup2 } from "motion/react";
252
+ import { useId as useId2 } from "react";
253
+ import { jsx as jsx5 } from "react/jsx-runtime";
254
+ var MotionLayoutGroup = ({ children }) => {
255
+ const id = useId2();
256
+ return /* @__PURE__ */ jsx5(LayoutGroupContext.Provider, { value: true, children: /* @__PURE__ */ jsx5(LayoutGroup2, { id, children }) });
257
+ };
258
+
259
+ // src/spring-value/use-spring-value.ts
260
+ import { useLayoutEffect as useLayoutEffect2, useState as useState2 } from "react";
261
+
262
+ // src/spring-value/controller.ts
263
+ import { animate, motionValue } from "motion/react";
264
+ var finite = (value) => {
265
+ if (!Number.isFinite(value)) {
266
+ throw new RangeError("Spring values and velocities must be finite.");
267
+ }
268
+ };
269
+ var createSpringValue = (initial, initialOptions = {}, initialPolicy = { tempo: 1, immediate: false }) => {
270
+ finite(initial);
271
+ const value = motionValue(initial);
272
+ let disposed = false;
273
+ const assertActive = () => {
274
+ if (disposed) {
275
+ throw new Error("Cannot use a disposed spring value.");
276
+ }
277
+ };
278
+ let options = {};
279
+ let tempo = 1;
280
+ let immediate = true;
281
+ let target = initial;
282
+ let pending;
283
+ let run;
284
+ const getVelocity = () => !run ? 0 : run.animation.time === 0 ? run.initialVelocity * run.startTempo / tempo : value.getVelocity();
285
+ const stop = () => {
286
+ const previous = run;
287
+ run = void 0;
288
+ previous?.animation.stop();
289
+ previous?.resolve("cancelled");
290
+ };
291
+ const jump = (next) => {
292
+ assertActive();
293
+ finite(next);
294
+ stop();
295
+ target = next;
296
+ value.jump(next);
297
+ };
298
+ const api = {
299
+ get: () => value.get(),
300
+ getVelocity,
301
+ subscribe: (changed) => {
302
+ assertActive();
303
+ return value.on("change", changed);
304
+ },
305
+ stop,
306
+ jump,
307
+ set: (next, velocity) => {
308
+ assertActive();
309
+ finite(next);
310
+ if (run && next === target && velocity === void 0) {
311
+ return pending;
312
+ }
313
+ const releaseVelocity = velocity ?? getVelocity();
314
+ finite(releaseVelocity);
315
+ stop();
316
+ target = next;
317
+ if (immediate || value.get() === next && releaseVelocity === 0) {
318
+ value.jump(next);
319
+ return Promise.resolve("finished");
320
+ }
321
+ const { precision, ...spring } = options;
322
+ const animation = animate(value, next, {
323
+ type: "spring",
324
+ stiffness: 170,
325
+ damping: 26,
326
+ mass: 1,
327
+ ...spring,
328
+ velocity: releaseVelocity * tempo,
329
+ ...precision === void 0 ? {} : { restDelta: precision, restSpeed: precision * tempo }
330
+ });
331
+ animation.speed = 1 / tempo;
332
+ pending = new Promise((resolve) => {
333
+ const current = { animation, initialVelocity: releaseVelocity, startTempo: tempo, resolve };
334
+ run = current;
335
+ void animation.then(() => {
336
+ if (run !== current) {
337
+ return;
338
+ }
339
+ run = void 0;
340
+ resolve("finished");
341
+ });
342
+ });
343
+ return pending;
344
+ }
345
+ };
346
+ const configure = (next, policy) => {
347
+ assertActive();
348
+ validateTransition({ type: "spring", ...next });
349
+ if (!Number.isFinite(policy.tempo) || policy.tempo <= 0 || next.precision !== void 0 && (!Number.isFinite(next.precision) || next.precision <= 0)) {
350
+ throw new RangeError("Spring tempo and precision must be finite and positive.");
351
+ }
352
+ options = next;
353
+ tempo = policy.tempo;
354
+ immediate = policy.immediate;
355
+ if (run) {
356
+ if (immediate) {
357
+ const previous = run;
358
+ run = void 0;
359
+ previous.animation.stop();
360
+ value.jump(target);
361
+ previous.resolve("finished");
362
+ } else {
363
+ run.animation.speed = 1 / tempo;
364
+ }
365
+ }
366
+ };
367
+ configure(initialOptions, initialPolicy);
368
+ return { ...api, configure, dispose: () => {
369
+ if (disposed) {
370
+ return;
371
+ }
372
+ disposed = true;
373
+ stop();
374
+ value.destroy();
375
+ } };
376
+ };
377
+
378
+ // src/spring-value/use-spring-value.ts
379
+ var useSpringValue = (initial, options) => {
380
+ const policy = useMotionPolicy();
381
+ const { tempo, immediate } = policy;
382
+ const [controller] = useState2(() => createSpringValue(initial));
383
+ const spring = options ?? (policy.transition?.type === "spring" ? policy.transition : void 0);
384
+ const { stiffness, damping, mass, precision } = spring ?? {};
385
+ useLayoutEffect2(() => {
386
+ controller.configure({
387
+ ...stiffness === void 0 ? {} : { stiffness },
388
+ ...damping === void 0 ? {} : { damping },
389
+ ...mass === void 0 ? {} : { mass },
390
+ ...precision === void 0 ? {} : { precision }
391
+ }, { tempo, immediate });
392
+ }, [controller, stiffness, damping, mass, precision, tempo, immediate]);
393
+ useLayoutEffect2(() => () => void controller.stop(), [controller]);
394
+ return controller;
395
+ };
396
+
397
+ // src/motion-size/motion-size.tsx
398
+ import { useImperativeHandle as useImperativeHandle2, useLayoutEffect as useLayoutEffect3, useRef as useRef2 } from "react";
399
+ import { jsx as jsx6 } from "react/jsx-runtime";
400
+ var MotionSize = ({ children, spring, ref, style, ...props }) => {
401
+ const outer = useRef2(null);
402
+ const inner = useRef2(null);
403
+ const height = useSpringValue(0, spring);
404
+ useImperativeHandle2(ref, () => outer.current, []);
405
+ useLayoutEffect3(() => {
406
+ const node = outer.current;
407
+ const content = inner.current;
408
+ let revision = 0;
409
+ height.jump(content.offsetHeight);
410
+ let requested = height.get();
411
+ const unsubscribe = height.subscribe((value) => {
412
+ node.style.height = `${Math.max(0, value)}px`;
413
+ });
414
+ const observer = new ResizeObserver(([entry]) => {
415
+ const target = entry?.borderBoxSize[0]?.blockSize ?? content.offsetHeight;
416
+ if (target === requested) {
417
+ return;
418
+ }
419
+ requested = target;
420
+ const run = ++revision;
421
+ node.style.height = `${height.get()}px`;
422
+ node.style.overflow = "clip";
423
+ void height.set(target).then((result) => {
424
+ if (result === "finished" && run === revision) {
425
+ node.style.height = "auto";
426
+ node.style.removeProperty("overflow");
427
+ }
428
+ });
429
+ });
430
+ observer.observe(content);
431
+ return () => {
432
+ revision++;
433
+ observer.disconnect();
434
+ unsubscribe();
435
+ height.stop();
436
+ node.style.removeProperty("height");
437
+ node.style.removeProperty("overflow");
438
+ };
439
+ }, [height]);
440
+ return /* @__PURE__ */ jsx6("div", { ...props, ref: outer, style, children: /* @__PURE__ */ jsx6("div", { ref: inner, style: { display: "flow-root" }, children }) });
441
+ };
442
+
443
+ // src/motion-sequence/use-motion-sequence.ts
444
+ import { useLayoutEffect as useLayoutEffect4, useState as useState3 } from "react";
445
+
446
+ // src/motion-sequence/controller.ts
447
+ var frames = (state) => ({
448
+ ...state.opacity === void 0 ? {} : { opacity: String(state.opacity) },
449
+ ...state.x === void 0 && state.y === void 0 && state.scale === void 0 ? {} : {
450
+ transform: `translate(${state.x ?? 0}px, ${state.y ?? 0}px) scale(${state.scale ?? 1})`
451
+ }
452
+ });
453
+ var easing = (ease) => Array.isArray(ease) ? `cubic-bezier(${ease.join(",")})` : { linear: "linear", easeIn: "ease-in", easeOut: "ease-out", easeInOut: "ease-in-out" }[ease];
454
+ var createSequenceController = (scope) => {
455
+ let policy = { tempo: 1, immediate: true, transition: void 0 };
456
+ let active;
457
+ const originals = /* @__PURE__ */ new Map();
458
+ const play = (steps, options = {}) => {
459
+ const stagger = options.stagger ?? 0;
460
+ if (!Number.isFinite(stagger) || stagger < 0) {
461
+ throw new RangeError("Stagger must be finite and nonnegative.");
462
+ }
463
+ const root = scope.current;
464
+ let offset = 0;
465
+ const previous = /* @__PURE__ */ new Map();
466
+ const plan = steps.map((step, index) => {
467
+ if (!root?.contains(step.element)) {
468
+ throw new RangeError("Sequence targets must belong to the mounted scope.");
469
+ }
470
+ const theme = readThemeTransition(step.element);
471
+ const base = policy.transition?.type === "spring" ? void 0 : policy.transition;
472
+ const duration = step.duration ?? base?.duration ?? theme.duration;
473
+ const ease = step.ease ?? base?.ease ?? theme.ease;
474
+ validateTransition({ duration, ease });
475
+ const to = frames(step.to);
476
+ const requestedFrom = step.from ? frames(step.from) : {};
477
+ const from = {};
478
+ const delay = options.order === "parallel" ? index * stagger : offset;
479
+ offset += duration + stagger;
480
+ const properties = previous.get(step.element) ?? /* @__PURE__ */ new Map();
481
+ const backwards = Object.keys(to).every((property) => !properties.has(property));
482
+ for (const property of Object.keys(to)) {
483
+ const last = properties.get(property);
484
+ if (last && delay < last.end) {
485
+ throw new RangeError("Parallel steps cannot own the same element property at the same time.");
486
+ }
487
+ const start = requestedFrom[property] ?? last?.value;
488
+ if (start !== void 0) {
489
+ from[property] = start;
490
+ }
491
+ properties.set(property, { end: delay + duration, value: to[property] });
492
+ }
493
+ previous.set(step.element, properties);
494
+ return { element: step.element, to, from, duration, ease: easing(ease), delay, backwards };
495
+ });
496
+ active?.run.cancel();
497
+ let resolve;
498
+ const finished = new Promise((done) => {
499
+ resolve = done;
500
+ });
501
+ const animations = [];
502
+ let settled = false;
503
+ const settle = (result) => {
504
+ if (settled) {
505
+ return;
506
+ }
507
+ settled = true;
508
+ if (result === "finished") {
509
+ for (const step of plan) {
510
+ for (const property of Object.keys(step.to)) {
511
+ step.element.style.setProperty(property, step.to[property]);
512
+ }
513
+ }
514
+ } else {
515
+ const current = /* @__PURE__ */ new Map();
516
+ for (const step of plan) {
517
+ const style = step.element.ownerDocument.defaultView.getComputedStyle(step.element);
518
+ const values = current.get(step.element) ?? {};
519
+ for (const property of Object.keys(step.to)) {
520
+ values[property] = style.getPropertyValue(property);
521
+ }
522
+ current.set(step.element, values);
523
+ }
524
+ for (const [element, values] of current) {
525
+ for (const property of Object.keys(values)) {
526
+ element.style.setProperty(property, values[property]);
527
+ }
528
+ }
529
+ }
530
+ for (const animation of animations) {
531
+ animation.cancel();
532
+ }
533
+ if (active?.run === run) {
534
+ active = void 0;
535
+ }
536
+ resolve(result);
537
+ };
538
+ const run = { finished, cancel: () => void settle("cancelled"), finish: () => void settle("finished") };
539
+ active = { run, animations };
540
+ try {
541
+ for (const step of plan) {
542
+ const saved = originals.get(step.element) ?? /* @__PURE__ */ new Map();
543
+ for (const property of Object.keys(step.to)) {
544
+ if (!saved.has(property)) {
545
+ saved.set(property, { value: step.element.style.getPropertyValue(property), priority: step.element.style.getPropertyPriority(property) });
546
+ }
547
+ }
548
+ originals.set(step.element, saved);
549
+ if (policy.immediate) {
550
+ continue;
551
+ }
552
+ const animation = step.element.animate([step.from, step.to], {
553
+ duration: step.duration,
554
+ delay: step.delay,
555
+ easing: step.ease,
556
+ fill: step.backwards ? "both" : "forwards"
557
+ });
558
+ animation.playbackRate = 1 / policy.tempo;
559
+ animations.push(animation);
560
+ }
561
+ void Promise.all(animations.map((animation) => animation.finished)).then(() => void run.finish(), () => void run.cancel());
562
+ if (policy.immediate || !plan.length) {
563
+ run.finish();
564
+ }
565
+ } catch (error) {
566
+ run.cancel();
567
+ throw error;
568
+ }
569
+ return run;
570
+ };
571
+ return {
572
+ api: { play },
573
+ configure: (next) => {
574
+ policy = next;
575
+ if (next.immediate) {
576
+ active?.run.finish();
577
+ } else {
578
+ for (const animation of active?.animations ?? []) {
579
+ animation.updatePlaybackRate(1 / next.tempo);
580
+ }
581
+ }
582
+ },
583
+ dispose: () => {
584
+ active?.run.cancel();
585
+ for (const [element, properties] of originals) {
586
+ for (const [property, original] of properties) {
587
+ element.style.setProperty(property, original.value, original.priority);
588
+ }
589
+ }
590
+ originals.clear();
591
+ }
592
+ };
593
+ };
594
+
595
+ // src/motion-sequence/use-motion-sequence.ts
596
+ var useMotionSequence = (scope) => {
597
+ const { tempo, immediate, transition } = useMotionPolicy();
598
+ const [controller] = useState3(() => createSequenceController(scope));
599
+ useLayoutEffect4(() => void controller.configure({ tempo, immediate, transition }), [controller, tempo, immediate, transition]);
600
+ useLayoutEffect4(() => () => void controller.dispose(), [controller]);
601
+ return controller.api;
602
+ };
603
+
604
+ // src/motion-reveal/motion-reveal.tsx
605
+ import { useEffect as useEffect2, useImperativeHandle as useImperativeHandle3, useRef as useRef3, useState as useState4 } from "react";
606
+ import { jsx as jsx7 } from "react/jsx-runtime";
607
+ var MotionReveal = ({
608
+ once = true,
609
+ amount = 0,
610
+ root = null,
611
+ rootMargin = "0px",
612
+ enter = { opacity: 0, y: 12 },
613
+ transition,
614
+ ref,
615
+ onFocusCapture,
616
+ ...props
617
+ }) => {
618
+ if (!Number.isFinite(amount) || amount < 0 || amount > 1) {
619
+ throw new RangeError("Reveal amount must be between zero and one.");
620
+ }
621
+ const node = useRef3(null);
622
+ const seen = useRef3(false);
623
+ const [visible, setVisible] = useState4(null);
624
+ const [focused, setFocused] = useState4(false);
625
+ const { immediate } = useMotionPolicy();
626
+ useImperativeHandle3(ref, () => node.current, []);
627
+ useEffect2(() => {
628
+ const element = node.current;
629
+ const Observer = element.ownerDocument.defaultView.IntersectionObserver;
630
+ if (immediate || focused || once && seen.current) {
631
+ return;
632
+ }
633
+ const observer = new Observer(([entry]) => {
634
+ if (!entry) {
635
+ return;
636
+ }
637
+ const inside = entry.isIntersecting && entry.intersectionRatio >= amount;
638
+ setVisible(inside);
639
+ if (inside) {
640
+ seen.current = true;
641
+ if (once) {
642
+ observer.disconnect();
643
+ }
644
+ }
645
+ }, { root, rootMargin, threshold: amount });
646
+ observer.observe(element);
647
+ return () => void observer.disconnect();
648
+ }, [immediate, focused, once, amount, root, rootMargin]);
649
+ const show = immediate || focused || visible !== false;
650
+ return /* @__PURE__ */ jsx7(
651
+ LayoutMotion,
652
+ {
653
+ ...props,
654
+ ref: node,
655
+ layout: false,
656
+ animate: show ? { opacity: 1, x: 0, y: 0, scale: 1 } : enter,
657
+ ...!show || focused ? { transition: { duration: 0 } } : transition ? { transition } : {},
658
+ onFocusCapture: (event) => {
659
+ setFocused(true);
660
+ onFocusCapture?.(event);
661
+ }
662
+ }
663
+ );
664
+ };
665
+ export {
666
+ LayoutMotion,
667
+ MotionConfig,
668
+ MotionLayoutGroup,
669
+ MotionReveal,
670
+ MotionScope,
671
+ MotionSize,
672
+ Presence,
673
+ createSpringValue,
674
+ observeMotionPolicy,
675
+ readMotionDuration,
676
+ readMotionPolicy,
677
+ useMotionPolicy,
678
+ useMotionSequence,
679
+ useMotionStyle,
680
+ useSpringValue
681
+ };
682
+ //# sourceMappingURL=index.js.map