From aae17f8c8a8ce4954bcf6ab532ba66989f5e7c64 Mon Sep 17 00:00:00 2001 From: Priyanshu0007 Date: Fri, 20 Nov 2026 22:38:44 +0530 Subject: [PATCH] feat(appbar): add a subtitle to the flexible modes `Appbar.Content` takes `subtitle`, `subtitleStyle` and `subtitleMaxFontSizeMultiplier`. The `medium-flexible` and `large-flexible` modes show the subtitle below the title, in `labelLarge` and `titleMedium` with the `onSurfaceVariant` color, and raise their minimum height to 136 and 152, following the Material 3 Expressive spec. The other modes don't show it. --- example/src/Examples/AppbarExample.tsx | 14 ++- src/components/Appbar/AppbarContent.tsx | 82 ++++++++++++- src/components/Appbar/utils.ts | 20 +++- .../__tests__/Appbar/Appbar.test.tsx | 112 +++++++++++++++++- 4 files changed, 220 insertions(+), 8 deletions(-) diff --git a/example/src/Examples/AppbarExample.tsx b/example/src/Examples/AppbarExample.tsx index 18097295fa..4bd8f5224c 100644 --- a/example/src/Examples/AppbarExample.tsx +++ b/example/src/Examples/AppbarExample.tsx @@ -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({ @@ -61,7 +63,11 @@ const AppbarExample = () => { {showLeftIcon && ( navigation.goBack()} /> )} - setShowSnackbar(true)} /> + setShowSnackbar(true)} + /> {isCenterAlignedMode ? false : showCalendarIcon && ( @@ -117,7 +123,11 @@ const AppbarExample = () => { Subtitle - + Search icon diff --git a/src/components/Appbar/AppbarContent.tsx b/src/components/Appbar/AppbarContent.tsx index 92fae42e7d..39df93669a 100644 --- a/src/components/Appbar/AppbarContent.tsx +++ b/src/components/Appbar/AppbarContent.tsx @@ -11,7 +11,10 @@ import type { import { useAppbarContext, useAppbarTheme } from './AppbarContext'; import { + APPBAR_CONTROLS_HEIGHT, APPBAR_HORIZONTAL_PADDING, + flexibleModeHeightWithSubtitle, + flexibleModeSubtitleVariant, getAppbarColor, isFlexibleMode, modeTextVariant, @@ -43,6 +46,19 @@ export type Props = Omit, 'children'> & { * Reference for the title. */ titleRef?: React.RefObject; + /** + * @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; /** * Function to execute on press. */ @@ -52,13 +68,19 @@ export type Props = Omit, '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; /** * @optional @@ -86,6 +108,20 @@ export type Props = Omit, 'children'> & { * * export default MyComponent; * ``` + * + * ### With a subtitle + * ```js + * import * as React from 'react'; + * import { Appbar } from 'react-native-paper'; + * + * const MyComponent = () => ( + * + * + * + * ); + * + * export default MyComponent; + * ``` */ const AppbarContent = ({ color: titleColor, @@ -96,6 +132,9 @@ const AppbarContent = ({ titleStyle, title, titleMaxFontSizeMultiplier, + subtitle, + subtitleStyle, + subtitleMaxFontSizeMultiplier, theme: themeOverrides, testID, onLayout, @@ -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 @@ -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]; @@ -195,6 +248,26 @@ const AppbarContent = ({ ) : ( title )} + {subtitleLayout ? ( + typeof subtitle === 'string' ? ( + + {subtitle} + + ) : ( + subtitle + ) + ) : null} ); @@ -262,6 +335,7 @@ const styles = StyleSheet.create({ paddingBottom: 28, }, flexibleBottom: { + justifyContent: 'flex-end', paddingBottom: 12, }, }); diff --git a/src/components/Appbar/utils.ts b/src/components/Appbar/utils.ts index aa6a31ea6d..7173d3b7a3 100644 --- a/src/components/Appbar/utils.ts +++ b/src/components/Appbar/utils.ts @@ -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 = ( @@ -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', @@ -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 diff --git a/src/components/__tests__/Appbar/Appbar.test.tsx b/src/components/__tests__/Appbar/Appbar.test.tsx index 28c927d95a..1657ce7287 100644 --- a/src/components/__tests__/Appbar/Appbar.test.tsx +++ b/src/components/__tests__/Appbar/Appbar.test.tsx @@ -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'; @@ -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( + + + + ); + + // 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( @@ -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( + + + + ); + + 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( + + + + ); + + expect(screen.queryByText('3 unread messages')).not.toBeOnTheScreen(); + } + ); + + it('renders the subtitle in the custom color of the content', async () => { + await render( + + + + ); + + expect(screen.getByText('3 unread')).toHaveStyle({ color: 'red' }); + }); + + it('renders the subtitle in a light color when the Appbar is dark', async () => { + await render( + + + + ); + + expect(screen.getByText('3 unread')).toHaveStyle({ color: '#ffffff' }); + }); + + it('applies subtitleStyle over the default subtitle style', async () => { + await render( + + + + ); + + expect(screen.getByText('3 unread')).toHaveStyle({ + color: LightTheme.colors.tertiary, + fontSize: 10, + }); + }); + + it('should render component passed to subtitle', async () => { + await render( + + 3 unread} + /> + + ); + + expect(screen.getByTestId('subtitle')).toBeOnTheScreen(); + }); + it('should render component passed to title', async () => { await render(