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

withAndroidModernAlertDialog.cjs

166 lines5.7 KB

1const fs = require("node:fs");
2const path = require("node:path");
3const themeVariables = require("../generated-uniwind-default-theme-variables.json");
4const {
5 AndroidConfig,
6 withAndroidColors,
7 withAndroidColorsNight,
8 withAndroidStyles,
9 withDangerousMod,
10} = require("expo/config-plugins");
11​
12// Preserve AppCompat's default 16dp dialog inset.
13const DIALOG_BACKGROUND_DRAWABLE = `<?xml version="1.0" encoding="utf-8"?>
14<inset xmlns:android="http://schemas.android.com/apk/res/android"
15 android:insetLeft="16dp"
16 android:insetTop="16dp"
17 android:insetRight="16dp"
18 android:insetBottom="16dp">
19 <shape android:shape="rectangle">
20 <solid android:color="@color/alert_dialog_background" />
21 <corners android:radius="24dp" />
22 </shape>
23</inset>
24`;
25​
26const colorsFor = (appearance) => {
27 const variables = themeVariables[appearance];
28 return {
29 background: variables["--color-card"],
30 text: variables["--color-foreground"],
31 secondaryText: variables["--color-foreground-secondary"],
32 buttonText: variables["--color-primary-text"],
33 };
34};
35​
36const COLORS = { light: colorsFor("light"), night: colorsFor("dark") };
37​
38function assignStyleItem(style, name, value) {
39 style.item = style.item ?? [];
40 const existing = style.item.find((item) => item.$?.name === name);
41 if (existing) {
42 existing._ = value;
43 } else {
44 style.item.push({ _: value, $: { name } });
45 }
47​
48function withAlertDialogStyles(config) {
49 return withAndroidStyles(config, (config) => {
50 const resources = config.modResults.resources;
51 resources.style = resources.style ?? [];
52​
53 const appTheme = resources.style.find((style) => style.$?.name === "AppTheme");
54 if (appTheme) {
55 // React Native uses the AppCompat attr; platform dialogs need the framework attr too.
56 assignStyleItem(appTheme, "alertDialogTheme", "@style/AppAlertDialog");
57 assignStyleItem(appTheme, "android:alertDialogTheme", "@style/AppAlertDialog");
58 }
59​
60 resources.style = resources.style.filter(
61 (style) =>
62 !["AppAlertDialog", "AppAlertDialog.Title", "AppAlertDialog.Button"].includes(
63 style.$?.name,
64 ),
65 );
66 resources.style.push(
67 {
68 $: { name: "AppAlertDialog", parent: "ThemeOverlay.AppCompat.Dialog.Alert" },
69 item: [
70 { _: "@drawable/alert_dialog_background", $: { name: "android:windowBackground" } },
71 { _: "@color/alert_dialog_secondary_text", $: { name: "android:textColorPrimary" } },
72 { _: "@color/alert_dialog_secondary_text", $: { name: "android:textColorSecondary" } },
73 // Theme fontFamily covers text views without their own font, including the message body.
74 { _: "@font/xml_dm_sans_regular", $: { name: "android:fontFamily" } },
75 // AppCompat titles use android:windowTitleStyle; there is no unprefixed equivalent.
76 { _: "@style/AppAlertDialog.Title", $: { name: "android:windowTitleStyle" } },
77 { _: "@style/AppAlertDialog.Button", $: { name: "buttonBarPositiveButtonStyle" } },
78 { _: "@style/AppAlertDialog.Button", $: { name: "buttonBarNegativeButtonStyle" } },
79 { _: "@style/AppAlertDialog.Button", $: { name: "buttonBarNeutralButtonStyle" } },
80 ],
81 },
82 {
83 $: { name: "AppAlertDialog.Title", parent: "RtlOverlay.DialogWindowTitle.AppCompat" },
84 item: [
85 { _: "@font/dm_sans_500medium", $: { name: "android:fontFamily" } },
86 { _: "18sp", $: { name: "android:textSize" } },
87 { _: "@color/alert_dialog_text", $: { name: "android:textColor" } },
88 ],
89 },
90 {
91 $: {
92 name: "AppAlertDialog.Button",
93 parent: "Widget.AppCompat.Button.ButtonBar.AlertDialog",
94 },
95 item: [
96 // Override AppCompat's hardcoded sans-serif-medium button font.
97 { _: "@font/dm_sans_500medium", $: { name: "android:fontFamily" } },
98 { _: "@color/alert_dialog_button_text", $: { name: "android:textColor" } },
99 { _: "false", $: { name: "android:textAllCaps" } },
100 { _: "0", $: { name: "android:letterSpacing" } },
101 ],
102 },
103 );
104​
105 return config;
106 });
108​
109function assignColors(colorsResource, palette) {
110 let result = colorsResource;
111 result = AndroidConfig.Colors.assignColorValue(result, {
112 name: "alert_dialog_background",
113 value: palette.background,
114 });
115 result = AndroidConfig.Colors.assignColorValue(result, {
116 name: "alert_dialog_text",
117 value: palette.text,
118 });
119 result = AndroidConfig.Colors.assignColorValue(result, {
120 name: "alert_dialog_secondary_text",
121 value: palette.secondaryText,
122 });
123 result = AndroidConfig.Colors.assignColorValue(result, {
124 name: "alert_dialog_button_text",
125 value: palette.buttonText,
126 });
127 return result;
129​
130function withAlertDialogColors(config) {
131 config = withAndroidColors(config, (config) => {
132 config.modResults = assignColors(config.modResults, COLORS.light);
133 return config;
134 });
135 config = withAndroidColorsNight(config, (config) => {
136 config.modResults = assignColors(config.modResults, COLORS.night);
137 return config;
138 });
139 return config;
141​
142function withAlertDialogBackgroundDrawable(config) {
143 return withDangerousMod(config, [
144 "android",
145 async (config) => {
146 const drawableDir = path.join(
147 config.modRequest.platformProjectRoot,
148 "app",
149 "src",
150 "main",
151 "res",
152 "drawable",
153 );
154 fs.mkdirSync(drawableDir, { recursive: true });
155 fs.writeFileSync(
156 path.join(drawableDir, "alert_dialog_background.xml"),
157 DIALOG_BACKGROUND_DRAWABLE,
158 );
159 return config;
160 },
161 ]);
163​
164module.exports = function withAndroidModernAlertDialog(config) {
165 return withAlertDialogBackgroundDrawable(withAlertDialogColors(withAlertDialogStyles(config)));
166};