import * as React from 'react'; import { Animated, Easing, I18nManager, Platform, StyleProp, StyleSheet, ViewStyle, } from 'react-native'; import type { NavigationState, Route, SceneRendererProps } from './types'; import { useAnimatedValue } from './useAnimatedValue'; export type GetTabWidth = (index: number) => number; export type Props = SceneRendererProps & { navigationState: NavigationState; width: string | number; style?: StyleProp; getTabWidth: GetTabWidth; gap?: number; }; const getTranslateX = ( position: Animated.AnimatedInterpolation, routes: Route[], getTabWidth: GetTabWidth, gap?: number ) => { const inputRange = routes.map((_, i) => i); // every index contains widths at all previous indices const outputRange = routes.reduce((acc, _, i) => { if (i === 0) return [0]; return [...acc, acc[i - 1] + getTabWidth(i - 1) + (gap ?? 0)]; }, []); const translateX = position.interpolate({ inputRange, outputRange, extrapolate: 'clamp', }); return Animated.multiply(translateX, I18nManager.isRTL ? -1 : 1); }; export function TabBarIndicator({ getTabWidth, layout, navigationState, position, width, gap, style, }: Props) { const isIndicatorShown = React.useRef(false); const isWidthDynamic = width === 'auto'; const opacity = useAnimatedValue(isWidthDynamic ? 0 : 1); const indicatorVisible = isWidthDynamic ? layout.width && navigationState.routes .slice(0, navigationState.index) .every((_, r) => getTabWidth(r)) : true; React.useEffect(() => { const fadeInIndicator = () => { if ( !isIndicatorShown.current && isWidthDynamic && // We should fade-in the indicator when we have widths for all the tab items indicatorVisible ) { isIndicatorShown.current = true; Animated.timing(opacity, { toValue: 1, duration: 150, easing: Easing.in(Easing.linear), useNativeDriver: true, }).start(); } }; fadeInIndicator(); return () => opacity.stopAnimation(); }, [indicatorVisible, isWidthDynamic, opacity]); const { routes } = navigationState; const transform = []; if (layout.width) { const translateX = routes.length > 1 ? getTranslateX(position, routes, getTabWidth, gap) : 0; transform.push({ translateX }); } if (width === 'auto') { const inputRange = routes.map((_, i) => i); const outputRange = inputRange.map(getTabWidth); transform.push( { scaleX: routes.length > 1 ? position.interpolate({ inputRange, outputRange, extrapolate: 'clamp', }) : outputRange[0], }, { translateX: 0.5 } ); } return ( ); } const styles = StyleSheet.create({ indicator: { backgroundColor: '#ffeb3b', position: 'absolute', left: 0, bottom: 0, right: 0, height: 2, }, });