Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: improve keyboard handling with bottom tab bar
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Oct 10, 2019
1 parent e789846 commit 42beb66
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 57 deletions.
2 changes: 1 addition & 1 deletion packages/bottom-tabs/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export type BottomTabNavigationConfig = {

export type BottomTabBarOptions = {
/**
* Whether the tab bar gets hidden when the keyboard is shown.
* Whether the tab bar gets hidden when the keyboard is shown. Defaults to `false`.
*/
keyboardHidesTabBar?: boolean;
/**
Expand Down
119 changes: 63 additions & 56 deletions packages/bottom-tabs/src/views/BottomTabBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {
View,
Animated,
StyleSheet,
Keyboard,
Expand Down Expand Up @@ -32,7 +33,7 @@ const DEFAULT_MAX_TAB_ITEM_WIDTH = 125;

export default class TabBarBottom extends React.Component<Props, State> {
static defaultProps = {
keyboardHidesTabBar: true,
keyboardHidesTabBar: false,
activeTintColor: '#007AFF',
activeBackgroundColor: 'transparent',
inactiveTintColor: '#8E8E93',
Expand Down Expand Up @@ -82,18 +83,20 @@ export default class TabBarBottom extends React.Component<Props, State> {
this.setState({ keyboard: true }, () =>
Animated.timing(this.state.visible, {
toValue: 0,
duration: 150,
duration: 200,
useNativeDriver: true,
}).start()
);

private handleKeyboardHide = () =>
Animated.timing(this.state.visible, {
toValue: 1,
duration: 100,
duration: 250,
useNativeDriver: true,
}).start(() => {
this.setState({ keyboard: false });
}).start(({ finished }) => {
if (finished) {
this.setState({ keyboard: false });
}
});

private handleLayout = (e: LayoutChangeEvent) => {
Expand Down Expand Up @@ -305,58 +308,59 @@ export default class TabBarBottom extends React.Component<Props, State> {
pointerEvents={
keyboardHidesTabBar && this.state.keyboard ? 'none' : 'auto'
}
onLayout={this.handleLayout}
>
{routes.map((route, index) => {
const focused = index === state.index;
const scene = { route, focused };
const accessibilityLabel = getAccessibilityLabel({
route,
});

const accessibilityRole = getAccessibilityRole({
route,
});

const accessibilityStates = getAccessibilityStates(scene);

const testID = getTestID({ route });

const backgroundColor = focused
? activeBackgroundColor
: inactiveBackgroundColor;

const ButtonComponent =
getButtonComponent({ route }) ||
TouchableWithoutFeedbackWrapper;

return (
<NavigationContext.Provider
key={route.key}
value={descriptors[route.key].navigation}
>
<ButtonComponent
onPress={() => onTabPress({ route })}
onLongPress={() => onTabLongPress({ route })}
testID={testID}
accessibilityLabel={accessibilityLabel}
accessibilityRole={accessibilityRole}
accessibilityStates={accessibilityStates}
style={[
styles.tab,
{ backgroundColor },
this.shouldUseHorizontalLabels()
? styles.tabLandscape
: styles.tabPortrait,
tabStyle,
]}
<View style={styles.content} onLayout={this.handleLayout}>
{routes.map((route, index) => {
const focused = index === state.index;
const scene = { route, focused };
const accessibilityLabel = getAccessibilityLabel({
route,
});

const accessibilityRole = getAccessibilityRole({
route,
});

const accessibilityStates = getAccessibilityStates(scene);

const testID = getTestID({ route });

const backgroundColor = focused
? activeBackgroundColor
: inactiveBackgroundColor;

const ButtonComponent =
getButtonComponent({ route }) ||
TouchableWithoutFeedbackWrapper;

return (
<NavigationContext.Provider
key={route.key}
value={descriptors[route.key].navigation}
>
{this.renderIcon(scene)}
{this.renderLabel(scene)}
</ButtonComponent>
</NavigationContext.Provider>
);
})}
<ButtonComponent
onPress={() => onTabPress({ route })}
onLongPress={() => onTabLongPress({ route })}
testID={testID}
accessibilityLabel={accessibilityLabel}
accessibilityRole={accessibilityRole}
accessibilityStates={accessibilityStates}
style={[
styles.tab,
{ backgroundColor },
this.shouldUseHorizontalLabels()
? styles.tabLandscape
: styles.tabPortrait,
tabStyle,
]}
>
{this.renderIcon(scene)}
{this.renderLabel(scene)}
</ButtonComponent>
</NavigationContext.Provider>
);
})}
</View>
</Animated.View>
)}
</SafeAreaConsumer>
Expand All @@ -375,9 +379,12 @@ const styles = StyleSheet.create({
backgroundColor: '#fff',
borderTopWidth: StyleSheet.hairlineWidth,
borderTopColor: 'rgba(0, 0, 0, .3)',
flexDirection: 'row',
elevation: 8,
},
content: {
flex: 1,
flexDirection: 'row',
},
tab: {
flex: 1,
alignItems: isIos ? 'center' : 'stretch',
Expand Down

0 comments on commit 42beb66

Please sign in to comment.