diff --git a/package-lock.json b/package-lock.json index 307a046..50d1969 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "unified": "^9.2.2" }, "devDependencies": { + "@expo/ui": "~58.0.27", "@react-native/eslint-config": "^0.81.2", "@react-native/typescript-config": "^0.81.2", "@types/jest": "^29.5.14", @@ -41,7 +42,7 @@ "node": ">=20" }, "peerDependencies": { - "@expo/ui": ">=58.0.0", + "@expo/ui": ">=58.0.26", "@gtkx/cairo": ">=1.4.0", "@gtkx/react": ">=1.4.0", "react": ">=19.0.0", diff --git a/package.json b/package.json index 68306a7..107d3e5 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "node": ">=20" }, "peerDependencies": { - "@expo/ui": ">=58.0.0", + "@expo/ui": ">=58.0.26", "@gtkx/cairo": ">=1.4.0", "@gtkx/react": ">=1.4.0", "react": ">=19.0.0", @@ -109,6 +109,7 @@ "unified": "^9.2.2" }, "devDependencies": { + "@expo/ui": "~58.0.27", "@react-native/eslint-config": "^0.81.2", "@react-native/typescript-config": "^0.81.2", "@types/jest": "^29.5.14", diff --git a/src/menu/CanvasNodeMenu.android.tsx b/src/menu/CanvasNodeMenu.android.tsx index 067c67c..7e7db7f 100644 --- a/src/menu/CanvasNodeMenu.android.tsx +++ b/src/menu/CanvasNodeMenu.android.tsx @@ -1,28 +1,39 @@ -import {useCallback, useEffect, useRef, type ReactElement} from 'react'; +import React, {useCallback, useEffect, useRef, type ReactElement} from 'react'; import {StyleSheet, View} from 'react-native'; import { Box, - DropdownMenu, + DropdownMenuGroup, DropdownMenuItem, + DropdownMenuPopup, Host, - HorizontalDivider, Icon, + Spacer, Text, useMaterialColors, } from '@expo/ui/jetpack-compose'; -import {fillMaxSize} from '@expo/ui/jetpack-compose/modifiers'; +import {defaultMinSize, fillMaxSize, height, padding} from '@expo/ui/jetpack-compose/modifiers'; import {menuAnchor} from './menuAnchor'; +import {menuGroups} from './menuGroups'; import type {CanvasNodeMenuProps} from './types'; /** A card's corners on the canvas, in its own points (SkiaCardRenderer's). */ const CARD_RADIUS = 8; const GROUP_RADIUS = 12; const ICON_SIZE = 24; +/** A menu item's least width and a group label's least height, in dp (Material's). */ +const LABEL_MIN_WIDTH = 112; +const LABEL_MIN_HEIGHT = 32; + +/** Material's gap between a menu's groups, in dp (MenuDefaults.GroupSpacing). */ +const GROUP_SPACING = 2; /** - * A card's menu on Android: Material 3's dropdown menu (Compose's, through - * Expo UI) anchored to the long-pressed card, opening below it, or above - * with no room below, and lined up with its leading edge. The card is + * A card's menu on Android: Material 3 Expressive's menu (Compose's + * DropdownMenuPopup, through Expo UI) anchored to the long-pressed card, + * opening below it, or above with no room below, and lined up with its + * leading edge. Its actions are a rounded group, the destructive ones a + * group of their own after them, each item rounded in its place; the title, + * when there is one, is the first group's label. The card is * outlined in the theme's primary colour while its menu is open, so it's * clear which card the menu is for (Android has no lifted preview, as iOS's * context menu has). A tap outside the menu, or Back, closes it. @@ -45,6 +56,7 @@ export function CanvasNodeMenu({node, press, items, title, onSelect, onDismiss}: }, [onDismiss]); if (items.length === 0) return null; + const groups = menuGroups(items); const anchor = menuAnchor(press); const {frame} = press; const zoom = node.width > 0 ? frame.width / node.width : 1; @@ -60,42 +72,49 @@ export function CanvasNodeMenu({node, press, items, title, onSelect, onDismiss}: ]} /> - - - - - - {title ? ( - - - - {title} - - - - ) : null} - {title ? : null} - {items.map(item => ( - { - dismiss(); - onSelect(item.key); - }} - > - {item.icon ? ( - - - - ) : null} - - {item.title} - - + + + {groups.map((group, g) => ( + + {g > 0 ? : null} + + {g === 0 && title ? ( + // Material's group label (MenuDefaults.DropdownMenuGroupLabel): labelLarge, 12dp in. + + {title} + + ) : null} + {group.map((item, i) => ( + { + dismiss(); + onSelect(item.key); + }} + > + {item.icon ? ( + + + + ) : null} + + {item.title} + + + ))} + + ))} - - + + ); diff --git a/src/menu/__tests__/menuGroups.test.ts b/src/menu/__tests__/menuGroups.test.ts new file mode 100644 index 0000000..00c7133 --- /dev/null +++ b/src/menu/__tests__/menuGroups.test.ts @@ -0,0 +1,18 @@ +import {menuGroups} from '../menuGroups'; + +const item = (key: string, destructive?: boolean) => ({key, title: key, destructive}); + +describe('menuGroups', () => { + it('keeps actions in one group, in order', () => { + expect(menuGroups([item('open'), item('copy'), item('share')])).toEqual([[item('open'), item('copy'), item('share')]]); + }); + + it('puts destructive actions in a group of their own, after the rest', () => { + expect(menuGroups([item('delete', true), item('copy'), item('share')])).toEqual([[item('copy'), item('share')], [item('delete', true)]]); + }); + + it('makes no empty group', () => { + expect(menuGroups([item('delete', true)])).toEqual([[item('delete', true)]]); + expect(menuGroups([])).toEqual([]); + }); +}); diff --git a/src/menu/menuGroups.ts b/src/menu/menuGroups.ts new file mode 100644 index 0000000..932c819 --- /dev/null +++ b/src/menu/menuGroups.ts @@ -0,0 +1,12 @@ +import type {CanvasNodeMenuItem} from './types'; + +/** + * A menu's items in Material 3 Expressive's groups: the actions in the order + * given, then the destructive ones in a group of their own, as Material + * separates an action that removes something. No empty groups. + */ +export function menuGroups(items: CanvasNodeMenuItem[]): CanvasNodeMenuItem[][] { + const plain = items.filter(item => !item.destructive); + const destructive = items.filter(item => item.destructive); + return [plain, destructive].filter(group => group.length > 0); +}