Open History to see the last commit
1import { NativeThemeHost } from "./NativeThemeHost.android";
2import {
3 Box,
4 Column,
5 DropdownMenu,
6 DropdownMenuItem,
7 RNHostView,
8 Text,
9} from "@expo/ui/jetpack-compose";
10import { defaultMinSize, padding, size, width } from "@expo/ui/jetpack-compose/modifiers";
11import type { MenuAction } from "@react-native-menu/menu";
12import { Pressable } from "react-native";
13import { resolveScaledTextRole } from "../../lib/appearancePreferences";
14
15import { useAndroidControlSizing } from "./useAndroidControlSizing";
16import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
17import type { MenuPopupProps } from "./MenuPopup";
18import { isAppSymbolName, SymbolView, type AppSymbolName } from "./AppSymbol";
19
20// The Compose glyph host consumes touches; run the action here and keep accessibility on the row.
21function MenuIcon(props: {
22 readonly name: AppSymbolName;
23 readonly color?: MenuAction["imageColor"];
24 readonly onPress: () => void;
25 readonly destructive?: boolean;
26 readonly disabled?: boolean;
27}) {
28 const { iconSize } = useAndroidControlSizing();
29 return (
30 <RNHostView matchContents modifiers={[size(iconSize, iconSize)]}>
31 <Pressable
32 accessible={false}
33 focusable={false}
34 disabled={props.disabled}
35 onPress={props.onPress}
36 style={{ width: iconSize, height: iconSize }}
37 importantForAccessibility="no-hide-descendants"
38 >
39 <SymbolView
40 name={props.name}
41 size={iconSize}
42 type="monochrome"
43 {...(typeof props.color === "string" && !props.disabled
44 ? { tintColor: props.color }
45 : {
46 tintColorClassName: props.disabled
47 ? "accent-icon-subtle"
48 : props.destructive
49 ? "accent-danger-foreground"
50 : "accent-foreground",
51 })}
52 />
53 </Pressable>
54 </RNHostView>
55 );
56}
57
58export function MenuPopup(props: MenuPopupProps) {
59 const { appearance, themeVariables: colors } = useAppearancePreferences();
60 const { scale, menuItemHeight } = useAndroidControlSizing();
61 const body = resolveScaledTextRole("body", appearance.baseFontSize);
62 const caption = resolveScaledTextRole("caption", appearance.baseFontSize);
63 const foreground = colors["--color-foreground"];
64 const muted = colors["--color-foreground-muted"];
65 // A fixed native item height clips wrapped labels; a minimum lets each row grow.
66 const itemModifiers = [width(props.menuWidth), defaultMinSize({ minHeight: menuItemHeight })];
67 const items = (
68 <>
69 {props.parent ? (
70 <DropdownMenuItem onClick={props.onBack} modifiers={itemModifiers}>
71 <DropdownMenuItem.LeadingIcon>
72 <MenuIcon name="arrow.left" onPress={props.onBack} />
73 </DropdownMenuItem.LeadingIcon>
74 <DropdownMenuItem.Text>
75 <Text color={foreground} style={{ typography: "bodyLarge", ...body }}>
76 {props.parent.title}
77 </Text>
78 </DropdownMenuItem.Text>
79 </DropdownMenuItem>
80 ) : props.title ? (
81 <Text
82 color={muted}
83 style={{ typography: "bodySmall", ...caption }}
84 modifiers={[padding(16 * scale, 8 * scale, 16 * scale, 8 * scale)]}
85 >
86 {props.title}
87 </Text>
88 ) : null}
89 {props.actions.map((action, index) => (
90 <DropdownMenuItem
91 key={action.id ?? `${index}-${action.title}`}
92 enabled={!action.attributes?.disabled}
93 modifiers={itemModifiers}
94 onClick={() => props.onPress(action)}
95 >
96 {action.image && isAppSymbolName(action.image) ? (
97 <DropdownMenuItem.LeadingIcon>
98 <MenuIcon
99 name={action.image}
100 color={action.imageColor}
101 onPress={() => props.onPress(action)}
102 destructive={action.attributes?.destructive}
103 disabled={action.attributes?.disabled}
104 />
105 </DropdownMenuItem.LeadingIcon>
106 ) : null}
107 <DropdownMenuItem.Text>
108 <Column>
109 <Text
110 style={{ typography: "bodyLarge", ...body }}
111 color={
112 action.attributes?.disabled
113 ? muted
114 : action.attributes?.destructive
115 ? colors["--color-danger-foreground"]
116 : foreground
117 }
118 >
119 {action.title}
120 </Text>
121 {action.subtitle ? (
122 <Text color={muted} style={{ typography: "bodySmall", ...caption }}>
123 {action.subtitle}
124 </Text>
125 ) : null}
126 </Column>
127 </DropdownMenuItem.Text>
128 {(action.subactions?.length ?? 0) > 0 ? (
129 <DropdownMenuItem.TrailingIcon>
130 <MenuIcon
131 name="chevron.right"
132 onPress={() => props.onPress(action)}
133 disabled={action.attributes?.disabled}
134 />
135 </DropdownMenuItem.TrailingIcon>
136 ) : action.state === "on" ? (
137 <DropdownMenuItem.TrailingIcon>
138 <MenuIcon
139 name="checkmark"
140 onPress={() => props.onPress(action)}
141 disabled={action.attributes?.disabled}
142 />
143 </DropdownMenuItem.TrailingIcon>
144 ) : null}
145 </DropdownMenuItem>
146 ))}
147 </>
148 );
149 if (props.inline) {
150 return (
151 <NativeThemeHost
152 ignoreSafeAreaKeyboardInsets
153 matchContents
154 style={{ width: props.menuWidth }}
155 >
156 <Column>{items}</Column>
157 </NativeThemeHost>
158 );
159 }
160 return (
161 <NativeThemeHost
162 ignoreSafeAreaKeyboardInsets
163 style={{
164 position: "absolute",
165 left: props.anchor.x,
166 top: props.anchor.y,
167 width: props.anchor.width,
168 height: props.anchor.height,
169 }}
170 >
171 <DropdownMenu expanded onDismissRequest={props.onClose} color={colors["--color-card-alt"]}>
172 <DropdownMenu.Trigger>
173 <Box modifiers={[size(props.anchor.width, props.anchor.height)]} />
174 </DropdownMenu.Trigger>
175 <DropdownMenu.Items>{items}</DropdownMenu.Items>
176 </DropdownMenu>
177 </NativeThemeHost>
178 );
179}