1 | 'use strict';
|
2 | import type {
|
3 | ILayoutAnimationBuilder,
|
4 | LayoutAnimationFunction,
|
5 | LayoutAnimationsValues,
|
6 | } from './reanimated2/layoutReanimation';
|
7 | import type { StyleProps } from './reanimated2/commonTypes';
|
8 | import type { NestedArray } from './createAnimatedComponent/commonTypes';
|
9 |
|
10 | const mockTargetValues: LayoutAnimationsValues = {
|
11 | targetOriginX: 0,
|
12 | targetOriginY: 0,
|
13 | targetWidth: 0,
|
14 | targetHeight: 0,
|
15 | targetGlobalOriginX: 0,
|
16 | targetGlobalOriginY: 0,
|
17 | targetBorderRadius: 0,
|
18 | windowWidth: 0,
|
19 | windowHeight: 0,
|
20 | currentOriginX: 0,
|
21 | currentOriginY: 0,
|
22 | currentWidth: 0,
|
23 | currentHeight: 0,
|
24 | currentGlobalOriginX: 0,
|
25 | currentGlobalOriginY: 0,
|
26 | currentBorderRadius: 0,
|
27 | };
|
28 |
|
29 | function getCommonProperties(
|
30 | layoutStyle: StyleProps,
|
31 | componentStyle: StyleProps | Array<StyleProps>
|
32 | ) {
|
33 | let componentStyleFlat = Array.isArray(componentStyle)
|
34 | ? componentStyle.flat()
|
35 | : [componentStyle];
|
36 |
|
37 | componentStyleFlat = componentStyleFlat.filter(Boolean);
|
38 |
|
39 | componentStyleFlat = componentStyleFlat.map((style) =>
|
40 | 'initial' in style
|
41 | ? style.initial.value
|
42 | : style
|
43 | );
|
44 |
|
45 | const componentStylesKeys = componentStyleFlat.flatMap((style) =>
|
46 | Object.keys(style)
|
47 | );
|
48 |
|
49 | const commonKeys = Object.keys(layoutStyle).filter((key) =>
|
50 | componentStylesKeys.includes(key)
|
51 | );
|
52 |
|
53 | return commonKeys;
|
54 | }
|
55 |
|
56 | function maybeReportOverwrittenProperties(
|
57 | layoutAnimationStyle: StyleProps,
|
58 | style: NestedArray<StyleProps>,
|
59 | displayName: string
|
60 | ) {
|
61 | const commonProperties = getCommonProperties(layoutAnimationStyle, style);
|
62 |
|
63 | if (commonProperties.length > 0) {
|
64 | console.warn(
|
65 | `[Reanimated] ${
|
66 | commonProperties.length === 1 ? 'Property' : 'Properties'
|
67 | } "${commonProperties.join(
|
68 | ', '
|
69 | )}" of ${displayName} may be overwritten by a layout animation. Please wrap your component with an animated view and apply the layout animation on the wrapper.`
|
70 | );
|
71 | }
|
72 | }
|
73 |
|
74 | export function maybeBuild(
|
75 | layoutAnimationOrBuilder:
|
76 | | ILayoutAnimationBuilder
|
77 | | LayoutAnimationFunction
|
78 | | Keyframe,
|
79 | style: NestedArray<StyleProps> | undefined,
|
80 | displayName: string
|
81 | ): LayoutAnimationFunction | Keyframe {
|
82 | const isAnimationBuilder = (
|
83 | value: ILayoutAnimationBuilder | LayoutAnimationFunction | Keyframe
|
84 | ): value is ILayoutAnimationBuilder =>
|
85 | 'build' in layoutAnimationOrBuilder &&
|
86 | typeof layoutAnimationOrBuilder.build === 'function';
|
87 |
|
88 | if (isAnimationBuilder(layoutAnimationOrBuilder)) {
|
89 | const animationFactory = layoutAnimationOrBuilder.build();
|
90 |
|
91 | if (__DEV__ && style) {
|
92 | const layoutAnimation = animationFactory(mockTargetValues);
|
93 | maybeReportOverwrittenProperties(
|
94 | layoutAnimation.animations,
|
95 | style,
|
96 | displayName
|
97 | );
|
98 | }
|
99 |
|
100 | return animationFactory;
|
101 | } else {
|
102 | return layoutAnimationOrBuilder;
|
103 | }
|
104 | }
|