import { NativeThemeHost } from "./NativeThemeHost.android"; import { AlertDialog, Button, OutlinedButton, Icon, FlowRow, Column, OutlinedTextField, Text, useNativeState, } from "@expo/ui/jetpack-compose"; import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider"; import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole"; import { useSyncExternalStore } 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[0]; readonly message: Parameters[1]; readonly buttons: Parameters[2]; readonly options: Parameters[3]; }; type DialogRequest = | { readonly kind: "alert"; readonly request: AlertRequest } | { readonly kind: "confirm"; readonly request: ConfirmDialogRequest } | { readonly kind: "text-input"; readonly request: TextInputDialogRequest }; type PresentedDialog = DialogRequest & { readonly id: number }; // App services raise dialogs before the root mounts, so the request lives in // the module and the Host renders whatever is current. One dialog shows at a // time: a new request replaces the presented one. let presented: PresentedDialog | null = null; let nextPresentationId = 0; const listeners = new Set<() => void>(); function setPresented(next: PresentedDialog | null) { presented = next; for (const listener of listeners) listener(); } function cancelPresented(dialog: PresentedDialog) { if (dialog.kind === "alert") dialog.request.options?.onDismiss?.(); else dialog.request.onCancel?.(); } function submitRequest(dialog: DialogRequest) { const previous = presented; setPresented({ ...dialog, id: ++nextPresentationId }); // A newer dialog replaces the current one without a button press; cancel the // previous so awaiters (settle confirms, busy flags) do not hang. if (previous !== null) { queueMicrotask(() => { if (presented?.id === previous.id) return; cancelPresented(previous); }); } } /** Closes the dialog only if it is still the one the action came from. */ function dismiss(id: number) { if (presented?.id === id) setPresented(null); } function subscribe(listener: () => void) { listeners.add(listener); return () => { listeners.delete(listener); }; } function getPresented() { return presented; } export function alert(...[title, message, buttons, options]: Parameters): 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, ); submitRequest({ kind: "alert", request: { title, message, buttons: normalized, options }, }); } export function confirm(request: ConfirmDialogRequest): void { submitRequest({ kind: "confirm", request }); } export function prompt(request: TextInputDialogRequest): void { submitRequest({ kind: "text-input", request }); } export function Host() { const presented = useSyncExternalStore(subscribe, getPresented, getPresented); const handleCancel = () => { if (!presented) return; dismiss(presented.id); if (presented.kind === "alert") presented.request.options?.onDismiss?.(); else presented.request.onCancel?.(); }; const handleConfirm = (nativeInputValue?: string) => { if (!presented) return; dismiss(presented.id); if (presented.kind === "confirm") { presented.request.onConfirm(); } else if (presented.kind === "text-input") { presented.request.onConfirm(nativeInputValue ?? presented.request.initialValue); } }; if (presented?.kind === "alert") { return ( { dismiss(presented.id); action.onPress?.(); }} onCancel={handleCancel} onConfirm={handleConfirm} /> ); } return presented ? ( ) : null; } function DialogPresentation(props: DialogPresentationProps) { const { themeVariables: colors } = useAppearancePreferences(); const titleTypography = useScaledTextRole("title"); const bodyTypography = useScaledTextRole("footnote"); const inputTypography = useScaledTextRole("body"); const inputState = useNativeState(props.inputInitialValue ?? ""); const inputSelection = useNativeState({ start: 0, end: props.inputInitialValue?.length ?? 0 }); const confirm = () => { 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 ( (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"], }} > {action.text ?? ""} ); }; return ( {destructive ? ( ) : null} {props.request.title} {props.inputInitialValue !== undefined ? ( {props.request.message ? ( {props.request.message} ) : null} {props.request.title} {props.inputPlaceholder ? ( {props.inputPlaceholder} ) : null} ) : props.request.message ? ( {props.request.message} ) : null} {actions.length > 1 ? ( {actions.slice(0, -1).map(renderAction)} ) : null} {actions.length > 0 ? ( {renderAction(actions[actions.length - 1]!, actions.length - 1)} ) : null} ); } interface DialogPresentationProps { readonly alertActions?: readonly AlertButton[]; readonly onAlertAction?: (action: AlertButton) => void; readonly dismissable?: boolean; readonly request: Pick< ConfirmDialogRequest, "title" | "message" | "cancelText" | "confirmText" | "destructive" >; readonly inputInitialValue?: string; readonly inputPlaceholder?: string; readonly onCancel: () => void; readonly onConfirm: (inputValue?: string) => void; }