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
9 changes: 7 additions & 2 deletions src/components/Appbar/AppbarContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -51,6 +51,9 @@ export type Props = Omit<React.PropsWithoutRef<ViewProps>, '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;
/**
Expand Down Expand Up @@ -144,7 +147,9 @@ const AppbarContent = ({
numberOfLines={1}
accessible
role={onPress ? 'none' : 'heading'}
maxFontSizeMultiplier={titleMaxFontSizeMultiplier}
maxFontSizeMultiplier={
titleMaxFontSizeMultiplier ?? modeTitleMaxFontSizeMultiplier[mode]
}
>
{title}
</Text>
Expand Down
10 changes: 10 additions & 0 deletions src/components/Appbar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/components/__tests__/Appbar/Appbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Appbar mode={mode}>
<Appbar.Content title="Title" />
</Appbar>
);

expect(screen.getByText('Title')).toHaveProp(
'maxFontSizeMultiplier',
multiplier
);
})
);

it('uses titleMaxFontSizeMultiplier instead of the default limit', async () => {
await render(
<Appbar mode="large">
<Appbar.Content title="Title" titleMaxFontSizeMultiplier={3} />
</Appbar>
);

expect(screen.getByText('Title')).toHaveProp('maxFontSizeMultiplier', 3);
});

it('should render component passed to title', async () => {
await render(
<Appbar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A
>
<Text
accessible={true}
maxFontSizeMultiplier={2}
numberOfLines={1}
role="heading"
style={
Expand Down
Loading