Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanzitelli/9f5b27e095339afd5309d1d087872615 to your computer and use it in GitHub Desktop.
Save kanzitelli/9f5b27e095339afd5309d1d087872615 to your computer and use it in GitHub Desktop.
React Native Navigation (v7.18.1) Tab Indicator Example
// The purpose of this gist is to give an idea of how to implement tab indicator functionality using react-native-navigation
// This example is based on rnn-starter (https://github.com/kanzitelli/rnn-starter)
import { Navigation, LayoutComponent } from 'react-native-navigation';
import merge from 'lodash/merge';
// types
type TabScreen = 'Home' | 'Explore' | 'Account';
type TabScreenInfo = {
name: TabScreen;
componentId: string;
};
type TabScreens = Array<TabScreenInfo>;
type TabScreensLayouts = {
[key in Screen]: LayoutComponent;
};
// help methods
const withTabIndicator = (yes = true): Options => ({
bottomTab: {
dotIndicator: {
visible: yes,
color: 'blue',
size: 12,
},
},
});
// screens
const tabScreens: TabScreens = [
{ name: 'Home', componentId: 'HomeComponentId' },
{ name: 'Explore', componentId: 'ExploreComponentId' },
{ name: 'Account', componentId: 'AccountComponentId' },
];
const tabScreensLayouts: TabScreensLayouts = {
Home: {
id: tabScreens[0].componentId,
name: 'Home',
options: {
...withTabIndicator(), // we set indicator for the first tab
// other props...
},
},
Explore: {
id: tabScreens[1].componentId,
name: 'Explore',
options: {
// other props...
},
},
Account: {
id: tabScreens[2].componentId,
name: 'Account',
options: {
// other props...
},
},
}
// then, after you register the screens above, we need to register an event from Navigation
Navigation.events().registerBottomTabSelectedListener(
async ({ selectedTabIndex, unselectedTabIndex }) => {
const sameTab = selectedTabIndex === unselectedTabIndex;
const selectedTab = tabScreens[selectedTabIndex];
if (selectedTab && !sameTab) {
// clear previous tab indicators
for (const ts of tabScreens) {
Navigation.mergeOptions(ts.componentId, withTabIndicator(false));
}
// setting tab indicator to a selected tab
Navigation.mergeOptions(selectedTab.componentId, withTabIndicator());
}
},
);
// tab indicators should work :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment