import React, { useRef, forwardRef, useCallback, useLayoutEffect, useMemo, createElement, } from "react"; import { AnimatedProps } from "./node/AnimatedProps"; import { Animated } from "./node/Animated"; import { isFunction, updateRef, useForceUpdate } from "./helpers"; import * as Global from "./global"; export type AnimatableComponent = React.ElementType | React.ComponentType; export type AnimatableComponentProps = Omit< { [property in keyof React.ComponentPropsWithoutRef]: | React.ComponentPropsWithoutRef[property] | Animated; }, "style" > & { style?: { [key in keyof React.CSSProperties]: React.CSSProperties[key] | Animated; }; }; export function createAnimatedComponent( Component: C ) { const hasInstance: boolean = !isFunction(Component) || Component.prototype.isReactComponent; return forwardRef( (props: AnimatableComponentProps, forwardedRef: React.Ref) => { const instanceRef = useRef(null); const ref = hasInstance && useCallback( (value: any) => { instanceRef.current = updateRef(forwardedRef, value); }, [forwardedRef] ); const animatedProps = useRef(null); const forceUpdate = useForceUpdate(); const attachProps = useCallback(() => { const oldAnimatedProps = animatedProps.current; const callback = () => { const instance = instanceRef.current; if (hasInstance && !instance) { return; } if (animatedProps.current) { const didUpdate = instance ? Global.applyAnimatedValues.current( instance, animatedProps.current.__getValue() ) : false; // force re-render when the instance is not available if (didUpdate === false) { forceUpdate(); } } }; animatedProps.current = new AnimatedProps(props, callback); oldAnimatedProps && oldAnimatedProps.__detach(); }, []); useLayoutEffect(() => { attachProps(); }, []); /** * TODO: - create a custom function to retrieve the props * rather than using new AnimatedProps node ( creating new instance ) */ const initialProps = useMemo( () => new AnimatedProps(props, () => false).__getValue(), [props] ); return createElement(Component, { ...initialProps, ref }); } ); }