-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathNavigationBar.js
More file actions
112 lines (93 loc) · 3.19 KB
/
NavigationBar.js
File metadata and controls
112 lines (93 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import {
StatusBar,
Animated,
Platform,
View,
} from 'react-native';
import _ from 'lodash';
import color from 'tinycolor2';
import { connectStyle } from '@shoutem/theme';
import { connectAnimation } from '@shoutem/animation';
import { Device } from '../../helpers';
import composeChildren from './composeChildren';
function getBackgroundColor(style) {
const styleWithBg = _.find(style, (styleDef) =>
styleDef.backgroundColor && styleDef.backgroundColor !== 'transparent'
);
return styleWithBg && styleWithBg.backgroundColor || 'transparent';
}
// eslint-disable-next-line react/prefer-stateless-function
class NavigationBar extends PureComponent {
static propTypes = {
leftComponent: PropTypes.node,
centerComponent: PropTypes.node,
rightComponent: PropTypes.node,
style: PropTypes.object,
id: PropTypes.string,
statusBarColor: PropTypes.string,
};
static defaultProps = {
id: 'default',
};
setStatusBarStyle(backgroundColor) {
function chooseBarStyle(bgColor) {
return color(bgColor).isDark() ? 'light-content' : 'default';
}
function setStyle(bgColor) {
const statusBarColor = _.get(this.props, 'statusBarColor', bgColor);
const color = statusBarColor || bgColor;
if (Platform.OS === 'android') {
StatusBar.setBackgroundColor('rgba(0, 0, 0, 0.2)');
} else {
const barStyle = chooseBarStyle(color);
StatusBar.setBarStyle(barStyle);
}
}
// This is little bit hacky, but is the only way
// to determine the current value of interpolated Animated.Value
// Other way would be to ask developer to provide Animated.Value
// used to interpolate backgroundColor. But this way developer doesn't
// have to concern about status bar if he animates navigation bar color
if (backgroundColor && backgroundColor._parent instanceof Animated.Value) {
backgroundColor._parent.addListener((animation) => {
setStyle(backgroundColor._interpolation(animation.value));
});
setStyle(backgroundColor._interpolation(0));
} else {
setStyle(backgroundColor);
}
}
renderStatusBar() {
const { style } = this.props;
return (<View style={style.statusBar} />);
}
render() {
const {
leftComponent,
rightComponent,
centerComponent,
style,
id,
} = this.props;
const backgroundColor = getBackgroundColor(style);
this.setStatusBarStyle(backgroundColor);
// Key must be set to render new screen NavigationBar
return (
<Animated.View style={style.container} key={id}>
{this.renderStatusBar()}
<View style={style.componentsContainer}>
<View style={style.leftComponent}>{leftComponent}</View>
<View style={style.centerComponent}>{centerComponent}</View>
<View style={style.rightComponent}>{rightComponent}</View>
</View>
</Animated.View>
);
}
}
const AnimatedNavigationBar = connectAnimation(composeChildren(NavigationBar));
const StyledNavigationBar = connectStyle('shoutem.ui.NavigationBar')(AnimatedNavigationBar);
export {
StyledNavigationBar as NavigationBar,
};