Raw
1 import { FocusModal, 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 StackedFocusModalProps = 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 * An optional callback that is called when the modal is opened or closed.
21 */
22 onOpenChangeCallback?: (open: boolean) => void
23 }>
24
25 /**
26 * A stacked modal that can be rendered above a parent modal.
27 */
28 export const Root = ({
29 id,
30 onOpenChangeCallback,
31 children,
32 }: StackedFocusModalProps) => {
33 const { register, unregister, getIsOpen, setIsOpen } = useStackedModal()
34
35 useEffect(() => {
36 register(id)
37
38 return () => unregister(id)
39 // eslint-disable-next-line react-hooks/exhaustive-deps
40 }, [])
41
42 const handleOpenChange = (open: boolean) => {
43 setIsOpen(id, open)
44 onOpenChangeCallback?.(open)
45 }
46
47 return (
48 <FocusModal open={getIsOpen(id)} onOpenChange={handleOpenChange}>
49 {children}
50 </FocusModal>
51 )
52 }
53
54 const Close = FocusModal.Close
55 Close.displayName = "StackedFocusModal.Close"
56
57 const Header = FocusModal.Header
58 Header.displayName = "StackedFocusModal.Header"
59
60 const Body = FocusModal.Body
61 Body.displayName = "StackedFocusModal.Body"
62
63 const Trigger = FocusModal.Trigger
64 Trigger.displayName = "StackedFocusModal.Trigger"
65
66 const Footer = FocusModal.Footer
67 Footer.displayName = "StackedFocusModal.Footer"
68
69 const Title = FocusModal.Title
70 Title.displayName = "StackedFocusModal.Title"
71
72 const Description = FocusModal.Description
73 Description.displayName = "StackedFocusModal.Description"
74
75 export type StackedFocusModalContentProps = ComponentPropsWithoutRef<
76 typeof FocusModal.Content
77 >
78
79 const Content: ForwardRefExoticComponent<
80 PropsWithoutRef<StackedFocusModalContentProps> & RefAttributes<HTMLDivElement>
81 > = forwardRef<HTMLDivElement, StackedFocusModalContentProps>(
82 ({ className, ...props }, ref) => {
83 return (
84 <FocusModal.Content
85 ref={ref}
86 className={clx("!top-6", className)}
87 overlayProps={{
88 className: "bg-transparent",
89 }}
90 {...props}
91 />
92 )
93 }
94 )
95 Content.displayName = "StackedFocusModal.Content"
96
97 type StackedFocusModalComponent = typeof Root & {
98 Close: typeof FocusModal.Close
99 Header: typeof FocusModal.Header
100 Body: typeof FocusModal.Body
101 Content: typeof Content
102 Trigger: typeof FocusModal.Trigger
103 Footer: typeof FocusModal.Footer
104 Description: typeof FocusModal.Description
105 Title: typeof FocusModal.Title
106 }
107
108 export const StackedFocusModal: StackedFocusModalComponent = Object.assign(
109 Root,
110 {
111 Close,
112 Header,
113 Body,
114 Content,
115 Trigger,
116 Footer,
117 Description,
118 Title,
119 }
120 )