Skip to content

Commit

Permalink
fix: add support for changing the surface mode in the menu (#4378)
Browse files Browse the repository at this point in the history
* fix: add support for changing the surface mode in the menu

* test: adding tests for new mode prop
  • Loading branch information
lenzi-e authored Jul 27, 2024
1 parent eed911b commit eebc77f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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');
});

0 comments on commit eebc77f

Please sign in to comment.