Skip to content
TypeScript

fix(mobile): route alerts through the themed dialog presenter (#19997)

Julius Marminge505ccfd0e450
diff --git a/apps/mobile/assets/icons/delete.xml b/apps/mobile/assets/icons/delete.xml
new file mode 100644
index 00000000000..a8edf81d93b
--- /dev/null
+++ b/apps/mobile/assets/icons/delete.xml
@@ -0,0 +1,5 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
+ <path android:fillColor="#FFFFFFFF" android:fillType="evenOdd"
+ android:pathData="M9,3 H15 V5 H21 V7 H19 V21 H5 V7 H3 V5 H9 Z M11,5 H13 V4.75 H11 Z M7,7 V19 H17 V7 Z M9,9 H11 V17 H9 Z M13,9 H15 V17 H13 Z" />
+</vector>
diff --git a/apps/mobile/src/components/Dialog.android.tsx b/apps/mobile/src/components/Dialog.android.tsx
index fae9f174592..0d7d74fc374 100644
--- a/apps/mobile/src/components/Dialog.android.tsx
+++ b/apps/mobile/src/components/Dialog.android.tsx
@@ -1,9 +1,13 @@
import { NativeThemeHost } from "./NativeThemeHost.android";
import {
AlertDialog,
+ Button,
+ OutlinedButton,
+ Icon,
+ FlowRow,
+ Column,
OutlinedTextField,
Text,
- TextButton,
useNativeState,
} from "@expo/ui/jetpack-compose";
@@ -11,16 +15,37 @@ import { useAppearancePreferences } from "../features/settings/appearance/Appear
import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
import { useCallback, useEffect, useRef, useState } from "react";
+import { themeColorWithContrast } from "../lib/mobileTheme";
+import type { Alert, AlertButton } from "react-native";
import type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.types";
export type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.types";
+type AlertRequest = {
+ readonly title: Parameters<typeof Alert.alert>[0];
+ readonly message: Parameters<typeof Alert.alert>[1];
+ readonly buttons: Parameters<typeof Alert.alert>[2];
+ readonly options: Parameters<typeof Alert.alert>[3];
+};
+
type DialogRequest =
+ | { readonly kind: "alert"; readonly request: AlertRequest }
| { readonly kind: "confirm"; readonly request: ConfirmDialogRequest }
| { readonly kind: "text-input"; readonly request: TextInputDialogRequest };
let presentRequest: ((request: DialogRequest) => void) | null = null;
+export function alert(...[title, message, buttons, options]: Parameters<typeof Alert.alert>): void {
+ const actions = buttons?.length ? buttons.slice(0, 3) : [{ text: "OK" }];
+ const normalized = actions.map((action, index) =>
+ index === actions.length - 1 ? { ...action, text: action.text || "OK" } : action,
+ );
+ presentRequest?.({
+ kind: "alert",
+ request: { title, message, buttons: normalized, options },
+ });
+}
+
export function confirm(request: ConfirmDialogRequest): void {
presentRequest?.({ kind: "confirm", request });
}
@@ -29,7 +54,7 @@ export function prompt(request: TextInputDialogRequest): void {
presentRequest?.({ kind: "text-input", request });
}
-/** Android presenter for Dialog.confirm and Dialog.prompt; mounted once at the app root. */
+/** Android presenter for Dialog.alert, Dialog.confirm and Dialog.prompt; mounted once at the app root. */
export function Host() {
const [presented, setPresented] = useState<(DialogRequest & { readonly id: number }) | null>(
null,
@@ -48,7 +73,8 @@ export function Host() {
const handleCancel = useCallback(() => {
setPresented(null);
- presented?.request.onCancel?.();
+ if (presented?.kind === "alert") presented.request.options?.onDismiss?.();
+ else presented?.request.onCancel?.();
}, [presented]);
const handleConfirm = useCallback(
@@ -63,6 +89,27 @@ export function Host() {
[inputValue, presented],
);
+ if (presented?.kind === "alert") {
+ return (
+ <DialogPresentation
+ key={presented.id}
+ request={{
+ title: presented.request.title ?? "",
+ message: presented.request.message ?? undefined,
+ confirmText: "",
+ }}
+ alertActions={presented.request.buttons ?? [{ text: "OK" }]}
+ dismissable={presented.request.options?.cancelable ?? false}
+ onAlertAction={(action) => {
+ setPresented(null);
+ action.onPress?.();
+ }}
+ onCancel={handleCancel}
+ onConfirm={handleConfirm}
+ />
+ );
+ }
+
return presented ? (
<DialogPresentation
key={presented.id}
@@ -88,19 +135,68 @@ function DialogPresentation(props: DialogPresentationProps) {
const value = props.inputInitialValue === undefined ? undefined : inputState.get();
props.onConfirm(value);
};
+ const actions = props.alertActions ?? [
+ { text: props.request.cancelText ?? "Cancel", onPress: props.onCancel },
+ {
+ text: props.request.confirmText,
+ style: props.request.destructive ? ("destructive" as const) : ("default" as const),
+ onPress: confirm,
+ },
+ ];
+ const destructive = actions.some((action) => action.style === "destructive");
+ // Error text colors can be pastel in dark themes. Filled actions need a white-label contrast pair.
+ const destructiveFill = themeColorWithContrast(colors["--color-danger-foreground"], "#ffffff");
+ const renderAction = (action: AlertButton, index: number) => {
+ const isDestructive = action.style === "destructive";
+ const isPrimary = isDestructive || index === actions.length - 1;
+ const Component = isPrimary ? Button : OutlinedButton;
+ return (
+ <Component
+ key={index}
+ onClick={() => (props.alertActions ? props.onAlertAction?.(action) : action.onPress?.())}
+ colors={{
+ containerColor: isDestructive
+ ? destructiveFill
+ : isPrimary
+ ? colors["--color-prominent-action"]
+ : "#00000000",
+ contentColor: isDestructive
+ ? "#ffffff"
+ : isPrimary
+ ? colors["--color-prominent-action-foreground"]
+ : colors["--color-foreground"],
+ }}
+ >
+ <Text style={bodyTypography}>{action.text ?? ""}</Text>
+ </Component>
+ );
+ };
return (
<NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ height: 0, width: 0 }}>
<AlertDialog
onDismissRequest={props.onCancel}
+ properties={{
+ dismissOnBackPress: props.dismissable ?? true,
+ dismissOnClickOutside: props.dismissable ?? true,
+ }}
tonalElevation={0}
colors={{
- containerColor: colors["--color-card-alt"],
+ containerColor: colors["--color-card"],
titleContentColor: colors["--color-foreground"],
textContentColor: colors["--color-foreground-secondary"],
}}
>
<AlertDialog.Title>
- <Text style={titleTypography}>{props.request.title}</Text>
+ <Column verticalArrangement={{ spacedBy: 16 }}>
+ {destructive ? (
+ <Icon
+ source={require("../../assets/icons/delete.xml")}
+ tint={colors["--color-danger-foreground"]}
+ size={24}
+ />
+ ) : null}
+ <Text style={titleTypography}>{props.request.title}</Text>
+ </Column>
</AlertDialog.Title>
{props.inputInitialValue !== undefined ? (
<AlertDialog.Text>
@@ -133,33 +229,25 @@ function DialogPresentation(props: DialogPresentationProps) {
<Text style={bodyTypography}>{props.request.message}</Text>
</AlertDialog.Text>
) : null}
- <AlertDialog.DismissButton>
- <TextButton
- onClick={props.onCancel}
- colors={{ contentColor: colors["--color-primary-text"] }}
- >
- <Text style={bodyTypography}>{props.request.cancelText ?? "Cancel"}</Text>
- </TextButton>
- </AlertDialog.DismissButton>
- <AlertDialog.ConfirmButton>
- <TextButton
- onClick={confirm}
- colors={{
- contentColor:
- colors[
- props.request.destructive ? "--color-danger-foreground" : "--color-primary-text"
- ],
- }}
- >
- <Text style={bodyTypography}>{props.request.confirmText}</Text>
- </TextButton>
- </AlertDialog.ConfirmButton>
+ {actions.length > 1 ? (
+ <AlertDialog.DismissButton>
+ <FlowRow>{actions.slice(0, -1).map(renderAction)}</FlowRow>
+ </AlertDialog.DismissButton>
+ ) : null}
+ {actions.length > 0 ? (
+ <AlertDialog.ConfirmButton>
+ {renderAction(actions[actions.length - 1]!, actions.length - 1)}
+ </AlertDialog.ConfirmButton>
+ ) : null}
</AlertDialog>
</NativeThemeHost>
);
}
interface DialogPresentationProps {
+ readonly alertActions?: readonly AlertButton[];
+ readonly onAlertAction?: (action: AlertButton) => void;
+ readonly dismissable?: boolean;
readonly request: Pick<
ConfirmDialogRequest,
"title" | "message" | "cancelText" | "confirmText" | "destructive"
diff --git a/apps/mobile/src/components/Dialog.ts b/apps/mobile/src/components/Dialog.tsx
similarity index 85%
rename from apps/mobile/src/components/Dialog.ts
rename to apps/mobile/src/components/Dialog.tsx
index e7706322141..8096f0836c7 100644
--- a/apps/mobile/src/components/Dialog.ts
+++ b/apps/mobile/src/components/Dialog.tsx
@@ -3,6 +3,11 @@ import type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.type
export type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.types";
+/** Informational and multi-action alerts share the same platform presenter. */
+export function alert(...args: Parameters<typeof Alert.alert>): void {
+ Alert.alert(...args);
+}
+
export function confirm(request: ConfirmDialogRequest): void {
Alert.alert(request.title, request.message, [
{ text: request.cancelText ?? "Cancel", style: "cancel", onPress: request.onCancel },
diff --git a/apps/mobile/src/features/development/ComponentCatalogScreen.tsx b/apps/mobile/src/features/development/ComponentCatalogScreen.tsx
index 94e703de974..a1f2e6555d9 100644
--- a/apps/mobile/src/features/development/ComponentCatalogScreen.tsx
+++ b/apps/mobile/src/features/development/ComponentCatalogScreen.tsx
@@ -224,6 +224,20 @@ export function ComponentCatalogScreen() {
<View className="flex-row flex-wrap gap-2">
<Button label="Sheet" onPress={() => setSheet(true)} />
<Button label="Dialog" onPress={confirm} />
+ <Button
+ label="Alert"
+ onPress={() =>
+ Dialog.alert("Example alert", "Choose an action.", [
+ {
+ text: "More info",
+ onPress: () =>
+ Dialog.alert("More information", "This second alert stays open."),
+ },
+ { text: "Cancel", style: "cancel" },
+ { text: "Continue", onPress: announceAction },
+ ])
+ }
+ />
<Button
label="Prompt"
onPress={() =>