|
1
|
import { FocusModal, clx } from "@medusajs/ui" |
|
2
|
import { PropsWithChildren, useEffect, useState } from "react" |
|
3
|
import { useNavigate } from "react-router-dom" |
|
4
|
import { RouteModalForm } from "../route-modal-form" |
|
5
|
import { useRouteModal } from "../route-modal-provider" |
|
6
|
import { RouteModalProvider } from "../route-modal-provider/route-provider" |
|
7
|
import { StackedModalProvider } from "../stacked-modal-provider" |
|
8
|
|
|
9
|
type RouteFocusModalProps = PropsWithChildren<{ |
|
10
|
prev?: string |
|
11
|
onClose?: (() => boolean | Promise<boolean>) | undefined |
|
12
|
}> |
|
13
|
|
|
14
|
const Root = ({ prev = "..", onClose, children }: RouteFocusModalProps) => { |
|
15
|
const navigate = useNavigate() |
|
16
|
const [open, setOpen] = useState(false) |
|
17
|
const [stackedModalOpen, onStackedModalOpen] = useState(false) |
|
18
|
|
|
19
|
/** |
|
20
|
* Open the modal when the component mounts. This |
|
21
|
* ensures that the entry animation is played. |
|
22
|
*/ |
|
23
|
useEffect(() => { |
|
24
|
setOpen(true) |
|
25
|
|
|
26
|
return () => { |
|
27
|
setOpen(false) |
|
28
|
onStackedModalOpen(false) |
|
29
|
} |
|
30
|
}, []) |
|
31
|
|
|
32
|
const handleOpenChange = async (open: boolean) => { |
|
33
|
if (!open) { |
|
34
|
if (onClose) { |
|
35
|
const ret = await onClose() |
|
36
|
|
|
37
|
if (!ret) { |
|
38
|
return |
|
39
|
} |
|
40
|
} |
|
41
|
|
|
42
|
document.body.style.pointerEvents = "auto" |
|
43
|
navigate(prev, { replace: true }) |
|
44
|
return |
|
45
|
} |
|
46
|
|
|
47
|
setOpen(open) |
|
48
|
} |
|
49
|
|
|
50
|
return ( |
|
51
|
<FocusModal open={open} onOpenChange={handleOpenChange}> |
|
52
|
<RouteModalProvider prev={prev}> |
|
53
|
<StackedModalProvider onOpenChange={onStackedModalOpen}> |
|
54
|
<Content stackedModalOpen={stackedModalOpen}>{children}</Content> |
|
55
|
</StackedModalProvider> |
|
56
|
</RouteModalProvider> |
|
57
|
</FocusModal> |
|
58
|
) |
|
59
|
} |
|
60
|
|
|
61
|
type ContentProps = PropsWithChildren<{ |
|
62
|
stackedModalOpen: boolean |
|
63
|
}> |
|
64
|
|
|
65
|
const Content = ({ stackedModalOpen, children }: ContentProps) => { |
|
66
|
const { __internal } = useRouteModal() |
|
67
|
|
|
68
|
const shouldPreventClose = !__internal.closeOnEscape |
|
69
|
|
|
70
|
return ( |
|
71
|
<FocusModal.Content |
|
72
|
onEscapeKeyDown={ |
|
73
|
shouldPreventClose |
|
74
|
? (e) => { |
|
75
|
e.preventDefault() |
|
76
|
} |
|
77
|
: undefined |
|
78
|
} |
|
79
|
className={clx({ |
|
80
|
"!bg-ui-bg-disabled !inset-x-5 !inset-y-3": stackedModalOpen, |
|
81
|
})} |
|
82
|
> |
|
83
|
{children} |
|
84
|
</FocusModal.Content> |
|
85
|
) |
|
86
|
} |
|
87
|
|
|
88
|
const Header = FocusModal.Header |
|
89
|
const Title = FocusModal.Title |
|
90
|
const Description = FocusModal.Description |
|
91
|
const Footer = FocusModal.Footer |
|
92
|
const Body = FocusModal.Body |
|
93
|
const Close = FocusModal.Close |
|
94
|
const Form = RouteModalForm |
|
95
|
|
|
96
|
/** |
|
97
|
* FocusModal that is used to render a form on a separate route. |
|
98
|
* |
|
99
|
* Typically used for forms creating a resource or forms that require |
|
100
|
* a lot of space. |
|
101
|
*/ |
|
102
|
export const RouteFocusModal = Object.assign(Root, { |
|
103
|
Header, |
|
104
|
Title, |
|
105
|
Body, |
|
106
|
Description, |
|
107
|
Footer, |
|
108
|
Close, |
|
109
|
Form, |
|
110
|
}) |