-
Notifications
You must be signed in to change notification settings - Fork 184
/
BpkNavigationBar.android.js
249 lines (225 loc) · 6.52 KB
/
BpkNavigationBar.android.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Backpack - Skyscanner's Design System
*
* Copyright 2018 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* @flow */
import React, { Component, isValidElement } from 'react';
import PropTypes from 'prop-types';
import { type Element, StyleSheet, View, ViewPropTypes } from 'react-native';
import {
withTheme,
getThemeAttributes,
makeThemePropType,
} from 'react-native-bpk-theming';
import {
colorBlue300,
colorBlue500,
colorWhite,
} from 'bpk-tokens/tokens/base.react.native';
import {
type CommonTheme,
type TitleProp,
THEME_ATTRIBUTES,
TITLE_PROPTYPE,
} from './common-types';
import BpkNavigationBarButtonAndroid from './BpkNavigationBarButtonAndroid.android';
import TitleView from './TitleView';
const ANDROID_THEME_ATTRIBUTES = [
...THEME_ATTRIBUTES,
'navigationBarStatusBarStyle',
'navigationBarStatusBarColor',
];
// NOTE: this file explicitly does not use the Backpack tokens(for spacing) because it's based on Material design tokens not Backpack.
const styles = StyleSheet.create({
barOuter: {
flexDirection: 'column',
paddingHorizontal: 8, // eslint-disable-line backpack/use-tokens
width: '100%',
backgroundColor: colorBlue500,
elevation: 3,
},
barOuterWithSubtitle: {
paddingBottom: 16, // eslint-disable-line backpack/use-tokens
},
bar: {
flexDirection: 'row',
justifyContent: 'space-between',
height: 56, // eslint-disable-line backpack/use-tokens
alignItems: 'center',
},
barOnlyTrailingButton: {
justifyContent: 'flex-end',
},
titleString: {
position: 'absolute',
start: 72, // eslint-disable-line backpack/use-tokens
end: 72, // eslint-disable-line backpack/use-tokens
bottom: 15, // eslint-disable-line backpack/use-tokens
},
titleViewOuter: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center',
},
leadingIcon: {
padding: 8, // eslint-disable-line backpack/use-tokens
color: colorWhite,
},
subtitleViewContainer: {
paddingHorizontal: 8, // eslint-disable-line backpack/use-tokens
},
});
type AndroidTheme = {
...$Exact<CommonTheme>,
navigationBarStatusBarColor: string,
};
export type Props = {
title: TitleProp,
theme: ?AndroidTheme,
leadingButton: ?Element<typeof BpkNavigationBarButtonAndroid>,
trailingButton: ?Element<typeof BpkNavigationBarButtonAndroid>,
subtitleView: ?Element<any>,
// FIXME: We need a better flow type for style
style: any,
};
class BpkNavigationBar extends Component<Props, {}> {
theme: ?AndroidTheme;
static propTypes = {
title: TITLE_PROPTYPE.isRequired,
theme: makeThemePropType(ANDROID_THEME_ATTRIBUTES),
leadingButton: PropTypes.element,
trailingButton: PropTypes.element,
subtitleView: PropTypes.element,
style: ViewPropTypes.style,
};
static defaultProps = {
theme: null,
leadingButton: null,
trailingButton: null,
subtitleView: null,
style: null,
};
constructor(props) {
super(props);
this.theme = getThemeAttributes(
ANDROID_THEME_ATTRIBUTES,
this.props.theme || {},
);
}
componentDidUpdate() {
this.theme = getThemeAttributes(
ANDROID_THEME_ATTRIBUTES,
this.props.theme || {},
);
}
render() {
const {
title,
leadingButton,
trailingButton,
subtitleView,
style,
} = this.props;
const hasSubtitleView = subtitleView !== null;
const barStyle = [styles.bar];
const outerBarStyle = [styles.barOuter];
const titleStyle = [styles.titleString];
let tintColor = colorWhite;
let disabledTintColor = colorBlue300;
let touchableColor = colorWhite;
let titleView = null;
// This if ensures Flow correctly refines the type of
// title in the body of the if to `'string' | TitleWithIcon
if (
typeof title === 'string' ||
(typeof title === 'object' && title.icon)
) {
titleView = (
<TitleView
title={title}
tintColor={tintColor}
style={styles.titleString}
/>
);
}
// This if ensures Flow correctly refines the type of
// title in the body of the if to `Element`.
// While this if is mutually exclusive to the above it
// cannot be an else if as Flow seems unable to handle this.
if (typeof title === 'object' && title.type && isValidElement(title)) {
titleView = (
<View style={styles.titleViewOuter}>
{React.cloneElement(title, {
style: [
title.props.style ? title.props.style : null,
{ maxHeight: 32 }, // eslint-disable-line backpack/use-tokens
],
})}
</View>
);
}
if (this.theme) {
const {
navigationBarTintColor,
navigationBarDisabledTintColor,
navigationBarBackgroundColor,
} = this.theme;
titleStyle.push({ color: navigationBarTintColor });
outerBarStyle.push({
backgroundColor: navigationBarBackgroundColor,
});
tintColor = navigationBarTintColor;
disabledTintColor = navigationBarDisabledTintColor;
touchableColor = navigationBarTintColor;
}
if (hasSubtitleView) {
outerBarStyle.push(styles.barOuterWithSubtitle);
}
if (!leadingButton && trailingButton) {
barStyle.push(styles.barOnlyTrailingButton);
}
if (style) {
outerBarStyle.push(style);
}
return (
<View style={outerBarStyle}>
<View style={barStyle}>
{leadingButton &&
React.cloneElement(leadingButton, {
disabledTintColor,
touchableColor,
tintColor,
})}
{titleView}
{trailingButton &&
React.cloneElement(trailingButton, {
disabledTintColor,
touchableColor,
tintColor,
})}
</View>
{hasSubtitleView && (
<View style={styles.subtitleViewContainer}>{subtitleView}</View>
)}
</View>
);
}
}
export default withTheme(BpkNavigationBar);