Open History to see the last commit
1import { useCallback, useEffect, useState } from "react";
2import { Modal, Pressable, View } from "react-native";
3
4import { useThemeColor } from "../lib/useThemeColor";
5import { cn } from "../lib/cn";
6import { AppText } from "./AppText";
7
8export type ConfirmDialogRequest = {
9 readonly title: string;
10 readonly message?: string;
11 readonly cancelText?: string;
12 readonly confirmText: string;
13 readonly destructive?: boolean;
14 readonly onConfirm: () => void;
15 readonly onCancel?: () => void;
16};
17
18let presentRequest: ((request: ConfirmDialogRequest) => void) | null = null;
19
20/**
21 * Imperative confirm dialog, Alert.alert-shaped. Native iOS alerts already
22 * match the app (and support per-button destructive red), so this is for
23 * Android, where the native dialog can only theme all confirm buttons at
24 * once. Requires ConfirmDialogHost to be mounted at the app root.
25 */
26export function showConfirmDialog(request: ConfirmDialogRequest): void {
27 presentRequest?.(request);
28}
29
30/**
31 * Android-style alert dialog matching the native one themed by
32 * withAndroidModernAlertDialog — left-aligned text, right-aligned text
33 * buttons — with what the native theme can't do: a per-dialog destructive
34 * button color and a dimmer message than the title.
35 */
36export function ConfirmDialogHost() {
37 const [request, setRequest] = useState<ConfirmDialogRequest | null>(null);
38 const pressedOverlay = useThemeColor("--color-subtle");
39
40 useEffect(() => {
41 presentRequest = setRequest;
42 return () => {
43 presentRequest = null;
44 };
45 }, []);
46
47 const handleCancel = useCallback(() => {
48 request?.onCancel?.();
49 setRequest(null);
50 }, [request]);
51
52 const handleConfirm = useCallback(() => {
53 request?.onConfirm();
54 setRequest(null);
55 }, [request]);
56
57 return (
58 <Modal
59 visible={request !== null}
60 transparent
61 animationType="fade"
62 statusBarTranslucent
63 navigationBarTranslucent
64 onRequestClose={handleCancel}
65 >
66 {request === null ? null : (
67 <View className="flex-1 items-center justify-center bg-backdrop px-8">
68 <View className="w-full rounded-[24px] bg-card px-6 pb-4 pt-5">
69 <AppText className="text-lg font-t3-medium">{request.title}</AppText>
70 {request.message === undefined ? null : (
71 <AppText className="mt-2 text-sm text-foreground-secondary">
72 {request.message}
73 </AppText>
74 )}
75 <View className="mt-5 flex-row justify-end gap-1">
76 <View className="overflow-hidden rounded-full">
77 <Pressable
78 accessibilityRole="button"
79 className="min-h-10 items-center justify-center px-4"
80 android_ripple={{ color: pressedOverlay }}
81 onPress={handleCancel}
82 >
83 <AppText className="text-base font-t3-medium">
84 {request.cancelText ?? "Cancel"}
85 </AppText>
86 </Pressable>
87 </View>
88 <View className="overflow-hidden rounded-full">
89 <Pressable
90 accessibilityRole="button"
91 className="min-h-10 items-center justify-center px-4"
92 android_ripple={{ color: pressedOverlay }}
93 onPress={handleConfirm}
94 >
95 <AppText
96 className={cn(
97 "text-base font-t3-medium",
98 request.destructive && "text-danger-foreground",
99 )}
100 >
101 {request.confirmText}
102 </AppText>
103 </Pressable>
104 </View>
105 </View>
106 </View>
107 </View>
108 )}
109 </Modal>
110 );
111}