UNPKG

4.05 kBPlain TextView Raw
1'use strict';
2import type {
3 ILayoutAnimationBuilder,
4 LayoutAnimationFunction,
5} from '../animationBuilder/commonTypes';
6import { BaseAnimationBuilder } from '../animationBuilder';
7import type { EasingFunction } from '../../Easing';
8import { Easing } from '../../Easing';
9import { withTiming } from '../../animation';
10import { assertEasingIsWorklet } from '../../animation/util';
11
12/**
13 * Layout transitions with a curved animation. You can modify the behavior by chaining methods like `.duration(500)` or `.delay(500)`.
14 *
15 * You pass it to the `layout` prop on [an Animated component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
16 *
17 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/layout-transitions#fading-transition
18 */
19export class CurvedTransition
20 extends BaseAnimationBuilder
21 implements ILayoutAnimationBuilder
22{
23 easingXV: EasingFunction = Easing.in(Easing.ease);
24 easingYV: EasingFunction = Easing.out(Easing.ease);
25 easingWidthV: EasingFunction = Easing.in(Easing.exp);
26 easingHeightV: EasingFunction = Easing.out(Easing.exp);
27
28 static createInstance<T extends typeof BaseAnimationBuilder>(
29 this: T
30 ): InstanceType<T> {
31 return new CurvedTransition() as InstanceType<T>;
32 }
33
34 static easingX(easing: EasingFunction): CurvedTransition {
35 const instance = this.createInstance();
36 return instance.easingX(easing);
37 }
38
39 easingX(easing: EasingFunction): CurvedTransition {
40 if (__DEV__) {
41 assertEasingIsWorklet(easing);
42 }
43 this.easingXV = easing;
44 return this;
45 }
46
47 static easingY(easing: EasingFunction): CurvedTransition {
48 const instance = this.createInstance();
49 return instance.easingY(easing);
50 }
51
52 easingY(easing: EasingFunction): CurvedTransition {
53 if (__DEV__) {
54 assertEasingIsWorklet(easing);
55 }
56 this.easingYV = easing;
57 return this;
58 }
59
60 static easingWidth(easing: EasingFunction): CurvedTransition {
61 const instance = this.createInstance();
62 return instance.easingWidth(easing);
63 }
64
65 easingWidth(easing: EasingFunction): CurvedTransition {
66 if (__DEV__) {
67 assertEasingIsWorklet(easing);
68 }
69 this.easingWidthV = easing;
70 return this;
71 }
72
73 static easingHeight(easing: EasingFunction): CurvedTransition {
74 const instance = this.createInstance();
75 return instance.easingHeight(easing);
76 }
77
78 easingHeight(easing: EasingFunction): CurvedTransition {
79 if (__DEV__) {
80 assertEasingIsWorklet(easing);
81 }
82 this.easingHeightV = easing;
83 return this;
84 }
85
86 build = (): LayoutAnimationFunction => {
87 const delayFunction = this.getDelayFunction();
88 const callback = this.callbackV;
89 const delay = this.getDelay();
90 const duration = this.durationV ?? 300;
91 const easing = {
92 easingX: this.easingXV,
93 easingY: this.easingYV,
94 easingWidth: this.easingWidthV,
95 easingHeight: this.easingHeightV,
96 };
97
98 return (values) => {
99 'worklet';
100
101 return {
102 initialValues: {
103 originX: values.currentOriginX,
104 originY: values.currentOriginY,
105 width: values.currentWidth,
106 height: values.currentHeight,
107 },
108 animations: {
109 originX: delayFunction(
110 delay,
111 withTiming(values.targetOriginX, {
112 duration,
113 easing: easing.easingX,
114 })
115 ),
116 originY: delayFunction(
117 delay,
118 withTiming(values.targetOriginY, {
119 duration,
120 easing: easing.easingY,
121 })
122 ),
123 width: delayFunction(
124 delay,
125 withTiming(values.targetWidth, {
126 duration,
127 easing: easing.easingWidth,
128 })
129 ),
130 height: delayFunction(
131 delay,
132 withTiming(values.targetHeight, {
133 duration,
134 easing: easing.easingHeight,
135 })
136 ),
137 },
138 callback,
139 };
140 };
141 };
142}