@lynx-js/lynx-ui-common 0.0.0

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 (215) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +28 -0
  3. package/dist/const/BaseScrollEventsMapping.d.ts +2 -0
  4. package/dist/const/BaseScrollEventsMapping.d.ts.map +1 -0
  5. package/dist/const/BaseScrollEventsMapping.jsx +13 -0
  6. package/dist/const/BaseScrollEventsMapping.jsx.map +1 -0
  7. package/dist/const/BaseUIEventsMapping.d.ts +4 -0
  8. package/dist/const/BaseUIEventsMapping.d.ts.map +1 -0
  9. package/dist/const/BaseUIEventsMapping.jsx +24 -0
  10. package/dist/const/BaseUIEventsMapping.jsx.map +1 -0
  11. package/dist/const/index.d.ts +3 -0
  12. package/dist/const/index.d.ts.map +1 -0
  13. package/dist/const/index.jsx +2 -0
  14. package/dist/hooks/docs/useBounce.jsx +0 -0
  15. package/dist/hooks/docs/useRefresh.jsx +0 -0
  16. package/dist/hooks/index.d.ts +19 -0
  17. package/dist/hooks/index.d.ts.map +1 -0
  18. package/dist/hooks/index.jsx +73 -0
  19. package/dist/hooks/index.jsx.map +1 -0
  20. package/dist/hooks/useBounce.d.ts +26 -0
  21. package/dist/hooks/useBounce.d.ts.map +1 -0
  22. package/dist/hooks/useBounce.jsx +383 -0
  23. package/dist/hooks/useBounce.jsx.map +1 -0
  24. package/dist/hooks/useGCCollector.d.ts +18 -0
  25. package/dist/hooks/useGCCollector.d.ts.map +1 -0
  26. package/dist/hooks/useGCCollector.jsx +48 -0
  27. package/dist/hooks/useGCCollector.jsx.map +1 -0
  28. package/dist/hooks/useGlobalEventListener.d.ts +9 -0
  29. package/dist/hooks/useGlobalEventListener.d.ts.map +1 -0
  30. package/dist/hooks/useGlobalEventListener.jsx +19 -0
  31. package/dist/hooks/useGlobalEventListener.jsx.map +1 -0
  32. package/dist/hooks/useLatest.d.ts +6 -0
  33. package/dist/hooks/useLatest.d.ts.map +1 -0
  34. package/dist/hooks/useLatest.jsx +9 -0
  35. package/dist/hooks/useLatest.jsx.map +1 -0
  36. package/dist/hooks/useMemoizedFn.d.ts +8 -0
  37. package/dist/hooks/useMemoizedFn.d.ts.map +1 -0
  38. package/dist/hooks/useMemoizedFn.jsx +15 -0
  39. package/dist/hooks/useMemoizedFn.jsx.map +1 -0
  40. package/dist/hooks/usePrevious.d.ts +16 -0
  41. package/dist/hooks/usePrevious.d.ts.map +1 -0
  42. package/dist/hooks/usePrevious.jsx +11 -0
  43. package/dist/hooks/usePrevious.jsx.map +1 -0
  44. package/dist/hooks/useRefresh.d.ts +24 -0
  45. package/dist/hooks/useRefresh.d.ts.map +1 -0
  46. package/dist/hooks/useRefresh.jsx +554 -0
  47. package/dist/hooks/useRefresh.jsx.map +1 -0
  48. package/dist/hooks/useRegisteredEvents.d.ts +19 -0
  49. package/dist/hooks/useRegisteredEvents.d.ts.map +1 -0
  50. package/dist/hooks/useRegisteredEvents.jsx +24 -0
  51. package/dist/hooks/useRegisteredEvents.jsx.map +1 -0
  52. package/dist/hooks/useUnmount.d.ts +2 -0
  53. package/dist/hooks/useUnmount.d.ts.map +1 -0
  54. package/dist/hooks/useUnmount.jsx +11 -0
  55. package/dist/hooks/useUnmount.jsx.map +1 -0
  56. package/dist/index.d.ts +8 -0
  57. package/dist/index.d.ts.map +1 -0
  58. package/dist/index.jsx +6 -0
  59. package/dist/reactive/hooks/useReactiveValue.d.ts +22 -0
  60. package/dist/reactive/hooks/useReactiveValue.d.ts.map +1 -0
  61. package/dist/reactive/hooks/useReactiveValue.jsx +75 -0
  62. package/dist/reactive/hooks/useReactiveValue.jsx.map +1 -0
  63. package/dist/reactive/hooks/useReactiveValueChange.d.ts +4 -0
  64. package/dist/reactive/hooks/useReactiveValueChange.d.ts.map +1 -0
  65. package/dist/reactive/hooks/useReactiveValueChange.jsx +7 -0
  66. package/dist/reactive/hooks/useReactiveValueChange.jsx.map +1 -0
  67. package/dist/reactive/hooks/useReactiveValueEvent.d.ts +5 -0
  68. package/dist/reactive/hooks/useReactiveValueEvent.d.ts.map +1 -0
  69. package/dist/reactive/hooks/useReactiveValueEvent.jsx +15 -0
  70. package/dist/reactive/hooks/useReactiveValueEvent.jsx.map +1 -0
  71. package/dist/reactive/index.d.ts +6 -0
  72. package/dist/reactive/index.d.ts.map +1 -0
  73. package/dist/reactive/index.jsx +5 -0
  74. package/dist/reactive/types.d.ts +17 -0
  75. package/dist/reactive/types.d.ts.map +1 -0
  76. package/dist/reactive/types.jsx +0 -0
  77. package/dist/reactive/updateReactive.d.ts +4 -0
  78. package/dist/reactive/updateReactive.d.ts.map +1 -0
  79. package/dist/reactive/updateReactive.jsx +8 -0
  80. package/dist/reactive/updateReactive.jsx.map +1 -0
  81. package/dist/types/index.d.ts +2 -0
  82. package/dist/types/index.d.ts.map +1 -0
  83. package/dist/types/index.jsx +0 -0
  84. package/dist/types/interfaces/BaseScrollEvents.d.ts +40 -0
  85. package/dist/types/interfaces/BaseScrollEvents.d.ts.map +1 -0
  86. package/dist/types/interfaces/BaseScrollEvents.jsx +0 -0
  87. package/dist/types/interfaces/BaseUIEvents.d.ts +132 -0
  88. package/dist/types/interfaces/BaseUIEvents.d.ts.map +1 -0
  89. package/dist/types/interfaces/BaseUIEvents.jsx +0 -0
  90. package/dist/types/interfaces/BaseUIProps.d.ts +19 -0
  91. package/dist/types/interfaces/BaseUIProps.d.ts.map +1 -0
  92. package/dist/types/interfaces/BaseUIProps.jsx +0 -0
  93. package/dist/types/interfaces/BounceableInterface.d.ts +120 -0
  94. package/dist/types/interfaces/BounceableInterface.d.ts.map +1 -0
  95. package/dist/types/interfaces/BounceableInterface.jsx +0 -0
  96. package/dist/types/interfaces/EventMTSInterface.d.ts +1 -0
  97. package/dist/types/interfaces/EventMTSInterface.d.ts.map +1 -0
  98. package/dist/types/interfaces/EventMTSInterface.jsx +0 -0
  99. package/dist/types/interfaces/LazyInterface.d.ts +72 -0
  100. package/dist/types/interfaces/LazyInterface.d.ts.map +1 -0
  101. package/dist/types/interfaces/LazyInterface.jsx +0 -0
  102. package/dist/types/interfaces/RefreshInterface.d.ts +98 -0
  103. package/dist/types/interfaces/RefreshInterface.d.ts.map +1 -0
  104. package/dist/types/interfaces/RefreshInterface.jsx +10 -0
  105. package/dist/types/interfaces/RefreshInterface.jsx.map +1 -0
  106. package/dist/types/interfaces/index.d.ts +36 -0
  107. package/dist/types/interfaces/index.d.ts.map +1 -0
  108. package/dist/types/interfaces/index.jsx +0 -0
  109. package/dist/utils/common.d.ts +15 -0
  110. package/dist/utils/common.d.ts.map +1 -0
  111. package/dist/utils/common.jsx +12 -0
  112. package/dist/utils/common.jsx.map +1 -0
  113. package/dist/utils/common.test.jsx +55 -0
  114. package/dist/utils/common.test.jsx.map +1 -0
  115. package/dist/utils/convertToPx.d.ts +4 -0
  116. package/dist/utils/convertToPx.d.ts.map +1 -0
  117. package/dist/utils/convertToPx.jsx +20 -0
  118. package/dist/utils/convertToPx.jsx.map +1 -0
  119. package/dist/utils/delayFrames.d.ts +2 -0
  120. package/dist/utils/delayFrames.d.ts.map +1 -0
  121. package/dist/utils/delayFrames.jsx +12 -0
  122. package/dist/utils/delayFrames.jsx.map +1 -0
  123. package/dist/utils/getEventDetail.d.ts +23 -0
  124. package/dist/utils/getEventDetail.d.ts.map +1 -0
  125. package/dist/utils/getEventDetail.jsx +7 -0
  126. package/dist/utils/getEventDetail.jsx.map +1 -0
  127. package/dist/utils/index.d.ts +13 -0
  128. package/dist/utils/index.d.ts.map +1 -0
  129. package/dist/utils/index.jsx +12 -0
  130. package/dist/utils/interpolation.d.ts +40 -0
  131. package/dist/utils/interpolation.d.ts.map +1 -0
  132. package/dist/utils/interpolation.jsx +86 -0
  133. package/dist/utils/interpolation.jsx.map +1 -0
  134. package/dist/utils/interpolationJS.d.ts +39 -0
  135. package/dist/utils/interpolationJS.d.ts.map +1 -0
  136. package/dist/utils/interpolationJS.jsx +80 -0
  137. package/dist/utils/interpolationJS.jsx.map +1 -0
  138. package/dist/utils/log.d.ts +3 -0
  139. package/dist/utils/log.d.ts.map +1 -0
  140. package/dist/utils/log.jsx +10 -0
  141. package/dist/utils/log.jsx.map +1 -0
  142. package/dist/utils/mainThreadify.d.ts +2 -0
  143. package/dist/utils/mainThreadify.d.ts.map +1 -0
  144. package/dist/utils/mainThreadify.jsx +11 -0
  145. package/dist/utils/mainThreadify.jsx.map +1 -0
  146. package/dist/utils/popoverUtils.d.ts +3 -0
  147. package/dist/utils/popoverUtils.d.ts.map +1 -0
  148. package/dist/utils/popoverUtils.jsx +12 -0
  149. package/dist/utils/popoverUtils.jsx.map +1 -0
  150. package/dist/utils/renderContentWithExtraProps.d.ts +5 -0
  151. package/dist/utils/renderContentWithExtraProps.d.ts.map +1 -0
  152. package/dist/utils/renderContentWithExtraProps.jsx +10 -0
  153. package/dist/utils/renderContentWithExtraProps.jsx.map +1 -0
  154. package/dist/utils/selector.d.ts +36 -0
  155. package/dist/utils/selector.d.ts.map +1 -0
  156. package/dist/utils/selector.jsx +85 -0
  157. package/dist/utils/selector.jsx.map +1 -0
  158. package/dist/utils/selector.test.jsx +215 -0
  159. package/dist/utils/selector.test.jsx.map +1 -0
  160. package/dist/utils/version.d.ts +6 -0
  161. package/dist/utils/version.d.ts.map +1 -0
  162. package/dist/utils/version.jsx +28 -0
  163. package/dist/utils/version.jsx.map +1 -0
  164. package/dist/utils/version.test.jsx +77 -0
  165. package/dist/utils/version.test.jsx.map +1 -0
  166. package/package.json +52 -0
  167. package/src/const/BaseScrollEventsMapping.ts +14 -0
  168. package/src/const/BaseUIEventsMapping.ts +25 -0
  169. package/src/const/index.ts +6 -0
  170. package/src/hooks/docs/useBounce.mdx +2 -0
  171. package/src/hooks/docs/useRefresh.mdx +2 -0
  172. package/src/hooks/index.ts +96 -0
  173. package/src/hooks/useBounce.tsx +814 -0
  174. package/src/hooks/useGCCollector.tsx +76 -0
  175. package/src/hooks/useGlobalEventListener.ts +50 -0
  176. package/src/hooks/useLatest.ts +16 -0
  177. package/src/hooks/useMemoizedFn.ts +35 -0
  178. package/src/hooks/usePrevious.ts +28 -0
  179. package/src/hooks/useRefresh.ts +1054 -0
  180. package/src/hooks/useRegisteredEvents.ts +69 -0
  181. package/src/hooks/useUnmount.ts +18 -0
  182. package/src/index.tsx +25 -0
  183. package/src/reactive/hooks/useReactiveValue.ts +132 -0
  184. package/src/reactive/hooks/useReactiveValueChange.ts +15 -0
  185. package/src/reactive/hooks/useReactiveValueEvent.ts +26 -0
  186. package/src/reactive/index.ts +20 -0
  187. package/src/reactive/types.ts +29 -0
  188. package/src/reactive/updateReactive.ts +16 -0
  189. package/src/types/index.ts +5 -0
  190. package/src/types/interfaces/BaseScrollEvents.ts +47 -0
  191. package/src/types/interfaces/BaseUIEvents.ts +142 -0
  192. package/src/types/interfaces/BaseUIProps.ts +22 -0
  193. package/src/types/interfaces/BounceableInterface.ts +125 -0
  194. package/src/types/interfaces/EventMTSInterface.ts +35 -0
  195. package/src/types/interfaces/LazyInterface.tsx +78 -0
  196. package/src/types/interfaces/RefreshInterface.ts +105 -0
  197. package/src/types/interfaces/index.ts +44 -0
  198. package/src/utils/common.test.ts +64 -0
  199. package/src/utils/common.ts +35 -0
  200. package/src/utils/convertToPx.ts +32 -0
  201. package/src/utils/delayFrames.ts +24 -0
  202. package/src/utils/getEventDetail.ts +36 -0
  203. package/src/utils/index.ts +16 -0
  204. package/src/utils/interpolation.ts +222 -0
  205. package/src/utils/interpolationJS.ts +212 -0
  206. package/src/utils/log.ts +16 -0
  207. package/src/utils/mainThreadify.ts +17 -0
  208. package/src/utils/popoverUtils.ts +25 -0
  209. package/src/utils/renderContentWithExtraProps.ts +20 -0
  210. package/src/utils/selector.test.ts +237 -0
  211. package/src/utils/selector.ts +222 -0
  212. package/src/utils/version.test.ts +98 -0
  213. package/src/utils/version.ts +50 -0
  214. package/tsconfig.docs.json +7 -0
  215. package/tsconfig.json +12 -0
