import React, { FC, useContext, useRef, useState } from 'react'; import { Animated, View } from 'react-native'; import { ScrollIndicatorProps } from './types'; import styles from './styles'; import { ApplicationContext, MiniAppContext } from '../Context'; const INDICATOR_WIDTH = 24; const INDICATOR_CONTAINER_WIDTH = 72; const PaginationScroll: FC = ({ style, children, scrollViewRef, }) => { const { theme } = useContext(ApplicationContext); const context = useContext(MiniAppContext); const left = useRef(new Animated.Value(0)).current; const [scrollViewWidth, setScrollViewWidth] = useState(0); const [scrollContentWidth, setScrollContentWidth] = useState(0); const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false; const translateX = () => { if (scrollViewWidth && scrollContentWidth) { const value = left.interpolate({ inputRange: [0, scrollContentWidth - scrollViewWidth], outputRange: [0, INDICATOR_CONTAINER_WIDTH - INDICATOR_WIDTH], extrapolate: 'clamp', }); return { transform: [{ translateX: value }] }; } return {}; }; const renderScrollView = () => { return ( { setScrollContentWidth(width); }} onLayout={e => { setScrollViewWidth(e.nativeEvent.layout.width); }} > {children} ); }; const renderIndicator = () => { return ( ); }; return ( {renderScrollView()} {scrollContentWidth > scrollViewWidth && renderIndicator()} ); }; export default PaginationScroll;