-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathAppbar.tsx
More file actions
378 lines (358 loc) · 10 KB
/
Appbar.tsx
File metadata and controls
378 lines (358 loc) · 10 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import * as React from 'react';
import {
Animated,
Platform,
StyleProp,
StyleSheet,
View,
ViewStyle,
ColorValue,
} from 'react-native';
import color from 'color';
import { useInternalTheme } from '@/core/theming';
import type { MD3Elevation, ThemeProp } from '@/types';
import AppbarContent from './AppbarContent';
import {
AppbarModes,
DEFAULT_APPBAR_HEIGHT,
getAppbarBackgroundColor,
modeAppbarHeight,
renderAppbarContent,
filterAppbarActions,
AppbarChildProps,
} from './utils';
import Surface from '../Surface';
export type Props = Omit<
Partial<React.ComponentPropsWithRef<typeof View>>,
'style'
> & {
/**
* Whether the background color is a dark color. A dark appbar will render light text and vice-versa.
*/
dark?: boolean;
/**
* Content of the `Appbar`.
*/
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;
/**
* Safe area insets for the Appbar. This can be used to avoid elements like the navigation bar on Android and bottom safe area on iOS.
*/
safeAreaInsets?: {
bottom?: number;
top?: number;
left?: number;
right?: number;
};
/**
* @optional
*/
theme?: ThemeProp;
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
};
/**
* A component to display action items in a bar. It can be placed at the top or bottom.
* The top bar usually contains the screen title, controls such as navigation buttons, menu button etc.
* The bottom bar usually provides access to a drawer and up to four actions.
*
* By default Appbar uses primary color as a background, in dark theme with `adaptive` mode it will use surface colour instead.
* See [Dark Theme](https://callstack.github.io/react-native-paper/docs/guides/theming#dark-theme) for more informations
*
* ## Usage
* ### Top bar
* ```js
* import * as React from 'react';
* import { Appbar } from 'react-native-paper';
*
* const MyComponent = () => (
* <Appbar.Header>
* <Appbar.BackAction onPress={() => {}} />
* <Appbar.Content title="Title" />
* <Appbar.Action icon="calendar" onPress={() => {}} />
* <Appbar.Action icon="magnify" onPress={() => {}} />
* </Appbar.Header>
* );
*
* export default MyComponent;
* ```
*
* ### Bottom bar
* ```js
* import * as React from 'react';
* import { StyleSheet } from 'react-native';
* import { Appbar, FAB, useTheme } from 'react-native-paper';
* import { useSafeAreaInsets } from 'react-native-safe-area-context';
*
* const BOTTOM_APPBAR_HEIGHT = 80;
* const MEDIUM_FAB_HEIGHT = 56;
*
* const MyComponent = () => {
* const { bottom } = useSafeAreaInsets();
* const theme = useTheme();
*
* return (
* <Appbar
* style={[
* styles.bottom,
* {
* height: BOTTOM_APPBAR_HEIGHT + bottom,
* backgroundColor: theme.colors.elevation.level2,
* },
* ]}
* safeAreaInsets={{ bottom }}
* >
* <Appbar.Action icon="archive" onPress={() => {}} />
* <Appbar.Action icon="email" onPress={() => {}} />
* <Appbar.Action icon="label" onPress={() => {}} />
* <Appbar.Action icon="delete" onPress={() => {}} />
* <FAB
* mode="flat"
* size="medium"
* icon="plus"
* onPress={() => {}}
* style={[
* styles.fab,
* { top: (BOTTOM_APPBAR_HEIGHT - MEDIUM_FAB_HEIGHT) / 2 },
* ]}
* />
* </Appbar>
* );
* };
*
* const styles = StyleSheet.create({
* bottom: {
* backgroundColor: 'aquamarine',
* position: 'absolute',
* left: 0,
* right: 0,
* bottom: 0,
* },
* fab: {
* position: 'absolute',
* right: 16,
* },
* });
*
* export default MyComponent;
* ```
*/
const Appbar = ({
children,
dark,
style,
mode = 'small',
elevated,
safeAreaInsets,
theme: themeOverrides,
...rest
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const { isV3 } = theme;
const flattenedStyle = StyleSheet.flatten(style);
const {
backgroundColor: customBackground,
elevation = isV3 ? (elevated ? 2 : 0) : 4,
...restStyle
} = (flattenedStyle || {}) as Exclude<typeof flattenedStyle, number> & {
elevation?: number;
backgroundColor?: ColorValue;
};
const backgroundColor = getAppbarBackgroundColor(
theme,
elevation,
customBackground,
elevated
);
const isMode = (modeToCompare: AppbarModes) => {
return isV3 && mode === modeToCompare;
};
let isDark = false;
if (typeof dark === 'boolean') {
isDark = dark;
} else if (!isV3) {
isDark =
backgroundColor === 'transparent'
? false
: typeof backgroundColor === 'string'
? !color(backgroundColor).isLight()
: true;
}
const isV3CenterAlignedMode = isV3 && isMode('center-aligned');
let shouldCenterContent = false;
let shouldAddLeftSpacing = false;
let shouldAddRightSpacing = false;
if ((!isV3 && Platform.OS === 'ios') || isV3CenterAlignedMode) {
let hasAppbarContent = false;
let leftItemsCount = 0;
let rightItemsCount = 0;
React.Children.forEach(children, (child) => {
if (React.isValidElement<AppbarChildProps>(child)) {
const isLeading = child.props.isLeading === true;
if (child.type === AppbarContent) {
hasAppbarContent = true;
} else if (isLeading || !hasAppbarContent) {
leftItemsCount++;
} else {
rightItemsCount++;
}
}
});
shouldCenterContent =
hasAppbarContent &&
leftItemsCount < 2 &&
rightItemsCount < (isV3 ? 3 : 2);
shouldAddLeftSpacing = shouldCenterContent && leftItemsCount === 0;
shouldAddRightSpacing = shouldCenterContent && rightItemsCount === 0;
}
const spacingStyle = isV3 ? styles.v3Spacing : styles.spacing;
const insets = {
paddingBottom: safeAreaInsets?.bottom,
paddingTop: safeAreaInsets?.top,
paddingLeft: safeAreaInsets?.left,
paddingRight: safeAreaInsets?.right,
};
return (
<Surface
style={[
{ backgroundColor },
styles.appbar,
{
height: isV3 ? modeAppbarHeight[mode] : DEFAULT_APPBAR_HEIGHT,
},
insets,
restStyle,
!theme.isV3 && { elevation },
]}
elevation={elevation as MD3Elevation}
container
{...rest}
>
{shouldAddLeftSpacing ? <View style={spacingStyle} /> : null}
{(!isV3 || isMode('small') || isMode('center-aligned')) && (
<>
{/* Render only the back action at first place */}
{renderAppbarContent({
children,
isDark,
theme,
isV3,
renderOnly: ['Appbar.BackAction'],
shouldCenterContent: isV3CenterAlignedMode || shouldCenterContent,
})}
{/* Render the rest of the content except the back action */}
{renderAppbarContent({
// Filter appbar actions - first leading icons, then trailing icons
children: [
...filterAppbarActions(children, true),
...filterAppbarActions(children),
],
isDark,
theme,
isV3,
renderExcept: ['Appbar.BackAction'],
shouldCenterContent: isV3CenterAlignedMode || shouldCenterContent,
})}
</>
)}
{(isMode('medium') || isMode('large')) && (
<View
style={[
styles.columnContainer,
isMode('center-aligned') && styles.centerAlignedContainer,
]}
>
{/* Appbar top row with controls */}
<View style={styles.controlsRow}>
{/* Left side of row container, can contain AppbarBackAction or AppbarAction if it's leading icon */}
{renderAppbarContent({
children,
isDark,
isV3,
renderOnly: ['Appbar.BackAction'],
mode,
})}
{renderAppbarContent({
children: filterAppbarActions(children, true),
isDark,
isV3,
renderOnly: ['Appbar.Action'],
mode,
})}
{/* Right side of row container, can contain other AppbarAction if they are not leading icons */}
<View style={styles.rightActionControls}>
{renderAppbarContent({
children: filterAppbarActions(children),
isDark,
isV3,
renderExcept: [
'Appbar',
'Appbar.BackAction',
'Appbar.Content',
'Appbar.Header',
],
mode,
})}
</View>
</View>
{renderAppbarContent({
children,
isDark,
isV3,
renderOnly: ['Appbar.Content'],
mode,
})}
</View>
)}
{shouldAddRightSpacing ? <View style={spacingStyle} /> : null}
</Surface>
);
};
const styles = StyleSheet.create({
appbar: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 4,
},
spacing: {
width: 48,
},
v3Spacing: {
width: 52,
},
controlsRow: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
rightActionControls: {
flexDirection: 'row',
flex: 1,
justifyContent: 'flex-end',
},
columnContainer: {
flexDirection: 'column',
flex: 1,
paddingTop: 8,
},
centerAlignedContainer: {
paddingTop: 0,
},
});
export default Appbar;
// @component-docs ignore-next-line
export { Appbar };