-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.tsx
More file actions
98 lines (90 loc) · 2.09 KB
/
Modal.tsx
File metadata and controls
98 lines (90 loc) · 2.09 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { ReactNode } from 'react';
import { motion } from 'framer-motion';
import { cn } from '@/shared';
import { Flex } from '../Flex';
import { Portal } from '../Portal';
type Props = {
/**
* Controls whether the modal is open or closed.
*/
isOpen: boolean;
/**
* Function to close the modal.
*/
closeModal: () => void;
/**
* The content to be displayed inside the modal.
*/
children: ReactNode;
/**
* Whether clicking outside the modal closes it.
* @default true
*/
closeOnOutsideClick?: boolean;
/**
* Additional classes to apply to the modal.
*/
className?: string;
/**
* Additional classes to apply to the modal content.
*/
contentClassName?: string;
};
const MODAL_MOTION = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.3 },
} as const;
export function Modal({
isOpen,
closeModal,
children,
closeOnOutsideClick = true,
className,
contentClassName,
}: Props) {
const handleOutsideClick = (e: React.MouseEvent) => {
if (
closeOnOutsideClick &&
e.target instanceof HTMLElement &&
e.target === e.currentTarget &&
closeModal
) {
closeModal();
}
};
return (
<Portal isOpen={isOpen}>
<motion.div
initial={MODAL_MOTION.initial}
animate={MODAL_MOTION.animate}
exit={MODAL_MOTION.exit}
transition={MODAL_MOTION.transition}
className={cn('fixed inset-0 z-50 flex w-full items-center justify-center', className)}
>
<div className="absolute inset-0 bg-black/50" onClick={handleOutsideClick} />
<ModalContent contentClassName={contentClassName}>{children}</ModalContent>
</motion.div>
</Portal>
);
}
export function ModalContent({
children,
contentClassName,
}: {
children: React.ReactNode;
contentClassName?: string;
}) {
return (
<Flex
role="dialog"
aria-modal="true"
justifyContent="center"
alignItems="center"
className={cn('relative z-50 rounded-lg bg-white p-6', contentClassName)}
>
{children}
</Flex>
);
}