Skip to content

Commit 28a3ddd

Browse files
committed
refactor(menu): addressed comments, updated tokens, added submenu
1 parent 876eb8d commit 28a3ddd

11 files changed

Lines changed: 400 additions & 511 deletions

File tree

‎docs/6.x/docs/guides/migration.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ In 5.x, each of these components multiplied `roundness` by a fixed number, and t
261261

262262
| Role | Default | 5.x multiplier | Components |
263263
| ---------------- | ------- | -------------- | --------------------------------------------------- |
264-
| `extraSmall` | 4 | 1 | `Menu`, `Snackbar`, `Tooltip` |
264+
| `extraSmall` | 4 | 1 | `Snackbar`, `Tooltip` |
265265
| `small` | 8 | 2 | `Chip` |
266266
| `medium` | 12 | 3 | `Card` |
267267
| `largeIncreased` | 20 | 5 | `SegmentedButtons` |
@@ -901,6 +901,10 @@ The `overlayAccessibilityLabel` prop was renamed to `dismissAccessibilityLabel`:
901901
</Menu>
902902
```
903903

904+
`Menu` and `Menu.Item` now follow the Material Design 3 menu specs. The surface is `surfaceContainerLow` instead of `theme.colors.elevation.level2`. The `elevation` prop is unchanged but now only sets the shadow; it no longer changes the fill color. Its corners are 16dp instead of 4dp. To change the fill, override `surfaceContainerLow` in your theme. Item titles use `labelLarge` instead of `bodyLarge`, and items have 4dp corners, or 12dp when `selected`.
905+
906+
New: `Menu` takes `colorScheme` (`standard` or `vibrant`), and `Menu.Item` takes `selected`, `supportingText`, `trailingSupportingText` and `submenu`.
907+
904908
On web, a menu now closes on the Escape key when it is pressed down, instead of when it is released. Back and Escape dismiss only the topmost overlay, so a menu opened from a `Modal` or `Dialog` closes first and leaves the modal open.
905909

906910
On Android, an open menu now also keeps the back press: it calls `onDismiss`, and the screen behind no longer goes back. Previously the press reached that screen as well. Without an `onDismiss`, the menu leaves the press to whatever is underneath, so pass one to close the menu on back.
@@ -1599,11 +1603,7 @@ For `filled`, the default and toggle-on containers use `primary`. Toggle-off (`s
15991603
<Card.Title
16001604
title="Card title"
16011605
right={(props) => (
1602-
<IconButton
1603-
icon="dots-vertical"
1604-
iconSize={props.size}
1605-
onPress={onPress}
1606-
/>
1606+
<IconButton icon="dots-vertical" iconSize={props.size} onPress={onPress} />
16071607
)}
16081608
/>
16091609
```

‎example/src/Examples/MenuExample.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,39 @@ const MenuExample = () => {
192192
</Menu>
193193
</View>
194194

195+
<View style={styles.alignCenter}>
196+
<Menu
197+
visible={_getVisible('menu7')}
198+
onDismiss={() => setVisible({})}
199+
anchor={
200+
<Button mode="outlined" onPress={_toggleMenu('menu7')}>
201+
Submenu
202+
</Button>
203+
}
204+
>
205+
<Menu.Item
206+
leadingIcon="content-cut"
207+
onPress={() => {}}
208+
title="Cut"
209+
/>
210+
<Menu.Item
211+
leadingIcon="content-copy"
212+
onPress={() => {}}
213+
title="Copy"
214+
/>
215+
<Menu.Item
216+
leadingIcon="dots-horizontal"
217+
title="More actions"
218+
submenu={
219+
<>
220+
<Menu.Item onPress={() => {}} title="Archive" />
221+
<Menu.Item onPress={() => {}} title="Report" />
222+
</>
223+
}
224+
/>
225+
</Menu>
226+
</View>
227+
195228
<View style={styles.bottomMenu}>
196229
<Menu
197230
visible={_getVisible('menu4')}

‎src/components/Menu/Menu.tsx‎

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
2929
import { scheduleOnRN } from 'react-native-worklets';
3030
import useLatestCallback from 'use-latest-callback';
3131

32-
import { MenuItemPositionContext, MenuRootContext } from './context';
32+
import { MenuRootContext } from './context';
3333
import MenuItem from './MenuItem';
3434
import { MenuTokens, type MenuColorScheme } from './tokens';
3535
import { getMenuContainerBorderRadius, getMenuContainerColor } from './utils';
@@ -43,11 +43,6 @@ import Portal from '../Portal/Portal';
4343
import Surface from '../Surface';
4444
import type { SurfaceStyle } from '../Surface';
4545

46-
const isMenuItemElement = (
47-
child: React.ReactNode
48-
): child is React.ReactElement<React.ComponentProps<typeof MenuItem>> =>
49-
React.isValidElement(child) && child.type === MenuItem;
50-
5146
export type Props = {
5247
/**
5348
* Whether the Menu is currently visible.
@@ -700,39 +695,12 @@ const Menu = ({
700695

701696
const pointerEvents = visible ? 'box-none' : 'none';
702697

703-
const rootContext = React.useMemo(() => ({ colorScheme }), [colorScheme]);
704-
705-
// Positional only, like `Card.Actions`. Nothing is injected into children —
706-
// `colorScheme` comes from `MenuRootContext`, so wrappers keep working.
707-
const items = React.Children.toArray(children);
708-
const firstItemIndex = items.findIndex(isMenuItemElement);
709-
const lastItemIndex = items.reduce(
710-
(last, child, index) => (isMenuItemElement(child) ? index : last),
711-
-1
698+
const rootContext = React.useMemo(
699+
() => ({ colorScheme, MenuComponent: Menu }),
700+
[colorScheme]
712701
);
713-
const renderedChildren = items.map((child, index) => {
714-
if (!isMenuItemElement(child)) {
715-
return child;
716-
}
717702

718-
return (
719-
<MenuItemPositionContext.Provider
720-
key={child.key ?? index}
721-
value={{
722-
roundedTop: index === firstItemIndex,
723-
roundedBottom: index === lastItemIndex,
724-
}}
725-
>
726-
{child}
727-
</MenuItemPositionContext.Provider>
728-
);
729-
});
730-
731-
const surfaceBackground = getMenuContainerColor({
732-
theme,
733-
elevation,
734-
colorScheme,
735-
});
703+
const surfaceBackground = getMenuContainerColor({ theme, colorScheme });
736704

737705
return (
738706
<View
@@ -792,9 +760,9 @@ const Menu = ({
792760
<ScrollView
793761
keyboardShouldPersistTaps={keyboardShouldPersistTaps}
794762
>
795-
{renderedChildren}
763+
{children}
796764
</ScrollView>
797-
)) || <React.Fragment>{renderedChildren}</React.Fragment>}
765+
)) || <React.Fragment>{children}</React.Fragment>}
798766
</MenuRootContext.Provider>
799767
</Animated.View>
800768
</Surface>
@@ -816,7 +784,9 @@ const styles = StyleSheet.create({
816784
opacity: 0,
817785
},
818786
menuContent: {
787+
paddingHorizontal: MenuTokens.sizes.containerPaddingHorizontal,
819788
paddingVertical: MenuTokens.sizes.containerPaddingVertical,
789+
marginVertical: MenuTokens.sizes.containerMarginVertical,
820790
},
821791
fill: {
822792
height: '100%',

0 commit comments

Comments
 (0)