diff --git a/src/components/Appbar/AppbarContent.tsx b/src/components/Appbar/AppbarContent.tsx index a7ed41f2d9..cb7f90442e 100644 --- a/src/components/Appbar/AppbarContent.tsx +++ b/src/components/Appbar/AppbarContent.tsx @@ -8,7 +8,7 @@ import type { ViewProps, } from 'react-native'; -import { modeTextVariant } from './utils'; +import { modeTextVariant, modeTitleMaxFontSizeMultiplier } from './utils'; import { useInternalTheme } from '../../core/theming'; import type { ThemeProp } from '../../theme/types'; import { mergeDataSetProps, useFocusRing } from '../../utils/useFocusRing'; @@ -51,6 +51,9 @@ export type Props = Omit, 'children'> & { color?: string; /** * Specifies the largest possible scale a title font can reach. + * Defaults to the largest scale at which the title fits in the appbar: + * `2` in `small` and `center-aligned` mode, `1.25` in `medium` mode + * and `1.1` in `large` mode. Pass `0` to remove the limit. */ titleMaxFontSizeMultiplier?: number; /** @@ -144,7 +147,9 @@ const AppbarContent = ({ numberOfLines={1} accessible role={onPress ? 'none' : 'heading'} - maxFontSizeMultiplier={titleMaxFontSizeMultiplier} + maxFontSizeMultiplier={ + titleMaxFontSizeMultiplier ?? modeTitleMaxFontSizeMultiplier[mode] + } > {title} diff --git a/src/components/Appbar/utils.ts b/src/components/Appbar/utils.ts index f1c4437a2f..3470115052 100644 --- a/src/components/Appbar/utils.ts +++ b/src/components/Appbar/utils.ts @@ -75,6 +75,16 @@ export const modeTextVariant = { 'center-aligned': 'titleLarge', } as const; +// The appbar has a fixed height, so the title can only scale until its line +// height fills the space left for it: 64 in small mode, 40 below the actions +// row in medium and large mode. Past that, the bar clips the title. +export const modeTitleMaxFontSizeMultiplier = { + small: 2, + medium: 1.25, + large: 1.1, + 'center-aligned': 2, +}; + export const filterAppbarActions = ( children: React.ReactNode, isLeading = false diff --git a/src/components/__tests__/Appbar/Appbar.test.tsx b/src/components/__tests__/Appbar/Appbar.test.tsx index 49f14e6ec1..24b072df32 100644 --- a/src/components/__tests__/Appbar/Appbar.test.tsx +++ b/src/components/__tests__/Appbar/Appbar.test.tsx @@ -312,6 +312,38 @@ describe('AppbarContent', () => { }) ); + ( + [ + ['small', 2], + ['medium', 1.25], + ['large', 1.1], + ['center-aligned', 2], + ] as const + ).forEach(([mode, multiplier]) => + it(`limits the title font scale so it fits the appbar in ${mode} mode`, async () => { + await render( + + + + ); + + expect(screen.getByText('Title')).toHaveProp( + 'maxFontSizeMultiplier', + multiplier + ); + }) + ); + + it('uses titleMaxFontSizeMultiplier instead of the default limit', async () => { + await render( + + + + ); + + expect(screen.getByText('Title')).toHaveProp('maxFontSizeMultiplier', 3); + }); + it('should render component passed to title', async () => { await render( diff --git a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap index 3280897b93..4fe0546b68 100644 --- a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap +++ b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap @@ -741,6 +741,7 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A >