Skip to content

Commit

Permalink
refactor(Worklets): Merge TypeScript type files (#6556)
Browse files Browse the repository at this point in the history
## Summary

Required by:
- #6557

`NativeReanimatedModule` depends on types from Layout Animations - this
makes impossible to embed its interface in `commonTypes.ts` since they
can't have any imports. I merged some types to prevent circular
dependencies.

## Test plan

- [x] CI Green
  • Loading branch information
tjzel authored Oct 24, 2024
1 parent 1d417a5 commit 94593ad
Show file tree
Hide file tree
Showing 39 changed files with 308 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Value3D,
ValueRotation,
ShareableRef,
LayoutAnimationBatchItem,
} from '../commonTypes';
import { checkCppVersion } from '../platform-specific/checkCppVersion';
import { jsVersion } from '../platform-specific/jsVersion';
Expand All @@ -12,7 +13,6 @@ import { getValueUnpackerCode } from '../valueUnpacker';
import { isFabric } from '../PlatformChecker';
import type React from 'react';
import { getShadowNodeWrapperFromRef } from '../fabricUtils';
import type { LayoutAnimationBatchItem } from '../layoutReanimation/animationBuilder/commonTypes';
import ReanimatedModule from '../specs/NativeReanimatedModule';
import { ReanimatedError } from '../errors';

Expand Down
16 changes: 7 additions & 9 deletions packages/react-native-reanimated/src/UpdateLayoutAnimations.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict';
import { isFabric, shouldBeUseWeb } from './PlatformChecker';
import type {
LayoutAnimationBatchItem,
LayoutAnimationFunction,
SharedTransitionAnimationsFunction,
ProgressAnimationCallback,
LayoutAnimationType,
} from './commonTypes';
import {
configureLayoutAnimationBatch,
makeShareableCloneRecursive,
} from './core';
import type {
LayoutAnimationFunction,
LayoutAnimationType,
} from './layoutReanimation';
import type {
LayoutAnimationBatchItem,
ProgressAnimationCallback,
SharedTransitionAnimationsFunction,
} from './layoutReanimation/animationBuilder/commonTypes';

function createUpdateManager() {
const animations: LayoutAnimationBatchItem[] = [];
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-reanimated/src/animationBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
ILayoutAnimationBuilder,
LayoutAnimationFunction,
LayoutAnimationsValues,
} from './layoutReanimation';
import type { StyleProps } from './commonTypes';
StyleProps,
} from './commonTypes';
import type { NestedArray } from './createAnimatedComponent/commonTypes';
import { logger } from './logger';

Expand Down
189 changes: 189 additions & 0 deletions packages/react-native-reanimated/src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,195 @@ import type {
ImageStyle,
} from 'react-native';

export type LayoutAnimationsOptions =
| 'originX'
| 'originY'
| 'width'
| 'height'
| 'borderRadius'
| 'globalOriginX'
| 'globalOriginY';

type CurrentLayoutAnimationsValues = {
[K in LayoutAnimationsOptions as `current${Capitalize<string & K>}`]: number;
};

type TargetLayoutAnimationsValues = {
[K in LayoutAnimationsOptions as `target${Capitalize<string & K>}`]: number;
};

interface WindowDimensions {
windowWidth: number;
windowHeight: number;
}

export interface KeyframeProps extends StyleProps {
easing?: EasingFunction;
}

type FirstFrame =
| {
0: KeyframeProps & { easing?: never };
from?: never;
}
| {
0?: never;
from: KeyframeProps & { easing?: never };
};

type LastFrame =
| { 100?: KeyframeProps; to?: never }
| { 100?: never; to: KeyframeProps };

export type ValidKeyframeProps = FirstFrame &
LastFrame &
Record<number, KeyframeProps>;

export type MaybeInvalidKeyframeProps = Record<number, KeyframeProps> & {
to?: KeyframeProps;
from?: KeyframeProps;
};

export type LayoutAnimation = {
initialValues: StyleProps;
animations: StyleProps;
callback?: (finished: boolean) => void;
};

export type AnimationFunction = (a?: any, b?: any, c?: any) => any; // this is just a temporary mock

export type EntryAnimationsValues = TargetLayoutAnimationsValues &
WindowDimensions;

export type ExitAnimationsValues = CurrentLayoutAnimationsValues &
WindowDimensions;

export type EntryExitAnimationFunction =
| ((targetValues: EntryAnimationsValues) => LayoutAnimation)
| ((targetValues: ExitAnimationsValues) => LayoutAnimation)
| (() => LayoutAnimation);

