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
18 changes: 14 additions & 4 deletions src/components/Appbar/Appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
APPBAR_CONTROLS_HEIGHT,
APPBAR_CONTROLS_OFFSET,
APPBAR_HORIZONTAL_PADDING,
APPBAR_TITLE_TOP,
getAppbarBackgroundColor,
getCenteringMargins,
isFlexibleMode,
Expand Down Expand Up @@ -45,8 +46,8 @@ export type Props = Omit<Partial<ViewProps>, '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.
Expand Down Expand Up @@ -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 (
<AppbarContext.Provider value={appbarContext}>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Appbar/AppbarHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export type Props = Omit<AppbarProps, 'safeAreaInsets' | '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.
Expand Down
7 changes: 7 additions & 0 deletions src/components/Appbar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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';

Expand Down
43 changes: 43 additions & 0 deletions src/components/__tests__/Appbar/Appbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Appbar mode={mode} safeAreaInsets={{ top: 20 }} testID="appbar">
<Appbar.Content title="Title" testID="content" />
</Appbar>
);

// 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(
<Appbar mode={mode} safeAreaInsets={{ top: 20 }} testID="appbar">
<Appbar.BackAction />
<Appbar.Content title="Title" testID="content" />
</Appbar>
);

// 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(
<Appbar mode="large-flexible">
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getMinInteractiveSizeHitSlop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading