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 Surface from '../Surface'; import AppbarAction from './AppbarAction'; import AppbarBackAction from './AppbarBackAction'; import AppbarContent from './AppbarContent'; import AppbarHeader from './AppbarHeader'; import { AppbarModes, DEFAULT_APPBAR_HEIGHT, getAppbarColor, modeAppbarHeight, renderAppbarContent, } from './utils'; export type Props = Omit< Partial>, '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>; }; /** * 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 = () => ( * * {}} /> * * {}} /> * {}} /> * * ); * * 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 ( * * {}} /> * {}} /> * {}} /> * {}} /> * {}} * style={[ * styles.fab, * { top: (BOTTOM_APPBAR_HEIGHT - MEDIUM_FAB_HEIGHT) / 2 }, * ]} * /> * * ); * }; * * 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 & { elevation?: number; backgroundColor?: ColorValue; }; let isDark: boolean; const backgroundColor = getAppbarColor( theme, elevation, customBackground, elevated ); const isMode = (modeToCompare: AppbarModes) => { return isV3 && mode === modeToCompare; }; if (typeof dark === 'boolean') { isDark = dark; } else { 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(child)) { if (child.type === AppbarContent) { hasAppbarContent = true; } else if (hasAppbarContent) { rightItemsCount++; } else { leftItemsCount++; } } }); shouldCenterContent = hasAppbarContent && leftItemsCount < 2 && rightItemsCount < (isV3 ? 3 : 2); shouldAddLeftSpacing = shouldCenterContent && leftItemsCount === 0; shouldAddRightSpacing = shouldCenterContent && rightItemsCount === 0; } const filterAppbarActions = React.useCallback( (isLeading = false) => React.Children.toArray(children).filter((child) => // @ts-expect-error: TypeScript complains about the type of type but it doesn't matter isLeading ? child.props.isLeading : !child.props.isLeading ), [children] ); const spacingStyle = isV3 ? styles.v3Spacing : styles.spacing; const insets = { paddingBottom: safeAreaInsets?.bottom, paddingTop: safeAreaInsets?.top, paddingLeft: safeAreaInsets?.left, paddingRight: safeAreaInsets?.right, }; return ( {shouldAddLeftSpacing ? : null} {(!isV3 || isMode('small') || isMode('center-aligned')) && renderAppbarContent({ children, isDark, theme, isV3, shouldCenterContent: isV3CenterAlignedMode || shouldCenterContent, })} {(isMode('medium') || isMode('large')) && ( {/* Appbar top row with controls */} {/* Left side of row container, can contain AppbarBackAction or AppbarAction if it's leading icon */} {renderAppbarContent({ children, isDark, isV3, renderOnly: [AppbarBackAction], mode, })} {renderAppbarContent({ children: filterAppbarActions(true), isDark, isV3, renderOnly: [AppbarAction], mode, })} {/* Right side of row container, can contain other AppbarAction if they are not leading icons */} {renderAppbarContent({ children: filterAppbarActions(false), isDark, isV3, renderExcept: [ Appbar, AppbarBackAction, AppbarContent, AppbarHeader, ], mode, })} {/* Middle of the row, can contain only AppbarContent */} {renderAppbarContent({ children, isDark, isV3, renderOnly: [AppbarContent], mode, })} )} {shouldAddRightSpacing ? : null} ); }; 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 };