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
11// React Native's Alert renders an AppCompat AlertDialog on Android, which
12// inherits the dated framework dialog chrome (square gray panel, teal
13// all-caps buttons) from the app theme. These resources restyle it with the
14// app's uniwind tokens from global.css: --color-card panel, --color-foreground
15// text, --color-primary buttons, DM Sans type. The @font resources referenced
16// here are embedded by the expo-font plugin config in app.config.ts.
17
18// AppCompat's default dialog window background is an inset rounded rect, so
19// the replacement keeps the same 16dp inset to preserve the dialog's margins.
20const DIALOG_BACKGROUND_DRAWABLE = `<?xml version="1.0" encoding="utf-8"?>
21<inset xmlns:android="http://schemas.android.com/apk/res/android"
22 android:insetLeft="16dp"
23 android:insetTop="16dp"
24 android:insetRight="16dp"
25 android:insetBottom="16dp">
26 <shape android:shape="rectangle">
27 <solid android:color="@color/alert_dialog_background" />
28 <corners android:radius="24dp" />
29 </shape>
30</inset>
31`;
32
33const COLORS = {
34 light: {
35 background: "#FFFFFF", // --color-card
36 text: "#262626", // --color-foreground
37 secondaryText: "#525252", // --color-foreground-secondary
38 buttonText: "#262626", // --color-primary
39 },
40 night: {
41 background: "#171717",
42 text: "#F5F5F5",
43 secondaryText: "#A3A3A3",
44 buttonText: "#F5F5F5",
45 },
46};
47
48function assignStyleItem(style, name, value) {
49 style.item = style.item ?? [];
50 const existing = style.item.find((item) => item.$?.name === name);
51 if (existing) {
52 existing._ = value;
53 } else {
54 style.item.push({ _: value, $: { name } });
55 }
56}
57
58function withAlertDialogStyles(config) {
59 return withAndroidStyles(config, (config) => {
60 const resources = config.modResults.resources;
61 resources.style = resources.style ?? [];
62
63 const appTheme = resources.style.find((style) => style.$?.name === "AppTheme");
64 if (appTheme) {
65 // React Native's dialog module builds an androidx.appcompat AlertDialog,
66 // which resolves its theme from the AppCompat attr; the framework attr is
67 // set too for any native code that inflates a platform AlertDialog.
68 assignStyleItem(appTheme, "alertDialogTheme", "@style/AppAlertDialog");
69 assignStyleItem(appTheme, "android:alertDialogTheme", "@style/AppAlertDialog");
70 }
71
72 resources.style = resources.style.filter(
73 (style) =>
74 !["AppAlertDialog", "AppAlertDialog.Title", "AppAlertDialog.Button"].includes(
75 style.$?.name,
76 ),
77 );
78 resources.style.push(
79 {
80 $: { name: "AppAlertDialog", parent: "ThemeOverlay.AppCompat.Dialog.Alert" },
81 item: [
82 { _: "@drawable/alert_dialog_background", $: { name: "android:windowBackground" } },
83 // The message body resolves textColorPrimary in AppCompat's alert
84 // layout; pointing it at the secondary token dims the message
85 // relative to the title, which keeps full-strength text via the
86 // explicit color in AppAlertDialog.Title.
87 { _: "@color/alert_dialog_secondary_text", $: { name: "android:textColorPrimary" } },
88 { _: "@color/alert_dialog_secondary_text", $: { name: "android:textColorSecondary" } },
89 // Theme-level fontFamily is the lowest-priority fallback in attribute
90 // resolution, so it reaches every text view in the dialog that does
91 // not carry its own fontFamily (the message body in particular).
92 { _: "@font/xml_dm_sans_regular", $: { name: "android:fontFamily" } },
93 // AppCompat's alert title view styles itself from the framework
94 // attr (?android:attr/windowTitleStyle); there is no unprefixed
95 // AppCompat equivalent.
96 { _: "@style/AppAlertDialog.Title", $: { name: "android:windowTitleStyle" } },
97 { _: "@style/AppAlertDialog.Button", $: { name: "buttonBarPositiveButtonStyle" } },
98 { _: "@style/AppAlertDialog.Button", $: { name: "buttonBarNegativeButtonStyle" } },
99 { _: "@style/AppAlertDialog.Button", $: { name: "buttonBarNeutralButtonStyle" } },
100 ],
101 },
102 {
103 $: { name: "AppAlertDialog.Title", parent: "RtlOverlay.DialogWindowTitle.AppCompat" },
104 item: [
105 { _: "@font/dm_sans_500medium", $: { name: "android:fontFamily" } },
106 { _: "18sp", $: { name: "android:textSize" } },
107 { _: "@color/alert_dialog_text", $: { name: "android:textColor" } },
108 ],
109 },
110 {
111 $: {
112 name: "AppAlertDialog.Button",
113 parent: "Widget.AppCompat.Button.ButtonBar.AlertDialog",
114 },
115 item: [
116 // The AppCompat button appearance hardcodes sans-serif-medium, so
117 // the font must be set here rather than relying on the theme
118 // fallback.
119 { _: "@font/dm_sans_500medium", $: { name: "android:fontFamily" } },
120 { _: "@color/alert_dialog_button_text", $: { name: "android:textColor" } },
121 { _: "false", $: { name: "android:textAllCaps" } },
122 { _: "0", $: { name: "android:letterSpacing" } },
123 ],
124 },
125 );
126
127 return config;
128 });
129}
130
131function assignColors(colorsResource, palette) {
132 let result = colorsResource;
133 result = AndroidConfig.Colors.assignColorValue(result, {
134 name: "alert_dialog_background",
135 value: palette.background,
136 });
137 result = AndroidConfig.Colors.assignColorValue(result, {
138 name: "alert_dialog_text",
139 value: palette.text,
140 });
141 result = AndroidConfig.Colors.assignColorValue(result, {
142 name: "alert_dialog_secondary_text",
143 value: palette.secondaryText,
144 });
145 result = AndroidConfig.Colors.assignColorValue(result, {
146 name: "alert_dialog_button_text",
147 value: palette.buttonText,
148 });
149 return result;
150}
151
152function withAlertDialogColors(config) {
153 config = withAndroidColors(config, (config) => {
154 config.modResults = assignColors(config.modResults, COLORS.light);
155 return config;
156 });
157 config = withAndroidColorsNight(config, (config) => {
158 config.modResults = assignColors(config.modResults, COLORS.night);
159 return config;
160 });
161 return config;
162}
163
164function withAlertDialogBackgroundDrawable(config) {
165 return withDangerousMod(config, [
166 "android",
167 async (config) => {
168 const drawableDir = path.join(
169 config.modRequest.platformProjectRoot,
170 "app",
171 "src",
172 "main",
173 "res",
174 "drawable",
175 );
176 fs.mkdirSync(drawableDir, { recursive: true });
177 fs.writeFileSync(
178 path.join(drawableDir, "alert_dialog_background.xml"),
179 DIALOG_BACKGROUND_DRAWABLE,
180 );
181 return config;
182 },
183 ]);
184}
185
186module.exports = function withAndroidModernAlertDialog(config) {
187 return withAlertDialogBackgroundDrawable(withAlertDialogColors(withAlertDialogStyles(config)));
188};