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 { Text } from "@/components/text"
10 import { clx } from "@/utils/clx"
11
12 interface DrawerRootProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Root> {}
13
14 /**
15 * This component is based on the [Radix UI Dialog](https://www.radix-ui.com/primitives/docs/components/dialog) primitives.
16 */
17 const DrawerRoot = (
18 props: DrawerRootProps
19 ) => {
20 return <RadixDialog.Root {...props} />
21 }
22 DrawerRoot.displayName = "Drawer"
23
24 interface DrawerTriggerProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Trigger> {}
25
26 /**
27 * This component is used to create the trigger button that opens the drawer.
28 * It accepts props from the [Radix UI Dialog Trigger](https://www.radix-ui.com/primitives/docs/components/dialog#trigger) component.
29 */
30 const DrawerTrigger = React.forwardRef<
31 React.ElementRef<typeof RadixDialog.Trigger>,
32 DrawerTriggerProps
33 >(({ className, ...props }: DrawerTriggerProps, ref) => {
34 return (
35 <RadixDialog.Trigger ref={ref} className={clx(className)} {...props} />
36 )
37 })
38 DrawerTrigger.displayName = "Drawer.Trigger"
39
40 interface DrawerCloseProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Close> {}
41
42 /**
43 * This component is used to create the close button for the drawer.
44 * It accepts props from the [Radix UI Dialog Close](https://www.radix-ui.com/primitives/docs/components/dialog#close) component.
45 */
46 const DrawerClose = React.forwardRef<
47 React.ElementRef<typeof RadixDialog.Close>,
48 DrawerCloseProps
49 >(({ className, ...props }: DrawerCloseProps, ref) => {
50 return (
51 <RadixDialog.Close ref={ref} className={clx(className)} {...props} />
52 )
53 })
54 DrawerClose.displayName = "Drawer.Close"
55
56 interface DrawerPortalProps extends RadixDialog.DialogPortalProps {}
57
58 /**
59 * The `Drawer.Content` component uses this component to wrap the drawer content.
60 * It accepts props from the [Radix UI Dialog Portal](https://www.radix-ui.com/primitives/docs/components/dialog#portal) component.
61 */
62 const DrawerPortal = (props: DrawerPortalProps) => {
63 return <RadixDialog.DialogPortal {...props} />
64 }
65 DrawerPortal.displayName = "Drawer.Portal"
66
67 interface DrawerOverlayProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Overlay> {}
68
69 /**
70 * This component is used to create the overlay for the drawer.
71 * It accepts props from the [Radix UI Dialog Overlay](https://www.radix-ui.com/primitives/docs/components/dialog#overlay) component.
72 */
73 const DrawerOverlay = React.forwardRef<
74 React.ElementRef<typeof RadixDialog.Overlay>,
75 DrawerOverlayProps
76 >(({ className, ...props }: DrawerOverlayProps, ref) => {
77 return (
78 <RadixDialog.Overlay
79 ref={ref}
80 className={clx(
81 "bg-ui-bg-overlay fixed inset-0",
82 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
83 className
84 )}
85 {...props}
86 />
87 )
88 })
89 DrawerOverlay.displayName = "Drawer.Overlay"
90
91 interface DrawerContentProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Content> {
92 /**
93 * Props for the overlay component.
94 * It accepts props from the [Radix UI Dialog Overlay](https://www.radix-ui.com/primitives/docs/components/dialog#overlay) component.
95 */
96 overlayProps?: React.ComponentPropsWithoutRef<typeof DrawerOverlay>
97 /**
98 * Props for the portal component that wraps the drawer content.
99 * It accepts props from the [Radix UI Dialog Portal](https://www.radix-ui.com/primitives/docs/components/dialog#portal) component.
100 */
101 portalProps?: React.ComponentPropsWithoutRef<typeof DrawerPortal>
102 }
103
104 /**
105 * This component wraps the content of the drawer.
106 * It accepts props from the [Radix UI Dialog Content](https://www.radix-ui.com/primitives/docs/components/dialog#content) component.
107 */
108 const DrawerContent = React.forwardRef<
109 React.ElementRef<typeof RadixDialog.Content>,
110 DrawerContentProps
111 >(({ className, overlayProps, portalProps, ...props }: DrawerContentProps, ref) => {
112 return (
113 <DrawerPortal {...portalProps}>
114 <DrawerOverlay {...overlayProps} />
115 <RadixDialog.Content
116 ref={ref}
117 className={clx(
118 "bg-ui-bg-base shadow-elevation-modal border-ui-border-base fixed inset-y-2 flex w-full flex-1 flex-col rounded-lg border outline-none max-sm:inset-x-2 max-sm:w-[calc(100%-16px)] sm:right-2 sm:max-w-[560px]",
119 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:slide-out-to-right-1/2 data-[state=open]:slide-in-from-right-1/2 duration-200",
120 className
121 )}
122 {...props}
123 />
124 </DrawerPortal>
125 )
126 })
127 DrawerContent.displayName = "Drawer.Content"
128
129 interface DrawerHeaderProps extends React.ComponentPropsWithoutRef<"div"> {}
130
131 /**
132 * This component is used to wrap the header content of the drawer.
133 * This component is based on the `div` element and supports all of its props.
134 */
135 const DrawerHeader = React.forwardRef<
136 HTMLDivElement,
137 DrawerHeaderProps
138 >(({ children, className, ...props }: DrawerHeaderProps, ref) => {
139 return (
140 <div
141 ref={ref}
142 className="border-ui-border-base flex items-start justify-between gap-x-4 border-b px-6 py-4"
143 {...props}
144 >
145 <div className={clx("flex flex-col gap-y-1", className)}>{children}</div>
146 <div className="flex items-center gap-x-2">
147 <Kbd>esc</Kbd>
148 <RadixDialog.Close asChild>
149 <IconButton size="small" type="button" variant="transparent">
150 <XMark />
151 </IconButton>
152 </RadixDialog.Close>
153 </div>
154 </div>
155 )
156 })
157 DrawerHeader.displayName = "Drawer.Header"
158
159 interface DrawerBodyProps extends React.ComponentPropsWithoutRef<"div"> {}
160
161 /**
162 * This component is used to wrap the body content of the drawer.
163 * This component is based on the `div` element and supports all of its props
164 */
165 const DrawerBody = React.forwardRef<
166 HTMLDivElement,
167 DrawerBodyProps
168 >(({ className, ...props }: DrawerBodyProps, ref) => {
169 return (
170 <div ref={ref} className={clx("flex-1 px-6 py-4", className)} {...props} />
171 )
172 })
173 DrawerBody.displayName = "Drawer.Body"
174
175 interface DrawerFooterProps extends React.HTMLAttributes<HTMLDivElement> {}
176
177 /**
178 * This component is used to wrap the footer content of the drawer.
179 * This component is based on the `div` element and supports all of its props.
180 */
181 const DrawerFooter = ({
182 className,
183 ...props
184 }: DrawerFooterProps) => {
185 return (
186 <div
187 className={clx(
188 "border-ui-border-base flex items-center justify-end space-x-2 overflow-y-auto border-t px-6 py-4",
189 className
190 )}
191 {...props}
192 />
193 )
194 }
195 DrawerFooter.displayName = "Drawer.Footer"
196
197 /**
198 * This component adds an accessible title to the drawer.
199 * It accepts props from the [Radix UI Dialog Title](https://www.radix-ui.com/primitives/docs/components/dialog#title) component.
200 */
201 const DrawerTitle = RadixDialog.Title
202 DrawerTitle.displayName = "Drawer.Title"
203
204 interface DrawerDescriptionProps extends React.ComponentPropsWithoutRef<typeof RadixDialog.Description> {}
205
206 /**
207 * This component adds accessible description to the drawer.
208 * It accepts props from the [Radix UI Dialog Description](https://www.radix-ui.com/primitives/docs/components/dialog#description) component.
209 */
210 const DrawerDescription = React.forwardRef<
211 React.ElementRef<typeof RadixDialog.Description>,
212 DrawerDescriptionProps
213 >(({ className, children, ...props }: DrawerDescriptionProps, ref) => (
214 <RadixDialog.Description
215 ref={ref}
216 className={clx(className)}
217 asChild
218 {...props}
219 >
220 <Text>{children}</Text>
221 </RadixDialog.Description>
222 ))
223 DrawerDescription.displayName = "Drawer.Description"
224
225 const Drawer = Object.assign(DrawerRoot, {
226 Body: DrawerBody,
227 Close: DrawerClose,
228 Content: DrawerContent,
229 Description: DrawerDescription,
230 Footer: DrawerFooter,
231 Header: DrawerHeader,
232 Title: DrawerTitle,
233 Trigger: DrawerTrigger,
234 })
235
236 export { Drawer }