Skip to content

Commit

Permalink
fix: use react-native-iphone-x-helper in bottom-tabs navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Oct 26, 2020
1 parent 256e5ae commit ebe70f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/tabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"hoist-non-react-statics": "^3.3.2",
"react-lifecycles-compat": "^3.0.4",
"react-native-safe-area-view": "^0.14.9",
"react-native-iphone-x-helper": "^1.3.0",
"react-native-tab-view": "^2.15.2"
},
"devDependencies": {
Expand Down
8 changes: 6 additions & 2 deletions packages/tabs/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
TextStyle,
ViewStyle,
} from 'react-native';
import SafeAreaView from 'react-native-safe-area-view';
import Animated from 'react-native-reanimated';
import {
NavigationRoute,
Expand Down Expand Up @@ -94,7 +93,12 @@ export type BottomTabBarOptions = {
| LabelPosition
| ((options: { deviceOrientation: Orientation }) => LabelPosition);
adaptive?: boolean;
safeAreaInset?: React.ComponentProps<typeof SafeAreaView>['forceInset'];
safeAreaInset?: {
top?: 'always' | 'never' | number;
right?: 'always' | 'never' | number;
bottom?: 'always' | 'never' | number;
left?: 'always' | 'never' | number;
};
style?: StyleProp<ViewStyle>;
};

Expand Down
63 changes: 44 additions & 19 deletions packages/tabs/src/views/BottomTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
Platform,
LayoutChangeEvent,
} from 'react-native';
import SafeAreaView from 'react-native-safe-area-view';
import {
getStatusBarHeight,
getBottomSpace,
} from 'react-native-iphone-x-helper';
import { ThemeColors, ThemeContext, NavigationRoute } from 'react-navigation';

import CrossFadeIcon from './CrossFadeIcon';
Expand All @@ -30,6 +33,9 @@ const majorVersion = parseInt(Platform.Version as string, 10);
const isIos = Platform.OS === 'ios';
const isIOS11 = majorVersion >= 11 && isIos;

const DEFAULT_HEIGHT = 49;
const COMPACT_HEIGHT = 29;

const DEFAULT_MAX_TAB_ITEM_WIDTH = 125;
const DEFAULT_KEYBOARD_ANIMATION_CONFIG: KeyboardHidesTabBarAnimationConfig = {
show: {
Expand Down Expand Up @@ -103,9 +109,7 @@ class TabBarBottom extends React.Component<BottomTabBarProps, State> {
showIcon: true,
allowFontScaling: true,
adaptive: isIOS11,
safeAreaInset: { bottom: 'always', top: 'never' } as React.ComponentProps<
typeof SafeAreaView
>['forceInset'],
safeAreaInset: { bottom: 'always', top: 'never' } as const,
};

// eslint-disable-next-line react/sort-comp
Expand Down Expand Up @@ -394,6 +398,7 @@ class TabBarBottom extends React.Component<BottomTabBarProps, State> {
keyboardHidesTabBar,
onTabPress,
onTabLongPress,
isLandscape,
safeAreaInset,
style,
tabStyle,
Expand Down Expand Up @@ -436,13 +441,42 @@ class TabBarBottom extends React.Component<BottomTabBarProps, State> {
marginVertical,
};

const statusBarHeight = getStatusBarHeight(true);
const horizontalInset = isLandscape ? statusBarHeight : 0;
const insets = {
bottom:
typeof safeAreaInset?.bottom === 'number'
? safeAreaInset.bottom
: safeAreaInset?.bottom === 'never'
? 0
: getBottomSpace(),
left:
typeof safeAreaInset?.left === 'number'
? safeAreaInset.left
: safeAreaInset?.left === 'never'
? 0
: horizontalInset,
right:
typeof safeAreaInset?.right === 'number'
? safeAreaInset.right
: safeAreaInset?.right === 'never'
? 0
: horizontalInset,
};

const tabBarStyle = [
{
height:
// @ts-ignore: isPad exists in runtime but not available in type defs
(this._shouldUseHorizontalLabels() && !Platform.isPad
? COMPACT_HEIGHT
: DEFAULT_HEIGHT) + insets.bottom,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.right,
},
styles.tabBar,
isDark ? styles.tabBarDark : styles.tabBarLight,
// @ts-ignore
this._shouldUseHorizontalLabels() && !Platform.isPad
? styles.tabBarCompact
: styles.tabBarRegular,
innerStyle,
];

Expand Down Expand Up @@ -473,7 +507,7 @@ class TabBarBottom extends React.Component<BottomTabBarProps, State> {
}
onLayout={this._handleLayout}
>
<SafeAreaView style={tabBarStyle} forceInset={safeAreaInset}>
<View style={tabBarStyle}>
{routes.map((route, index) => {
const focused = index === navigation.state.index;
const scene = { route, focused };
Expand Down Expand Up @@ -524,15 +558,12 @@ class TabBarBottom extends React.Component<BottomTabBarProps, State> {
</ButtonComponent>
);
})}
</SafeAreaView>
</View>
</Animated.View>
);
}
}

const DEFAULT_HEIGHT = 49;
const COMPACT_HEIGHT = 29;

const styles = StyleSheet.create({
tabBar: {
borderTopWidth: StyleSheet.hairlineWidth,
Expand All @@ -549,12 +580,6 @@ const styles = StyleSheet.create({
container: {
elevation: 8,
},
tabBarCompact: {
height: COMPACT_HEIGHT,
},
tabBarRegular: {
height: DEFAULT_HEIGHT,
},
tab: {
flex: 1,
alignItems: isIos ? 'center' : 'stretch',
Expand Down

0 comments on commit ebe70f5

Please sign in to comment.