@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
@@ -1,6 +1,6 @@
1
1
  import { Platform, TurboModuleRegistry, ScrollView } from 'react-native';
2
- import { createContext, forwardRef, useRef, useState, useEffect, useMemo, useContext } from 'react';
3
- import Animated, { useSharedValue, useAnimatedStyle, interpolateColor, useReducedMotion, withSequence, LinearTransition, runOnJS as runOnJS$1, withRepeat, withDelay, withDecay, withTiming, Easing, withSpring } from 'react-native-reanimated';
2
+ import { createContext, forwardRef, useRef, useState, useEffect, useMemo, useContext, useCallback } from 'react';
3
+ import Animated, { useSharedValue, useAnimatedStyle, interpolateColor, useReducedMotion, withSequence, LinearTransition, runOnJS as runOnJS$1, withRepeat, withTiming, withSpring, withDelay, withDecay, Easing } from 'react-native-reanimated';
4
4
  import { jsx } from 'react/jsx-runtime';
5
5
 
6
6
  var __typeError = (msg) => {
@@ -1201,8 +1201,8 @@ function setupSetTimeout() {
1201
1201
  const start = performance.now();
1202
1202
  let timeoutHandle = 0;
1203
1203
  const rafCallback = () => {
1204
- const now = performance.now();
1205
- if (now - start >= delay) {
1204
+ const now2 = performance.now();
1205
+ if (now2 - start >= delay) {
1206
1206
  callback(...args);
1207
1207
  timeoutHandleToRafHandle.delete(timeoutHandle);
1208
1208
  } else {
@@ -1496,6 +1496,157 @@ function resolveLayoutTransition(layout) {
1496
1496
  if ("delay" in spring && spring.delay) builder = builder.delay(spring.delay);
1497
1497
  return builder;
1498
1498
  }
1499
+
1500
+ // src/layout/sharedRegistry.ts
1501
+ var REGISTRY = /* @__PURE__ */ new Map();
1502
+ var SHARED_LAYOUT_TTL_MS = 1e3;
1503
+ var now = () => Date.now();
1504
+ function registerLayout(id, rect) {
1505
+ REGISTRY.set(id, { rect, expiresAt: now() + SHARED_LAYOUT_TTL_MS });
1506
+ }
1507
+ function releaseLayout(id, rect) {
1508
+ REGISTRY.set(id, { rect, expiresAt: now() + SHARED_LAYOUT_TTL_MS });
1509
+ }
1510
+ function consumeLayout(id) {
1511
+ const entry = REGISTRY.get(id);
1512
+ if (!entry) return void 0;
1513
+ REGISTRY.delete(id);
1514
+ if (entry.expiresAt < now()) return void 0;
1515
+ return entry.rect;
1516
+ }
1517
+ function useSharedLayout(options) {
1518
+ const { layoutId, userRef, transition, shouldReduceMotion, userOnLayout } = options;
1519
+ const dx = useSharedValue(0);
1520
+ const dy = useSharedValue(0);
1521
+ const sx = useSharedValue(1);
1522
+ const sy = useSharedValue(1);
1523
+ const lastRectRef = useRef(null);
1524
+ const consumedRef = useRef(false);
1525
+ const transitionRef = useRef(transition);
1526
+ transitionRef.current = transition;
1527
+ const reducedMotionRef = useRef(shouldReduceMotion);
1528
+ reducedMotionRef.current = shouldReduceMotion;
1529
+ const setRef = useCallback(
1530
+ (node) => {
1531
+ if (typeof userRef === "function") userRef(node);
1532
+ else if (userRef) userRef.current = node;
1533
+ },
1534
+ [userRef]
1535
+ );
1536
+ const onLayout = useCallback(
1537
+ (event) => {
1538
+ userOnLayout?.(event);
1539
+ if (!layoutId) return;
1540
+ const { x, y, width, height } = event.nativeEvent.layout;
1541
+ const rect = { x, y, width, height };
1542
+ lastRectRef.current = rect;
1543
+ let source;
1544
+ if (!consumedRef.current) {
1545
+ consumedRef.current = true;
1546
+ source = consumeLayout(layoutId);
1547
+ }
1548
+ registerLayout(layoutId, rect);
1549
+ if (source) {
1550
+ applyFlip({
1551
+ source,
1552
+ target: rect,
1553
+ dx,
1554
+ dy,
1555
+ sx,
1556
+ sy,
1557
+ transition: transitionRef.current,
1558
+ shouldReduceMotion: reducedMotionRef.current
1559
+ });
1560
+ }
1561
+ },
1562
+ // dx/dy/sx/sy are stable refs from useSharedValue, but eslint's
1563
+ // exhaustive-deps would flag them — including them is harmless and
1564
+ // silences the warning.
1565
+ [layoutId, userOnLayout, dx, dy, sx, sy]
1566
+ );
1567
+ useEffect(() => {
1568
+ consumedRef.current = false;
1569
+ }, [layoutId]);
1570
+ useEffect(() => {
1571
+ return () => {
1572
+ if (!layoutId) return;
1573
+ const rect = lastRectRef.current;
1574
+ if (!rect) return;
1575
+ releaseLayout(layoutId, rect);
1576
+ };
1577
+ }, [layoutId]);
1578
+ return useMemo(
1579
+ () => ({
1580
+ flip: { dx, dy, sx, sy },
1581
+ setRef,
1582
+ onLayout
1583
+ }),
1584
+ [dx, dy, sx, sy, setRef, onLayout]
1585
+ );
1586
+ }
1587
+ function applyFlip(args) {
1588
+ const { source, target, dx, dy, sx, sy, transition, shouldReduceMotion } = args;
1589
+ const sourceCenterX = source.x + source.width / 2;
1590
+ const sourceCenterY = source.y + source.height / 2;
1591
+ const targetCenterX = target.x + target.width / 2;
1592
+ const targetCenterY = target.y + target.height / 2;
1593
+ const deltaX = sourceCenterX - targetCenterX;
1594
+ const deltaY = sourceCenterY - targetCenterY;
1595
+ const scaleX = target.width > 0 ? source.width / target.width : 1;
1596
+ const scaleY = target.height > 0 ? source.height / target.height : 1;
1597
+ if (shouldReduceMotion) {
1598
+ dx.value = 0;
1599
+ dy.value = 0;
1600
+ sx.value = 1;
1601
+ sy.value = 1;
1602
+ return;
1603
+ }
1604
+ if (transition?.type === "no-animation") {
1605
+ dx.value = 0;
1606
+ dy.value = 0;
1607
+ sx.value = 1;
1608
+ sy.value = 1;
1609
+ return;
1610
+ }
1611
+ if (transition?.type === "timing") {
1612
+ const duration = transition.duration ?? 300;
1613
+ dx.value = withSequence(
1614
+ withTiming(deltaX, { duration: 0 }),
1615
+ withTiming(0, { duration })
1616
+ );
1617
+ dy.value = withSequence(
1618
+ withTiming(deltaY, { duration: 0 }),
1619
+ withTiming(0, { duration })
1620
+ );
1621
+ sx.value = withSequence(
1622
+ withTiming(scaleX, { duration: 0 }),
1623
+ withTiming(1, { duration })
1624
+ );
1625
+ sy.value = withSequence(
1626
+ withTiming(scaleY, { duration: 0 }),
1627
+ withTiming(1, { duration })
1628
+ );
1629
+ return;
1630
+ }
1631
+ const springCfg = transition?.type === "spring" ? { ...DEFAULT_SPRING, ...transition } : { ...DEFAULT_SPRING };
1632
+ const springParams = springToReanimated(springCfg);
1633
+ dx.value = withSequence(
1634
+ withTiming(deltaX, { duration: 0 }),
1635
+ withSpring(0, springParams)
1636
+ );
1637
+ dy.value = withSequence(
1638
+ withTiming(deltaY, { duration: 0 }),
1639
+ withSpring(0, springParams)
1640
+ );
1641
+ sx.value = withSequence(
1642
+ withTiming(scaleX, { duration: 0 }),
1643
+ withSpring(1, springParams)
1644
+ );
1645
+ sy.value = withSequence(
1646
+ withTiming(scaleY, { duration: 0 }),
1647
+ withSpring(1, springParams)
1648
+ );
1649
+ }
1499
1650
  var PresenceContext = createContext(null);
1500
1651
  function usePresence() {
1501
1652
  return useContext(PresenceContext);
@@ -1784,10 +1935,17 @@ function createMotionComponent(Component) {
1784
1935
  controller,
1785
1936
  gesture,
1786
1937
  layout,
1938
+ layoutId,
1787
1939
  onAnimationEnd,
1788
1940
  style,
1941
+ onLayout: userOnLayout,
1789
1942
  ...rest
1790
1943
  } = props;
1944
+ if (__DEV__ && typeof style === "function") {
1945
+ throw new Error(
1946
+ "[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."
1947
+ );
1948
+ }
1791
1949
  const presence = usePresence();
1792
1950
  const isExiting = presence !== null && presence.isPresent === false;
1793
1951
  const shouldReduceMotion = useShouldReduceMotion();
@@ -1961,6 +2119,15 @@ function createMotionComponent(Component) {
1961
2119
  isExiting,
1962
2120
  shouldReduceMotion
1963
2121
  );
2122
+ const sharedLayout = useSharedLayout({
2123
+ layoutId,
2124
+ userRef: ref,
2125
+ transition: isTopLevelTransition(transition) ? transition : void 0,
2126
+ shouldReduceMotion,
2127
+ userOnLayout
2128
+ });
2129
+ const flip = sharedLayout.flip;
2130
+ const hasLayoutId = layoutId !== void 0;
1964
2131
  const animatedStyle = useAnimatedStyle(() => {
1965
2132
  const activeKeys = activeKeysRef.current;
1966
2133
  const hasTransform = hasTransformRef.current;
@@ -2009,7 +2176,13 @@ function createMotionComponent(Component) {
2009
2176
  out[key] = v;
2010
2177
  }
2011
2178
  }
2012
- if (hasTransform) out.transform = transform;
2179
+ if (hasLayoutId) {
2180
+ transform.push({ translateX: flip.dx.value });
2181
+ transform.push({ translateY: flip.dy.value });
2182
+ transform.push({ scaleX: flip.sx.value });
2183
+ transform.push({ scaleY: flip.sy.value });
2184
+ }
2185
+ if (hasTransform || hasLayoutId) out.transform = transform;
2013
2186
  if (hasShadowOffset) {
2014
2187
  out.shadowOffset = { width: shadowOffsetW, height: shadowOffsetH };
2015
2188
  }
@@ -2036,9 +2209,10 @@ function createMotionComponent(Component) {
2036
2209
  return /* @__PURE__ */ jsx(
2037
2210
  AnimatedComponent,
2038
2211
  {
2039
- ref,
2212
+ ref: sharedLayout.setRef,
2040
2213
  ...rest,
2041
2214
  ...gestureHandlers,
2215
+ onLayout: sharedLayout.onLayout,
2042
2216
  layout: layoutTransition,
2043
2217
  style: mergedStyle
2044
2218
  }