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