export type AnimationConfigFunction<T> = (targetValues: T) => LayoutAnimation;

export type LayoutAnimationsValues = CurrentLayoutAnimationsValues &
TargetLayoutAnimationsValues &
WindowDimensions;

export interface SharedTransitionAnimationsValues
extends LayoutAnimationsValues {
currentTransformMatrix: number[];
targetTransformMatrix: number[];
}

export type SharedTransitionAnimationsFunction = (
values: SharedTransitionAnimationsValues
) => LayoutAnimation;

export enum LayoutAnimationType {
ENTERING = 1,
EXITING = 2,
LAYOUT = 3,
SHARED_ELEMENT_TRANSITION = 4,
SHARED_ELEMENT_TRANSITION_PROGRESS = 5,
}

export type LayoutAnimationFunction = (
targetValues: LayoutAnimationsValues
) => LayoutAnimation;

export type LayoutAnimationStartFunction = (
tag: number,
type: LayoutAnimationType,
yogaValues: Partial<SharedTransitionAnimationsValues>,
config: (arg: Partial<SharedTransitionAnimationsValues>) => LayoutAnimation
) => void;

export interface ILayoutAnimationBuilder {
build: () => LayoutAnimationFunction;
}

export interface BaseLayoutAnimationConfig {
duration?: number;
easing?: EasingFunction;
type?: AnimationFunction;
damping?: number;
dampingRatio?: number;
mass?: number;
stiffness?: number;
overshootClamping?: number;
restDisplacementThreshold?: number;
restSpeedThreshold?: number;
}

export interface BaseBuilderAnimationConfig extends BaseLayoutAnimationConfig {
rotate?: number | string;
}

export type LayoutAnimationAndConfig = [
AnimationFunction,
BaseBuilderAnimationConfig,
];

export interface IEntryExitAnimationBuilder {
build: () => EntryExitAnimationFunction;
}

export interface IEntryAnimationBuilder {
build: () => AnimationConfigFunction<EntryAnimationsValues>;
}

export interface IExitAnimationBuilder {
build: () => AnimationConfigFunction<ExitAnimationsValues>;
}

export type ProgressAnimationCallback = (
viewTag: number,
progress: number
) => void;

export type ProgressAnimation = (
viewTag: number,
values: SharedTransitionAnimationsValues,
progress: number
) => void;

export type CustomProgressAnimation = (
values: SharedTransitionAnimationsValues,
progress: number
) => StyleProps;

/**
* Used to configure the `.defaultTransitionType()` shared transition modifier.
*
* @experimental
*/
export enum SharedTransitionType {
ANIMATION = 'animation',
PROGRESS_ANIMATION = 'progressAnimation',
}

export type EntryExitAnimationsValues =
| EntryAnimationsValues
| ExitAnimationsValues;

export type StylePropsWithArrayTransform = StyleProps & {
transform?: TransformArrayItem[];
};

export interface LayoutAnimationBatchItem {
viewTag: number;
type: LayoutAnimationType;
config:
| ShareableRef<
| Keyframe
| LayoutAnimationFunction
| SharedTransitionAnimationsFunction
| ProgressAnimationCallback
>
| undefined;
sharedTransitionTag?: string;
}

