Skip to content

Commit 04ebd1c

Browse files
fix(bottom-navigation): keep the active indicator mounted so it fully expands (#5206)
On the New Architecture the active indicator could stay at half width after switching tabs: it was mounted only for the focused tab, so the native-driven animation started before the view was attached. The indicator is now mounted for every tab and hidden with opacity. It grows from 40% width and fades in over the first fifth of a switch, and the previous indicator fades out over the first fifth, as Material Components for Android does. activeIndicatorStyle now styles the pill inside the animated wrapper, so a custom opacity or transform no longer breaks the animation, and sizes and positions still resolve against the icon slot as before. Fixes #4767 Fixes #4819 Co-authored-by: Doberjohn <johnfanidis@gmail.com>
1 parent ffa3baa commit 04ebd1c

4 files changed

Lines changed: 1473 additions & 100 deletions

File tree

‎src/components/BottomNavigation/BottomNavigation.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ export type Props<Route extends BaseRoute> = {
254254
*/
255255
labelMaxFontSizeMultiplier?: number;
256256
style?: StyleProp<ViewStyle>;
257+
/**
258+
* @supported Available in v5.x with theme version 3
259+
* Style for the active indicator pill.
260+
* The show and hide animation (opacity and horizontal scale) is applied on top of it.
261+
*/
257262
activeIndicatorStyle?: StyleProp<ViewStyle>;
258263
/**
259264
* @optional

‎src/components/BottomNavigation/BottomNavigationBar.tsx‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ export type Props<Route extends BaseRoute> = {
201201
*/
202202
labelMaxFontSizeMultiplier?: number;
203203
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
204+
/**
205+
* @supported Available in v5.x with theme version 3
206+
* Style for the active indicator pill.
207+
* The show and hide animation (opacity and horizontal scale) is applied on top of it.
208+
*/
204209
activeIndicatorStyle?: StyleProp<ViewStyle>;
205210
/**
206211
* @optional
@@ -671,12 +676,27 @@ const BottomNavigationBar = <Route extends BaseRoute>({
671676
: 1;
672677

673678
// Scale horizontally the outline pill
674-
const outlineScale = focused
675-
? active.interpolate({
676-
inputRange: [0, 1],
677-
outputRange: [0.5, 1],
678-
})
679-
: 0;
679+
const outlineScale = active.interpolate({
680+
inputRange: [0, 1],
681+
outputRange: [0.4, 1],
682+
});
683+
684+
// Fade the pill in over the first fifth of showing it and out
685+
// over the first fifth of hiding it, as Material Components for
686+
// Android does.
687+
const outlineOpacity = active.interpolate(
688+
focused
689+
? {
690+
inputRange: [0, 0.2, 1],
691+
outputRange: [0, 1, 1],
692+
extrapolate: 'clamp',
693+
}
694+
: {
695+
inputRange: [0, 0.8, 1],
696+
outputRange: [0, 0, 1],
697+
extrapolate: 'clamp',
698+
}
699+
);
680700

681701
const badge = getBadge({ route });
682702

@@ -740,21 +760,26 @@ const BottomNavigationBar = <Route extends BaseRoute>({
740760
},
741761
]}
742762
>
743-
{isV3 && focused && (
763+
{isV3 && (
744764
<Animated.View
745765
style={[
746-
styles.outline,
766+
styles.outlineContainer,
747767
{
748-
transform: [
749-
{
750-
scaleX: outlineScale,
751-
},
752-
],
753-
backgroundColor: theme.colors.secondaryContainer,
768+
opacity: outlineOpacity,
769+
transform: [{ scaleX: outlineScale }],
754770
},
755-
activeIndicatorStyle,
756771
]}
757-
/>
772+
>
773+
<View
774+
style={[
775+
styles.outline,
776+
{
777+
backgroundColor: theme.colors.secondaryContainer,
778+
},
779+
activeIndicatorStyle,
780+
]}
781+
/>
782+
</Animated.View>
758783
)}
759784
<Animated.View
760785
style={[
@@ -996,6 +1021,12 @@ const styles = StyleSheet.create({
9961021
justifyContent: 'center',
9971022
alignItems: 'center',
9981023
},
1024+
// Fills the 32x32 icon container, so sizes, alignSelf and absolute positions
1025+
// in `activeIndicatorStyle` resolve against the icon slot.
1026+
outlineContainer: {
1027+
...StyleSheet.absoluteFill,
1028+
justifyContent: 'center',
1029+
},
9991030
outline: {
10001031
width: OUTLINE_WIDTH,
10011032
height: OUTLINE_WIDTH / 2,

‎src/components/__tests__/BottomNavigation.test.tsx‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const styles = StyleSheet.create({
2121
backgroundColor: {
2222
backgroundColor: red300,
2323
},
24+
activeIndicator: {
25+
opacity: 0.5,
26+
transform: [{ translateY: 2 }],
27+
},
2428
});
2529

2630
const icons = ['magnify', 'camera', 'inbox', 'heart', 'shopping-music'];
@@ -599,6 +603,86 @@ it("allows customizing Route's type via generics", () => {
599603
expect(tree).toMatchSnapshot();
600604
});
601605

606+
it('keeps inactive indicators hidden when activeIndicatorStyle sets opacity and transform', () => {
607+
const { container } = render(
608+
<BottomNavigation
609+
navigationState={createState(0, 3)}
610+
onIndexChange={jest.fn()}
611+
renderScene={({ route }) => route.title}
612+
activeIndicatorStyle={styles.activeIndicator}
613+
/>
614+
);
615+
616+
const pills = container.findAll(
617+
(node) =>
618+
typeof node.type === 'string' &&
619+
StyleSheet.flatten(node.props.style)?.opacity ===
620+
styles.activeIndicator.opacity
621+
);
622+
expect(pills).toHaveLength(3);
623+
624+
const [focused, ...unfocused] = pills.map((pill) => {
625+
let parent = pill.parent;
626+
while (parent && typeof parent.type !== 'string') {
627+
parent = parent.parent;
628+
}
629+
return parent;
630+
});
631+
632+
pills.forEach((pill) => {
633+
expect(pill).toHaveStyle(styles.activeIndicator);
634+
});
635+
expect(focused).toHaveStyle({ opacity: 1, transform: [{ scaleX: 1 }] });
636+
unfocused.forEach((indicator) => {
637+
expect(indicator).toHaveStyle({ opacity: 0 });
638+
});
639+
});
640+
641+
it('hides the previous indicator and shows the new one after switching tabs', () => {
642+
const props = {
643+
onIndexChange: jest.fn(),
644+
renderScene: ({ route }: { route: { title?: string } }) => route.title,
645+
activeIndicatorStyle: styles.activeIndicator,
646+
};
647+
const { getAllByRole, rerender } = render(
648+
<BottomNavigation navigationState={createState(0, 3)} {...props} />
649+
);
650+
651+
rerender(<BottomNavigation navigationState={createState(1, 3)} {...props} />);
652+
act(() => {
653+
jest.runAllTimers();
654+
});
655+
656+
// Multiply opacity and collect transforms from the pill up through its
657+
// ancestors, so the check doesn't depend on which view holds the animated
658+
// styles.
659+
const indicators = getAllByRole('button').map((tab) => {
660+
const pill = tab.find(
661+
(node) =>
662+
typeof node.type === 'string' &&
663+
StyleSheet.flatten(node.props.style)?.opacity ===
664+
styles.activeIndicator.opacity
665+
);
666+
let opacity = 1;
667+
const transforms: unknown[] = [];
668+
let node: typeof pill | null = pill;
669+
while (node) {
670+
if (typeof node.type === 'string') {
671+
const style = StyleSheet.flatten(node.props.style) ?? {};
672+
if (typeof style.opacity === 'number') opacity *= style.opacity;
673+
if (Array.isArray(style.transform)) transforms.push(...style.transform);
674+
}
675+
node = node.parent;
676+
}
677+
return { opacity, transforms };
678+
});
679+
680+
expect(indicators.map(({ opacity }) => opacity)).toEqual([0, 0.5, 0]);
681+
expect(indicators[1].transforms).toEqual(
682+
expect.arrayContaining([{ scaleX: 1 }, { translateY: 2 }])
683+
);
684+
});
685+
602686
describe.each([
603687
{ supported: true, touchable: 'TouchableRipple' },
604688
{ supported: false, touchable: 'Pressable' },

0 commit comments

Comments
 (0)