Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MaterialMenuPopup.android.tsx

153 lines5.0 KB

1import {
2 Box,
3 Column,
4 DropdownMenu,
5 DropdownMenuItem,
6 Host,
7 RNHostView,
8 Text,
9} from "@expo/ui/jetpack-compose";
10import { padding, size, width } from "@expo/ui/jetpack-compose/modifiers";
11import { View } from "react-native";
12​
13import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
14import type { MaterialMenuPopupProps } from "./MaterialMenuPopup";
15import { isAppSymbolName, SymbolView, type AppSymbolName } from "./AppSymbol";
16​
17function MenuIcon(props: {
18 readonly name: AppSymbolName;
19 readonly destructive?: boolean;
20 readonly disabled?: boolean;
21}) {
22 return (
23 <RNHostView matchContents modifiers={[size(24, 24)]}>
24 <View style={{ width: 24, height: 24 }} importantForAccessibility="no-hide-descendants">
25 <SymbolView
26 name={props.name}
27 size={24}
28 type="monochrome"
29 tintColorClassName={
30 props.disabled
31 ? "accent-icon-subtle"
32 : props.destructive
33 ? "accent-danger-foreground"
34 : "accent-foreground"
35 }
36 />
37 </View>
38 </RNHostView>
39 );
41​
42/** Native popup positioned at the original trigger, outside virtualized rows. */
43export function MaterialMenuPopup(props: MaterialMenuPopupProps) {
44 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
45 const foreground = colors["--color-foreground"];
46 const muted = colors["--color-foreground-muted"];
47 const items = (
48 <>
49 {props.parent ? (
50 <DropdownMenuItem onClick={props.onBack} modifiers={[width(250)]}>
51 <DropdownMenuItem.LeadingIcon>
52 <MenuIcon name="arrow.left" />
53 </DropdownMenuItem.LeadingIcon>
54 <DropdownMenuItem.Text>
55 <Text color={foreground} style={{ typography: "bodyLarge" }}>
56 {props.parent.title}
57 </Text>
58 </DropdownMenuItem.Text>
59 </DropdownMenuItem>
60 ) : props.title ? (
61 <Text color={muted} style={{ typography: "bodySmall" }} modifiers={[padding(16, 8, 16, 8)]}>
62 {props.title}
63 </Text>
64 ) : null}
65 {props.actions.map((action, index) => (
66 <DropdownMenuItem
67 key={action.id ?? `${index}-${action.title}`}
68 enabled={!action.attributes?.disabled}
69 modifiers={[width(250)]}
70 elementColors={{
71 textColor: action.attributes?.destructive
72 ? colors["--color-danger-foreground"]
73 : foreground,
74 disabledTextColor: muted,
75 }}
76 onClick={() => props.onPress(action)}
77 >
78 <DropdownMenuItem.Text>
79 <Column>
80 <Text
81 style={{ typography: "bodyLarge" }}
82 color={
83 action.attributes?.disabled
84 ? muted
85 : action.attributes?.destructive
86 ? colors["--color-danger-foreground"]
87 : foreground
88 }
89 >
90 {action.title}
91 </Text>
92 {action.subtitle ? (
93 <Text color={muted} style={{ typography: "bodySmall" }}>
94 {action.subtitle}
95 </Text>
96 ) : null}
97 </Column>
98 </DropdownMenuItem.Text>
99 {action.image && isAppSymbolName(action.image) ? (
100 <DropdownMenuItem.LeadingIcon>
101 <MenuIcon
102 name={action.image}
103 destructive={action.attributes?.destructive}
104 disabled={action.attributes?.disabled}
105 />
106 </DropdownMenuItem.LeadingIcon>
107 ) : null}
108 {(action.subactions?.length ?? 0) > 0 ? (
109 <DropdownMenuItem.TrailingIcon>
110 <MenuIcon name="chevron.right" disabled={action.attributes?.disabled} />
111 </DropdownMenuItem.TrailingIcon>
112 ) : action.state === "on" ? (
113 <DropdownMenuItem.TrailingIcon>
114 <MenuIcon name="checkmark" disabled={action.attributes?.disabled} />
115 </DropdownMenuItem.TrailingIcon>
116 ) : null}
117 </DropdownMenuItem>
118 ))}
119 </>
120 );
121 if (props.inline) {
122 return (
123 <Host
124 colorScheme={themeAppearance}
125 ignoreSafeAreaKeyboardInsets
126 matchContents
127 style={{ width: 250 }}
128 >
129 <Column>{items}</Column>
130 </Host>
131 );
132 }
133 return (
134 <Host
135 colorScheme={themeAppearance}
136 ignoreSafeAreaKeyboardInsets
137 style={{
138 position: "absolute",
139 left: props.anchor.x,
140 top: props.anchor.y,
141 width: props.anchor.width,
142 height: props.anchor.height,
143 }}
144 >
145 <DropdownMenu expanded onDismissRequest={props.onClose} color={colors["--color-card-alt"]}>
146 <DropdownMenu.Trigger>
147 <Box modifiers={[size(props.anchor.width, props.anchor.height)]} />
148 </DropdownMenu.Trigger>
149 <DropdownMenu.Items>{items}</DropdownMenu.Items>
150 </DropdownMenu>
151 </Host>
152 );