/
modal.tsx
64 lines (61 loc) · 2.01 KB
/
modal.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
"use client";
import { Dispatch, SetStateAction } from "react";
import { cn } from "@/lib/utils";
import { Drawer } from "vaul";
import * as Dialog from "@radix-ui/react-dialog";
import useMediaQuery from "@/lib/hooks/use-media-query";
export default function Modal({
children,
className,
showModal,
setShowModal,
}: {
children: React.ReactNode;
className?: string;
showModal: boolean;
setShowModal: Dispatch<SetStateAction<boolean>>;
}) {
const { isMobile } = useMediaQuery();
if (isMobile) {
return (
<Drawer.Root open={showModal} onOpenChange={setShowModal}>
<Drawer.Overlay className="fixed inset-0 z-40 bg-gray-100 bg-opacity-10 backdrop-blur" />
<Drawer.Portal>
<Drawer.Content
className={cn(
"fixed bottom-0 left-0 right-0 z-50 mt-24 rounded-t-[10px] border-t border-gray-200 bg-white",
className,
)}
>
<div className="sticky top-0 z-20 flex w-full items-center justify-center rounded-t-[10px] bg-inherit">
<div className="my-3 h-1 w-12 rounded-full bg-gray-300" />
</div>
{children}
</Drawer.Content>
<Drawer.Overlay />
</Drawer.Portal>
</Drawer.Root>
);
}
return (
<Dialog.Root open={showModal} onOpenChange={setShowModal}>
<Dialog.Portal>
<Dialog.Overlay
// for detecting when there's an active opened modal
id="modal-backdrop"
className="animate-fade-in fixed inset-0 z-40 bg-gray-100 bg-opacity-50 backdrop-blur-md"
/>
<Dialog.Content
onOpenAutoFocus={(e) => e.preventDefault()}
onCloseAutoFocus={(e) => e.preventDefault()}
className={cn(
"animate-scale-in fixed inset-0 z-40 m-auto max-h-fit w-full max-w-md overflow-hidden border border-gray-200 bg-white p-0 shadow-xl md:rounded-2xl",
className,
)}
>
{children}
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
);
}