Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions example/src/Examples/AppbarExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const AppbarExample = () => {
const height = 80;

const isCenterAlignedMode = appbarMode === 'center-aligned';
const isFlexibleMode =
appbarMode === 'medium-flexible' || appbarMode === 'large-flexible';

React.useLayoutEffect(() => {
navigation.setOptions({
Expand All @@ -61,7 +63,11 @@ const AppbarExample = () => {
{showLeftIcon && (
<Appbar.BackAction onPress={() => navigation.goBack()} />
)}
<Appbar.Content title="Title" onPress={() => setShowSnackbar(true)} />
<Appbar.Content
title="Title"
subtitle={showSubtitle ? 'Subtitle' : undefined}
onPress={() => setShowSnackbar(true)}
/>
{isCenterAlignedMode
? false
: showCalendarIcon && (
Expand Down Expand Up @@ -117,7 +123,11 @@ const AppbarExample = () => {
</View>
<View style={styles.row}>
<Text>Subtitle</Text>
<Switch value={showSubtitle} onValueChange={setShowSubtitle} />
<Switch
value={isFlexibleMode ? showSubtitle : false}
disabled={!isFlexibleMode}
onValueChange={setShowSubtitle}
/>
</View>
<View style={styles.row}>
<Text>Search icon</Text>
Expand Down
82 changes: 78 additions & 4 deletions src/components/Appbar/AppbarContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import type {

import { useAppbarContext, useAppbarTheme } from './AppbarContext';
import {
APPBAR_CONTROLS_HEIGHT,
APPBAR_HORIZONTAL_PADDING,
flexibleModeHeightWithSubtitle,
flexibleModeSubtitleVariant,
getAppbarColor,
isFlexibleMode,
modeTextVariant,
Expand Down Expand Up @@ -43,6 +46,19 @@ export type Props = Omit<React.PropsWithoutRef<ViewProps>, 'children'> & {
* Reference for the title.
*/
titleRef?: React.RefObject<TextRef>;
/**
* @supported Available in v6.x
*
* Text or component for the subtitle, shown below the title in the `medium-flexible` and `large-flexible` modes.
* The other modes don't show it. A subtitle raises the minimum height of the Appbar to `136` in the `medium-flexible` mode and to `152` in the `large-flexible` mode.
*/
subtitle?: React.ReactNode;
/**
* @supported Available in v6.x
*
* Style for the subtitle, if `subtitle` is a string.
*/
subtitleStyle?: StyleProp<TextStyle>;
/**
* Function to execute on press.
*/
Expand All @@ -52,13 +68,19 @@ export type Props = Omit<React.PropsWithoutRef<ViewProps>, 'children'> & {
*/
disabled?: boolean;
/**
* Custom color for the text.
* Custom color for the title and the subtitle.
*/
color?: string;
/**
* Specifies the largest possible scale a title font can reach.
*/
titleMaxFontSizeMultiplier?: number;
/**
* @supported Available in v6.x
*
* Specifies the largest possible scale a subtitle font can reach.
*/
subtitleMaxFontSizeMultiplier?: number;
style?: StyleProp<ViewStyle>;
/**
* @optional
Expand Down Expand Up @@ -86,6 +108,20 @@ export type Props = Omit<React.PropsWithoutRef<ViewProps>, 'children'> & {
*
* export default MyComponent;
* ```
*
* ### With a subtitle
* ```js
* import * as React from 'react';
* import { Appbar } from 'react-native-paper';
*
* const MyComponent = () => (
* <Appbar.Header mode="medium-flexible">
* <Appbar.Content title="Inbox" subtitle="3 unread messages" />
* </Appbar.Header>
* );
*
* export default MyComponent;
* ```
*/
const AppbarContent = ({
color: titleColor,
Expand All @@ -96,6 +132,9 @@ const AppbarContent = ({
titleStyle,
title,
titleMaxFontSizeMultiplier,
subtitle,
subtitleStyle,
subtitleMaxFontSizeMultiplier,
theme: themeOverrides,
testID,
onLayout,
Expand Down Expand Up @@ -123,9 +162,22 @@ const AppbarContent = ({
// callback changes, so the Appbar forgets the layout of a content that is gone
React.useEffect(() => () => onContentLayout?.(null), [onContentLayout]);

const titleTextColor =
getAppbarColor({ color: titleColor, isDark: appbar?.dark ?? false }) ??
colors.onSurface;
const customTextColor = getAppbarColor({
color: titleColor,
isDark: appbar?.dark ?? false,
});
const titleTextColor = customTextColor ?? colors.onSurface;

// Only the flexible modes show the subtitle. The Appbar grows with the content,
// so the content takes up the space that the subtitle adds to the minimum height.
const subtitleLayout =
isFlexibleMode(mode) && subtitle
? {
variant: flexibleModeSubtitleVariant[mode],
minHeight:
flexibleModeHeightWithSubtitle[mode] - APPBAR_CONTROLS_HEIGHT,
}
: null;

// The `medium` and `large` modes show the title below the actions, and the flexible modes
// grow with the title, so the title is positioned at the bottom of the Appbar
Expand All @@ -142,6 +194,7 @@ const AppbarContent = ({
mode === 'medium' && styles.mediumBottom,
mode === 'large' && styles.largeBottom,
isFlexible && styles.flexibleBottom,
subtitleLayout && { minHeight: subtitleLayout.minHeight },
];

const variant = modeTextVariant[mode];
Expand Down Expand Up @@ -195,6 +248,26 @@ const AppbarContent = ({
) : (
title
)}
{subtitleLayout ? (
typeof subtitle === 'string' ? (
<Text
variant={subtitleLayout.variant}
style={[
{
color: customTextColor ?? colors.onSurfaceVariant,
...fonts[subtitleLayout.variant],
},
subtitleStyle,
]}
numberOfLines={1}
maxFontSizeMultiplier={subtitleMaxFontSizeMultiplier}
>
{subtitle}
</Text>
) : (
subtitle
)
) : null}
</>
);

Expand Down Expand Up @@ -262,6 +335,7 @@ const styles = StyleSheet.create({
paddingBottom: 28,
},
flexibleBottom: {
justifyContent: 'flex-end',
paddingBottom: 12,
},
});
Expand Down
20 changes: 19 additions & 1 deletion src/components/Appbar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export const APPBAR_CONTROLS_HEIGHT = 64;
export const APPBAR_CONTROLS_OFFSET =
(APPBAR_CONTROLS_HEIGHT - APPBAR_ACTION_WIDTH) / 2;

export const isFlexibleMode = (mode: AppbarModes) =>
type AppbarFlexibleModes = 'medium-flexible' | 'large-flexible';

export const isFlexibleMode = (
mode: AppbarModes
): mode is AppbarFlexibleModes =>
mode === 'medium-flexible' || mode === 'large-flexible';

export const getAppbarBackgroundColor = (
Expand Down Expand Up @@ -79,6 +83,15 @@ export const modeAppbarHeight = {
'large-flexible': 120,
};

/**
* Minimum height of the content area of a flexible Appbar with a subtitle,
* without the safe area insets.
*/
export const flexibleModeHeightWithSubtitle = {
'medium-flexible': 136,
'large-flexible': 152,
};

export const modeTextVariant = {
small: 'titleLarge',
medium: 'headlineSmall',
Expand All @@ -88,6 +101,11 @@ export const modeTextVariant = {
'large-flexible': 'displaySmall',
} as const;

export const flexibleModeSubtitleVariant = {
'medium-flexible': 'labelLarge',
'large-flexible': 'titleMedium',
} as const;

/**
* Margins that center the title of the `center-aligned` Appbar,
* from the width of the actions on each side of it. Wider actions on one side
Expand Down
112 changes: 111 additions & 1 deletion src/components/__tests__/Appbar/Appbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import { fireEvent, render, screen, userEvent } from '../../../test-utils';
import { DarkTheme, LightTheme } from '../../../theme/schemes';
import { tokens } from '../../../theme/tokens';
import Appbar from '../../Appbar';
import { getAppbarBackgroundColor, modeTextVariant } from '../../Appbar/utils';
import {
flexibleModeSubtitleVariant,
getAppbarBackgroundColor,
modeTextVariant,
} from '../../Appbar/utils';
import Searchbar from '../../Searchbar';
import Tooltip from '../../Tooltip/Tooltip';
import Text from '../../Typography/Text';
Expand Down Expand Up @@ -523,6 +527,31 @@ describe('Appbar layout', () => {
});
});

it.each([
{ mode: 'medium-flexible', height: 136 },
{ mode: 'large-flexible', height: 152 },
] as const)(
'grows the $mode mode to $height with a subtitle',
async ({ mode, height }) => {
await render(
<Appbar mode={mode} testID="appbar">
<Appbar.Content title="Inbox" subtitle="3 unread" testID="content" />
</Appbar>
);

// The content takes up the space below the controls
const content = screen.getByTestId('content');
expect(content).toHaveStyle({ minHeight: height - 64 });

await fireEvent(content, 'layout', {
nativeEvent: {
layout: { x: 0, y: 0, width: 300, height: height - 64 },
},
});
expect(screen.getByTestId('appbar')).toHaveStyle({ minHeight: height });
}
);

it('resets the height of a flexible Appbar when the content unmounts', async () => {
const { rerender } = await render(
<Appbar mode="large-flexible" testID="appbar">
Expand Down Expand Up @@ -1016,6 +1045,87 @@ describe('AppbarContent', () => {
})
);

it.each(['medium-flexible', 'large-flexible'] as const)(
'renders the subtitle with the type style of the %s mode',
async (mode) => {
await render(
<Appbar mode={mode}>
<Appbar.Content title="Inbox" subtitle="3 unread messages" />
</Appbar>
);

const subtitle = screen.getByText('3 unread messages');
expect(subtitle).toHaveStyle({
...LightTheme.fonts[flexibleModeSubtitleVariant[mode]],
color: LightTheme.colors.onSurfaceVariant,
});
expect(subtitle).toHaveProp('numberOfLines', 1);
}
);

it.each(['small', 'medium', 'large', 'center-aligned'] as const)(
'does not render the subtitle in the %s mode',
async (mode) => {
await render(
<Appbar mode={mode}>
<Appbar.Content title="Inbox" subtitle="3 unread messages" />
</Appbar>
);

expect(screen.queryByText('3 unread messages')).not.toBeOnTheScreen();
}
);

it('renders the subtitle in the custom color of the content', async () => {
await render(
<Appbar mode="medium-flexible">
<Appbar.Content title="Inbox" subtitle="3 unread" color="red" />
</Appbar>
);

expect(screen.getByText('3 unread')).toHaveStyle({ color: 'red' });
});

it('renders the subtitle in a light color when the Appbar is dark', async () => {
await render(
<Appbar mode="large-flexible" dark>
<Appbar.Content title="Inbox" subtitle="3 unread" />
</Appbar>
);

expect(screen.getByText('3 unread')).toHaveStyle({ color: '#ffffff' });
});

it('applies subtitleStyle over the default subtitle style', async () => {
await render(
<Appbar mode="medium-flexible">
<Appbar.Content
title="Inbox"
subtitle="3 unread"
subtitleStyle={{ color: LightTheme.colors.tertiary, fontSize: 10 }}
/>
</Appbar>
);

expect(screen.getByText('3 unread')).toHaveStyle({
color: LightTheme.colors.tertiary,
fontSize: 10,
});
});

it('should render component passed to subtitle', async () => {
await render(
<Appbar mode="large-flexible">
<Appbar.Content
title="Inbox"
subtitle={<Text testID="subtitle">3 unread</Text>}
/>
</Appbar>
);

expect(screen.getByTestId('subtitle')).toBeOnTheScreen();
});

it('should render component passed to title', async () => {
await render(
<Appbar>
Expand Down
Loading