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