UNPKG

6.44 kBPlain TextView Raw
1'use strict';
2/*
3This file is a legacy remainder of manual types from react-native-reanimated.d.ts file.
4I wasn't able to get rid of all of them from the code.
5They should be treated as a temporary solution
6until time comes to refactor the code and get necessary types right.
7This will not be easy though!
8*/
9
10import type {
11 ImageStyle,
12 RegisteredStyle,
13 StyleProp,
14 TextStyle,
15 TransformsStyle,
16 ViewStyle,
17} from 'react-native';
18import type { AnimatableValue, SharedValue } from './commonTypes';
19import type { BaseAnimationBuilder } from './layoutReanimation/animationBuilder/BaseAnimationBuilder';
20import type {
21 EntryExitAnimationFunction,
22 LayoutAnimationFunction,
23} from './layoutReanimation/animationBuilder/commonTypes';
24import type { ReanimatedKeyframe } from './layoutReanimation/animationBuilder/Keyframe';
25import type { SharedTransition } from './layoutReanimation/sharedTransitions';
26
27export type TransformArrayItem = Extract<
28 TransformsStyle['transform'],
29 Array<unknown>
30>[number];
31
32export type AnimatedTransform = MaybeSharedValueRecursive<
33 TransformsStyle['transform']
34>;
35
36type MaybeSharedValue<Value> =
37 | Value
38 | (Value extends AnimatableValue ? SharedValue<Value> : never);
39
40type MaybeSharedValueRecursive<Value> = Value extends (infer Item)[]
41 ? SharedValue<Item[]> | (MaybeSharedValueRecursive<Item> | Item)[]
42 : Value extends object
43 ?
44 | SharedValue<Value>
45 | {
46 [Key in keyof Value]:
47 | MaybeSharedValueRecursive<Value[Key]>
48 | Value[Key];
49 }
50 : MaybeSharedValue<Value>;
51
52type DefaultStyle = ViewStyle & ImageStyle & TextStyle;
53
54// Ideally we want AnimatedStyle to not be generic, but there are
55// so many depenedencies on it being generic that it's not feasible at the moment.
56export type AnimatedStyle<Style = DefaultStyle> =
57 | Style
58 | MaybeSharedValueRecursive<Style>;
59
60type EntryOrExitLayoutType =
61 | BaseAnimationBuilder
62 | typeof BaseAnimationBuilder
63 | EntryExitAnimationFunction
64 | ReanimatedKeyframe;
65
66/*
67 Style type properties (properties that extends StyleProp<ViewStyle>)
68 can be defined with other property names than "style". For example `contentContainerStyle` in FlatList.
69 Type definition for all style type properties should act similarly, hence we
70 pick keys with 'Style' substring with the use of this utility type.
71*/
72type PickStyleProps<Props> = Pick<
73 Props,
74 {
75 [Key in keyof Props]-?: Key extends `${string}Style` | 'style'
76 ? Key
77 : never;
78 }[keyof Props]
79>;
80
81type AnimatedStyleProps<Props extends object> = {
82 [Key in keyof PickStyleProps<Props>]: StyleProp<AnimatedStyle<Props[Key]>>;
83};
84
85/**
86 * Component props that are not specially handled by us.
87 */
88type RestProps<Props extends object> = {
89 [K in keyof Omit<Props, keyof PickStyleProps<Props> | 'style'>]:
90 | Props[K]
91 | SharedValue<Props[K]>;
92};
93
94type LayoutProps = {
95 /**
96 * Lets you animate the layout changes when components are added to or removed from the view hierarchy.
97 *
98 * You can use the predefined layout transitions (eg. `LinearTransition`, `FadingTransition`) or create your own ones.
99 *
100 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/layout-transitions
101 */
102 layout?:
103 | BaseAnimationBuilder
104 | LayoutAnimationFunction
105 | typeof BaseAnimationBuilder;
106 /**
107 * Lets you animate an element when it's added to or removed from the view hierarchy.
108 *
109 * You can use the predefined entering animations (eg. `FadeIn`, `SlideInLeft`) or create your own ones.
110 *
111 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations
112 */
113 entering?: EntryOrExitLayoutType;
114 /**
115 * Lets you animate an element when it's added to or removed from the view hierarchy.
116 *
117 * You can use the predefined entering animations (eg. `FadeOut`, `SlideOutRight`) or create your own ones.
118 *
119 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/entering-exiting-animations
120 */
121 exiting?: EntryOrExitLayoutType;
122};
123
124type SharedTransitionProps = {
125 /**
126 * Lets you animate components between two navigation screens.
127 *
128 * Assign the same `sharedTransitionTag` to [animated components](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component) on two different navigation screens to create a shared transition.
129 *
130 * @see https://docs.swmansion.com/react-native-reanimated/docs/shared-element-transitions/overview
131 * @experimental
132 */
133 sharedTransitionTag?: string;
134 /**
135 * Lets you create a custom shared transition animation.
136 *
137 * Used alongside `SharedTransition.custom()` method.
138 *
139 * @see https://docs.swmansion.com/react-native-reanimated/docs/shared-element-transitions/overview
140 * @experimental
141 */
142 sharedTransitionStyle?: SharedTransition;
143};
144
145type AnimatedPropsProp<Props extends object> = RestProps<Props> &
146 AnimatedStyleProps<Props> &
147 LayoutProps &
148 SharedTransitionProps;
149
150export type AnimatedProps<Props extends object> = RestProps<Props> &
151 AnimatedStyleProps<Props> &
152 LayoutProps &
153 SharedTransitionProps & {
154 /**
155 * Lets you animate component props.
156 *
157 * @see https://docs.swmansion.com/react-native-reanimated/docs/core/useAnimatedProps
158 */
159 animatedProps?: Partial<AnimatedPropsProp<Props>>;
160 };
161
162// THE LAND OF THE DEPRECATED
163
164/**
165 * @deprecated This type is no longer relevant.
166 */
167export type Adaptable<T> =
168 | T
169 | ReadonlyArray<T | ReadonlyArray<T>>
170 | SharedValue<T>;
171
172/**
173 * @deprecated This type is no longer relevant.
174 */
175export type AdaptTransforms<T> = {
176 [P in keyof T]: Adaptable<T[P]>;
177};
178
179/**
180 * @deprecated Please use {@link TransformArrayItem} type instead.
181 */
182export type TransformStyleTypes = TransformArrayItem;
183
184/**
185 * @deprecated Please use {@link AnimatedStyle} type instead.
186 */
187export type AnimateStyle<Style = DefaultStyle> = AnimatedStyle<Style>;
188
189/**
190 * @deprecated This type is no longer relevant.
191 */
192export type StylesOrDefault<T> = 'style' extends keyof T
193 ? MaybeSharedValueRecursive<T['style']>
194 : Record<string, unknown>;
195
196/**
197 * @deprecated This type is no longer relevant.
198 */
199export type AnimatedStyleProp<T> =
200 | AnimatedStyle<T>
201 | RegisteredStyle<AnimatedStyle<T>>;
202
203/**
204 * @deprecated Please use {@link AnimatedProps} type instead.
205 */
206export type AnimateProps<Props extends object> = AnimatedProps<Props>;