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