UNPKG

3.14 kBPlain TextView Raw
1'use strict';
2import { shouldBeUseWeb } from './PlatformChecker';
3import {
4 configureLayoutAnimationBatch,
5 makeShareableCloneRecursive,
6} from './core';
7import type {
8 LayoutAnimationFunction,
9 LayoutAnimationType,
10} from './layoutReanimation';
11import type {
12 LayoutAnimationBatchItem,
13 ProgressAnimationCallback,
14 SharedTransitionAnimationsFunction,
15} from './layoutReanimation/animationBuilder/commonTypes';
16
17function createUpdateManager() {
18 const animations: LayoutAnimationBatchItem[] = [];
19 // When a stack is rerendered we reconfigure all the shared elements.
20 // To do that we want them to appear in our batch in the correct order,
21 // so we defer some of the updates to appear at the end of the batch.
22 const deferredAnimations: LayoutAnimationBatchItem[] = [];
23
24 return {
25 update(batchItem: LayoutAnimationBatchItem, isUnmounting?: boolean) {
26 if (isUnmounting) {
27 deferredAnimations.push(batchItem);
28 } else {
29 animations.push(batchItem);
30 }
31 if (animations.length + deferredAnimations.length === 1) {
32 setImmediate(this.flush);
33 }
34 },
35 flush(this: void) {
36 configureLayoutAnimationBatch(animations.concat(deferredAnimations));
37 animations.length = 0;
38 deferredAnimations.length = 0;
39 },
40 };
41}
42
43/**
44 * Lets you update the current configuration of the layout animation or shared element transition for a given component.
45 * Configurations are batched and applied at the end of the current execution block, right before sending the response back to native.
46 *
47 * @param viewTag - The tag of the component you'd like to configure.
48 * @param type - The type of the animation you'd like to configure - {@link LayoutAnimationType}.
49 * @param config - The animation configuration - {@link LayoutAnimationFunction}, {@link SharedTransitionAnimationsFunction}, {@link ProgressAnimationCallback} or {@link Keyframe}. Passing `undefined` will remove the animation.
50 * @param sharedTransitionTag - The tag of the shared element transition you'd like to configure. Passing `undefined` will remove the transition.
51 * @param isUnmounting - Determines whether the configuration should be included at the end of the batch, after all the non-deferred configurations (even those that were updated later). This is used to retain the correct ordering of shared elements. Defaults to `false`.
52 */
53export let updateLayoutAnimations: (
54 viewTag: number,
55 type: LayoutAnimationType,
56 config?:
57 | Keyframe
58 | LayoutAnimationFunction
59 | SharedTransitionAnimationsFunction
60 | ProgressAnimationCallback,
61 sharedTransitionTag?: string,
62 isUnmounting?: boolean
63) => void;
64
65if (shouldBeUseWeb()) {
66 updateLayoutAnimations = () => {
67 // no-op
68 };
69} else {
70 const updateLayoutAnimationsManager = createUpdateManager();
71 updateLayoutAnimations = (
72 viewTag,
73 type,
74 config,
75 sharedTransitionTag,
76 isUnmounting
77 ) =>
78 updateLayoutAnimationsManager.update(
79 {
80 viewTag,
81 type,
82 config: config ? makeShareableCloneRecursive(config) : undefined,
83 sharedTransitionTag,
84 },
85 isUnmounting
86 );
87}