diff --git a/src/components/Appbar/Appbar.tsx b/src/components/Appbar/Appbar.tsx index 85a67eb980..f8cfc077e3 100644 --- a/src/components/Appbar/Appbar.tsx +++ b/src/components/Appbar/Appbar.tsx @@ -14,6 +14,7 @@ import { APPBAR_CONTROLS_HEIGHT, APPBAR_CONTROLS_OFFSET, APPBAR_HORIZONTAL_PADDING, + APPBAR_TITLE_TOP, getAppbarBackgroundColor, getCenteringMargins, isFlexibleMode, @@ -45,8 +46,8 @@ export type Props = Omit, 'style'> & * * Mode of the Appbar. * - `small` - Appbar with default height (64). - * - `medium` - Appbar with medium height (112). - * - `large` - Appbar with large height (152). + * - `medium` - Appbar with medium height (112). It grows when the title would reach the actions, e.g. at larger text sizes. + * - `large` - Appbar with large height (152). It grows when the title would reach the actions, e.g. at larger text sizes. * - `center-aligned` - Appbar with default height and center-aligned title. * - `medium-flexible` - Available in v6.x. Material 3 Expressive Appbar with a larger title and a minimum height of 112. It grows when the title wraps onto a second line. * - `large-flexible` - Available in v6.x. Material 3 Expressive Appbar with a prominent title and a minimum height of 120. It grows when the title wraps onto a second line. @@ -306,15 +307,24 @@ const Appbar = ({ // The safe area insets are applied as padding, so they need to be included in the height const verticalInsets = insets.top + insets.bottom; + const contentHeight = content?.layout.height ?? 0; const heightStyle = isFlexible ? { minHeight: Math.max( modeAppbarHeight[mode], - APPBAR_CONTROLS_HEIGHT + (content?.layout.height ?? 0) + APPBAR_CONTROLS_HEIGHT + contentHeight ) + verticalInsets, } - : { height: modeAppbarHeight[mode] + verticalInsets }; + : { + // The title of the `medium` and `large` modes grows upward from the bottom of the bar, + // so the bar grows with a title that would otherwise reach the actions, + // e.g. at larger text sizes + height: + (isTopAligned + ? Math.max(modeAppbarHeight[mode], APPBAR_TITLE_TOP + contentHeight) + : modeAppbarHeight[mode]) + verticalInsets, + }; return ( diff --git a/src/components/Appbar/AppbarHeader.tsx b/src/components/Appbar/AppbarHeader.tsx index 2675abba1d..74af3f31d8 100644 --- a/src/components/Appbar/AppbarHeader.tsx +++ b/src/components/Appbar/AppbarHeader.tsx @@ -31,8 +31,8 @@ export type Props = Omit & { * * Mode of the Appbar. * - `small` - Appbar with default height (64). - * - `medium` - Appbar with medium height (112). - * - `large` - Appbar with large height (152). + * - `medium` - Appbar with medium height (112). It grows when the title would reach the actions, e.g. at larger text sizes. + * - `large` - Appbar with large height (152). It grows when the title would reach the actions, e.g. at larger text sizes. * - `center-aligned` - Appbar with default height and center-aligned title. * - `medium-flexible` - Available in v6.x. Material 3 Expressive Appbar with a larger title and a minimum height of 112. It grows when the title wraps onto a second line. * - `large-flexible` - Available in v6.x. Material 3 Expressive Appbar with a prominent title and a minimum height of 120. It grows when the title wraps onto a second line. diff --git a/src/components/Appbar/utils.ts b/src/components/Appbar/utils.ts index aa6a31ea6d..8b3ac43636 100644 --- a/src/components/Appbar/utils.ts +++ b/src/components/Appbar/utils.ts @@ -3,6 +3,7 @@ import type { ColorValue } from 'react-native'; import { white } from '../../theme/colors'; import type { InternalTheme } from '../../theme/types'; +import { MIN_INTERACTIVE_SIZE } from '../../utils/getMinInteractiveSizeHitSlop'; export type AppbarModes = | 'small' @@ -25,6 +26,12 @@ export const APPBAR_CONTROLS_HEIGHT = 64; export const APPBAR_CONTROLS_OFFSET = (APPBAR_CONTROLS_HEIGHT - APPBAR_ACTION_WIDTH) / 2; +// Top of the title in the `medium` and `large` modes: the bottom of the touch +// targets of the actions, which are centered in the row with the controls. +// A title that reaches higher would take the presses of the actions. +export const APPBAR_TITLE_TOP = + (APPBAR_CONTROLS_HEIGHT + MIN_INTERACTIVE_SIZE) / 2; + export const isFlexibleMode = (mode: AppbarModes) => mode === 'medium-flexible' || mode === 'large-flexible'; diff --git a/src/components/__tests__/Appbar/Appbar.test.tsx b/src/components/__tests__/Appbar/Appbar.test.tsx index 28c927d95a..41b36aa794 100644 --- a/src/components/__tests__/Appbar/Appbar.test.tsx +++ b/src/components/__tests__/Appbar/Appbar.test.tsx @@ -272,6 +272,49 @@ describe('Appbar modes', () => { } ); + it.each([ + { mode: 'medium', height: 112, contentHeight: 56 }, + { mode: 'large', height: 152, contentHeight: 64 }, + ] as const)( + 'keeps the $mode height while the title stays below the actions', + async ({ mode, height, contentHeight }) => { + await render( + + + + ); + + // the default title with its padding + await fireEvent(screen.getByTestId('content'), 'layout', { + nativeEvent: { layout: { width: 300, height: contentHeight } }, + }); + + expect(screen.getByTestId('appbar')).toHaveStyle({ height: height + 20 }); + } + ); + + it.each(['medium', 'large'] as const)( + 'grows the %s Appbar so the title does not cover the actions', + async (mode) => { + await render( + + + + + ); + + // a title at a larger text size + await fireEvent(screen.getByTestId('content'), 'layout', { + nativeEvent: { layout: { width: 300, height: 120 } }, + }); + + // the bottom of the touch targets of the actions, the content and the safe area inset + expect(screen.getByTestId('appbar')).toHaveStyle({ + height: 56 + 120 + 20, + }); + } + ); + it('allows the title of a flexible Appbar to wrap onto two lines', async () => { await render( diff --git a/src/utils/getMinInteractiveSizeHitSlop.ts b/src/utils/getMinInteractiveSizeHitSlop.ts index cb6a017432..b66de259a8 100644 --- a/src/utils/getMinInteractiveSizeHitSlop.ts +++ b/src/utils/getMinInteractiveSizeHitSlop.ts @@ -4,7 +4,7 @@ import type { Insets } from 'react-native'; * Minimum size of an interactive target. * @see https://m3.material.io/foundations/designing/structure */ -const MIN_INTERACTIVE_SIZE = 48; +export const MIN_INTERACTIVE_SIZE = 48; /** * Hit slop needed to bring a fixed-size element up to the 48dp minimum