|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import React, { useCallback, useEffect, useRef } from "react" |
|
5
|
import { useModal } from "@/providers/Modal" |
|
6
|
import { ModalHeader } from "./Header" |
|
7
|
import { ModalFooter } from "./Footer" |
|
8
|
import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut" |
|
9
|
import { ButtonProps } from "@/components/Button" |
|
10
|
import { Ref } from "types" |
|
11
|
|
|
12
|
export type ModalProps = { |
|
13
|
className?: string |
|
14
|
title?: React.ReactNode |
|
15
|
actions?: ButtonProps[] |
|
16
|
modalContainerClassName?: string |
|
17
|
contentClassName?: string |
|
18
|
onClose?: React.ReactEventHandler<HTMLDialogElement> |
|
19
|
open?: boolean |
|
20
|
footerContent?: React.ReactNode |
|
21
|
passedRef?: Ref<HTMLDialogElement> |
|
22
|
headerClassName?: string |
|
23
|
} & Omit<React.ComponentProps<"dialog">, "ref" | "title" | "key"> |
|
24
|
|
|
25
|
export const Modal = ({ |
|
26
|
className, |
|
27
|
title, |
|
28
|
actions, |
|
29
|
children, |
|
30
|
contentClassName, |
|
31
|
modalContainerClassName, |
|
32
|
onClose, |
|
33
|
open = true, |
|
34
|
footerContent, |
|
35
|
passedRef, |
|
36
|
...props |
|
37
|
}: ModalProps) => { |
|
38
|
const { closeModal } = useModal() |
|
39
|
const ref = useRef<HTMLDialogElement | null>(null) |
|
40
|
|
|
41
|
const setRefs = useCallback( |
|
42
|
(node: HTMLDialogElement) => { |
|
43
|
// Ref's from useRef needs to have the node assigned to `current` |
|
44
|
ref.current = node |
|
45
|
if (typeof passedRef === "function") { |
|
46
|
passedRef(node) |
|
47
|
} else if (passedRef && "current" in passedRef) { |
|
48
|
passedRef.current = node |
|
49
|
} |
|
50
|
}, |
|
51
|
[passedRef] |
|
52
|
) |
|
53
|
|
|
54
|
useKeyboardShortcut({ |
|
55
|
metakey: false, |
|
56
|
checkEditing: false, |
|
57
|
shortcutKeys: ["escape"], |
|
58
|
action: () => { |
|
59
|
if (open) { |
|
60
|
ref.current?.close() |
|
61
|
} |
|
62
|
}, |
|
63
|
}) |
|
64
|
|
|
65
|
const handleClick = (e: React.MouseEvent<HTMLDialogElement, MouseEvent>) => { |
|
66
|
// close modal when the user clicks outside the content |
|
67
|
if (e.target === ref.current) { |
|
68
|
closeModal() |
|
69
|
onClose?.(e) |
|
70
|
} |
|
71
|
} |
|
72
|
|
|
73
|
const handleClose = (e: React.SyntheticEvent<HTMLDialogElement, Event>) => { |
|
74
|
onClose?.(e) |
|
75
|
closeModal() |
|
76
|
} |
|
77
|
|
|
78
|
useEffect(() => { |
|
79
|
if (open) { |
|
80
|
document.body.setAttribute("data-modal", "opened") |
|
81
|
} else { |
|
82
|
document.body.removeAttribute("data-modal") |
|
83
|
} |
|
84
|
}, [open]) |
|
85
|
|
|
86
|
return ( |
|
87
|
<dialog |
|
88
|
{...props} |
|
89
|
className={clsx( |
|
90
|
"fixed top-0 left-0 flex h-screen w-screen items-center justify-center", |
|
91
|
"bg-medusa-bg-overlay z-[499]", |
|
92
|
"hidden open:flex border-0 px-docs_0.5 lg:p-0", |
|
93
|
className |
|
94
|
)} |
|
95
|
onClick={handleClick} |
|
96
|
ref={setRefs} |
|
97
|
onClose={handleClose} |
|
98
|
open={open} |
|
99
|
> |
|
100
|
<div |
|
101
|
className={clsx( |
|
102
|
"bg-medusa-bg-base rounded-docs_lg", |
|
103
|
"shadow-elevation-modal dark:shadow-elevation-modal-dark", |
|
104
|
"max-w-full sm:max-w-modal-sm md:max-w-modal-md lg:max-w-modal-lg", |
|
105
|
"h-auto w-full", |
|
106
|
modalContainerClassName |
|
107
|
)} |
|
108
|
data-testid="modal-container" |
|
109
|
> |
|
110
|
{title && <ModalHeader title={title} />} |
|
111
|
<div |
|
112
|
className={clsx( |
|
113
|
"overflow-auto py-docs_1.5 px-docs_2 rounded-docs_lg", |
|
114
|
contentClassName |
|
115
|
)} |
|
116
|
data-testid="modal-content" |
|
117
|
> |
|
118
|
{children} |
|
119
|
</div> |
|
120
|
{actions && actions?.length > 0 && <ModalFooter actions={actions} />} |
|
121
|
{footerContent && <ModalFooter>{footerContent}</ModalFooter>} |
|
122
|
</div> |
|
123
|
</dialog> |
|
124
|
) |
|
125
|
} |