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

Commit 08a6268

Browse files
adrianhasatya164
authored andcommitted
feat: add keyboardHidesTabBarAnimationConfig props (#195)
1 parent b4f288f commit 08a6268

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

src/types.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,49 @@ export type Orientation = 'horizontal' | 'vertical';
3838

3939
export type LabelPosition = 'beside-icon' | 'below-icon';
4040

41+
interface BaseAnimation {
42+
useNativeDriver?: boolean;
43+
}
44+
interface TimingAnimation extends BaseAnimation {
45+
easing?: (value: number) => number;
46+
duration?: number;
47+
delay?: number;
48+
}
49+
interface SpringAnimation extends BaseAnimation {
50+
overshootClamping?: boolean;
51+
restDisplacementThreshold?: number;
52+
restSpeedThreshold?: number;
53+
velocity?: number | { x: number; y: number };
54+
bounciness?: number;
55+
speed?: number;
56+
tension?: number;
57+
friction?: number;
58+
stiffness?: number;
59+
mass?: number;
60+
damping?: number;
61+
delay?: number;
62+
}
63+
export type TimingKeyboardAnimationConfig = {
64+
animation: 'timing';
65+
config?: TimingAnimation;
66+
};
67+
export type SpringKeyboardAnimationConfig = {
68+
animation: 'spring';
69+
config?: SpringAnimation;
70+
};
71+
export type KeyboardAnimationConfig =
72+
| TimingKeyboardAnimationConfig
73+
| SpringKeyboardAnimationConfig;
74+
export type KeyboardHidesTabBarAnimationConfig = {
75+
show: KeyboardAnimationConfig;
76+
hide: KeyboardAnimationConfig;
77+
};
78+
4179
export type BottomTabBarOptions = {
4280
keyboardHidesTabBar?: boolean;
81+
keyboardHidesTabBarAnimationConfig?: Partial<
82+
KeyboardHidesTabBarAnimationConfig
83+
>;
4384
activeTintColor?: ThemedColor;
4485
inactiveTintColor?: ThemedColor;
4586
activeBackgroundColor?: ThemedColor;

src/views/BottomTabBar.tsx

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { ThemeColors, ThemeContext, NavigationRoute } from 'react-navigation';
1313

1414
import CrossFadeIcon from './CrossFadeIcon';
1515
import withDimensions from '../utils/withDimensions';
16-
import { BottomTabBarProps, ButtonComponentProps } from '../types';
16+
import {
17+
BottomTabBarProps,
18+
ButtonComponentProps,
19+
KeyboardHidesTabBarAnimationConfig,
20+
KeyboardAnimationConfig,
21+
} from '../types';
1722

1823
type State = {
1924
layout: { height: number; width: number };
@@ -26,6 +31,22 @@ const isIos = Platform.OS === 'ios';
2631
const isIOS11 = majorVersion >= 11 && isIos;
2732

2833
const DEFAULT_MAX_TAB_ITEM_WIDTH = 125;
34+
const DEFAULT_KEYBOARD_ANIMATION_CONFIG: KeyboardHidesTabBarAnimationConfig = {
35+
show: {
36+
animation: 'timing',
37+
config: {
38+
useNativeDriver: true,
39+
duration: 150,
40+
},
41+
},
42+
hide: {
43+
animation: 'timing',
44+
config: {
45+
useNativeDriver: true,
46+
duration: 100,
47+
},
48+
},
49+
};
2950

3051
class TouchableWithoutFeedbackWrapper extends React.Component<
3152
ButtonComponentProps
@@ -64,6 +85,7 @@ class TouchableWithoutFeedbackWrapper extends React.Component<
6485
class TabBarBottom extends React.Component<BottomTabBarProps, State> {
6586
static defaultProps = {
6687
keyboardHidesTabBar: true,
88+
keyboardHidesTabBarAnimationConfig: DEFAULT_KEYBOARD_ANIMATION_CONFIG,
6789
activeTintColor: {
6890
light: '#007AFF',
6991
dark: '#fff',
@@ -115,23 +137,58 @@ class TabBarBottom extends React.Component<BottomTabBarProps, State> {
115137
// @ts-ignore
116138
context: 'light' | 'dark';
117139

118-
_handleKeyboardShow = () =>
119-
this.setState({ keyboard: true }, () =>
120-
Animated.timing(this.state.visible, {
140+
_getKeyboardAnimationConfigByType = (
141+
type: keyof KeyboardHidesTabBarAnimationConfig
142+
): KeyboardAnimationConfig => {
143+
const { keyboardHidesTabBarAnimationConfig } = this.props;
144+
const defaultKeyboardAnimationConfig =
145+
DEFAULT_KEYBOARD_ANIMATION_CONFIG[type];
146+
const keyboardAnimationConfig =
147+
(keyboardHidesTabBarAnimationConfig &&
148+
keyboardHidesTabBarAnimationConfig[type]) ||
149+
defaultKeyboardAnimationConfig;
150+
151+
// merge config only `timing` animation
152+
if (
153+
keyboardAnimationConfig &&
154+
keyboardAnimationConfig.animation === 'timing'
155+
) {
156+
return {
157+
...defaultKeyboardAnimationConfig,
158+
...keyboardAnimationConfig,
159+
config: {
160+
...defaultKeyboardAnimationConfig.config,
161+
...keyboardAnimationConfig.config,
162+
},
163+
};
164+
}
165+
166+
return keyboardAnimationConfig as KeyboardAnimationConfig;
167+
};
168+
169+
_handleKeyboardShow = () => {
170+
this.setState({ keyboard: true }, () => {
171+
const { animation, config } = this._getKeyboardAnimationConfigByType(
172+
'show'
173+
);
174+
Animated[animation](this.state.visible, {
121175
toValue: 0,
122-
duration: 150,
123-
useNativeDriver: true,
124-
}).start()
125-
);
176+
...config,
177+
}).start();
178+
});
179+
};
126180

127-
_handleKeyboardHide = () =>
128-
Animated.timing(this.state.visible, {
181+
_handleKeyboardHide = () => {
182+
const { animation, config } = this._getKeyboardAnimationConfigByType(
183+
'hide'
184+
);
185+
Animated[animation](this.state.visible, {
129186
toValue: 1,
130-
duration: 100,
131-
useNativeDriver: true,
187+
...config,
132188
}).start(() => {
133189
this.setState({ keyboard: false });
134190
});
191+
};
135192

136193
_handleLayout = (e: LayoutChangeEvent) => {
137194
const { layout } = this.state;

0 commit comments

Comments
 (0)