|
| 1 | +import { forwardRef, type ReactNode } from 'react' |
| 2 | +import * as Dialog from '@radix-ui/react-dialog' |
| 3 | +import { Theme } from '@radix-ui/themes' |
| 4 | +import { Cross2Icon } from '@radix-ui/react-icons' |
| 5 | +import './drawer.css' |
| 6 | + |
| 7 | +type DrawerDirection = 'left' | 'right' | 'top' | 'bottom' |
| 8 | + |
| 9 | +interface DrawerProps { |
| 10 | + /** |
| 11 | + * Controls whether the drawer is open |
| 12 | + */ |
| 13 | + open: boolean |
| 14 | + /** |
| 15 | + * Callback when the drawer open state changes |
| 16 | + */ |
| 17 | + onOpenChange: (open: boolean) => void |
| 18 | + /** |
| 19 | + * Content to display in the drawer |
| 20 | + */ |
| 21 | + children: ReactNode |
| 22 | + /** |
| 23 | + * Direction from which the drawer slides in |
| 24 | + * @default 'right' |
| 25 | + */ |
| 26 | + direction?: DrawerDirection |
| 27 | + /** |
| 28 | + * Whether to include the Theme wrapper. Default true for styled content. |
| 29 | + * @default true |
| 30 | + */ |
| 31 | + includeTheme?: boolean |
| 32 | + /** |
| 33 | + * Whether clicking backdrop closes drawer. Default true. |
| 34 | + * @default true |
| 35 | + */ |
| 36 | + closeOnBackdropClick?: boolean |
| 37 | + /** |
| 38 | + * Whether ESC key closes drawer. Default true. |
| 39 | + * @default true |
| 40 | + */ |
| 41 | + closeOnEsc?: boolean |
| 42 | + /** |
| 43 | + * Custom width for left/right drawers or height for top/bottom drawers |
| 44 | + */ |
| 45 | + size?: string |
| 46 | + /** |
| 47 | + * Whether to show close button |
| 48 | + * @default true |
| 49 | + */ |
| 50 | + showCloseButton?: boolean |
| 51 | + /** |
| 52 | + * Title for the drawer (for accessibility) |
| 53 | + */ |
| 54 | + title?: string |
| 55 | + /** |
| 56 | + * Description for the drawer (for accessibility) |
| 57 | + */ |
| 58 | + description?: string |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * A drawer component built on Radix UI Dialog with CSS animations. |
| 63 | + * Provides slide-in functionality from any edge of the viewport. |
| 64 | + * |
| 65 | + * Features: |
| 66 | + * - Built on Radix Dialog for proper accessibility and focus management |
| 67 | + * - CSS-based animations for test compatibility |
| 68 | + * - Supports all four directions |
| 69 | + * - Portal rendering to escape shadow DOM |
| 70 | + * - ESC key and backdrop click support |
| 71 | + */ |
| 72 | +export const Drawer = forwardRef<HTMLDivElement, DrawerProps>( |
| 73 | + ( |
| 74 | + { |
| 75 | + open, |
| 76 | + onOpenChange, |
| 77 | + children, |
| 78 | + direction = 'right', |
| 79 | + includeTheme = true, |
| 80 | + closeOnBackdropClick = true, |
| 81 | + closeOnEsc = true, |
| 82 | + size, |
| 83 | + showCloseButton = true, |
| 84 | + title, |
| 85 | + description, |
| 86 | + }, |
| 87 | + ref |
| 88 | + ) => { |
| 89 | + const content = ( |
| 90 | + <Dialog.Root open={open} onOpenChange={onOpenChange}> |
| 91 | + <Dialog.Portal> |
| 92 | + <Dialog.Overlay |
| 93 | + className="drawer-overlay" |
| 94 | + onClick={closeOnBackdropClick ? () => onOpenChange(false) : undefined} |
| 95 | + /> |
| 96 | + <Dialog.Content |
| 97 | + ref={ref} |
| 98 | + className={`drawer-content drawer-${direction}`} |
| 99 | + style={ |
| 100 | + size |
| 101 | + ? { |
| 102 | + ...(direction === 'left' || direction === 'right' |
| 103 | + ? { width: size } |
| 104 | + : { height: size }), |
| 105 | + } |
| 106 | + : undefined |
| 107 | + } |
| 108 | + onEscapeKeyDown={closeOnEsc ? undefined : (e) => e.preventDefault()} |
| 109 | + onPointerDownOutside={closeOnBackdropClick ? undefined : (e) => e.preventDefault()} |
| 110 | + onInteractOutside={closeOnBackdropClick ? undefined : (e) => e.preventDefault()} |
| 111 | + > |
| 112 | + {/* Always render title and description for accessibility, but hide them visually */} |
| 113 | + <Dialog.Title className="drawer-title">{title || 'Dialog'}</Dialog.Title> |
| 114 | + <Dialog.Description className="drawer-description"> |
| 115 | + {description || 'Dialog content'} |
| 116 | + </Dialog.Description> |
| 117 | + |
| 118 | + {showCloseButton && ( |
| 119 | + <Dialog.Close asChild> |
| 120 | + <button className="drawer-close" aria-label="Close"> |
| 121 | + <Cross2Icon /> |
| 122 | + </button> |
| 123 | + </Dialog.Close> |
| 124 | + )} |
| 125 | + |
| 126 | + <div className="drawer-body">{children}</div> |
| 127 | + </Dialog.Content> |
| 128 | + </Dialog.Portal> |
| 129 | + </Dialog.Root> |
| 130 | + ) |
| 131 | + |
| 132 | + return includeTheme ? <Theme>{content}</Theme> : content |
| 133 | + } |
| 134 | +) |
| 135 | + |
| 136 | +Drawer.displayName = 'Drawer' |
0 commit comments