@rootnative/inertia 0.0.0-alpha.0 → 0.0.0-alpha.2

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 (79) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -0
  3. package/dist/gestureLayer/index.d.mts +48 -7
  4. package/dist/gestureLayer/index.d.ts +48 -7
  5. package/dist/gestureLayer/index.js +107 -30
  6. package/dist/gestureLayer/index.mjs +107 -30
  7. package/dist/index.d.mts +127 -16
  8. package/dist/index.d.ts +127 -16
  9. package/dist/index.js +177 -62
  10. package/dist/index.mjs +175 -63
  11. package/dist/motion/Image.d.mts +1 -1
  12. package/dist/motion/Image.d.ts +1 -1
  13. package/dist/motion/Image.js +90 -50
  14. package/dist/motion/Image.mjs +90 -50
  15. package/dist/motion/Pressable.d.mts +1 -1
  16. package/dist/motion/Pressable.d.ts +1 -1
  17. package/dist/motion/Pressable.js +90 -50
  18. package/dist/motion/Pressable.mjs +90 -50
  19. package/dist/motion/ScrollView.d.mts +1 -1
  20. package/dist/motion/ScrollView.d.ts +1 -1
  21. package/dist/motion/ScrollView.js +90 -50
  22. package/dist/motion/ScrollView.mjs +90 -50
  23. package/dist/motion/Text.d.mts +1 -1
  24. package/dist/motion/Text.d.ts +1 -1
  25. package/dist/motion/Text.js +90 -50
  26. package/dist/motion/Text.mjs +90 -50
  27. package/dist/motion/View.d.mts +1 -1
  28. package/dist/motion/View.d.ts +1 -1
  29. package/dist/motion/View.js +90 -50
  30. package/dist/motion/View.mjs +90 -50
  31. package/dist/testing/index.js +0 -2
  32. package/dist/testing/index.mjs +0 -2
  33. package/dist/touch/index.d.mts +1 -1
  34. package/dist/touch/index.d.ts +1 -1
  35. package/dist/touch/index.js +0 -2
  36. package/dist/touch/index.mjs +0 -2
  37. package/dist/{types-cU43dEmH.d.mts → types-BzEgiUdJ.d.mts} +58 -9
  38. package/dist/{types-cU43dEmH.d.ts → types-BzEgiUdJ.d.ts} +58 -9
  39. package/dist/{useGesture-BnY65PlQ.d.ts → useGesture-BRLgiNOC.d.ts} +6 -4
  40. package/dist/{useGesture-DxtXdz-K.d.mts → useGesture-C0GBS7d2.d.mts} +6 -4
  41. package/llms.txt +9 -5
  42. package/package.json +2 -1
  43. package/src/config/MotionConfig.tsx +55 -9
  44. package/src/config/MotionConfigContext.ts +21 -1
  45. package/src/config/index.ts +7 -1
  46. package/src/config/namedTransitions.ts +80 -0
  47. package/src/gestureLayer/index.ts +1 -0
  48. package/src/gestureLayer/useGestureLayer.ts +92 -8
  49. package/src/index.ts +20 -2
  50. package/src/motion/createMotionComponent.tsx +36 -6
  51. package/src/transitions/cubicBezier.ts +126 -0
  52. package/src/transitions/index.ts +1 -0
  53. package/src/types.ts +63 -8
  54. package/src/values/useAnimation.ts +12 -6
  55. package/src/values/useBooleanSpring.ts +5 -3
  56. package/src/values/useGesture.ts +25 -7
  57. package/src/values/useMotionValue.ts +11 -0
  58. package/src/values/useScroll.ts +8 -1
  59. package/src/values/useSpring.ts +46 -9
  60. package/dist/gestureLayer/index.js.map +0 -1
  61. package/dist/gestureLayer/index.mjs.map +0 -1
  62. package/dist/index.js.map +0 -1
  63. package/dist/index.mjs.map +0 -1
  64. package/dist/motion/Image.js.map +0 -1
  65. package/dist/motion/Image.mjs.map +0 -1
  66. package/dist/motion/Pressable.js.map +0 -1
  67. package/dist/motion/Pressable.mjs.map +0 -1
  68. package/dist/motion/ScrollView.js.map +0 -1
  69. package/dist/motion/ScrollView.mjs.map +0 -1
  70. package/dist/motion/Text.js.map +0 -1
  71. package/dist/motion/Text.mjs.map +0 -1
  72. package/dist/motion/View.js.map +0 -1
  73. package/dist/motion/View.mjs.map +0 -1
  74. package/dist/testing/index.js.map +0 -1
  75. package/dist/testing/index.mjs.map +0 -1
  76. package/dist/touch/index.js.map +0 -1
  77. package/dist/touch/index.mjs.map +0 -1
  78. package/src/__type-tests__/animate.test-d.tsx +0 -88
  79. package/src/__type-tests__/variants.test-d.tsx +0 -67
