react-native-reanimated
Version:
More powerful alternative to Animated library for React Native.
533 lines (531 loc) • 13.9 kB
JavaScript
'use strict';
import { createElement, useState } from 'react';
import { Animated as AnimatedRN, Image as ImageRN, processColor as processColorRN, Text as TextRN, View as ViewRN } from 'react-native';
import { advanceAnimationByFrame, advanceAnimationByTime, contrastColor, convertToRGBA, css as cssStyleSheet, cubicBezier, DynamicColorIOS, Extrapolation, GentleSpringConfig, GentleSpringConfigWithDuration, getAnimatedStyle, getUseOfValueInStyleWarning, InterfaceOrientation, IOSReferenceFrame, isColor, isSharedValue as isSharedValueReal, isWorkletFunction, KeyboardState, linear, PlatformColor, Reanimated3DefaultSpringConfig, Reanimated3DefaultSpringConfigWithDuration, ReanimatedLogLevel, reanimatedVersion, ReduceMotion, ScreenTransition as ScreenTransitionPresets, SensorType, setUpTests, SharedTransitionType, SnappySpringConfig, SnappySpringConfigWithDuration, steps, WigglySpringConfig, WigglySpringConfigWithDuration, withReanimatedTimer } from "./index.js";
// eslint-disable-next-line @typescript-eslint/no-empty-function
const NOOP = () => {};
const NOOP_FACTORY = () => NOOP;
const ID = t => t;
const IMMEDIATE_CALLBACK_INVOCATION = callback => callback();
function useMockScrollOffset(_animatedRef, providedOffset) {
return useState(() => providedOffset ?? createMockSharedValue(0))[0];
}
function createMockSharedValue(init) {
const value = {
value: init
};
return new Proxy(value, {
get(target, prop) {
if (prop === '_isReanimatedSharedValue') {
return true;
}
if (prop === 'value') {
return target.value;
}
if (prop === 'get') {
return () => target.value;
}
if (prop === 'set') {
return newValue => {
if (typeof newValue === 'function') {
target.value = newValue(target.value);
} else {
target.value = newValue;
}
};
}
if (prop === 'modify') {
return modifier => {
if (modifier !== undefined) {
target.value = modifier(target.value);
}
};
}
if (prop === 'addListener' || prop === 'removeListener') {
return NOOP;
}
},
set(target, prop, newValue) {
if (prop === 'value') {
target.value = newValue;
return true;
}
return false;
}
});
}
const hook = {
useAnimatedKeyboard: () => ({
height: 0,
state: 0
}),
useAnimatedProps: IMMEDIATE_CALLBACK_INVOCATION,
useAnimatedReaction: NOOP,
useAnimatedRef: () => ({
current: null
}),
useAnimatedScrollHandler: NOOP_FACTORY,
useAnimatedSensor: () => ({
sensor: {
value: {
x: 0,
y: 0,
z: 0,
interfaceOrientation: 0,
qw: 0,
qx: 0,
qy: 0,
qz: 0,
yaw: 0,
pitch: 0,
roll: 0
}
},
unregister: NOOP,
isAvailable: false,
config: {
interval: 0,
adjustToInterfaceOrientation: false,
iosReferenceFrame: 0
}
}),
useAnimatedStyle: IMMEDIATE_CALLBACK_INVOCATION,
useComposedEventHandler: NOOP_FACTORY,
useDerivedValue: processor => {
const result = processor();
return {
value: result,
get: () => result,
modify: NOOP,
addListener: NOOP,
removeListener: NOOP,
_isReanimatedSharedValue: true
};
},
useEvent: (_handler, _eventNames, _rebuild) => NOOP,
useFrameCallback: () => ({
callbackId: -1,
isActive: false,
setActive: NOOP
}),
useHandler: () => ({
context: {},
doDependenciesDiffer: false,
useWeb: false
}),
useReducedMotion: () => false,
useScrollOffset: useMockScrollOffset,
useScrollViewOffset: useMockScrollOffset,
useSharedValue: init => useState(() => createMockSharedValue(typeof init === 'function' ? init() : init))[0],
useTimestamp: () => useState(() => createMockSharedValue(0))[0]
};
const animation = {
cancelAnimation: NOOP,
defineAnimation: (_starting, factory) => factory(),
GentleSpringConfig,
GentleSpringConfigWithDuration,
Reanimated3DefaultSpringConfig,
Reanimated3DefaultSpringConfigWithDuration,
SnappySpringConfig,
SnappySpringConfigWithDuration,
WigglySpringConfig,
WigglySpringConfigWithDuration,
withClamp: (_config, animationToClamp) => animationToClamp,
withDecay: (_userConfig, callback) => {
callback?.(true);
return 0;
},
withDelay: (_delayMs, nextAnimation) => {
return nextAnimation;
},
withRepeat: ID,
withSequence: () => 0,
withSpring: (toValue, _userConfig, callback) => {
callback?.(true);
return toValue;
},
withTiming: (toValue, _userConfig, callback) => {
callback?.(true);
return toValue;
}
};
const interpolation = {
clamp: NOOP,
Extrapolation,
interpolate: NOOP
};
const interpolateColor = {
Extrapolate: Extrapolation,
Extrapolation,
interpolateColor: NOOP
};
const Easing = {
cubicBezier,
Easing: {
back: ID,
bezier: () => ({
factory: ID
}),
bezierFn: ID,
bounce: ID,
circle: ID,
cubic: ID,
ease: ID,
elastic: ID,
exp: ID,
in: ID,
inOut: ID,
linear: ID,
out: ID,
poly: ID,
quad: ID,
sin: ID,
steps: ID
},
linear,
steps
};
const platformFunctions = {
dispatchCommand: NOOP,
getRelativeCoords: () => ({
x: 0,
y: 0
}),
measure: () => ({
x: 0,
y: 0,
width: 0,
height: 0,
pageX: 0,
pageY: 0
}),
scrollTo: NOOP,
setGestureState: NOOP,
setNativeProps: NOOP
};
const Colors = {
contrastColor,
convertToRGBA,
DynamicColorIOS,
isColor,
PlatformColor,
processColor: processColorRN
};
const PropAdapters = {
createAnimatedPropAdapter: ID
};
class BaseAnimationMock {
duration() {
return this;
}
delay() {
return this;
}
springify() {
return this;
}
damping() {
return this;
}
stiffness() {
return this;
}
energyThreshold() {
return this;
}
withCallback() {
return this;
}
randomDelay() {
return this;
}
withInitialValues() {
return this;
}
withTargetValues() {
return this;
}
easing(_) {
return this;
}
rotate(_) {
return this;
}
mass(_) {
return this;
}
restDisplacementThreshold(_) {
return this;
}
restSpeedThreshold(_) {
return this;
}
overshootClamping(_) {
return this;
}
dampingRatio(_) {
return this;
}
getDelay() {
return 0;
}
getDelayFunction() {
return NOOP;
}
getDuration() {
return 300;
}
getReduceMotion() {
return ReduceMotion.System;
}
getAnimationAndConfig() {
return [NOOP, {}];
}
build() {
return () => ({
initialValues: {},
animations: {}
});
}
reduceMotion() {
return this;
}
}
class SharedTransitionMock extends BaseAnimationMock {
fadeContent() {
return this;
}
custom() {
return this;
}
progressAnimation() {
return this;
}
defaultTransitionType() {
return this;
}
}
const SharedTransition = withStaticModifiers(SharedTransitionMock);
function withStaticModifiers(BuilderClass) {
const statics = BuilderClass;
for (let prototype = BuilderClass.prototype; prototype !== Object.prototype; prototype = Object.getPrototypeOf(prototype)) {
for (const name of Object.getOwnPropertyNames(prototype)) {
if (name !== 'constructor' && !(name in BuilderClass)) {
statics[name] = (...args) => new BuilderClass()[name](...args);
}
}
}
return BuilderClass;
}
class NativeEventsManagerMock {
attachEvents = NOOP;
detachEvents = NOOP;
updateEvents = NOOP;
}
const core = {
configureReanimatedLogger: NOOP,
createAnimatedComponent: ID,
createCSSAnimatedComponent: ID,
createWorkletRuntime: NOOP,
enableLayoutAnimations: NOOP,
executeOnUIRuntimeSync: ID,
getDynamicFeatureFlag: () => false,
getStaticFeatureFlag: () => false,
getViewProp: () => Promise.resolve(undefined),
isConfigured: () => false,
isReanimated3: () => false,
isWorkletFunction,
makeMutable: createMockSharedValue,
makeShareableCloneRecursive: ID,
NativeEventsManager: NativeEventsManagerMock,
ReanimatedLogLevel,
runOnJS: ID,
runOnRuntime: NOOP,
runOnUI: ID,
setDynamicFeatureFlag: NOOP
};
const layoutReanimation = {
BaseAnimationBuilder: new BaseAnimationMock(),
BounceIn: new BaseAnimationMock(),
BounceInDown: new BaseAnimationMock(),
BounceInLeft: new BaseAnimationMock(),
BounceInRight: new BaseAnimationMock(),
BounceInUp: new BaseAnimationMock(),
BounceOut: new BaseAnimationMock(),
BounceOutDown: new BaseAnimationMock(),
BounceOutLeft: new BaseAnimationMock(),
BounceOutRight: new BaseAnimationMock(),
BounceOutUp: new BaseAnimationMock(),
ComplexAnimationBuilder: new BaseAnimationMock(),
CurvedTransition: new BaseAnimationMock(),
EntryExitTransition: new BaseAnimationMock(),
FadeIn: new BaseAnimationMock(),
FadeInDown: new BaseAnimationMock(),
FadeInLeft: new BaseAnimationMock(),
FadeInRight: new BaseAnimationMock(),
FadeInUp: new BaseAnimationMock(),
FadeOut: new BaseAnimationMock(),
FadeOutDown: new BaseAnimationMock(),
FadeOutLeft: new BaseAnimationMock(),
FadeOutRight: new BaseAnimationMock(),
FadeOutUp: new BaseAnimationMock(),
FadingTransition: new BaseAnimationMock(),
FlipInEasyX: new BaseAnimationMock(),
FlipInEasyY: new BaseAnimationMock(),
FlipInXDown: new BaseAnimationMock(),
FlipInXUp: new BaseAnimationMock(),
FlipInYLeft: new BaseAnimationMock(),
FlipInYRight: new BaseAnimationMock(),
FlipOutEasyX: new BaseAnimationMock(),
FlipOutEasyY: new BaseAnimationMock(),
FlipOutXDown: new BaseAnimationMock(),
FlipOutXUp: new BaseAnimationMock(),
FlipOutYLeft: new BaseAnimationMock(),
FlipOutYRight: new BaseAnimationMock(),
JumpingTransition: new BaseAnimationMock(),
Keyframe: BaseAnimationMock,
Layout: new BaseAnimationMock(),
LightSpeedInLeft: new BaseAnimationMock(),
LightSpeedInRight: new BaseAnimationMock(),
LightSpeedOutLeft: new BaseAnimationMock(),
LightSpeedOutRight: new BaseAnimationMock(),
LinearTransition: new BaseAnimationMock(),
PinwheelIn: new BaseAnimationMock(),
PinwheelOut: new BaseAnimationMock(),
RollInLeft: new BaseAnimationMock(),
RollInRight: new BaseAnimationMock(),
RollOutLeft: new BaseAnimationMock(),
RollOutRight: new BaseAnimationMock(),
RotateInDownLeft: new BaseAnimationMock(),
RotateInDownRight: new BaseAnimationMock(),
RotateInUpLeft: new BaseAnimationMock(),
RotateInUpRight: new BaseAnimationMock(),
RotateOutDownLeft: new BaseAnimationMock(),
RotateOutDownRight: new BaseAnimationMock(),
RotateOutUpLeft: new BaseAnimationMock(),
RotateOutUpRight: new BaseAnimationMock(),
SequencedTransition: new BaseAnimationMock(),
SharedTransition,
SharedTransitionType,
SlideInDown: new BaseAnimationMock(),
SlideInLeft: new BaseAnimationMock(),
SlideInRight: new BaseAnimationMock(),
SlideInUp: new BaseAnimationMock(),
SlideOutDown: new BaseAnimationMock(),
SlideOutLeft: new BaseAnimationMock(),
SlideOutRight: new BaseAnimationMock(),
SlideOutUp: new BaseAnimationMock(),
StretchInX: new BaseAnimationMock(),
StretchInY: new BaseAnimationMock(),
StretchOutX: new BaseAnimationMock(),
StretchOutY: new BaseAnimationMock(),
ZoomIn: new BaseAnimationMock(),
ZoomInDown: new BaseAnimationMock(),
ZoomInEasyDown: new BaseAnimationMock(),
ZoomInEasyUp: new BaseAnimationMock(),
ZoomInLeft: new BaseAnimationMock(),
ZoomInRight: new BaseAnimationMock(),
ZoomInRotate: new BaseAnimationMock(),
ZoomInUp: new BaseAnimationMock(),
ZoomOut: new BaseAnimationMock(),
ZoomOutDown: new BaseAnimationMock(),
ZoomOutEasyDown: new BaseAnimationMock(),
ZoomOutEasyUp: new BaseAnimationMock(),
ZoomOutLeft: new BaseAnimationMock(),
ZoomOutRight: new BaseAnimationMock(),
ZoomOutRotate: new BaseAnimationMock(),
ZoomOutUp: new BaseAnimationMock()
};
const isSharedValue = {
isSharedValue: isSharedValueReal
};
const commonTypes = {
InterfaceOrientation,
IOSReferenceFrame,
KeyboardState,
ReduceMotion,
SensorType
};
const pluginUtils = {
getUseOfValueInStyleWarning
};
const jestUtils = {
advanceAnimationByFrame,
advanceAnimationByTime,
getAnimatedStyle,
setUpTests,
withReanimatedTimer
};
const LayoutAnimationConfig = {
LayoutAnimationConfig: ({
children
}) => children
};
const mappers = {
startMapper: () => 0,
stopMapper: NOOP
};
const components = {
PerformanceMonitor: () => null,
ReducedMotionConfig: () => null,
SharedTransitionBoundary: ({
children
}) => children
};
const css = {
css: cssStyleSheet
};
const screenTransition = {
finishScreenTransition: NOOP,
ScreenTransition: ScreenTransitionPresets,
startScreenTransition: NOOP
};
const AnimatedText = ({
children,
...props
}) => {
const content = Array.isArray(children) ? children : [children];
return /*#__PURE__*/createElement(TextRN, props, ...content.map(child => isSharedValueReal(child) ? child.value : child));
};
const Animated = {
addWhitelistedNativeProps: NOOP,
addWhitelistedUIProps: NOOP,
clamp: NOOP,
createAnimatedComponent: ID,
Extrapolate: Extrapolation,
FlatList: AnimatedRN.FlatList,
Image: ImageRN,
interpolate: NOOP,
interpolateColor: NOOP,
ScrollView: AnimatedRN.ScrollView,
Text: AnimatedText,
View: ViewRN
};
const Reanimated = {
...animation,
...Colors,
...commonTypes,
...components,
...core,
...css,
...Easing,
...hook,
...interpolateColor,
...interpolation,
...isSharedValue,
...jestUtils,
...LayoutAnimationConfig,
...layoutReanimation,
...mappers,
...platformFunctions,
...pluginUtils,
...PropAdapters,
...screenTransition
};
module.exports = {
__esModule: true,
reanimatedVersion,
...Reanimated,
default: Animated
};
//# sourceMappingURL=mock.js.map