@onlynative/inertia 0.0.1-alpha.3 → 0.0.1-alpha.4

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 (60) hide show
  1. package/README.md +19 -5
  2. package/dist/index.d.mts +259 -3
  3. package/dist/index.d.ts +259 -3
  4. package/dist/index.js +1711 -118
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +1709 -122
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/motion/Image.d.mts +1 -1
  9. package/dist/motion/Image.d.ts +1 -1
  10. package/dist/motion/Image.js +1502 -64
  11. package/dist/motion/Image.js.map +1 -1
  12. package/dist/motion/Image.mjs +1504 -66
  13. package/dist/motion/Image.mjs.map +1 -1
  14. package/dist/motion/Pressable.d.mts +1 -1
  15. package/dist/motion/Pressable.d.ts +1 -1
  16. package/dist/motion/Pressable.js +1502 -64
  17. package/dist/motion/Pressable.js.map +1 -1
  18. package/dist/motion/Pressable.mjs +1504 -66
  19. package/dist/motion/Pressable.mjs.map +1 -1
  20. package/dist/motion/ScrollView.d.mts +1 -1
  21. package/dist/motion/ScrollView.d.ts +1 -1
  22. package/dist/motion/ScrollView.js +1502 -64
  23. package/dist/motion/ScrollView.js.map +1 -1
  24. package/dist/motion/ScrollView.mjs +1504 -66
  25. package/dist/motion/ScrollView.mjs.map +1 -1
  26. package/dist/motion/Text.d.mts +1 -1
  27. package/dist/motion/Text.d.ts +1 -1
  28. package/dist/motion/Text.js +1502 -64
  29. package/dist/motion/Text.js.map +1 -1
  30. package/dist/motion/Text.mjs +1504 -66
  31. package/dist/motion/Text.mjs.map +1 -1
  32. package/dist/motion/View.d.mts +1 -1
  33. package/dist/motion/View.d.ts +1 -1
  34. package/dist/motion/View.js +1502 -64
  35. package/dist/motion/View.js.map +1 -1
  36. package/dist/motion/View.mjs +1504 -66
  37. package/dist/motion/View.mjs.map +1 -1
  38. package/dist/{types-DAhX3fC2.d.mts → types-CjztO3RW.d.mts} +49 -4
  39. package/dist/{types-DAhX3fC2.d.ts → types-CjztO3RW.d.ts} +49 -4
  40. package/llms.txt +29 -4
  41. package/package.json +1 -1
  42. package/src/__type-tests__/animate.test-d.tsx +88 -0
  43. package/src/index.ts +16 -1
  44. package/src/layout/index.ts +1 -0
  45. package/src/layout/resolveLayout.ts +54 -0
  46. package/src/motion/createMotionComponent.tsx +38 -60
  47. package/src/transitions/easing.ts +3 -1
  48. package/src/transitions/index.ts +3 -0
  49. package/src/transitions/keys.ts +32 -0
  50. package/src/transitions/resolve.ts +1 -24
  51. package/src/transitions/sig.ts +40 -0
  52. package/src/transitions/spring.ts +41 -0
  53. package/src/types.ts +52 -2
  54. package/src/values/index.ts +14 -0
  55. package/src/values/useAnimation.ts +69 -0
  56. package/src/values/useGesture.ts +144 -0
  57. package/src/values/useMotionValue.ts +33 -0
  58. package/src/values/useScroll.ts +72 -0
  59. package/src/values/useSpring.ts +93 -0
  60. package/src/values/useTransform.ts +132 -0
