-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathAppbarHeader.tsx
More file actions
174 lines (161 loc) · 4.48 KB
/
AppbarHeader.tsx
File metadata and controls
174 lines (161 loc) · 4.48 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
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
import * as React from 'react';
import {
Animated,
ColorValue,
Platform,
StyleProp,
StyleSheet,
View,
ViewStyle,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Appbar } from './Appbar';
import {
DEFAULT_APPBAR_HEIGHT,
getAppbarBackgroundColor,
modeAppbarHeight,
getAppbarBorders,
} from './utils';
import { useInternalTheme } from '../../core/theming';
import shadow from '../../styles/shadow';
import type { ThemeProp } from '../../types';
export type Props = Omit<
React.ComponentProps<typeof Appbar>,
'safeAreaInsets'
> & {
/**
* 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.ReactNode;
/**
* @supported Available in v5.x with theme version 3
*
* Mode of the Appbar.
* - `small` - Appbar with default height (64).
* - `medium` - Appbar with medium height (112).
* - `large` - Appbar with large height (152).
* - `center-aligned` - Appbar with default height and center-aligned title.
*/
mode?: 'small' | 'medium' | 'large' | 'center-aligned';
/**
* @supported Available in v5.x with theme version 3
* Whether Appbar background should have the elevation along with primary color pigment.
*/
elevated?: boolean;
/**
* @optional
*/
theme?: ThemeProp;
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
};
/**
* 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.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Appbar } from 'react-native-paper';
*
* const MyComponent = () => {
* const _goBack = () => console.log('Went back');
*
* const _handleSearch = () => console.log('Searching');
*
* const _handleMore = () => console.log('Shown more');
*
* return (
* <Appbar.Header>
* <Appbar.BackAction onPress={_goBack} />
* <Appbar.Content title="Title" />
* <Appbar.Action icon="magnify" onPress={_handleSearch} />
* <Appbar.Action icon="dots-vertical" onPress={_handleMore} />
* </Appbar.Header>
* );
* };
*
* export default MyComponent;
* ```
*/
const AppbarHeader = ({
// Don't use default props since we check it to know whether we should use SafeAreaView
statusBarHeight,
style,
dark,
mode = Platform.OS === 'ios' ? 'center-aligned' : 'small',
elevated = false,
theme: themeOverrides,
testID = 'appbar-header',
...rest
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const { isV3 } = theme;
const flattenedStyle = StyleSheet.flatten(style);
const {
height = isV3 ? modeAppbarHeight[mode] : DEFAULT_APPBAR_HEIGHT,
elevation = isV3 ? (elevated ? 2 : 0) : 4,
backgroundColor: customBackground,
zIndex = isV3 && elevated ? 1 : 0,
...restStyle
} = (flattenedStyle || {}) as Exclude<typeof flattenedStyle, number> & {
height?: number;
elevation?: number;
backgroundColor?: ColorValue;
zIndex?: number;
};
const borderRadius = getAppbarBorders(restStyle);
const backgroundColor = getAppbarBackgroundColor(
theme,
elevation,
customBackground,
elevated
);
const { top, left, right } = useSafeAreaInsets();
return (
<View
testID={`${testID}-root-layer`}
style={[
{
backgroundColor,
zIndex,
elevation,
paddingTop: statusBarHeight ?? top,
paddingHorizontal: Math.max(left, right),
},
borderRadius,
shadow(elevation) as ViewStyle,
]}
>
<Appbar
testID={testID}
style={[{ height, backgroundColor }, styles.appbar, restStyle]}
dark={dark}
{...(isV3 && {
mode,
})}
{...rest}
theme={theme}
/>
</View>
);
};
AppbarHeader.displayName = 'Appbar.Header';
const styles = StyleSheet.create({
appbar: {
elevation: 0,
},
});
export default AppbarHeader;
// @component-docs ignore-next-line
export { AppbarHeader };