import React from 'react' import { Animated, StyleSheet, View } from 'react-native' import { PaginationProps } from 'src/types' import { useSceneViewContext } from '../SceneViewContext' const Dots: React.FC = ({ style, length, spacing = 4, ...rest }) => { const { width, animatedValue: scrollX, activeColor, inactiveColor, } = useSceneViewContext() const [activeTabLeft, setActiveTabLeft] = React.useState(-1) const [activeTabTranslateX, setActiveTabTranslateX] = React.useState( Animated.diffClamp(scrollX, 0, width * length).interpolate({ inputRange: [0, width], outputRange: [0, (width - spacing * 4) / length], }) ) React.useEffect(() => { setActiveTabTranslateX( Animated.diffClamp(scrollX, 0, width * length).interpolate({ inputRange: [0, width], outputRange: [0, (width - spacing * 4) / length], }) ) }, [width, length, scrollX, spacing]) const getTabStyle = React.useCallback( () => ({ backgroundColor: inactiveColor, width: (width - spacing * 4) / length - spacing, height: spacing, borderRadius: spacing / 2, margin: spacing / 2, }), [inactiveColor, width, length, spacing] ) const renderTabs = React.useCallback( () => [...Array(length - 1)].map((_e, i) => ( )), [getTabStyle, length] ) return ( {activeTabLeft > -1 && ( )} {/* first tab */} { setActiveTabLeft(event.nativeEvent.layout.x - spacing / 2) }} style={[ getTabStyle(), { backgroundColor: activeTabLeft === -1 ? activeColor : inactiveColor, }, ]} /> {/* other tabs */} {renderTabs()} ) } const styles = StyleSheet.create({ root: { zIndex: 1000, position: 'absolute', top: 0, paddingVertical: 4, justifyContent: 'center', alignItems: 'center', flexDirection: 'row', }, activeTab: { position: 'absolute', zIndex: 100, }, tabs: { flexDirection: 'row', flex: 1, alignItems: 'center', justifyContent: 'center', }, }) export default Dots