Open History to see the last commit
1const fs = require("node:fs");
2const path = require("node:path");
3const {
4 AndroidConfig,
5 withAndroidColors,
6 withAndroidColorsNight,
7 withAndroidStyles,
8 withDangerousMod,
9} = require("expo/config-plugins");
10
11const POPUP_BACKGROUND_DRAWABLE = `<?xml version="1.0" encoding="utf-8"?>
12<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
13 <solid android:color="@color/popup_menu_background" />
14 <corners android:radius="12dp" />
15</shape>
16`;
17
18const CHECK_DRAWABLE = `<?xml version="1.0" encoding="utf-8"?>
19<vector xmlns:android="http://schemas.android.com/apk/res/android"
20 android:width="18dp"
21 android:height="18dp"
22 android:viewportWidth="24"
23 android:viewportHeight="24"
24 android:tint="@color/popup_menu_item_text">
25 <path
26 android:fillColor="#FFFFFFFF"
27 android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z" />
28</vector>
29`;
30
31const CHECKBOX_BUTTON_SELECTOR = `<?xml version="1.0" encoding="utf-8"?>
32<selector xmlns:android="http://schemas.android.com/apk/res/android">
33 <item android:state_checked="true" android:drawable="@drawable/ic_menu_check" />
34 <item android:drawable="@android:color/transparent" />
35</selector>
36`;
37
38const SUBMENU_ARROW_DRAWABLE = `<?xml version="1.0" encoding="utf-8"?>
39<vector xmlns:android="http://schemas.android.com/apk/res/android"
40 android:width="20dp"
41 android:height="20dp"
42 android:viewportWidth="24"
43 android:viewportHeight="24"
44 android:alpha="0.6"
45 android:tint="@color/popup_menu_item_text">
46 <path
47 android:pathData="M9,6l6,6l-6,6"
48 android:strokeColor="#FFFFFFFF"
49 android:strokeWidth="2"
50 android:strokeLineCap="round"
51 android:strokeLineJoin="round"
52 android:fillColor="#00000000" />
53</vector>
54`;
55
56const COLORS = {
57 light: { background: "#F7F7F7", itemText: "#262626" },
58 night: { background: "#161616", itemText: "#F5F5F5" },
59};
60
61function assignStyleItem(style, name, value) {
62 style.item = style.item ?? [];
63 const existing = style.item.find((item) => item.$?.name === name);
64 if (existing) {
65 existing._ = value;
66 } else {
67 style.item.push({ _: value, $: { name } });
68 }
69}
70
71function withPopupMenuStyles(config) {
72 return withAndroidStyles(config, (config) => {
73 const resources = config.modResults.resources;
74 resources.style = resources.style ?? [];
75
76 const appTheme = resources.style.find((style) => style.$?.name === "AppTheme");
77 if (appTheme) {
78 assignStyleItem(appTheme, "popupMenuStyle", "@style/AppPopupMenu");
79 assignStyleItem(appTheme, "android:popupMenuStyle", "@style/AppPopupMenu");
80 assignStyleItem(
81 appTheme,
82 "textAppearanceLargePopupMenu",
83 "@style/AppPopupMenu.TextAppearance",
84 );
85 assignStyleItem(
86 appTheme,
87 "textAppearanceSmallPopupMenu",
88 "@style/AppPopupMenu.TextAppearance",
89 );
90 // Submenu popups show their parent item as a header row that reads a
91 // separate theme attribute, so it needs the same themed text color.
92 assignStyleItem(
93 appTheme,
94 "textAppearancePopupMenuHeader",
95 "@style/AppPopupMenu.HeaderTextAppearance",
96 );
97 assignStyleItem(appTheme, "android:listMenuViewStyle", "@style/AppPopupMenuListMenuView");
98 // Both framework and AppCompat attrs are set since the popup may inflate either CheckBox
99 // flavor.
100 assignStyleItem(appTheme, "android:checkboxStyle", "@style/AppPopupMenuCheckBox");
101 assignStyleItem(appTheme, "checkboxStyle", "@style/AppPopupMenuCheckBoxCompat");
102 }
103
104 resources.style = resources.style.filter(
105 (style) =>
106 ![
107 "AppPopupMenu",
108 "AppPopupMenu.TextAppearance",
109 "AppPopupMenu.HeaderTextAppearance",
110 "AppPopupMenuListMenuView",
111 "AppPopupMenuCheckBox",
112 "AppPopupMenuCheckBoxCompat",
113 ].includes(style.$?.name),
114 );
115 resources.style.push(
116 {
117 $: { name: "AppPopupMenu", parent: "Widget.AppCompat.PopupMenu" },
118 item: [
119 { _: "@drawable/popup_menu_background", $: { name: "android:popupBackground" } },
120 { _: "false", $: { name: "android:overlapAnchor" } },
121 { _: "4dp", $: { name: "android:dropDownVerticalOffset" } },
122 ],
123 },
124 {
125 $: { name: "AppPopupMenu.TextAppearance", parent: "TextAppearance.AppCompat.Menu" },
126 item: [
127 { _: "15sp", $: { name: "android:textSize" } },
128 { _: "@color/popup_menu_item_text", $: { name: "android:textColor" } },
129 { _: "@font/xml_dm_sans_regular", $: { name: "android:fontFamily" } },
130 ],
131 },
132 {
133 $: {
134 name: "AppPopupMenu.HeaderTextAppearance",
135 parent: "TextAppearance.AppCompat.Widget.PopupMenu.Header",
136 },
137 item: [
138 { _: "15sp", $: { name: "android:textSize" } },
139 { _: "@color/popup_menu_item_text", $: { name: "android:textColor" } },
140 { _: "@font/xml_dm_sans_regular", $: { name: "android:fontFamily" } },
141 ],
142 },
143 // The framework default (Widget.Material.ListMenuView) only carries
144 // subMenuArrow, so replacing the style wholesale is safe.
145 {
146 $: { name: "AppPopupMenuListMenuView" },
147 item: [{ _: "@drawable/popup_menu_submenu_arrow", $: { name: "android:subMenuArrow" } }],
148 },
149 {
150 $: {
151 name: "AppPopupMenuCheckBox",
152 parent: "android:Widget.Material.CompoundButton.CheckBox",
153 },
154 item: [{ _: "@drawable/popup_menu_check_button", $: { name: "android:button" } }],
155 },
156 {
157 $: {
158 name: "AppPopupMenuCheckBoxCompat",
159 parent: "Widget.AppCompat.CompoundButton.CheckBox",
160 },
161 item: [
162 { _: "@drawable/popup_menu_check_button", $: { name: "android:button" } },
163 // buttonCompat wins over android:button in AppCompatCheckBox.
164 { _: "@drawable/popup_menu_check_button", $: { name: "buttonCompat" } },
165 ],
166 },
167 );
168
169 return config;
170 });
171}
172
173function withPopupMenuColors(config) {
174 config = withAndroidColors(config, (config) => {
175 config.modResults = AndroidConfig.Colors.assignColorValue(config.modResults, {
176 name: "popup_menu_background",
177 value: COLORS.light.background,
178 });
179 config.modResults = AndroidConfig.Colors.assignColorValue(config.modResults, {
180 name: "popup_menu_item_text",
181 value: COLORS.light.itemText,
182 });
183 return config;
184 });
185 config = withAndroidColorsNight(config, (config) => {
186 config.modResults = AndroidConfig.Colors.assignColorValue(config.modResults, {
187 name: "popup_menu_background",
188 value: COLORS.night.background,
189 });
190 config.modResults = AndroidConfig.Colors.assignColorValue(config.modResults, {
191 name: "popup_menu_item_text",
192 value: COLORS.night.itemText,
193 });
194 return config;
195 });
196 return config;
197}
198
199function withPopupMenuBackgroundDrawable(config) {
200 return withDangerousMod(config, [
201 "android",
202 async (config) => {
203 const drawableDir = path.join(
204 config.modRequest.platformProjectRoot,
205 "app",
206 "src",
207 "main",
208 "res",
209 "drawable",
210 );
211 fs.mkdirSync(drawableDir, { recursive: true });
212 fs.writeFileSync(
213 path.join(drawableDir, "popup_menu_background.xml"),
214 POPUP_BACKGROUND_DRAWABLE,
215 );
216 fs.writeFileSync(path.join(drawableDir, "ic_menu_check.xml"), CHECK_DRAWABLE);
217 fs.writeFileSync(
218 path.join(drawableDir, "popup_menu_check_button.xml"),
219 CHECKBOX_BUTTON_SELECTOR,
220 );
221 fs.writeFileSync(
222 path.join(drawableDir, "popup_menu_submenu_arrow.xml"),
223 SUBMENU_ARROW_DRAWABLE,
224 );
225 return config;
226 },
227 ]);
228}
229
230module.exports = function withAndroidModernPopupMenu(config) {
231 return withPopupMenuBackgroundDrawable(withPopupMenuColors(withPopupMenuStyles(config)));
232};