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