@onlynative/inertia 0.0.1-alpha.7 → 0.0.1-alpha.8

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 (69) hide show
  1. package/README.md +1 -1
  2. package/dist/gestureLayer/index.d.mts +119 -0
  3. package/dist/gestureLayer/index.d.ts +119 -0
  4. package/dist/gestureLayer/index.js +1745 -0
  5. package/dist/gestureLayer/index.js.map +1 -0
  6. package/dist/gestureLayer/index.mjs +1743 -0
  7. package/dist/gestureLayer/index.mjs.map +1 -0
  8. package/dist/index.d.mts +114 -74
  9. package/dist/index.d.ts +114 -74
  10. package/dist/index.js +279 -41
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +279 -44
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/motion/Image.d.mts +1 -1
  15. package/dist/motion/Image.d.ts +1 -1
  16. package/dist/motion/Image.js +178 -4
  17. package/dist/motion/Image.js.map +1 -1
  18. package/dist/motion/Image.mjs +180 -6
  19. package/dist/motion/Image.mjs.map +1 -1
  20. package/dist/motion/Pressable.d.mts +1 -1
  21. package/dist/motion/Pressable.d.ts +1 -1
  22. package/dist/motion/Pressable.js +178 -4
  23. package/dist/motion/Pressable.js.map +1 -1
  24. package/dist/motion/Pressable.mjs +180 -6
  25. package/dist/motion/Pressable.mjs.map +1 -1
  26. package/dist/motion/ScrollView.d.mts +1 -1
  27. package/dist/motion/ScrollView.d.ts +1 -1
  28. package/dist/motion/ScrollView.js +178 -4
  29. package/dist/motion/ScrollView.js.map +1 -1
  30. package/dist/motion/ScrollView.mjs +180 -6
  31. package/dist/motion/ScrollView.mjs.map +1 -1
  32. package/dist/motion/Text.d.mts +1 -1
  33. package/dist/motion/Text.d.ts +1 -1
  34. package/dist/motion/Text.js +178 -4
  35. package/dist/motion/Text.js.map +1 -1
  36. package/dist/motion/Text.mjs +180 -6
  37. package/dist/motion/Text.mjs.map +1 -1
  38. package/dist/motion/View.d.mts +1 -1
  39. package/dist/motion/View.d.ts +1 -1
  40. package/dist/motion/View.js +178 -4
  41. package/dist/motion/View.js.map +1 -1
  42. package/dist/motion/View.mjs +180 -6
  43. package/dist/motion/View.mjs.map +1 -1
  44. package/dist/touch/index.d.mts +146 -0
  45. package/dist/touch/index.d.ts +146 -0
  46. package/dist/touch/index.js +166 -0
  47. package/dist/touch/index.js.map +1 -0
  48. package/dist/touch/index.mjs +164 -0
  49. package/dist/touch/index.mjs.map +1 -0
  50. package/dist/{types-NmNeJjo1.d.mts → types-BwyvoH2V.d.mts} +24 -4
  51. package/dist/{types-NmNeJjo1.d.ts → types-BwyvoH2V.d.ts} +24 -4
  52. package/dist/useGesture-BPPp9LhV.d.ts +84 -0
  53. package/dist/useGesture-BnBF4OtT.d.mts +84 -0
  54. package/llms.txt +12 -3
  55. package/package.json +15 -1
  56. package/src/gestureLayer/index.ts +21 -0
  57. package/src/gestureLayer/useGestureLayer.ts +285 -0
  58. package/src/index.ts +7 -0
  59. package/src/layout/index.ts +15 -0
  60. package/src/layout/sharedRegistry.ts +108 -0
  61. package/src/layout/useSharedLayout.ts +289 -0
  62. package/src/motion/createMotionComponent.tsx +60 -4
  63. package/src/touch/index.ts +18 -0
  64. package/src/touch/useTouchDrag.ts +289 -0
  65. package/src/types.ts +23 -3
  66. package/src/values/index.ts +11 -0
  67. package/src/values/useBooleanSpring.ts +33 -0
  68. package/src/values/useColorTransition.ts +72 -0
  69. package/src/values/useShadow.ts +116 -0
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Platform, TurboModuleRegistry, Image, Pressable, ScrollView, Text, View } from 'react-native';
2
- import { createContext, forwardRef, useRef, useState, useEffect, useMemo, useContext, Children, isValidElement, useCallback } from 'react';
3
- import Animated, { useSharedValue, useAnimatedStyle, interpolateColor, useReducedMotion, withSequence, LinearTransition, runOnJS as runOnJS$1, withRepeat, withDelay, withDecay, withTiming, Easing, withSpring, useAnimatedReaction, useDerivedValue, Extrapolation, useAnimatedScrollHandler, interpolate } from 'react-native-reanimated';
2
+ import { createContext, forwardRef, useRef, useState, useEffect, useMemo, useContext, useCallback, Children, isValidElement } from 'react';
3
+ import Animated, { useSharedValue, useAnimatedStyle, interpolateColor, useReducedMotion, withSequence, LinearTransition, runOnJS as runOnJS$1, withRepeat, withTiming, withSpring, withDelay, withDecay, Easing, useAnimatedReaction, useDerivedValue, Extrapolation, useAnimatedScrollHandler, interpolate } from 'react-native-reanimated';
4
4
  import { jsx, Fragment } from 'react/jsx-runtime';
