forked from Mobilecn-UI/nativecn-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dialog.tsx
73 lines (62 loc) · 1.73 KB
/
Dialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { cloneElement, createContext, useContext, useState } from 'react';
import { Modal, TouchableOpacity, View } from 'react-native';
import { cn } from '../lib/utils';
interface DialogContextType {
open: boolean;
setOpen: (open: boolean) => void;
}
const DialogContext = createContext<DialogContextType | undefined>(undefined);
function Dialog({ children }: { children: React.ReactNode }) {
const [open, setOpen] = useState(false);
return (
<DialogContext.Provider value={{ open, setOpen }}>
{children}
</DialogContext.Provider>
);
}
function DialogTrigger({ children }: any) {
const { setOpen } = useDialog();
return cloneElement(children, { onPress: () => setOpen(true) });
}
function DialogContent({
className,
children,
}: {
className?: string;
children: React.ReactNode;
}) {
const { open, setOpen } = useDialog();
return (
<Modal
transparent
animationType="fade"
visible={open}
onRequestClose={() => setOpen(false)}
>
<TouchableOpacity
className="w-full h-full"
onPress={() => setOpen(false)}
>
<View className="flex flex-1 justify-center items-center bg-black/75">
<TouchableOpacity
className={cn(
'border border-border bg-background rounded-lg p-6 shadow-lg',
className
)}
activeOpacity={1}
>
{children}
</TouchableOpacity>
</View>
</TouchableOpacity>
</Modal>
);
}
const useDialog = () => {
const context = useContext(DialogContext);
if (!context) {
throw new Error('useDialog must be used within a DialogProvider');
}
return context;
};
export { Dialog, DialogTrigger, DialogContent, useDialog };