import React, { useRef, useImperativeHandle } from 'react'; import { Animated, Dimensions, Modal, StyleSheet } from 'react-native'; import type { Coords, StoryDetailExpanderProps, StoryDetailExpanderRefProps, } from '../../../types'; import { expandAnimation } from '../../../animations/expand'; import { styles } from './styles'; const { width, height } = Dimensions.get('screen'); export const StoryDetailExpander = React.forwardRef< StoryDetailExpanderRefProps, StoryDetailExpanderProps >(({ style, children, duration = 200, isVisible }, ref) => { const coordsRef = useRef({ x: 0, y: 0, }); const scaleXRef = useRef(new Animated.Value(0)); const scaleYRef = useRef(new Animated.Value(0)); const translateXRef = useRef(new Animated.Value(0)); const translateYRef = useRef(new Animated.Value(0)); useImperativeHandle(ref, () => ({ startExpandAnimation: ( coords: Coords, onAnimationEnd: Animated.EndCallback ) => { translateXRef.current.setValue(coords.x); translateYRef.current.setValue(coords.y); coordsRef.current = coords; const animations = [ scaleXRef.current, scaleYRef.current, translateXRef.current, translateYRef.current, ]; expandAnimation(animations, onAnimationEnd, { x: 0, y: 0, width, height, duration, }); }, resetExpandAnimation: (onAnimationEnd: Animated.EndCallback) => { const animations = [ scaleXRef.current, scaleYRef.current, translateXRef.current, translateYRef.current, ]; expandAnimation(animations, onAnimationEnd, { x: coordsRef.current.x, y: coordsRef.current.y, width: 0, height: 0, duration, }); }, })); return ( {children} ); }); StoryDetailExpander.displayName = 'StoryDetailExpander';