forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppbarHeader.js
132 lines (121 loc) · 3.61 KB
/
AppbarHeader.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
/* @flow */
import * as React from 'react';
import { View, Platform, SafeAreaView, StyleSheet } from 'react-native';
import Appbar, { DEFAULT_APPBAR_HEIGHT } from './Appbar';
import { withTheme } from '../../core/theming';
import type { Theme } from '../../types';
type Props = React.ElementConfig<typeof Appbar> & {|
/**
* Whether the background color is a dark color. A dark header will render light text and vice-versa.
*/
dark?: boolean,
/**
* Extra padding to add at the top of header to account for translucent status bar.
* This is automatically handled on iOS >= 11 including iPhone X using `SafeAreaView`.
* If you are using Expo, we assume translucent status bar and set a height for status bar automatically.
* Pass `0` or a custom value to disable the default behaviour, and customize the height.
*/
statusBarHeight?: number,
/**
* Content of the header.
*/
children: React.Node,
/**
* @optional
*/
theme: Theme,
style?: any,
|};
const DEFAULT_STATUSBAR_HEIGHT_EXPO =
global.__expo && global.__expo.Constants
? global.__expo.Constants.statusBarHeight
: 0;
const DEFAULT_STATUSBAR_HEIGHT = Platform.select({
android: DEFAULT_STATUSBAR_HEIGHT_EXPO,
ios: Platform.Version < 11 ? DEFAULT_STATUSBAR_HEIGHT_EXPO : 0,
});
/**
* A component to use as a header at the top of the screen.
* It can contain the screen title, controls such as navigation buttons, menu button etc.
*
* <div class="screenshots">
* <figure>
* <img class="medium" src="screenshots/appbar-header.android.png" />
* <figcaption>Android</figcaption>
* </figure>
* <figure>
* <img class="medium" src="screenshots/appbar-header.ios.png" />
* <figcaption>iOS</figcaption>
* </figure>
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Appbar } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* _goBack = () => console.log('Went back');
*
* _onSearch = () => console.log('Searching');
*
* _onMore = () => console.log('Shown more');
*
* render() {
* return (
* <Appbar.Header>
* <Appbar.BackAction
* onPress={this._goBack}
* />
* <Appbar.Content
* title="Title"
* subtitle="Subtitle"
* />
* <Appbar.Action icon="search" onPress={this._onSearch} />
* <Appbar.Action icon="more-vert" onPress={this._onMore} />
* </Appbar.Header>
* );
* }
* }
* ```
*/
class AppbarHeader extends React.Component<Props> {
static displayName = 'Appbar.Header';
render() {
const {
// Don't use default props since we check it to know whether we should use SafeAreaView
statusBarHeight = DEFAULT_STATUSBAR_HEIGHT,
style,
...rest
} = this.props;
const { colors } = rest.theme;
const {
height = DEFAULT_APPBAR_HEIGHT,
elevation = 4,
backgroundColor = colors.primary,
...restStyle
} = StyleSheet.flatten(style) || {};
// Let the user override the behaviour
const Wrapper =
typeof this.props.statusBarHeight === 'number' ? View : SafeAreaView;
return (
<Wrapper style={[{ backgroundColor, elevation }]}>
{/* $FlowFixMe: There seems to be conflict between Appbar's props and Header's props */}
<Appbar
style={[
{ height, backgroundColor, marginTop: statusBarHeight },
styles.appbar,
restStyle,
]}
{...rest}
/>
</Wrapper>
);
}
}
const styles = StyleSheet.create({
appbar: {
elevation: 0,
},
});
export default withTheme(AppbarHeader);