@@ -0,0 +1,222 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { interpolateJS } from './interpolationJS'
5
+
6
+ /**
7
+ * Extrapolation type.
8
+ *
9
+ * @param IDENTITY - Returns the provided value as is.
10
+ * @param CLAMP - Clamps the value to the edge of the output range.
11
+ * @param EXTEND - Predicts the values beyond the output range.
12
+ */
13
+ export enum Extrapolation {
14
+ IDENTITY = 'identity',
15
+ CLAMP = 'clamp',
16
+ EXTEND = 'extend',
17
+ }
18
+
19
+ /** Represents the possible values for extrapolation as a string. */
20
+ type ExtrapolationAsString = 'identity' | 'clamp' | 'extend'
21
+
22
+ interface InterpolationNarrowedInput {
23
+ leftEdgeInput: number
24
+ rightEdgeInput: number
25
+ leftEdgeOutput: number
26
+ rightEdgeOutput: number
27
+ }
28
+
29
+ /** Allows to specify extrapolation for left and right edge of the interpolation. */
30
+ export interface ExtrapolationConfig {
31
+ extrapolateLeft?: Extrapolation | string
32
+ extrapolateRight?: Extrapolation | string
33
+ }
34
+
35
+ interface RequiredExtrapolationConfig {
36
+ extrapolateLeft: Extrapolation
37
+ extrapolateRight: Extrapolation
38
+ }
39
+
40
+ /** Configuration options for extrapolation. */
41
+ export type ExtrapolationType =
42
+ | ExtrapolationConfig
43
+ | Extrapolation
44
+ | ExtrapolationAsString
45
+ | undefined
46
+
47
+ function getVal(
48
+ type: Extrapolation,
49
+ coefficient: number,
50
+ val: number,
51
+ leftEdgeOutput: number,
52
+ rightEdgeOutput: number,
53
+ x: number,
54
+ ): number {
55
+ 'main thread'
56
+
57
+ switch (type) {
58
+ case Extrapolation.IDENTITY:
59
+ return x
60
+ case Extrapolation.CLAMP:
61
+ if (coefficient * val < coefficient * leftEdgeOutput) {
62
+ return leftEdgeOutput
63
+ }
64
+ return rightEdgeOutput
65
+ default:
66
+ return val
67
+ }
68
+ }
69
+
70
+ function isExtrapolate(value: string): value is Extrapolation {
71
+ 'main thread'
72
+
73
+ return (
74
+ /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
75
+ value === Extrapolation.EXTEND
76
+ || value === Extrapolation.CLAMP
77
+ || value === Extrapolation.IDENTITY
78
+ /* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */
79
+ )
80
+ }
81
+
82
+ // validates extrapolations type
83
+ // if type is correct, converts it to ExtrapolationConfig
84
+ function validateType(type: ExtrapolationType): RequiredExtrapolationConfig {
85
+ 'main thread'
86
+ // initialize extrapolationConfig with default extrapolation
87
+ const extrapolationConfig: RequiredExtrapolationConfig = {
88
+ extrapolateLeft: Extrapolation.EXTEND,
89
+ extrapolateRight: Extrapolation.EXTEND,
90
+ }
91
+
92
+ if (!type) {
93
+ return extrapolationConfig
94
+ }
95
+
96
+ if (typeof type === 'string') {
97
+ if (!isExtrapolate(type)) {
98
+ throw new Error(
99
+ `Unsupported value for "interpolate" \nSupported values: ["extend", "clamp", "identity", Extrapolation.CLAMP, Extrapolation.EXTEND, Extrapolation.IDENTITY]\n Valid example:
100
+ interpolate(value, [inputRange], [outputRange], "clamp")`,
101
+ )
102
+ }
103
+ extrapolationConfig.extrapolateLeft = type
104
+ extrapolationConfig.extrapolateRight = type
105
+ return extrapolationConfig
106
+ }
107
+
108
+ // otherwise type is extrapolation config object
109
+ if (
110
+ (type.extrapolateLeft && !isExtrapolate(type.extrapolateLeft))
111
+ || (type.extrapolateRight && !isExtrapolate(type.extrapolateRight))
112
+ ) {
113
+ throw new Error(
114
+ `Unsupported value for "interpolate" \nSupported values: ["extend", "clamp", "identity", Extrapolation.CLAMP, Extrapolation.EXTEND, Extrapolation.IDENTITY]\n Valid example:
115
+ interpolate(value, [inputRange], [outputRange], {
116
+ extrapolateLeft: Extrapolation.CLAMP,
117
+ extrapolateRight: Extrapolation.IDENTITY
118
+ }})`,
119
+ )
120
+ }
121
+
122
+ Object.assign(extrapolationConfig, type)
123
+ return extrapolationConfig
124
+ }
125
+
126
+ function internalInterpolate(
127
+ x: number,
128
+ narrowedInput: InterpolationNarrowedInput,
129
+ extrapolationConfig: RequiredExtrapolationConfig,
130
+ ) {
131
+ 'main thread'
132
+ const { leftEdgeInput, rightEdgeInput, leftEdgeOutput, rightEdgeOutput } =
133
+ narrowedInput
134
+ if (rightEdgeInput - leftEdgeInput === 0) {
135
+ return leftEdgeOutput
136
+ }
137
+ const progress = (x - leftEdgeInput) / (rightEdgeInput - leftEdgeInput)
138
+ const val = leftEdgeOutput + progress * (rightEdgeOutput - leftEdgeOutput)
139
+ const coefficient = rightEdgeOutput >= leftEdgeOutput ? 1 : -1
140
+
141
+ if (coefficient * val < coefficient * leftEdgeOutput) {
142
+ return getVal(
143
+ extrapolationConfig.extrapolateLeft,
144
+ coefficient,
145
+ val,
146
+ leftEdgeOutput,
147
+ rightEdgeOutput,
148
+ x,
149
+ )
150
+ } else if (coefficient * val > coefficient * rightEdgeOutput) {
151
+ return getVal(
152
+ extrapolationConfig.extrapolateRight,
153
+ coefficient,
154
+ val,
155
+ leftEdgeOutput,
156
+ rightEdgeOutput,
157
+ x,
158
+ )
159
+ }
160
+
161
+ return val
162
+ }
163
+
164
+ /**
165
+ * Lets you map a value from one range to another using linear interpolation.
166
+ *
167
+ * @param value - A number from the `input` range that is going to be mapped to
168
+ * the `output` range.
169
+ * @param inputRange - An array of numbers specifying the input range of the
170
+ * interpolation.
171
+ * @param outputRange - An array of numbers specifying the output range of the
172
+ * interpolation.
173
+ * @param extrapolate - Determines what happens when the `value` goes beyond the
174
+ * `input` range. Defaults to `Extrapolation.EXTEND` -
175
+ * {@link ExtrapolationType}.
176
+ * @returns A mapped value within the output range.
177
+ * @see https://docs.swmansion.com/react-native-reanimated/docs/utilities/interpolate
178
+ */
179
+ export function interpolate(
180
+ x: number,
181
+ inputRange: readonly number[],
182
+ outputRange: readonly number[],
183
+ type?: ExtrapolationType,
184
+ ): number {
185
+ 'main thread'
186
+ if (inputRange.length < 2 || outputRange.length < 2) {
187
+ throw new Error(
188
+ 'Interpolation input and output ranges should contain at least two values.',
189
+ )
190
+ }
191
+
192
+ const extrapolationConfig = validateType(type)
193
+ const length = inputRange.length
194
+ const narrowedInput: InterpolationNarrowedInput = {
195
+ leftEdgeInput: inputRange[0],
196
+ rightEdgeInput: inputRange[1],
197
+ leftEdgeOutput: outputRange[0],
198
+ rightEdgeOutput: outputRange[1],
199
+ }
200
+ if (length > 2) {
201
+ if (x > inputRange[length - 1]) {
202
+ narrowedInput.leftEdgeInput = inputRange[length - 2]
203
+ narrowedInput.rightEdgeInput = inputRange[length - 1]
204
+ narrowedInput.leftEdgeOutput = outputRange[length - 2]
205
+ narrowedInput.rightEdgeOutput = outputRange[length - 1]
206
+ } else {
207
+ for (let i = 1; i < length; ++i) {
208
+ if (x <= inputRange[i]) {
209
+ narrowedInput.leftEdgeInput = inputRange[i - 1]
210
+ narrowedInput.rightEdgeInput = inputRange[i]
211
+ narrowedInput.leftEdgeOutput = outputRange[i - 1]
212
+ narrowedInput.rightEdgeOutput = outputRange[i]
213
+ break
214
+ }
215
+ }
216
+ }
217
+ }
218
+
219
+ return internalInterpolate(x, narrowedInput, extrapolationConfig)
220
+ }
221
+
222
+ export { interpolateJS }
@@ -0,0 +1,212 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ /**
6
+ * Extrapolation type.
7
+ *
8
+ * @param IDENTITY - Returns the provided value as is.
9
+ * @param CLAMP - Clamps the value to the edge of the output range.
10
+ * @param EXTEND - Predicts the values beyond the output range.
11
+ */
12
+ export enum Extrapolation {
13
+ IDENTITY = 'identity',
14
+ CLAMP = 'clamp',
15
+ EXTEND = 'extend',
16
+ }
17
+
18
+ /** Represents the possible values for extrapolation as a string. */
19
+ type ExtrapolationAsString = 'identity' | 'clamp' | 'extend'
20
+
21
+ interface InterpolationNarrowedInput {
22
+ leftEdgeInput: number
23
+ rightEdgeInput: number
24
+ leftEdgeOutput: number
25
+ rightEdgeOutput: number
26
+ }
27
+
28
+ /** Allows to specify extrapolation for left and right edge of the interpolation. */
29
+ export interface ExtrapolationConfig {
30
+ extrapolateLeft?: Extrapolation | string
31
+ extrapolateRight?: Extrapolation | string
32
+ }
33
+
34
+ interface RequiredExtrapolationConfig {
35
+ extrapolateLeft: Extrapolation
36
+ extrapolateRight: Extrapolation
37
+ }
38
+
39
+ /** Configuration options for extrapolation. */
40
+ export type ExtrapolationType =
41
+ | ExtrapolationConfig
42
+ | Extrapolation
43
+ | ExtrapolationAsString
44
+ | undefined
45
+
46
+ function getVal(
47
+ type: Extrapolation,
48
+ coef: number,
49
+ val: number,
50
+ leftEdgeOutput: number,
51
+ rightEdgeOutput: number,
52
+ x: number,
53
+ ): number {
54
+ switch (type) {
55
+ case Extrapolation.IDENTITY:
56
+ return x
57
+ case Extrapolation.CLAMP:
58
+ if (coef * val < coef * leftEdgeOutput) {
59
+ return leftEdgeOutput
60
+ }
61
+ return rightEdgeOutput
62
+ default:
63
+ return val
64
+ }
65
+ }
66
+
67
+ function isExtrapolate(value: string): value is Extrapolation {
68
+ return (
69
+ /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
70
+ value === Extrapolation.EXTEND
71
+ || value === Extrapolation.CLAMP
72
+ || value === Extrapolation.IDENTITY
73
+ /* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */
74
+ )
75
+ }
76
+
77
+ // validates extrapolations type
78
+ // if type is correct, converts it to ExtrapolationConfig
79
+ function validateType(type: ExtrapolationType): RequiredExtrapolationConfig {
80
+ // initialize extrapolationConfig with default extrapolation
81
+ const extrapolationConfig: RequiredExtrapolationConfig = {
82
+ extrapolateLeft: Extrapolation.EXTEND,
83
+ extrapolateRight: Extrapolation.EXTEND,
84
+ }
85
+
86
+ if (!type) {
87
+ return extrapolationConfig
88
+ }
89
+
90
+ if (typeof type === 'string') {
91
+ if (!isExtrapolate(type)) {
92
+ throw new Error(
93
+ `Unsupported value for "interpolate" \nSupported values: ["extend", "clamp", "identity", Extrapolation.CLAMP, Extrapolation.EXTEND, Extrapolation.IDENTITY]\n Valid example:
94
+ interpolate(value, [inputRange], [outputRange], "clamp")`,
95
+ )
96
+ }
97
+ extrapolationConfig.extrapolateLeft = type
98
+ extrapolationConfig.extrapolateRight = type
99
+ return extrapolationConfig
100
+ }
101
+
102
+ // otherwise type is extrapolation config object
103
+ if (
104
+ (type.extrapolateLeft && !isExtrapolate(type.extrapolateLeft))
105
+ || (type.extrapolateRight && !isExtrapolate(type.extrapolateRight))
106
+ ) {
107
+ throw new Error(
108
+ `Unsupported value for "interpolate" \nSupported values: ["extend", "clamp", "identity", Extrapolation.CLAMP, Extrapolation.EXTEND, Extrapolation.IDENTITY]\n Valid example:
109
+ interpolate(value, [inputRange], [outputRange], {
110
+ extrapolateLeft: Extrapolation.CLAMP,
111
+ extrapolateRight: Extrapolation.IDENTITY
112
+ }})`,
113
+ )
114
+ }
115
+
116
+ Object.assign(extrapolationConfig, type)
117
+ return extrapolationConfig
118
+ }
119
+
120
+ function internalInterpolate(
121
+ x: number,
122
+ narrowedInput: InterpolationNarrowedInput,
123
+ extrapolationConfig: RequiredExtrapolationConfig,
124
+ ) {
125
+ const { leftEdgeInput, rightEdgeInput, leftEdgeOutput, rightEdgeOutput } =
126
+ narrowedInput
127
+ if (rightEdgeInput - leftEdgeInput === 0) {
128
+ return leftEdgeOutput
129
+ }
130
+ const progress = (x - leftEdgeInput) / (rightEdgeInput - leftEdgeInput)
131
+ const val = leftEdgeOutput + progress * (rightEdgeOutput - leftEdgeOutput)
132
+ const coef = rightEdgeOutput >= leftEdgeOutput ? 1 : -1
133
+
134
+ if (coef * val < coef * leftEdgeOutput) {
135
+ return getVal(
136
+ extrapolationConfig.extrapolateLeft,
137
+ coef,
138
+ val,
139
+ leftEdgeOutput,
140
+ rightEdgeOutput,
141
+ x,
142
+ )
143
+ } else if (coef * val > coef * rightEdgeOutput) {
144
+ return getVal(
145
+ extrapolationConfig.extrapolateRight,
146
+ coef,
147
+ val,
148
+ leftEdgeOutput,
149
+ rightEdgeOutput,
150
+ x,
151
+ )
152
+ }
153
+
154
+ return val
155
+ }
156
+
157
+ /**
158
+ * Lets you map a value from one range to another using linear interpolation.
159
+ *
160
+ * @param value - A number from the `input` range that is going to be mapped to
161
+ * the `output` range.
162
+ * @param inputRange - An array of numbers specifying the input range of the
163
+ * interpolation.
164
+ * @param outputRange - An array of numbers specifying the output range of the
165
+ * interpolation.
166
+ * @param extrapolate - Determines what happens when the `value` goes beyond the
167
+ * `input` range. Defaults to `Extrapolation.EXTEND` -
168
+ * {@link ExtrapolationType}.
169
+ * @returns A mapped value within the output range.
170
+ * @see https://docs.swmansion.com/react-native-reanimated/docs/utilities/interpolate
171
+ */
172
+ export function interpolateJS(
173
+ x: number,
174
+ inputRange: readonly number[],
175
+ outputRange: readonly number[],
176
+ type?: ExtrapolationType,
177
+ ): number {
178
+ if (inputRange.length < 2 || outputRange.length < 2) {
179
+ throw new Error(
180
+ 'Interpolation input and output ranges should contain at least two values.',
181
+ )
182
+ }
183
+
184
+ const extrapolationConfig = validateType(type)
185
+ const length = inputRange.length
186
+ const narrowedInput: InterpolationNarrowedInput = {
187
+ leftEdgeInput: inputRange[0],
188
+ rightEdgeInput: inputRange[1],
189
+ leftEdgeOutput: outputRange[0],
190
+ rightEdgeOutput: outputRange[1],
191
+ }
192
+ if (length > 2) {
193
+ if (x > inputRange[length - 1]) {
194
+ narrowedInput.leftEdgeInput = inputRange[length - 2]
195
+ narrowedInput.rightEdgeInput = inputRange[length - 1]
196
+ narrowedInput.leftEdgeOutput = outputRange[length - 2]
197
+ narrowedInput.rightEdgeOutput = outputRange[length - 1]
198
+ } else {
199
+ for (let i = 1; i < length; ++i) {
200
+ if (x <= inputRange[i]) {
201
+ narrowedInput.leftEdgeInput = inputRange[i - 1]
202
+ narrowedInput.rightEdgeInput = inputRange[i]
203
+ narrowedInput.leftEdgeOutput = outputRange[i - 1]
204
+ narrowedInput.rightEdgeOutput = outputRange[i]
205
+ break
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+ return internalInterpolate(x, narrowedInput, extrapolationConfig)
212
+ }
@@ -0,0 +1,16 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ export const log = (showLog: boolean, ...data: unknown[]): void => {
6
+ if (showLog) {
7
+ console.info(...data)
8
+ }
9
+ }
10
+
11
+ export const mtsLog = (showLog: boolean, ...data: unknown[]): void => {
12
+ 'main thread'
13
+ if (showLog) {
14
+ console.info(...data)
15
+ }
16
+ }
@@ -0,0 +1,17 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ export const mainThreadifyEventsMapping = (
6
+ rawEvents: Record<string, string>,
7
+ ) => {
8
+ let mainThreadifyEvents: Record<string, string> = {}
9
+ mainThreadifyEvents = Object.entries(rawEvents).reduce(
10
+ (EventsWithMainThread: Record<string, string>, [key, value]) => {
11
+ EventsWithMainThread[`main-thread:${key}`] = `main-thread:${value}`
12
+ return EventsWithMainThread
13
+ },
14
+ {},
15
+ )
16
+ return mainThreadifyEvents
17
+ }
@@ -0,0 +1,25 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ const overlayModeMap = new Map<string, string>()
6
+
7
+ export const registerOverlayMode = (
8
+ mode: string,
9
+ controllerName: string,
10
+ ): void => {
11
+ overlayModeMap.set(mode, controllerName)
12
+ }
13
+
14
+ export const convertOverlayMode = (
15
+ mode: string,
16
+ ): string | undefined => {
17
+ if (!mode) {
18
+ return undefined
19
+ }
20
+ // only iOS need this info
21
+ if (overlayModeMap.has(mode)) {
22
+ return overlayModeMap.get(mode)
23
+ }
24
+ return mode
25
+ }
@@ -0,0 +1,20 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import type { ReactNode } from '@lynx-js/react'
6
+
7
+ export function renderContentWithExtraProps<T, U = { active: boolean }>(
8
+ extraProps: T,
9
+ renderFunction: ((innerProps: T & U) => ReactNode) | ReactNode,
10
+ ) {
11
+ const createContent = (
12
+ extraProps: T,
13
+ renderFunction: (innerProps: T & U) => ReactNode,
14
+ ) =>
15
+ (props: U) => renderFunction({ ...extraProps, ...props })
16
+
17
+ return typeof renderFunction === 'function'
18
+ ? createContent(extraProps, renderFunction)
19
+ : renderFunction
20
+ }