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