@@ -0,0 +1,132 @@
1
+ import {
2
+ Extrapolation,
3
+ interpolate,
4
+ interpolateColor,
5
+ useDerivedValue,
6
+ type SharedValue,
7
+ } from 'react-native-reanimated'
8
+ // `isWorkletFunction` was re-exported from `react-native-reanimated` historically
9
+ // but is deprecated there in favor of importing from `react-native-worklets`.
10
+ // `react-native-worklets` is a required peer of Reanimated 4, so the direct
11
+ // import is always available wherever Inertia is.
12
+ import { isWorkletFunction } from 'react-native-worklets'
13
+
14
+ /**
15
+ * Extrapolation behavior at the edges of the input range. Mirrors
16
+ * Reanimated's enum so consumers don't need a separate import.
17
+ *
18
+ * - `'clamp'` (default) — output stays pinned at the first/last value
19
+ * outside the input range. Matches Framer Motion's default.
20
+ * - `'identity'` — return the input unchanged outside the range.
21
+ * - `'extend'` — continue the linear slope beyond the range.
22
+ */
23
+ export type ExtrapolationMode = 'clamp' | 'identity' | 'extend'
24
+
25
+ export interface UseTransformOptions {
26
+ extrapolateLeft?: ExtrapolationMode
27
+ extrapolateRight?: ExtrapolationMode
28
+ }
29
+
30
+ /**
31
+ * Derive a value from one or more shared values via a transformer worklet.
32
+ *
33
+ * ```ts
34
+ * const x = useMotionValue(0)
35
+ * const y = useMotionValue(0)
36
+ * const distance = useTransform(() => Math.sqrt(x.value ** 2 + y.value ** 2))
37
+ * ```
38
+ *
39
+ * The transformer must be a worklet (or a plain function we auto-wrap —
40
+ * see the easing wrapper for the rationale). It runs on the UI thread on
41
+ * every frame where any read shared value changes.
42
+ */
43
+ export function useTransform<T>(transformer: () => T): SharedValue<T>
44
+
45
+ /**
46
+ * Interpolate a numeric shared value onto a range of numbers or colors.
47
+ *
48
+ * ```ts
49
+ * const scroll = useMotionValue(0)
50
+ * const headerOpacity = useTransform(scroll, [0, 100], [1, 0])
51
+ * const headerColor = useTransform(scroll, [0, 100], ['#fff', '#000'])
52
+ * ```
53
+ *
54
+ * When `outputRange` is numeric, this maps to Reanimated's `interpolate`;
55
+ * when it's a tuple of color strings, it maps to `interpolateColor`. The
56
+ * input range must be monotonically increasing.
57
+ */
58
+ export function useTransform(
59
+ value: SharedValue<number>,
60
+ inputRange: readonly number[],
61
+ outputRange: readonly number[],
62
+ options?: UseTransformOptions,
63
+ ): SharedValue<number>
64
+ export function useTransform(
65
+ value: SharedValue<number>,
66
+ inputRange: readonly number[],
67
+ outputRange: readonly string[],
68
+ options?: UseTransformOptions,
69
+ ): SharedValue<string>
70
+
71
+ export function useTransform<T>(
72
+ arg1: (() => T) | SharedValue<number>,
73
+ inputRange?: readonly number[],
74
+ outputRange?: readonly number[] | readonly string[],
75
+ options?: UseTransformOptions,
76
+ ): SharedValue<T> | SharedValue<number> | SharedValue<string> {
77
+ // Build the producer worklet on the JS thread, then call
78
+ // `useDerivedValue` exactly once. Keeping the hook call unconditional
79
+ // satisfies rules-of-hooks; per-call branching (transformer vs
80
+ // interpolation) is decided once at JS time, never at frame time.
81
+ let producer: () => unknown
82
+ if (typeof arg1 === 'function') {
83
+ // Transformer overload. The public surface accepts a plain function;
84
+ // Reanimated 3.9+ requires worklets in nested-derivation contexts, so
85
+ // we auto-wrap at JS time the same way `ensureWorkletEasing` does.
86
+ const userFn = arg1 as () => T
87
+ producer = isWorkletFunction(userFn)
88
+ ? userFn
89
+ : () => {
90
+ 'worklet'
91
+ return userFn()
92
+ }
93
+ } else {
94
+ // Interpolation overload. We pre-resolve everything JS-side so the
95
+ // worklet body only consumes flat values.
96
+ const source = arg1
97
+ const input = inputRange as readonly number[]
98
+ const output = outputRange as readonly (number | string)[]
99
+ const isColor = output.length > 0 && typeof output[0] === 'string'
100
+ const extrapolateLeft = mapExtrapolation(options?.extrapolateLeft)
101
+ const extrapolateRight = mapExtrapolation(options?.extrapolateRight)
102
+ producer = isColor
103
+ ? () => {
104
+ 'worklet'
105
+ return interpolateColor(
106
+ source.value,
107
+ input as number[],
108
+ output as string[],
109
+ )
110
+ }
111
+ : () => {
112
+ 'worklet'
113
+ return interpolate(
114
+ source.value,
115
+ input as number[],
116
+ output as number[],
117
+ { extrapolateLeft, extrapolateRight },
118
+ )
119
+ }
120
+ }
121
+
122
+ return useDerivedValue(producer as () => never) as unknown as
123
+ | SharedValue<T>
124
+ | SharedValue<number>
125
+ | SharedValue<string>
126
+ }
127
+
128
+ function mapExtrapolation(mode: ExtrapolationMode | undefined): Extrapolation {
129
+ if (mode === 'identity') return Extrapolation.IDENTITY
130
+ if (mode === 'extend') return Extrapolation.EXTEND
131
+ return Extrapolation.CLAMP
132
+ }