Open History to see the last commit
1import { useCallback, useEffect, useState } from "react";
2import { Platform, Modal, Pressable, TextInput, View } from "react-native";
3
4import { cn } from "../lib/cn";
5import { AppText } from "./AppText";
6import { MaterialConfirmDialog } from "./MaterialConfirmDialog";
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
18export type TextInputDialogRequest = {
19 readonly title: string;
20 readonly initialValue: string;
21 readonly cancelText?: string;
22 readonly confirmText: string;
23 readonly onConfirm: (value: string) => void;
24 readonly onCancel?: () => void;
25};
26
27type DialogRequest =
28 | { readonly kind: "confirm"; readonly request: ConfirmDialogRequest }
29 | { readonly kind: "text-input"; readonly request: TextInputDialogRequest };
30
31let presentRequest: ((request: DialogRequest) => void) | null = null;
32
33/**
34 * Imperative confirm dialog, Alert.alert-shaped. Native iOS alerts already
35 * match the app (and support per-button destructive red), so this is for
36 * Android, where the native dialog can only theme all confirm buttons at
37 * once. Requires ConfirmDialogHost to be mounted at the app root.
38 */
39export function showConfirmDialog(request: ConfirmDialogRequest): void {
40 presentRequest?.({ kind: "confirm", request });
41}
42
43export function showTextInputDialog(request: TextInputDialogRequest): void {
44 presentRequest?.({ kind: "text-input", request });
45}
46
47/**
48 * Android-style alert dialog matching the native one themed by
49 * withAndroidModernAlertDialog — left-aligned text, right-aligned text
50 * buttons — with what the native theme can't do: a per-dialog destructive
51 * button color and a dimmer message than the title.
52 */
53export function ConfirmDialogHost() {
54 const [presented, setPresented] = useState<DialogRequest | null>(null);
55 const [inputValue, setInputValue] = useState("");
56 useEffect(() => {
57 presentRequest = (request) => {
58 setInputValue(request.kind === "text-input" ? request.request.initialValue : "");
59 setPresented(request);
60 };
61 return () => {
62 presentRequest = null;
63 };
64 }, []);
65
66 const handleCancel = useCallback(() => {
67 presented?.request.onCancel?.();
68 setPresented(null);
69 }, [presented]);
70
71 const handleConfirm = useCallback(
72 (nativeInputValue?: string) => {
73 if (presented?.kind === "confirm") {
74 presented.request.onConfirm();
75 } else if (presented?.kind === "text-input") {
76 presented.request.onConfirm(nativeInputValue ?? inputValue);
77 }
78 setPresented(null);
79 },
80 [inputValue, presented],
81 );
82
83 const confirmDisabled = presented?.kind === "text-input" && inputValue.trim().length === 0;
84
85 if (Platform.OS === "android")
86 return presented ? (
87 <MaterialConfirmDialog
88 key={`${presented.kind}:${presented.request.title}:${presented.kind === "text-input" ? presented.request.initialValue : ""}`}
89 request={presented.request}
90 inputInitialValue={
91 presented.kind === "text-input" ? presented.request.initialValue : undefined
92 }
93 onInputChange={setInputValue}
94 confirmDisabled={confirmDisabled}
95 onCancel={handleCancel}
96 onConfirm={handleConfirm}
97 />
98 ) : null;
99
100 return (
101 <Modal
102 visible={presented !== null}
103 transparent
104 animationType="fade"
105 statusBarTranslucent
106 navigationBarTranslucent
107 onRequestClose={handleCancel}
108 >
109 {presented === null ? null : (
110 <View className="flex-1 items-center justify-center bg-backdrop px-8">
111 <View className="w-full rounded-[24px] bg-card px-6 pb-4 pt-5">
112 <AppText className="text-lg font-t3-medium">{presented.request.title}</AppText>
113 {presented.kind === "confirm" && presented.request.message !== undefined ? (
114 <AppText className="mt-2 text-sm text-foreground-secondary">
115 {presented.request.message}
116 </AppText>
117 ) : null}
118 {presented.kind === "text-input" ? (
119 <TextInput
120 accessibilityLabel={presented.request.title}
121 autoFocus
122 className="mt-4 rounded-xl border border-border bg-screen px-3 py-2.5 text-base text-foreground"
123 onChangeText={setInputValue}
124 onSubmitEditing={confirmDisabled ? undefined : () => handleConfirm()}
125 returnKeyType="done"
126 selectTextOnFocus
127 value={inputValue}
128 />
129 ) : null}
130 <View className="mt-5 flex-row justify-end gap-1">
131 <View className="overflow-hidden rounded-full">
132 <Pressable
133 accessibilityRole="button"
134 className="min-h-10 items-center justify-center px-4 active:bg-subtle"
135 onPress={handleCancel}
136 >
137 <AppText className="text-base font-t3-medium">
138 {presented.request.cancelText ?? "Cancel"}
139 </AppText>
140 </Pressable>
141 </View>
142 <View className="overflow-hidden rounded-full">
143 <Pressable
144 accessibilityRole="button"
145 disabled={confirmDisabled}
146 className="min-h-10 items-center justify-center px-4 active:bg-subtle"
147 onPress={() => handleConfirm()}
148 >
149 <AppText
150 className={cn(
151 "text-base font-t3-medium",
152 presented.kind === "confirm" &&
153 presented.request.destructive &&
154 "text-danger-foreground",
155 confirmDisabled && "text-foreground-tertiary",
156 )}
157 >
158 {presented.request.confirmText}
159 </AppText>
160 </Pressable>
161 </View>
162 </View>
163 </View>
164 </View>
165 )}
166 </Modal>
167 );
168}