Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for changing the surface mode in the menu #4378

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export type Props = {
* @supported Available in v5.x with theme version 3
*/
elevation?: MD3Elevation;
/**
* Mode of the menu's content.
* - `elevated` - Surface with a shadow and background color corresponding to set `elevation` value.
* - `flat` - Surface without a shadow, with the background color corresponding to set `elevation` value.
*
* @supported Available in v5.x with theme version 3
*/
mode?: 'flat' | 'elevated';
/**
* @optional
*/
Expand Down Expand Up @@ -114,6 +122,8 @@ export const ELEVATION_LEVELS_MAP = Object.values(
ElevationLevels
) as ElevationLevels[];

const DEFAULT_MODE = 'elevated';

/**
* Menus display a list of choices on temporary elevated surfaces. Their placement varies based on the element that opens them.
*
Expand Down Expand Up @@ -418,6 +428,7 @@ class Menu extends React.Component<Props, State> {
contentStyle,
style,
elevation = DEFAULT_ELEVATION,
mode = DEFAULT_MODE,
children,
theme,
statusBarHeight,
Expand Down Expand Up @@ -646,6 +657,7 @@ class Menu extends React.Component<Props, State> {
}}
>
<Surface
mode={mode}
pointerEvents={pointerEvents}
style={[
styles.shadowMenuContainer,
Expand Down
48 changes: 48 additions & 0 deletions src/components/__tests__/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,51 @@ it('animated value changes correctly', () => {
transform: [{ scale: 1.5 }],
});
});

it('renders menu with mode "elevated"', () => {
const { getByTestId } = render(
<Portal.Host>
<Menu
visible
onDismiss={jest.fn()}
anchor={<Button mode="outlined">Open menu</Button>}
mode="elevated"
>
<Menu.Item onPress={jest.fn()} title="Undo" />
<Menu.Item onPress={jest.fn()} title="Redo" />
</Menu>
</Portal.Host>
);

const menuSurface = getByTestId('menu-surface');

// Get flattened styles
const styles = StyleSheet.flatten(menuSurface.props.style);

expect(styles).toHaveProperty('shadowColor');
expect(styles).toHaveProperty('shadowOpacity');
});

it('renders menu with mode "flat"', () => {
const { getByTestId } = render(
<Portal.Host>
<Menu
visible
onDismiss={jest.fn()}
anchor={<Button mode="outlined">Open menu</Button>}
mode="flat"
>
<Menu.Item onPress={jest.fn()} title="Undo" />
<Menu.Item onPress={jest.fn()} title="Redo" />
</Menu>
</Portal.Host>
);

const menuSurface = getByTestId('menu-surface');

// Get flattened styles
const styles = StyleSheet.flatten(menuSurface.props.style);

expect(styles).not.toHaveProperty('shadowColor');
expect(styles).not.toHaveProperty('shadowOpacity');
});