|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Close from '@/shared/assets/icons/close_32.svg'; |
| 4 | +import Portal from './Portal'; |
| 5 | +import tw from '@/shared/utills/tw'; |
| 6 | +import { useEffect } from 'react'; |
| 7 | + |
| 8 | +interface Props { |
| 9 | + ref?: React.Ref<HTMLDivElement>; |
| 10 | + size?: 'sm' | 'md'; |
| 11 | + open: boolean; |
| 12 | + onClose: () => void; |
| 13 | + title?: string; |
| 14 | + description?: React.ReactNode; |
| 15 | + children?: React.ReactNode; |
| 16 | + buttons?: React.ReactNode; |
| 17 | +} |
| 18 | + |
| 19 | +function ModalLayout({ |
| 20 | + ref, |
| 21 | + size = 'md', |
| 22 | + open, |
| 23 | + onClose, |
| 24 | + title, |
| 25 | + description, |
| 26 | + children, |
| 27 | + buttons, |
| 28 | +}: Props) { |
| 29 | + // ESC키 모달 닫기 |
| 30 | + useEffect(() => { |
| 31 | + if (!open) return; |
| 32 | + |
| 33 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 34 | + if (e.key === 'Escape') onClose(); |
| 35 | + }; |
| 36 | + |
| 37 | + document.addEventListener('keydown', handleKeyDown); |
| 38 | + |
| 39 | + return () => document.removeEventListener('keydown', handleKeyDown); |
| 40 | + }, [open, onClose]); |
| 41 | + |
| 42 | + if (!open) return null; |
| 43 | + |
| 44 | + return ( |
| 45 | + <Portal> |
| 46 | + <div className="fixed inset-0 bg-black/80 flex-center " onClick={onClose} aria-hidden="true"> |
| 47 | + <div |
| 48 | + className={tw( |
| 49 | + 'relative p-8 rounded-lg bg-bg-pop shadow-[0_4px_9px_0_rgba(255,255,255,0.25)]', |
| 50 | + size === 'sm' && 'p-5 w-[18.75rem]', |
| 51 | + size === 'md' && 'w-[31.25rem]' |
| 52 | + )} |
| 53 | + ref={ref} |
| 54 | + aria-modal="true" |
| 55 | + aria-labelledby={title ? 'modal-title' : undefined} |
| 56 | + aria-describedby={description ? 'modal-description' : undefined} |
| 57 | + onClick={(e) => e.stopPropagation()} |
| 58 | + > |
| 59 | + <div className={tw('flex items-center flex-col gap-2')}> |
| 60 | + {title && ( |
| 61 | + <h1 |
| 62 | + id="modal-title" |
| 63 | + className={tw('text-2xl font-bold text-white', size === 'sm' && 'text-xl')} |
| 64 | + > |
| 65 | + {title} |
| 66 | + </h1> |
| 67 | + )} |
| 68 | + {description && ( |
| 69 | + <p id="modal-description" className="text-white"> |
| 70 | + {description} |
| 71 | + </p> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + |
| 75 | + {children && <div className="mt-5 py-2 text-white">{children}</div>} |
| 76 | + |
| 77 | + {buttons && ( |
| 78 | + <div className={tw('flex justify-center gap-2 pt-8', size === 'sm' && 'pt-5')}> |
| 79 | + {buttons} |
| 80 | + </div> |
| 81 | + )} |
| 82 | + <button onClick={onClose} aria-label="팝업 닫기" className="absolute top-2 right-2"> |
| 83 | + <Close aria-hidden className="w-8 h-8" /> |
| 84 | + </button> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + </Portal> |
| 88 | + ); |
| 89 | +} |
| 90 | +export default ModalLayout; |
0 commit comments