UNPKG

2.52 kBPlain TextView Raw
1'use strict';
2import type {
3 ILayoutAnimationBuilder,
4 LayoutAnimationFunction,
5} from '../animationBuilder/commonTypes';
6import { withSequence, withTiming } from '../../animation';
7import { Easing } from '../../Easing';
8import { BaseAnimationBuilder } from '../animationBuilder';
9
10/**
11 * Layout jumps - quite literally - from one position to another. You can modify the behavior by chaining methods like `.springify()` or `.duration(500)`.
12 *
13 * You pass it to the `layout` prop on [an Animated component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
14 *
15 * @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/layout-transitions#jumping-transition
16 */
17export class JumpingTransition
18 extends BaseAnimationBuilder
19 implements ILayoutAnimationBuilder
20{
21 static createInstance<T extends typeof BaseAnimationBuilder>(
22 this: T
23 ): InstanceType<T> {
24 return new JumpingTransition() as InstanceType<T>;
25 }
26
27 build = (): LayoutAnimationFunction => {
28 const delayFunction = this.getDelayFunction();
29 const callback = this.callbackV;
30 const delay = this.getDelay();
31 const duration = (this.durationV ?? 300) / 2;
32 const config = { duration: duration * 2 };
33
34 return (values) => {
35 'worklet';
36 const d = Math.max(
37 Math.abs(values.targetOriginX - values.currentOriginX),
38 Math.abs(values.targetOriginY - values.currentOriginY)
39 );
40 return {
41 initialValues: {
42 originX: values.currentOriginX,
43 originY: values.currentOriginY,
44 width: values.currentWidth,
45 height: values.currentHeight,
46 },
47 animations: {
48 originX: delayFunction(
49 delay,
50 withTiming(values.targetOriginX, config)
51 ),
52 originY: delayFunction(
53 delay,
54 withSequence(
55 withTiming(
56 Math.min(values.targetOriginY, values.currentOriginY) - d,
57 {
58 duration,
59 easing: Easing.out(Easing.exp),
60 }
61 ),
62 withTiming(values.targetOriginY, {
63 ...config,
64 duration,
65 easing: Easing.bounce,
66 })
67 )
68 ),
69 width: delayFunction(delay, withTiming(values.targetWidth, config)),
70 height: delayFunction(delay, withTiming(values.targetHeight, config)),
71 },
72 callback,
73 };
74 };
75 };
76}