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