export type RequiredKeys<T, K extends keyof T> = T & Required<Pick<T, K>>;
export interface StyleProps extends ViewStyle, TextStyle {
originX?: number;
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native-reanimated/src/component/FlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import type {
import { FlatList } from 'react-native';
import { AnimatedView } from './View';
import { createAnimatedComponent } from '../createAnimatedComponent';
import type { ILayoutAnimationBuilder } from '../layoutReanimation/animationBuilder/commonTypes';
import type { AnimatedStyle, ILayoutAnimationBuilder } from '../commonTypes';
import { LayoutAnimationConfig } from './LayoutAnimationConfig';
import type { AnimatedStyle } from '../commonTypes';
import type { AnimatedProps } from '../helperTypes';

const AnimatedFlatList = createAnimatedComponent(FlatList);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-reanimated/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NativeReanimatedModule from './NativeReanimated';
import { isWeb, shouldBeUseWeb, isFabric } from './PlatformChecker';
import type {
AnimatedKeyboardOptions,
LayoutAnimationBatchItem,
SensorConfig,
SensorType,
SharedValue,
Expand All @@ -11,7 +12,6 @@ import type {
} from './commonTypes';
import { makeShareableCloneRecursive } from './shareables';
import { initializeUIRuntime } from './initializers';
import type { LayoutAnimationBatchItem } from './layoutReanimation/animationBuilder/commonTypes';
import { SensorContainer } from './SensorContainer';
import { ReanimatedError } from './errors';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
import type { Ref, Component } from 'react';
import type {
EntryExitAnimationFunction,
ILayoutAnimationBuilder,
ShadowNodeWrapper,
SharedValue,
StyleProps,
Expand All @@ -9,8 +11,6 @@ import type { ViewConfig } from '../ConfigHelper';
import type { ViewDescriptorsSet } from '../ViewDescriptorsSet';
import type {
BaseAnimationBuilder,
EntryExitAnimationFunction,
ILayoutAnimationBuilder,
SharedTransition,
} from '../layoutReanimation';
import type { SkipEnteringContext } from '../component/LayoutAnimationConfig';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import invariant from 'invariant';
import { adaptViewConfig } from '../ConfigHelper';
import { RNRenderer } from '../platform-specific/RNRenderer';
import { enableLayoutAnimations } from '../core';
import { SharedTransition, LayoutAnimationType } from '../layoutReanimation';
import { SharedTransition } from '../layoutReanimation';
import { LayoutAnimationType } from '../commonTypes';
import type { StyleProps, ShadowNodeWrapper } from '../commonTypes';
import { getShadowNodeWrapperFromRef } from '../fabricUtils';
import { removeFromPropsRegistry } from '../PropsRegistry';
Expand Down
6 changes: 2 additions & 4 deletions packages/react-native-reanimated/src/helperTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ This will not be easy though!
import type { RegisteredStyle, StyleProp } from 'react-native';
import type {
AnimatedStyle,
EntryExitAnimationFunction,
LayoutAnimationFunction,
SharedValue,
TransformArrayItem,
} from './commonTypes';
import type { BaseAnimationBuilder } from './layoutReanimation/animationBuilder/BaseAnimationBuilder';
import type {
EntryExitAnimationFunction,
LayoutAnimationFunction,
} from './layoutReanimation/animationBuilder/commonTypes';
import type { ReanimatedKeyframe } from './layoutReanimation/animationBuilder/Keyframe';
import type { SharedTransition } from './layoutReanimation/sharedTransitions';

Expand Down
28 changes: 13 additions & 15 deletions packages/react-native-reanimated/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,6 @@ export {
export type { ParsedColorArray } from './Colors';
export { isColor, processColor, convertToRGBA } from './Colors';
export { createAnimatedPropAdapter } from './PropAdapters';
export type {
LayoutAnimation,
EntryAnimationsValues,
ExitAnimationsValues,
EntryExitAnimationFunction,
LayoutAnimationsValues,
LayoutAnimationFunction,
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
ILayoutAnimationBuilder,
IEntryExitAnimationBuilder,
BaseLayoutAnimationConfig,
} from './layoutReanimation';
export {
BaseAnimationBuilder,
ComplexAnimationBuilder,
Expand Down Expand Up @@ -227,7 +213,6 @@ export {
combineTransition,
// SET
SharedTransition,
SharedTransitionType,
} from './layoutReanimation';
export { isSharedValue } from './isSharedValue';
export type {
Expand All @@ -251,6 +236,18 @@ export type {
AnimateStyle,
AnimatedStyle,
StylesOrDefault,
LayoutAnimation,
EntryAnimationsValues,
ExitAnimationsValues,
EntryExitAnimationFunction,
LayoutAnimationsValues,
LayoutAnimationFunction,
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
ILayoutAnimationBuilder,
IEntryExitAnimationBuilder,
BaseLayoutAnimationConfig,
} from './commonTypes';
export {
SensorType,
Expand All @@ -259,6 +256,7 @@ export {
KeyboardState,
ReduceMotion,
isWorkletFunction,
SharedTransitionType,
} from './commonTypes';
export type { FrameInfo } from './frameCallback';
export { getUseOfValueInStyleWarning } from './pluginUtils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
import { withDelay } from '../../animation';

import { ReduceMotion } from '../../commonTypes';
import type {
EntryExitAnimationFunction,
AnimationFunction,
EntryExitAnimationFunction,
LayoutAnimationFunction,
} from './commonTypes';

import { ReduceMotion } from '../../commonTypes';
} from '../../commonTypes';
import { getReduceMotionFromConfig } from '../../animation/util';
import { ReanimatedError } from '../../errors';

Expand Down
Loading

0 comments on commit 94593ad

Please sign in to comment.