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

withAndroidModernAlertDialog.cjs

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