package/dist/index.js CHANGED
@@ -11,8 +11,31 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
11
  var Animated__default = /*#__PURE__*/_interopDefault(Animated);
12
12
 
13
13
  // src/motion/Image.tsx
14
+
15
+ // src/transitions/sig.ts
16
+ function stableSig(value) {
17
+ if (value === void 0) return "";
18
+ try {
19
+ return stableStringify(value);
20
+ } catch {
21
+ return String(value);
22
+ }
23
+ }
24
+ function stableStringify(v) {
25
+ if (v === null || typeof v !== "object") {
26
+ if (typeof v === "function" || v === void 0) return "null";
27
+ return JSON.stringify(v);
28
+ }
29
+ if (Array.isArray(v)) {
30
+ return "[" + v.map(stableStringify).join(",") + "]";
31
+ }
32
+ const obj = v;
33
+ const keys = Object.keys(obj).sort();
34
+ return "{" + keys.map((k) => JSON.stringify(k) + ":" + stableStringify(obj[k])).join(",") + "}";
35
+ }
14
36
  var DEFAULT_MOTION_CONFIG = {
15
- reducedMotion: "user"
37
+ reducedMotion: "user",
38
+ transitions: {}
16
39
  };
17
40
  var MotionConfigContext = react.createContext(
18
41
  DEFAULT_MOTION_CONFIG
@@ -20,6 +43,9 @@ var MotionConfigContext = react.createContext(
20
43
  function useMotionConfig() {
21
44
  return react.useContext(MotionConfigContext);
22
45
  }
46
+ function useNamedTransitions() {
47
+ return useMotionConfig().transitions;
48
+ }
23
49
  function useShouldReduceMotion() {
24
50
  const { reducedMotion } = useMotionConfig();
25
51
  const osReduced = Animated.useReducedMotion();
@@ -28,15 +54,80 @@ function useShouldReduceMotion() {
28
54
  return osReduced;
29
55
  }
30
56
  function MotionConfig({
31
- reducedMotion = "user",
57
+ reducedMotion,
58
+ transitions,
32
59
  children
33
60
  }) {
61
+ const parent = useMotionConfig();
62
+ const transitionsSig = stableSig(transitions);
34
63
  const value = react.useMemo(
35
- () => ({ reducedMotion }),
36
- [reducedMotion]
64
+ () => ({
65
+ reducedMotion: reducedMotion ?? parent.reducedMotion,
66
+ transitions: transitions ? { ...parent.transitions, ...transitions } : parent.transitions
67
+ }),
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ [reducedMotion, transitionsSig, parent]
37
70
  );
38
71
  return /* @__PURE__ */ jsxRuntime.jsx(MotionConfigContext.Provider, { value, children });
39
72
  }
73
+
74
+ // src/transitions/keys.ts
75
+ var TRANSITION_CONFIG_KEYS = /* @__PURE__ */ new Set([
76
+ "type",
77
+ "tension",
78
+ "friction",
79
+ "mass",
80
+ "velocity",
81
+ "restSpeedThreshold",
82
+ "restDisplacementThreshold",
83
+ "duration",
84
+ "easing",
85
+ "delay",
86
+ "repeat",
87
+ "deceleration",
88
+ "clamp"
89
+ ]);
90
+ function isTopLevelTransition(t) {
91
+ if (t === null || typeof t !== "object") return false;
92
+ const keys = Object.keys(t);
93
+ if (keys.length === 0) return false;
94
+ return keys.every((k) => TRANSITION_CONFIG_KEYS.has(k));
95
+ }
96
+
97
+ // src/config/namedTransitions.ts
98
+ var UNKNOWN_NAME_FALLBACK = { type: "spring" };
99
+ function lookupNamedTransition(name, registry) {
100
+ const cfg = registry[name];
101
+ if (cfg) return cfg;
102
+ if (__DEV__) {
103
+ console.warn(
104
+ `[inertia] Unknown transition name "${name}" \u2014 falling back to the default spring. Register it on a provider: <MotionConfig transitions={{ '${name}': { ... } }}>.`
105
+ );
106
+ }
107
+ return UNKNOWN_NAME_FALLBACK;
108
+ }
109
+ function resolveNamedTransition(input, registry) {
110
+ if (input === void 0) return void 0;
111
+ if (typeof input === "string") return lookupNamedTransition(input, registry);
112
+ return input;
113
+ }
114
+ function resolveNamedTransitionProp(transition, registry) {
115
+ if (transition === void 0) return void 0;
116
+ if (typeof transition === "string") {
117
+ return lookupNamedTransition(transition, registry);
118
+ }
119
+ if (isTopLevelTransition(transition)) return transition;
120
+ const map = transition;
121
+ let out = null;
122
+ for (const key in map) {
123
+ const value = map[key];
124
+ if (typeof value === "string") {
125
+ if (out === null) out = { ...map };
126
+ out[key] = lookupNamedTransition(value, registry);
127
+ }
128
+ }
129
+ return out ?? transition;
130
+ }
40
131
  var modality = "keyboard";
41
132
  var installed = false;
42
133
  function setKeyboard() {
@@ -464,28 +555,51 @@ function mergeTransition(base, override) {
464
555
  }
465
556
  return { ...base ?? { type: "spring" }, ...override };
466
557
  }
467
-
468
- // src/transitions/keys.ts
469
- var TRANSITION_CONFIG_KEYS = /* @__PURE__ */ new Set([
470
- "type",
471
- "tension",
472
- "friction",
473
- "mass",
474
- "velocity",
475
- "restSpeedThreshold",
476
- "restDisplacementThreshold",
477
- "duration",
478
- "easing",
479
- "delay",
480
- "repeat",
481
- "deceleration",
482
- "clamp"
483
- ]);
484
- function isTopLevelTransition(t) {
485
- if (t === null || typeof t !== "object") return false;
486
- const keys = Object.keys(t);
487
- if (keys.length === 0) return false;
488
- return keys.every((k) => TRANSITION_CONFIG_KEYS.has(k));
558
+ var CSS_KEYWORDS = {
559
+ ease: [0.25, 0.1, 0.25, 1],
560
+ "ease-in": [0.42, 0, 1, 1],
561
+ "ease-out": [0, 0, 0.58, 1],
562
+ "ease-in-out": [0.42, 0, 0.58, 1]
563
+ };
564
+ var CSS_FUNCTION = /^cubic-bezier\((.*)\)$/;
565
+ function cubicBezier(first, y1, x2, y2) {
566
+ if (typeof first === "string") return fromCss(first);
567
+ return bezier(first, y1, x2, y2, void 0);
568
+ }
569
+ function fromCss(input) {
570
+ const token = input.trim().toLowerCase();
571
+ if (token === "linear") return Animated.Easing.linear;
572
+ const keyword = CSS_KEYWORDS[token];
573
+ if (keyword) return bezier(...keyword, input);
574
+ const match = CSS_FUNCTION.exec(token);
575
+ if (!match) {
576
+ throw new Error(
577
+ `[inertia] cubicBezier: unsupported easing token ${JSON.stringify(input)}. Expected four numbers, a 'cubic-bezier(x1, y1, x2, y2)' string, or one of the CSS keywords 'linear' | 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out'.`
578
+ );
579
+ }
580
+ const parts = match[1].split(",").map((p) => Number(p.trim()));
581
+ if (parts.length !== 4 || parts.some((n) => !Number.isFinite(n))) {
582
+ throw new Error(
583
+ `[inertia] cubicBezier: could not parse ${JSON.stringify(input)} \u2014 expected exactly four finite numbers inside cubic-bezier(...).`
584
+ );
585
+ }
586
+ return bezier(parts[0], parts[1], parts[2], parts[3], input);
587
+ }
588
+ function bezier(x1, y1, x2, y2, source) {
589
+ const describe = () => source !== void 0 ? JSON.stringify(source) : `cubicBezier(${x1}, ${y1}, ${x2}, ${y2})`;
590
+ for (const n of [x1, y1, x2, y2]) {
591
+ if (typeof n !== "number" || !Number.isFinite(n)) {
592
+ throw new Error(
593
+ `[inertia] cubicBezier: ${describe()} \u2014 every control point must be a finite number.`
594
+ );
595
+ }
596
+ }
597
+ if (x1 < 0 || x1 > 1 || x2 < 0 || x2 > 1) {
598
+ throw new Error(
599
+ `[inertia] cubicBezier: ${describe()} \u2014 x1 and x2 must be within [0, 1] (got x1=${x1}, x2=${x2}).`
600
+ );
601
+ }
602
+ return Animated.Easing.bezier(x1, y1, x2, y2);
489
603
  }
490
604
  var DEFAULT_TIMING_DURATION2 = 250;
491
605
  function buildReleaseAnimation(transition, toValue) {
@@ -509,28 +623,6 @@ function buildReleaseAnimation(transition, toValue) {
509
623
  }
510
624
  return Animated.withSpring(toValue, springToReanimated(transition));
511
625
  }
512
-
513
- // src/transitions/sig.ts
514
- function stableSig(value) {
515
- if (value === void 0) return "";
516
- try {
517
- return stableStringify(value);
518
- } catch {
519
- return String(value);
520
- }
521
- }
522
- function stableStringify(v) {
523
- if (v === null || typeof v !== "object") {
524
- if (typeof v === "function" || v === void 0) return "null";
525
- return JSON.stringify(v);
526
- }
527
- if (Array.isArray(v)) {
528
- return "[" + v.map(stableStringify).join(",") + "]";
529
- }
530
- const obj = v;
531
- const keys = Object.keys(obj).sort();
532
- return "{" + keys.map((k) => JSON.stringify(k) + ":" + stableStringify(obj[k])).join(",") + "}";
533
- }
534
626
  var alreadyChecked = false;
535
627
  function ensureReanimatedInstalled() {
536
628
  if (!__DEV__ || alreadyChecked) return;
@@ -633,12 +725,14 @@ var DEFAULT_RESTING = {
633
725
  };
634
726
  function transitionFor(prop, transition) {
635
727
  if (!transition) return void 0;
728
+ if (typeof transition === "string") return void 0;
636
729
  if (isTopLevelTransition(transition)) return transition;
637
730
  if (GESTURE_LAYER_NAME_SET.has(prop)) return void 0;
638
731
  return transition[prop];
639
732
  }
640
733
  function gestureLayerTransitionFor(layer, transition) {
641
734
  if (!transition) return void 0;
735
+ if (typeof transition === "string") return void 0;
642
736
  if (isTopLevelTransition(transition)) return transition;
643
737
  return transition[layer];
644
738
  }
@@ -653,17 +747,23 @@ function createMotionComponent(Component) {
653
747
  initial,
654
748
  animate,
655
749
  exit,
656
- transition,
750
+ transition: transitionProp,
657
751
  variants,
658
752
  controller,
659
753
  gesture,
660
- layout,
754
+ layout: layoutProp,
661
755
  layoutId,
662
756
  onAnimationEnd,
663
757
  style,
664
758
  onLayout: userOnLayout,
665
759
  ...rest
666
760
  } = props;
761
+ const namedTransitions = useNamedTransitions();
762
+ const transition = resolveNamedTransitionProp(
763
+ transitionProp,
764
+ namedTransitions
765
+ );
766
+ const layout = typeof layoutProp === "string" ? lookupNamedTransition(layoutProp, namedTransitions) : layoutProp;
667
767
  if (__DEV__ && typeof style === "function") {
668
768
  throw new Error(
669
769
  "[inertia] `style` must be a style object or array of style objects, not a function. The function-form `style={(state) => ...}` Pressable API is not supported \u2014 use `gesture.pressed` (or `gesture.focused`, etc.) to drive state-dependent styling instead."
@@ -1271,28 +1371,30 @@ var Motion = {
1271
1371
  function useAnimation(target, transition) {
1272
1372
  const output = Animated.useSharedValue(target);
1273
1373
  const shouldReduceMotion = useShouldReduceMotion();
1274
- const cfgSig = stableSig(transition);
1374
+ const resolved = resolveNamedTransition(transition, useNamedTransitions());
1375
+ const cfgSig = stableSig(resolved);
1275
1376
  react.useEffect(() => {
1276
- const cfg = shouldReduceMotion ? { type: "no-animation" } : transition ?? { type: "spring" };
1377
+ const cfg = shouldReduceMotion ? { type: "no-animation" } : resolved ?? { type: "spring" };
1277
1378
  output.value = resolveTransition(cfg, target);
1278
1379
  }, [target, cfgSig, shouldReduceMotion]);
1279
1380
  return output;
1280
1381
  }
1281
1382
  function useSpring(target, config) {
1383
+ const spring = resolveSpringInput(config, useNamedTransitions());
1282
1384
  const reanimConfig = react.useMemo(
1283
- () => springToReanimated(config ?? {}),
1385
+ () => springToReanimated(spring ?? {}),
1284
1386
  // Keyed on the resolved primitive fields rather than the `config` object
1285
1387
  // identity: callers routinely pass a fresh object literal each render, so
1286
1388
  // depending on `config` itself would rebuild the Reanimated config on
1287
1389
  // every render and defeat the memo.
1288
1390
  // eslint-disable-next-line react-hooks/exhaustive-deps
1289
1391
  [
1290
- config?.tension,
1291
- config?.friction,
1292
- config?.mass,
1293
- config?.velocity,
1294
- config?.restSpeedThreshold,
1295
- config?.restDisplacementThreshold
1392
+ spring?.tension,
1393
+ spring?.friction,
1394
+ spring?.mass,
1395
+ spring?.velocity,
1396
+ spring?.restSpeedThreshold,
1397
+ spring?.restDisplacementThreshold
1296
1398
  ]
1297
1399
  );
1298
1400
  const isSharedTarget = isSharedValue(target);
@@ -1317,6 +1419,17 @@ function useSpring(target, config) {
1317
1419
  );
1318
1420
  return output;
1319
1421
  }
1422
+ function resolveSpringInput(config, registry) {
1423
+ if (typeof config !== "string") return config;
1424
+ const cfg = lookupNamedTransition(config, registry);
1425
+ if (cfg.type === void 0 || cfg.type === "spring") return cfg;
1426
+ if (__DEV__) {
1427
+ console.warn(
1428
+ `[inertia] Named transition "${config}" resolves to type '${cfg.type}', but useSpring / useBooleanSpring are spring-only \u2014 falling back to the default spring. Use useAnimation to honor non-spring named transitions.`
1429
+ );
1430
+ }
1431
+ return void 0;
1432
+ }
1320
1433
  function isSharedValue(v) {
1321
1434
  return typeof v === "object" && v !== null && "value" in v;
1322
1435
  }
@@ -1340,9 +1453,10 @@ function useGesture(transition) {
1340
1453
  const focusVisible = Animated.useSharedValue(0);
1341
1454
  const hovered = Animated.useSharedValue(0);
1342
1455
  const shouldReduceMotion = useShouldReduceMotion();
1456
+ const resolved = resolveNamedTransitionProp(transition, useNamedTransitions());
1343
1457
  const setLayer = react.useCallback(
1344
1458
  (sv, layer, target) => {
1345
- const cfg = shouldReduceMotion ? { type: "no-animation" } : layerTransition(layer, transition) ?? { type: "spring" };
1459
+ const cfg = shouldReduceMotion ? { type: "no-animation" } : layerTransition(layer, resolved) ?? { type: "spring" };
1346
1460
  sv.value = resolveTransition(cfg, target);
1347
1461
  },
1348
1462
  // The transition is intentionally read on every call rather than cooked
@@ -1526,8 +1640,10 @@ exports.MotionView = MotionView;
1526
1640
  exports.Presence = Presence;
1527
1641
  exports.buildReleaseAnimation = buildReleaseAnimation;
1528
1642
  exports.createMotionComponent = createMotionComponent;
1643
+ exports.cubicBezier = cubicBezier;
1529
1644
  exports.ensureWorkletEasing = ensureWorkletEasing;
1530
1645
  exports.resolveAnimatableValue = resolveAnimatableValue;
1646
+ exports.resolveNamedTransition = resolveNamedTransition;
1531
1647
  exports.resolveTransition = resolveTransition;
1532
1648
  exports.useAnimation = useAnimation;
1533
1649
  exports.useBooleanSpring = useBooleanSpring;
@@ -1535,6 +1651,7 @@ exports.useColorTransition = useColorTransition;
1535
1651
  exports.useGesture = useGesture;
1536
1652
  exports.useMotionConfig = useMotionConfig;
1537
1653
  exports.useMotionValue = useMotionValue;
1654
+ exports.useNamedTransitions = useNamedTransitions;
1538
1655
  exports.usePresence = usePresence;
1539
1656
  exports.useScroll = useScroll;
1540
1657
  exports.useShadow = useShadow;
@@ -1542,5 +1659,3 @@ exports.useShouldReduceMotion = useShouldReduceMotion;
1542
1659
  exports.useSpring = useSpring;
1543
1660
  exports.useTransform = useTransform;
1544
1661
  exports.useVariants = useVariants;
1545
- //# sourceMappingURL=index.js.map
1546
- //# sourceMappingURL=index.js.map