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
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
103 changes: 61 additions & 42 deletions src/menu/CanvasNodeMenu.android.tsx
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -45,6 +56,7 @@
}, [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;
Expand All @@ -59,43 +71,50 @@
{left: frame.x - 2, top: frame.y - 2, width: frame.width + 4, height: frame.height + 4, borderRadius: radius + 2, borderColor: colors.primary},
]}
/>
<Host style={{position: 'absolute', left: anchor.x, top: anchor.y, width: anchor.width, height: anchor.height}}>

Check warning on line 74 in src/menu/CanvasNodeMenu.android.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

Inline style: { position: 'absolute' }
<DropdownMenu expanded onDismissRequest={dismiss}>
<DropdownMenu.Trigger>
<Box modifiers={[fillMaxSize()]} />
</DropdownMenu.Trigger>
<DropdownMenu.Items>
{title ? (
<DropdownMenuItem enabled={false} elementColors={{disabledTextColor: colors.onSurfaceVariant}}>
<DropdownMenuItem.Text>
<Text style={{typography: 'labelLarge'}} maxLines={1} overflow="ellipsis">
{title}
</Text>
</DropdownMenuItem.Text>
</DropdownMenuItem>
) : null}
{title ? <HorizontalDivider /> : null}
{items.map(item => (
<DropdownMenuItem
key={item.key}
elementColors={item.destructive ? {textColor: colors.error, leadingIconColor: colors.error} : undefined}
onClick={() => {
dismiss();
onSelect(item.key);
}}
>
{item.icon ? (
<DropdownMenuItem.LeadingIcon>
<Icon source={item.icon} size={ICON_SIZE} />
</DropdownMenuItem.LeadingIcon>
) : null}
<DropdownMenuItem.Text>
<Text>{item.title}</Text>
</DropdownMenuItem.Text>
</DropdownMenuItem>
<Box modifiers={[fillMaxSize()]}>
<DropdownMenuPopup expanded onDismissRequest={dismiss}>
{groups.map((group, g) => (
<React.Fragment key={g}>
{g > 0 ? <Spacer modifiers={[height(GROUP_SPACING)]} /> : null}
<DropdownMenuGroup shapes={{index: g, count: groups.length}}>
{g === 0 && title ? (
// Material's group label (MenuDefaults.DropdownMenuGroupLabel): labelLarge, 12dp in.
<Text
style={{typography: 'labelLarge'}}

Check warning on line 84 in src/menu/CanvasNodeMenu.android.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

Inline style: { typography: 'labelLarge' }
color={colors.onSurfaceVariant}
maxLines={1}
overflow="ellipsis"
modifiers={[defaultMinSize({minWidth: LABEL_MIN_WIDTH, minHeight: LABEL_MIN_HEIGHT}), padding(12, 10, 12, 6)]}
>
{title}
</Text>
) : null}
{group.map((item, i) => (
<DropdownMenuItem
key={item.key}
shape={{index: i, count: group.length}}
elementColors={item.destructive ? {textColor: colors.error, leadingIconColor: colors.error} : undefined}
onClick={() => {
dismiss();
onSelect(item.key);
}}
>
{item.icon ? (
<DropdownMenuItem.LeadingIcon>
<Icon source={item.icon} size={ICON_SIZE} />
</DropdownMenuItem.LeadingIcon>
) : null}
<DropdownMenuItem.Text>
<Text>{item.title}</Text>
</DropdownMenuItem.Text>
</DropdownMenuItem>
))}
</DropdownMenuGroup>
</React.Fragment>
))}
</DropdownMenu.Items>
</DropdownMenu>
</DropdownMenuPopup>
</Box>
</Host>
</>
);
Expand Down
18 changes: 18 additions & 0 deletions src/menu/__tests__/menuGroups.test.ts
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
12 changes: 12 additions & 0 deletions src/menu/menuGroups.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading