Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change of width of tabBarIndicator due to change of leftPadding #11713

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/react-native-tab-view/src/TabBarIndicator.tsx
Expand Up @@ -65,6 +65,14 @@ export function TabBarIndicator<T extends Route>({
}: Props<T>) {
const isIndicatorShown = React.useRef(false);
const isWidthDynamic = width === 'auto';
const initialWidth = Platform.OS === 'android' ? 70 : 1;

const getShiftAfterScale = (modif: number, coef: number) => {
if (modif > 0) {
return ((initialWidth * (modif - 1)) / (2 * modif)) * coef;
}
return 1;
};

const opacity = useAnimatedValue(isWidthDynamic ? 0 : 1);

Expand Down Expand Up @@ -114,7 +122,10 @@ export function TabBarIndicator<T extends Route>({

if (width === 'auto') {
const inputRange = routes.map((_, i) => i);
const outputRange = inputRange.map(getTabWidth);
const outputRange = inputRange.map((i) => getTabWidth(i) / initialWidth);
const outputShiftRange = inputRange.map((i) =>
getShiftAfterScale(outputRange[i], direction === 'rtl' ? -1 : 1)
);

transform.push(
{
Expand All @@ -127,15 +138,24 @@ export function TabBarIndicator<T extends Route>({
})
: outputRange[0],
},
{ translateX: direction === 'rtl' ? -0.5 : 0.5 }
{
translateX:
routes.length > 1
? position.interpolate({
inputRange,
outputRange: outputShiftRange,
extrapolate: 'clamp',
})
: 0,
}
);
}

return (
<Animated.View
style={[
styles.indicator,
{ width: width === 'auto' ? 1 : width },
{ width: width === 'auto' ? initialWidth : width },
// If layout is not available, use `left` property for positioning the indicator
// This avoids rendering delay until we are able to calculate translateX
// If platform is macos use `left` property as `transform` is broken at the moment.
Expand Down