import React from 'react'; import type { FlatListProps, ScrollViewProps as RnScrollViewProps, SectionListProps, } from 'react-native'; import { Animated } from 'react-native'; import type { ActionGroupProps } from '../FAB/ActionGroup'; import type { FABProps } from '../FAB/FAB'; import AnimatedFAB from './AnimatedFAB'; interface AnimatedScrollerProps { /** * Scroll component, it can be ScrollView, FlatList or SectionList. */ ScrollComponent: React.ReactElement< RnScrollViewProps | FlatListProps | SectionListProps >; /** * FAB or FAB.ActionGroup props. */ fabProps: FABProps | ActionGroupProps; } function AnimatedScroller({ ScrollComponent, fabProps, }: AnimatedScrollerProps) { const contentOffsetY = React.useRef(new Animated.Value(0)).current; const contentHeight = React.useRef(new Animated.Value(0)).current; const layoutHeight = React.useRef(new Animated.Value(0)).current; // Common props for all ScrollView, FlatList and SectionList. const { onScroll, scrollEventThrottle } = ScrollComponent.props; return ( <> {React.cloneElement(ScrollComponent, { ...ScrollComponent.props, scrollEventThrottle: scrollEventThrottle || 100, onScroll: Animated.event( [ { nativeEvent: { contentOffset: { y: contentOffsetY }, contentSize: { height: contentHeight }, layoutMeasurement: { height: layoutHeight }, }, }, ], { useNativeDriver: false, listener: onScroll, } ), })} {!!fabProps && ( )} ); } export default AnimatedScroller;