Raw
1 "use client"
2
3 import { XMark } from "@medusajs/icons"
4 import { Dialog as RadixDialog } from "radix-ui"
5 import * as React from "react"
6
7 import { IconButton } from "@/components/icon-button"
8 import { Kbd } from "@/components/kbd"
9 import { clx } from "@/utils/clx"
10
11 /**
12 * @prop defaultOpen - Whether the modal is opened by default.
13 * @prop open - Whether the modal is opened.
14 * @prop onOpenChange - A function to handle when the modal is opened or closed.
15 */
16 interface FocusModalRootProps
17 extends React.ComponentPropsWithoutRef<typeof RadixDialog.Root> {}
18
19 /**
20 * This component is based on the [Radix UI Dialog](https://www.radix-ui.com/primitives/docs/components/dialog) primitives.
21 */
22 const FocusModalRoot = (props: FocusModalRootProps) => {
23 return <RadixDialog.Root {...props} />
24 }
25 FocusModalRoot.displayName = "FocusModal"
26
27 interface FocusModalTriggerProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Trigger> {}
28
29 /**
30 * This component is used to create the trigger button that opens the modal.
31 * It accepts props from the [Radix UI Dialog Trigger](https://www.radix-ui.com/primitives/docs/components/dialog#trigger) component.
32 */
33 const FocusModalTrigger = React.forwardRef<
34 React.ElementRef<typeof RadixDialog.Trigger>,
35 FocusModalTriggerProps
36 >((props: FocusModalTriggerProps, ref) => {
37 return <RadixDialog.Trigger ref={ref} {...props} />
38 })
39 FocusModalTrigger.displayName = "FocusModal.Trigger"
40
41 /**
42 * This component is used to create the close button for the modal.
43 * It accepts props from the [Radix UI Dialog Close](https://www.radix-ui.com/primitives/docs/components/dialog#close) component.
44 */
45 const FocusModalClose = RadixDialog.Close
46 FocusModalClose.displayName = "FocusModal.Close"
47
48 interface FocusModalPortalProps extends RadixDialog.DialogPortalProps {}
49
50 /**
51 * The `FocusModal.Content` component uses this component to wrap the modal content.
52 * It accepts props from the [Radix UI Dialog Portal](https://www.radix-ui.com/primitives/docs/components/dialog#portal) component.
53 */
54 const FocusModalPortal = (props: FocusModalPortalProps) => {
55 return (
56 <RadixDialog.DialogPortal {...props} />
57 )
58 }
59 FocusModalPortal.displayName = "FocusModal.Portal"
60
61 /**
62 * This component is used to create the overlay for the modal.
63 * It accepts props from the [Radix UI Dialog Overlay](https://www.radix-ui.com/primitives/docs/components/dialog#overlay) component.
64 */
65 const FocusModalOverlay = React.forwardRef<
66 React.ElementRef<typeof RadixDialog.Overlay>,
67 React.ComponentPropsWithoutRef<typeof RadixDialog.Overlay>
68 >(({ className, ...props }, ref) => {
69 return (
70 <RadixDialog.Overlay
71 ref={ref}
72 className={clx(
73 "bg-ui-bg-overlay fixed inset-0",
74 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
75 className
76 )}
77 {...props}
78 />
79 )
80 })
81 FocusModalOverlay.displayName = "FocusModal.Overlay"
82
83 /**
84 * This component wraps the content of the modal.
85 * It accepts props from the [Radix UI Dialog Content](https://www.radix-ui.com/primitives/docs/components/dialog#content) component.
86 */
87 const FocusModalContent = React.forwardRef<
88 React.ElementRef<typeof RadixDialog.Content>,
89 React.ComponentPropsWithoutRef<typeof RadixDialog.Content> & {
90 overlayProps?: React.ComponentPropsWithoutRef<typeof FocusModalOverlay>
91 portalProps?: React.ComponentPropsWithoutRef<typeof FocusModalPortal>
92 }
93 >(({ className, overlayProps, portalProps, ...props }, ref) => {
94 return (
95 <FocusModalPortal {...portalProps}>
96 <FocusModalOverlay {...overlayProps} />
97 <RadixDialog.Content
98 ref={ref}
99 className={clx(
100 "bg-ui-bg-base shadow-elevation-modal fixed inset-2 flex flex-col overflow-hidden rounded-lg border outline-none",
101 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=open]:slide-in-from-bottom-0 data-[state=closed]:slide-in-from-bottom-2 duration-200",
102 className
103 )}
104 {...props}
105 />
106 </FocusModalPortal>
107 )
108 })
109 FocusModalContent.displayName = "FocusModal.Content"
110
111 /**
112 * This component is used to wrap the header content of the modal.
113 * This component is based on the `div` element and supports all of its props
114 */
115 const FocusModalHeader = React.forwardRef<
116 HTMLDivElement,
117 React.ComponentPropsWithoutRef<"div">
118 >(({ children, className, ...props }, ref) => {
119 return (
120 <div
121 ref={ref}
122 className={clx(
123 "border-ui-border-base flex items-center justify-between gap-x-4 border-b px-4 py-2",
124 className
125 )}
126 {...props}
127 >
128 <div className="flex items-center gap-x-2">
129 <RadixDialog.Close asChild>
130 <IconButton size="small" type="button" variant="transparent">
131 <XMark />
132 </IconButton>
133 </RadixDialog.Close>
134 <Kbd>esc</Kbd>
135 </div>
136 {children}
137 </div>
138 )
139 })
140 FocusModalHeader.displayName = "FocusModal.Header"
141
142 /**
143 * This component is used to wrap the footer content of the modal.
144 * This component is based on the `div` element and supports all of its props
145 */
146 const FocusModalFooter = React.forwardRef<
147 HTMLDivElement,
148 React.ComponentPropsWithoutRef<"div">
149 >(({ children, className, ...props }, ref) => {
150 return (
151 <div
152 ref={ref}
153 className={clx(
154 "border-ui-border-base flex items-center justify-end gap-x-2 border-t p-4",
155 className
156 )}
157 {...props}
158 >
159 {children}
160 </div>
161 )
162 })
163 FocusModalFooter.displayName = "FocusModal.Footer"
164
165 interface FocusModalTitleProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Title> {}
166
167 /**
168 * This component adds an accessible title to the modal.
169 * It accepts props from the [Radix UI Dialog Title](https://www.radix-ui.com/primitives/docs/components/dialog#title) component.
170 */
171 const FocusModalTitle = React.forwardRef<
172 HTMLHeadingElement,
173 FocusModalTitleProps
174 >(({ className, ...props }: FocusModalTitleProps, ref) => {
175 return (
176 <RadixDialog.Title ref={ref} {...props} />
177 )
178 })
179 FocusModalTitle.displayName = "FocusModal.Title"
180
181 /**
182 * This component adds accessible description to the modal.
183 * It accepts props from the [Radix UI Dialog Description](https://www.radix-ui.com/primitives/docs/components/dialog#description) component.
184 */
185 const FocusModalDescription = RadixDialog.Description
186 FocusModalDescription.displayName = "FocusModal.Description"
187
188 /**
189 * This component is used to wrap the body content of the modal.
190 * This component is based on the `div` element and supports all of its props
191 */
192 const FocusModalBody = React.forwardRef<
193 HTMLDivElement,
194 React.ComponentPropsWithoutRef<"div">
195 >(({ className, ...props }, ref) => {
196 return <div ref={ref} className={clx("flex-1", className)} {...props} />
197 })
198 FocusModalBody.displayName = "FocusModal.Body"
199
200 const FocusModal = Object.assign(FocusModalRoot, {
201 Trigger: FocusModalTrigger,
202 Title: FocusModalTitle,
203 Description: FocusModalDescription,
204 Content: FocusModalContent,
205 Header: FocusModalHeader,
206 Body: FocusModalBody,
207 Close: FocusModalClose,
208 Footer: FocusModalFooter,
209 })
210
211 export { FocusModal }