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

ConfirmDialogHost.tsx

158 lines5.5 KB

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