5
5
 
6
6
  var __typeError = (msg) => {
@@ -1211,8 +1211,8 @@ function setupSetTimeout() {
1211
1211
  const start = performance.now();
1212
1212
  let timeoutHandle = 0;
1213
1213
  const rafCallback = () => {
1214
- const now = performance.now();
1215
- if (now - start >= delay) {
1214
+ const now2 = performance.now();
1215
+ if (now2 - start >= delay) {
1216
1216
  callback(...args);
1217
1217
  timeoutHandleToRafHandle.delete(timeoutHandle);
1218
1218
  } else {
@@ -1506,6 +1506,157 @@ function resolveLayoutTransition(layout) {
1506
1506
  if ("delay" in spring && spring.delay) builder = builder.delay(spring.delay);
1507
1507
  return builder;
1508
1508
  }
1509
+
1510
+ // src/layout/sharedRegistry.ts
1511
+ var REGISTRY = /* @__PURE__ */ new Map();
1512
+ var SHARED_LAYOUT_TTL_MS = 1e3;
1513
+ var now = () => Date.now();
1514
+ function registerLayout(id, rect) {
1515
+ REGISTRY.set(id, { rect, expiresAt: now() + SHARED_LAYOUT_TTL_MS });
1516
+ }
1517
+ function releaseLayout(id, rect) {
1518
+ REGISTRY.set(id, { rect, expiresAt: now() + SHARED_LAYOUT_TTL_MS });
1519
+ }
1520
+ function consumeLayout(id) {
1521
+ const entry = REGISTRY.get(id);
1522
+ if (!entry) return void 0;
1523
+ REGISTRY.delete(id);
1524
+ if (entry.expiresAt < now()) return void 0;
1525
+ return entry.rect;
1526
+ }
1527
+ function useSharedLayout(options) {
1528
+ const { layoutId, userRef, transition, shouldReduceMotion, userOnLayout } = options;
1529
+ const dx = useSharedValue(0);
1530
+ const dy = useSharedValue(0);
1531
+ const sx = useSharedValue(1);
1532
+ const sy = useSharedValue(1);
1533
+ const lastRectRef = useRef(null);
1534
+ const consumedRef = useRef(false);
1535
+ const transitionRef = useRef(transition);
1536
+ transitionRef.current = transition;
1537
+ const reducedMotionRef = useRef(shouldReduceMotion);
1538
+ reducedMotionRef.current = shouldReduceMotion;
1539
+ const setRef = useCallback(
1540
+ (node) => {
1541
+ if (typeof userRef === "function") userRef(node);
1542
+ else if (userRef) userRef.current = node;
1543
+ },
1544
+ [userRef]
1545
+ );
1546
+ const onLayout = useCallback(
1547
+ (event) => {
1548
+ userOnLayout?.(event);
1549
+ if (!layoutId) return;
1550
+ const { x, y, width, height } = event.nativeEvent.layout;
1551
+ const rect = { x, y, width, height };
1552
+ lastRectRef.current = rect;
1553
+ let source;
1554
+ if (!consumedRef.current) {
1555
+ consumedRef.current = true;
1556
+ source = consumeLayout(layoutId);
1557
+ }
1558
+ registerLayout(layoutId, rect);
1559
+ if (source) {
1560
+ applyFlip({
1561
+ source,
1562
+ target: rect,
1563
+ dx,
1564
+ dy,
1565
+ sx,
1566
+ sy,
1567
+ transition: transitionRef.current,
1568
+ shouldReduceMotion: reducedMotionRef.current
1569
+ });
1570
+ }
1571
+ },
1572
+ // dx/dy/sx/sy are stable refs from useSharedValue, but eslint's
1573
+ // exhaustive-deps would flag them — including them is harmless and
1574
+ // silences the warning.
1575
+ [layoutId, userOnLayout, dx, dy, sx, sy]
1576
+ );
1577
+ useEffect(() => {
1578
+ consumedRef.current = false;
1579
+ }, [layoutId]);
1580
+ useEffect(() => {
1581
+ return () => {
1582
+ if (!layoutId) return;
1583
+ const rect = lastRectRef.current;
1584
+ if (!rect) return;
1585
+ releaseLayout(layoutId, rect);
1586
+ };
1587
+ }, [layoutId]);
1588
+ return useMemo(
1589
+ () => ({
1590
+ flip: { dx, dy, sx, sy },
1591
+ setRef,
1592
+ onLayout
1593
+ }),
1594
+ [dx, dy, sx, sy, setRef, onLayout]
1595
+ );
1596
+ }
1597
+ function applyFlip(args) {
1598
+ const { source, target, dx, dy, sx, sy, transition, shouldReduceMotion } = args;
1599
+ const sourceCenterX = source.x + source.width / 2;
1600
+ const sourceCenterY = source.y + source.height / 2;
1601
+ const targetCenterX = target.x + target.width / 2;
1602
+ const targetCenterY = target.y + target.height / 2;
1603
+ const deltaX = sourceCenterX - targetCenterX;
1604
+ const deltaY = sourceCenterY - targetCenterY;
1605
+ const scaleX = target.width > 0 ? source.width / target.width : 1;
1606
+ const scaleY = target.height > 0 ? source.height / target.height : 1;
1607
+ if (shouldReduceMotion) {
1608
+ dx.value = 0;
1609
+ dy.value = 0;
1610
+ sx.value = 1;
1611
+ sy.value = 1;
1612
+ return;
1613
+ }
1614
+ if (transition?.type === "no-animation") {
1615
+ dx.value = 0;
1616
+ dy.value = 0;
1617
+ sx.value = 1;
1618
+ sy.value = 1;
1619
+ return;
1620
+ }
1621
+ if (transition?.type === "timing") {
1622
+ const duration = transition.duration ?? 300;
1623
+ dx.value = withSequence(
1624
+ withTiming(deltaX, { duration: 0 }),
1625
+ withTiming(0, { duration })
1626
+ );
1627
+ dy.value = withSequence(
1628
+ withTiming(deltaY, { duration: 0 }),
1629
+ withTiming(0, { duration })
1630
+ );
1631
+ sx.value = withSequence(
1632
+ withTiming(scaleX, { duration: 0 }),
1633
+ withTiming(1, { duration })
1634
+ );
1635
+ sy.value = withSequence(
1636
+ withTiming(scaleY, { duration: 0 }),
1637
+ withTiming(1, { duration })
1638
+ );
1639
+ return;
1640
+ }
1641
+ const springCfg = transition?.type === "spring" ? { ...DEFAULT_SPRING, ...transition } : { ...DEFAULT_SPRING };
1642
+ const springParams = springToReanimated(springCfg);
1643
+ dx.value = withSequence(
1644
+ withTiming(deltaX, { duration: 0 }),
1645
+ withSpring(0, springParams)
1646
+ );
1647
+ dy.value = withSequence(
1648
+ withTiming(deltaY, { duration: 0 }),
1649
+ withSpring(0, springParams)
1650
+ );
1651
+ sx.value = withSequence(
1652
+ withTiming(scaleX, { duration: 0 }),
1653
+ withSpring(1, springParams)
1654
+ );
1655
+ sy.value = withSequence(
1656
+ withTiming(scaleY, { duration: 0 }),
1657
+ withSpring(1, springParams)
1658
+ );
1659
+ }
1509
1660
  var PresenceContext = createContext(null);
1510
1661
  function usePresence() {
1511
1662
  return useContext(PresenceContext);
@@ -1907,10 +2058,17 @@ function createMotionComponent(Component) {
1907
2058
  controller,
1908
2059
  gesture,
1909
2060
  layout,
2061
+ layoutId,
1910
2062
  onAnimationEnd,
1911
2063
  style,
2064
+ onLayout: userOnLayout,
1912
2065
  ...rest
1913
2066
  } = props;
2067
+ if (__DEV__ && typeof style === "function") {
2068
+ throw new Error(
2069
+ "[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."
2070
+ );
2071
+ }
1914
2072
  const presence = usePresence();
1915
2073
  const isExiting = presence !== null && presence.isPresent === false;
1916
2074
  const shouldReduceMotion = useShouldReduceMotion();
@@ -2084,6 +2242,15 @@ function createMotionComponent(Component) {
2084
2242
  isExiting,
2085
2243
  shouldReduceMotion
2086
2244
  );
2245
+ const sharedLayout = useSharedLayout({
2246
+ layoutId,
2247
+ userRef: ref,
2248
+ transition: isTopLevelTransition(transition) ? transition : void 0,
2249
+ shouldReduceMotion,
2250
+ userOnLayout
2251
+ });
2252
+ const flip = sharedLayout.flip;
2253
+ const hasLayoutId = layoutId !== void 0;
2087
2254
  const animatedStyle = useAnimatedStyle(() => {
2088
2255
  const activeKeys = activeKeysRef.current;
2089
2256
  const hasTransform = hasTransformRef.current;
@@ -2132,7 +2299,13 @@ function createMotionComponent(Component) {
2132
2299
  out[key] = v;
2133
2300
  }
2134
2301
  }
2135
- if (hasTransform) out.transform = transform;
2302
+ if (hasLayoutId) {
2303
+ transform.push({ translateX: flip.dx.value });
2304
+ transform.push({ translateY: flip.dy.value });
2305
+ transform.push({ scaleX: flip.sx.value });
2306
+ transform.push({ scaleY: flip.sy.value });
2307
+ }
2308
+ if (hasTransform || hasLayoutId) out.transform = transform;
2136
2309
  if (hasShadowOffset) {
2137
2310
  out.shadowOffset = { width: shadowOffsetW, height: shadowOffsetH };
2138
2311
  }
@@ -2159,9 +2332,10 @@ function createMotionComponent(Component) {
2159
2332
  return /* @__PURE__ */ jsx(
2160
2333
  AnimatedComponent,
2161
2334
  {
2162
- ref,
2335
+ ref: sharedLayout.setRef,
2163
2336
  ...rest,
2164
2337
  ...gestureHandlers,
2338
+ onLayout: sharedLayout.onLayout,
2165
2339
  layout: layoutTransition,
2166
2340
  style: mergedStyle
2167
2341
  }
@@ -2490,6 +2664,57 @@ function useAnimation(target, transition) {
2490
2664
  }, [target, cfgSig, shouldReduceMotion]);
2491
2665
  return output;
2492
2666
  }
2667
+ function useSpring(target, config) {
2668
+ const reanimConfig = useMemo(
2669
+ () => springToReanimated(config ?? {}),
2670
+ [
2671
+ config?.tension,
2672
+ config?.friction,
2673
+ config?.mass,
2674
+ config?.velocity,
2675
+ config?.restSpeedThreshold,
2676
+ config?.restDisplacementThreshold
2677
+ ]
2678
+ );
2679
+ const isSharedTarget = isSharedValue(target);
2680
+ const initial = isSharedTarget ? target.value : target;
2681
+ const output = useSharedValue(initial);
2682
+ useEffect(() => {
2683
+ if (isSharedTarget) return;
2684
+ output.value = withSpring(target, reanimConfig);
2685
+ }, [isSharedTarget, target, reanimConfig]);
2686
+ useAnimatedReaction(
2687
+ () => {
2688
+ "worklet";
2689
+ if (!isSharedTarget) return null;
2690
+ return target.value;
2691
+ },
2692
+ (next, prev) => {
2693
+ "worklet";
2694
+ if (next === null || next === prev) return;
2695
+ output.value = withSpring(next, reanimConfig);
2696
+ },
2697
+ [isSharedTarget, reanimConfig]
2698
+ );
2699
+ return output;
2700
+ }
2701
+ function isSharedValue(v) {
2702
+ return typeof v === "object" && v !== null && "value" in v;
2703
+ }
2704
+
2705
+ // src/values/useBooleanSpring.ts
2706
+ function useBooleanSpring(active, springConfig) {
2707
+ return useSpring(active ? 1 : 0, springConfig);
2708
+ }
2709
+ function useColorTransition(progress, range, options) {
2710
+ const key = options?.key ?? "backgroundColor";
2711
+ const from = range[0];
2712
+ const to = range[1];
2713
+ return useAnimatedStyle(() => {
2714
+ "worklet";
2715
+ return { [key]: interpolateColor(progress.value, [0, 1], [from, to]) };
2716
+ });
2717
+ }
2493
2718
  function useGesture(transition) {
2494
2719
  const pressed = useSharedValue(0);
2495
2720
  const focused = useSharedValue(0);
@@ -2537,43 +2762,6 @@ function layerTransition(layer, transition) {
2537
2762
  function useMotionValue(initial) {
2538
2763
  return useSharedValue(initial);
2539
2764
  }
2540
- function useSpring(target, config) {
2541
- const reanimConfig = useMemo(
2542
- () => springToReanimated(config ?? {}),
2543
- [
2544
- config?.tension,
2545
- config?.friction,
2546
- config?.mass,
2547
- config?.velocity,
2548
- config?.restSpeedThreshold,
2549
- config?.restDisplacementThreshold
2550
- ]
2551
- );
2552
- const isSharedTarget = isSharedValue(target);
2553
- const initial = isSharedTarget ? target.value : target;
2554
- const output = useSharedValue(initial);
2555
- useEffect(() => {
2556
- if (isSharedTarget) return;
2557
- output.value = withSpring(target, reanimConfig);
2558
- }, [isSharedTarget, target, reanimConfig]);
2559
- useAnimatedReaction(
2560
- () => {
2561
- "worklet";
2562
- if (!isSharedTarget) return null;
2563
- return target.value;
2564
- },
2565
- (next, prev) => {
2566
- "worklet";
2567
- if (next === null || next === prev) return;
2568
- output.value = withSpring(next, reanimConfig);
2569
- },
2570
- [isSharedTarget, reanimConfig]
2571
- );
2572
- return output;
2573
- }
2574
- function isSharedValue(v) {
2575
- return typeof v === "object" && v !== null && "value" in v;
2576
- }
2577
2765
  function useTransform(arg1, inputRange, outputRange, options) {
2578
2766
  let producer;
2579
2767
  if (typeof arg1 === "function") {
@@ -2629,6 +2817,53 @@ function useScroll() {
2629
2817
  onScroll: handler
2630
2818
  };
2631
2819
  }
2820
+ function useShadow({
2821
+ from,
2822
+ to,
2823
+ progress
2824
+ }) {
2825
+ const hasOpacity = from.shadowOpacity !== void 0 || to.shadowOpacity !== void 0;
2826
+ const hasRadius = from.shadowRadius !== void 0 || to.shadowRadius !== void 0;
2827
+ const hasElevation = from.elevation !== void 0 || to.elevation !== void 0;
2828
+ const hasColor = from.shadowColor !== void 0 || to.shadowColor !== void 0;
2829
+ const hasOffset = from.shadowOffset !== void 0 || to.shadowOffset !== void 0;
2830
+ const opacityFrom = from.shadowOpacity ?? 0;
2831
+ const opacityTo = to.shadowOpacity ?? 0;
2832
+ const radiusFrom = from.shadowRadius ?? 0;
2833
+ const radiusTo = to.shadowRadius ?? 0;
2834
+ const elevationFrom = from.elevation ?? 0;
2835
+ const elevationTo = to.elevation ?? 0;
2836
+ const colorFrom = from.shadowColor ?? "transparent";
2837
+ const colorTo = to.shadowColor ?? "transparent";
2838
+ const offsetWFrom = from.shadowOffset?.width ?? 0;
2839
+ const offsetWTo = to.shadowOffset?.width ?? 0;
2840
+ const offsetHFrom = from.shadowOffset?.height ?? 0;
2841
+ const offsetHTo = to.shadowOffset?.height ?? 0;
2842
+ return useAnimatedStyle(() => {
2843
+ "worklet";
2844
+ const t = progress.value;
2845
+ const out = {};
2846
+ if (hasOpacity) {
2847
+ out.shadowOpacity = interpolate(t, [0, 1], [opacityFrom, opacityTo]);
2848
+ }
2849
+ if (hasRadius) {
2850
+ out.shadowRadius = interpolate(t, [0, 1], [radiusFrom, radiusTo]);
2851
+ }
2852
+ if (hasElevation) {
2853
+ out.elevation = interpolate(t, [0, 1], [elevationFrom, elevationTo]);
2854
+ }
2855
+ if (hasColor) {
2856
+ out.shadowColor = interpolateColor(t, [0, 1], [colorFrom, colorTo]);
2857
+ }
2858
+ if (hasOffset) {
2859
+ out.shadowOffset = {
2860
+ width: interpolate(t, [0, 1], [offsetWFrom, offsetWTo]),
2861
+ height: interpolate(t, [0, 1], [offsetHFrom, offsetHTo])
2862
+ };
2863
+ }
2864
+ return out;
2865
+ });
2866
+ }
2632
2867
  function useVariants(variants, initial) {
2633
2868
  const variantsRef = useRef(variants);
2634
2869
  return useMemo(() => {
@@ -2662,6 +2897,6 @@ function useVariants(variants, initial) {
2662
2897
  }, []);
2663
2898
  }
2664
2899
 
2665
- export { Motion, MotionConfig, MotionImage, MotionPressable, MotionScrollView, MotionText, MotionView, Presence, buildReleaseAnimation, createMotionComponent, ensureWorkletEasing, resolveAnimatableValue, resolveTransition, useAnimation, useGesture, useMotionConfig, useMotionValue, usePresence, useScroll, useShouldReduceMotion, useSpring, useTransform, useVariants };
2900
+ export { Motion, MotionConfig, MotionImage, MotionPressable, MotionScrollView, MotionText, MotionView, Presence, buildReleaseAnimation, createMotionComponent, ensureWorkletEasing, resolveAnimatableValue, resolveTransition, useAnimation, useBooleanSpring, useColorTransition, useGesture, useMotionConfig, useMotionValue, usePresence, useScroll, useShadow, useShouldReduceMotion, useSpring, useTransform, useVariants };
2666
2901
  //# sourceMappingURL=index.mjs.map
2667
2902
  //# sourceMappingURL=index.mjs.map