Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ConfirmDialogHost.tsx

149 lines5.3 KB

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