import React from 'react'; import { useState, useCallback } from 'react'; import { useWindowDimensions, View, ScrollView, Pressable } from 'react-native'; import type { StyleProp, ViewStyle } from 'react-native'; import type { RenderTabBar, Route } from './types'; type Props = { routes: ReadonlyArray; currentIndex: number; onPress: (index: number) => void; render: RenderTabBar; contentContainerStyle?: StyleProp; style: StyleProp; overflowWitdth?: number; }; export function TabBar({ routes, currentIndex, onPress, render, contentContainerStyle, overflowWitdth, style, }: Props) { const [isScrollEnabled, setIsScrollEnabled] = useState(false); const { width: containerWidth } = useWindowDimensions(); const onTabPress = useCallback( (index: number) => { return () => onPress(index); }, [onPress] ); const onContentSizeChange = useCallback( (width: number) => { if ( (overflowWitdth && overflowWitdth <= width) || containerWidth < width ) { setIsScrollEnabled(true); } else { setIsScrollEnabled(false); } }, [containerWidth, overflowWitdth] ); return ( {routes.map((route, index) => { const isActive = index === currentIndex; return ( {render({ route, isActive })} ); })} ); }