1 import { Drawer, clx } from "@medusajs/ui"
2 import {
3 ComponentPropsWithoutRef,
4 PropsWithChildren,
5 forwardRef,
6 useEffect,
7 } from "react"
8 import { useStackedModal } from "../stacked-modal-provider"
9
10 type StackedDrawerProps = PropsWithChildren<{
11 /**
12 * A unique identifier for the modal. This is used to differentiate stacked modals,
13 * when multiple stacked modals are registered to the same parent modal.
14 */
15 id: string
16 }>
17
18 /**
19 * A stacked modal that can be rendered above a parent modal.
20 */
21 export const Root = ({ id, children }: StackedDrawerProps) => {
22 const { register, unregister, getIsOpen, setIsOpen } = useStackedModal()
23
24 useEffect(() => {
25 register(id)
26
27 return () => unregister(id)
28 // eslint-disable-next-line react-hooks/exhaustive-deps
29 }, [])
30
31 return (
32 <Drawer open={getIsOpen(id)} onOpenChange={(open) => setIsOpen(id, open)}>
33 {children}
34 </Drawer>
35 )
36 }
37
38 const Close = Drawer.Close
39 Close.displayName = "StackedDrawer.Close"
40
41 const Header = Drawer.Header
42 Header.displayName = "StackedDrawer.Header"
43
44 const Body = Drawer.Body
45 Body.displayName = "StackedDrawer.Body"
46
47 const Trigger = Drawer.Trigger
48 Trigger.displayName = "StackedDrawer.Trigger"
49
50 const Footer = Drawer.Footer
51 Footer.displayName = "StackedDrawer.Footer"
52
53 const Title = Drawer.Title
54 Title.displayName = "StackedDrawer.Title"
55
56 const Description = Drawer.Description
57 Description.displayName = "StackedDrawer.Description"
58
59 const Content = forwardRef<
60 HTMLDivElement,
61 ComponentPropsWithoutRef<typeof Drawer.Content>
62 >(({ className, ...props }, ref) => {
63 return (
64 <Drawer.Content
65 ref={ref}
66 className={clx(className)}
67 overlayProps={{
68 className: "bg-transparent",
69 }}
70 {...props}
71 />
72 )
73 })
74 Content.displayName = "StackedDrawer.Content"
75
76 export const StackedDrawer = Object.assign(Root, {
77 Close,
78 Header,
79 Body,
80 Content,
81 Trigger,
82 Footer,
83 Description,
84 Title,
85 })