UNPKG

8.88 kBPlain TextView Raw
1'use strict';
2import type { ViewStyle, TextStyle } from 'react-native';
3
4export type RequiredKeys<T, K extends keyof T> = T & Required<Pick<T, K>>;
5export interface StyleProps extends ViewStyle, TextStyle {
6 originX?: number;
7 originY?: number;
8 [key: string]: any;
9}
10
11/**
12 * A value that can be used both on the [JavaScript thread](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#javascript-thread) and the [UI thread](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#ui-thread).
13 *
14 * Shared values are defined using [useSharedValue](https://docs.swmansion.com/react-native-reanimated/docs/core/useSharedValue) hook. You access and modify shared values by their `.value` property.
15 */
16export interface SharedValue<Value = unknown> {
17 value: Value;
18 addListener: (listenerID: number, listener: (value: Value) => void) => void;
19 removeListener: (listenerID: number) => void;
20 modify: (
21 modifier?: <T extends Value>(value: T) => T,
22 forceUpdate?: boolean
23 ) => void;
24}
25
26export interface Mutable<Value = unknown> extends SharedValue<Value> {
27 _isReanimatedSharedValue: true;
28 _animation?: AnimationObject<Value> | null; // only in Native
29 _value: Value;
30}
31
32// The below type is used for HostObjects returned by the JSI API that don't have
33// any accessible fields or methods but can carry data that is accessed from the
34// c++ side. We add a field to the type to make it possible for typescript to recognize
35// which JSI methods accept those types as arguments and to be able to correctly type
36// check other methods that may use them. However, this field is not actually defined
37// nor should be used for anything else as assigning any data to those objects will
38// throw an error.
39export type ShareableRef<T = unknown> = {
40 __hostObjectShareableJSRef: T;
41};
42
43// In case of objects with depth or arrays of objects or arrays of arrays etc.
44// we add this utility type that makes it a SharaebleRef of the outermost type.
45export type FlatShareableRef<T> = T extends ShareableRef<infer U>
46 ? ShareableRef<U>
47 : ShareableRef<T>;
48
49export type MapperRawInputs = unknown[];
50
51export type MapperOutputs = SharedValue[];
52
53export type MapperRegistry = {
54 start: (
55 mapperID: number,
56 worklet: () => void,
57 inputs: MapperRawInputs,
58 outputs?: MapperOutputs
59 ) => void;
60 stop: (mapperID: number) => void;
61};
62
63export type WorkletStackDetails = [
64 error: Error,
65 lineOffset: number,
66 columnOffset: number
67];
68
69type WorkletClosure = Record<string, unknown>;
70
71interface WorkletInitDataCommon {
72 code: string;
73}
74
75type WorkletInitDataRelease = WorkletInitDataCommon;
76
77interface WorkletInitDataDev extends WorkletInitDataCommon {
78 location: string;
79 sourceMap: string;
80 version: string;
81}
82
83interface WorkletBaseCommon {
84 __closure: WorkletClosure;
85 __workletHash: number;
86}
87
88interface WorkletBaseRelease extends WorkletBaseCommon {
89 __initData: WorkletInitDataRelease;
90}
91
92interface WorkletBaseDev extends WorkletBaseCommon {
93 __initData: WorkletInitDataDev;
94 /**
95 * `__stackDetails` is removed after parsing.
96 */
97 __stackDetails?: WorkletStackDetails;
98}
99
100export type WorkletFunction<
101 Args extends unknown[] = unknown[],
102 ReturnValue = unknown
103> = ((...args: Args) => ReturnValue) & (WorkletBaseRelease | WorkletBaseDev);
104
105/**
106 * This function allows you to determine if a given function is a worklet. It only works
107 * with Reanimated Babel plugin enabled. Unless you are doing something with internals of
108 * Reanimated you shouldn't need to use this function.
109 *
110 * ### Note
111 * Do not call it before the worklet is declared, as it will always return false then. E.g.:
112 *
113 * ```ts
114 * isWorkletFunction(myWorklet); // Will always return false.
115 *
116 * function myWorklet() {
117 * 'worklet';
118 * };
119 * ```
120 *
121 * ### Maintainer note
122 * This function works well on the JS thread performance-wise, since the JIT can inline it.
123 * However, on other threads it will not get optimized and we will get a function call overhead.
124 * We want to change it in the future, but it's not feasible at the moment.
125 */
126export function isWorkletFunction<
127 Args extends unknown[] = unknown[],
128 ReturnValue = unknown,
129 BuildType extends WorkletBaseDev | WorkletBaseRelease = WorkletBaseDev
130>(value: unknown): value is WorkletFunction<Args, ReturnValue> & BuildType {
131 'worklet';
132 // Since host objects always return true for `in` operator, we have to use dot notation to check if the property exists.
133 // See https://github.com/facebook/hermes/blob/340726ef8cf666a7cce75bc60b02fa56b3e54560/lib/VM/JSObject.cpp#L1276.
134 return (
135 typeof value === 'function' &&
136 !!(value as unknown as Record<string, unknown>).__workletHash
137 );
138}
139
140export type AnimatedPropsAdapterFunction = (
141 props: Record<string, unknown>
142) => void;
143
144export type AnimatedPropsAdapterWorklet = WorkletFunction<
145 [props: Record<string, unknown>],
146 void
147>;
148
149export interface NestedObject<T> {
150 [key: string]: NestedObjectValues<T>;
151}
152
153export type NestedObjectValues<T> =
154 | T
155 | Array<NestedObjectValues<T>>
156 | NestedObject<T>;
157
158type Animatable = number | string | Array<number>;
159
160export type AnimatableValueObject = { [key: string]: Animatable };
161
162export type AnimatableValue = Animatable | AnimatableValueObject;
163
164export interface AnimationObject<T = AnimatableValue> {
165 [key: string]: any;
166 callback?: AnimationCallback;
167 current?: T;
168 toValue?: AnimationObject<T>['current'];
169 startValue?: AnimationObject<T>['current'];
170 finished?: boolean;
171 strippedCurrent?: number;
172 cancelled?: boolean;
173 reduceMotion?: boolean;
174
175 __prefix?: string;
176 __suffix?: string;
177 onFrame: (animation: any, timestamp: Timestamp) => boolean;
178 onStart: (
179 nextAnimation: any,
180 current: any,
181 timestamp: Timestamp,
182 previousAnimation: any
183 ) => void;
184}
185
186export interface Animation<T extends AnimationObject> extends AnimationObject {
187 onFrame: (animation: T, timestamp: Timestamp) => boolean;
188 onStart: (
189 nextAnimation: T,
190 current: AnimatableValue,
191 timestamp: Timestamp,
192 previousAnimation: Animation<any> | null | T
193 ) => void;
194}
195
196export enum SensorType {
197 ACCELEROMETER = 1,
198 GYROSCOPE = 2,
199 GRAVITY = 3,
200 MAGNETIC_FIELD = 4,
201 ROTATION = 5,
202}
203export enum IOSReferenceFrame {
204 XArbitraryZVertical,
205 XArbitraryCorrectedZVertical,
206 XMagneticNorthZVertical,
207 XTrueNorthZVertical,
208 Auto,
209}
210
211export type SensorConfig = {
212 interval: number | 'auto';
213 adjustToInterfaceOrientation: boolean;
214 iosReferenceFrame: IOSReferenceFrame;
215};
216
217export type AnimatedSensor<T extends Value3D | ValueRotation> = {
218 sensor: SharedValue<T>;
219 unregister: () => void;
220 isAvailable: boolean;
221 config: SensorConfig;
222};
223
224/**
225 * A function called upon animation completion. If the animation is cancelled, the callback will receive `false` as the argument; otherwise, it will receive `true`.
226 */
227export type AnimationCallback = (
228 finished?: boolean,
229 current?: AnimatableValue
230) => void;
231
232export type Timestamp = number;
233
234export type Value3D = {
235 x: number;
236 y: number;
237 z: number;
238 interfaceOrientation: InterfaceOrientation;
239};
240
241export type ValueRotation = {
242 qw: number;
243 qx: number;
244 qy: number;
245 qz: number;
246 yaw: number;
247 pitch: number;
248 roll: number;
249 interfaceOrientation: InterfaceOrientation;
250};
251
252export enum InterfaceOrientation {
253 ROTATION_0 = 0,
254 ROTATION_90 = 90,
255 ROTATION_180 = 180,
256 ROTATION_270 = 270,
257}
258
259export type ShadowNodeWrapper = {
260 __hostObjectShadowNodeWrapper: never;
261};
262
263export enum KeyboardState {
264 UNKNOWN = 0,
265 OPENING = 1,
266 OPEN = 2,
267 CLOSING = 3,
268 CLOSED = 4,
269}
270
271export type AnimatedKeyboardInfo = {
272 height: SharedValue<number>;
273 state: SharedValue<KeyboardState>;
274};
275
276/**
277 * @param x - A number representing X coordinate relative to the parent component.
278 * @param y - A number representing Y coordinate relative to the parent component.
279 * @param width - A number representing the width of the component.
280 * @param height - A number representing the height of the component.
281 * @param pageX - A number representing X coordinate relative to the screen.
282 * @param pageY - A number representing Y coordinate relative to the screen.
283 * @see https://docs.swmansion.com/react-native-reanimated/docs/advanced/measure#returns
284 */
285export interface MeasuredDimensions {
286 x: number;
287 y: number;
288 width: number;
289 height: number;
290 pageX: number;
291 pageY: number;
292}
293
294export interface AnimatedKeyboardOptions {
295 isStatusBarTranslucentAndroid?: boolean;
296}
297
298/**
299 * @param System - If the `Reduce motion` accessibility setting is enabled on the device, disable the animation. Otherwise, enable the animation.
300 * @param Always - Disable the animation.
301 * @param Never - Enable the animation.
302 * @see https://docs.swmansion.com/react-native-reanimated/docs/guides/accessibility
303 */
304export enum ReduceMotion {
305 System = 'system',
306 Always = 'always',
307 Never = 'never',
308}