-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from jooyong-boo/develop
merge develop
- Loading branch information
Showing
14 changed files
with
383 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ReactNode, useRef } from 'react'; | ||
import ModalView from '@/components/Modal/ModalView'; | ||
|
||
interface ModalContainerProps { | ||
onClose: () => void; | ||
children: ReactNode; | ||
isOpen: boolean; | ||
displayClose?: string; | ||
} | ||
|
||
const ModalContainer = ({ | ||
onClose, | ||
children, | ||
isOpen, | ||
displayClose, | ||
}: ModalContainerProps) => { | ||
const modalRef = useRef<HTMLDivElement>(null); | ||
|
||
if (!isOpen) { | ||
return null; | ||
} | ||
return ( | ||
<ModalView | ||
onClose={onClose} | ||
modalRef={modalRef} | ||
displayClose={displayClose} | ||
> | ||
{children} | ||
</ModalView> | ||
); | ||
}; | ||
|
||
export default ModalContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import ReactDom from 'react-dom'; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
modalKey: string; | ||
} | ||
|
||
const ModalPortal = ({ children, modalKey }: Props) => { | ||
let el = document.getElementById(modalKey) as HTMLElement; | ||
if (!el) { | ||
const newPortal = document.createElement('div'); | ||
newPortal.setAttribute('id', modalKey); | ||
document.body.appendChild(newPortal); | ||
el = newPortal; | ||
} | ||
return ReactDom.createPortal(children, el); | ||
}; | ||
|
||
export default ModalPortal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ReactNode, useEffect } from 'react'; | ||
import { cn } from '@/utils/cn'; | ||
|
||
interface ModalViewProps { | ||
onClose: () => void; | ||
children: ReactNode; | ||
modalRef: React.RefObject<HTMLDivElement>; | ||
displayClose?: string; | ||
modalHeight?: string; | ||
} | ||
|
||
const ModalView = ({ | ||
onClose, | ||
children, | ||
modalRef, | ||
displayClose = 'block', | ||
}: ModalViewProps) => { | ||
useEffect(() => { | ||
const handleEscKey = (e: KeyboardEvent) => { | ||
if (e.key === 'Escape') { | ||
onClose(); | ||
} | ||
}; | ||
|
||
document.addEventListener('keydown', handleEscKey); | ||
|
||
return () => { | ||
document.removeEventListener('keydown', handleEscKey); | ||
}; | ||
}, [onClose]); | ||
|
||
const overlayClasses = `fixed inset-0 z-[99] w-full h-full backdrop-blur bg-black bg-opacity-50`; | ||
|
||
const modalClasses = cn( | ||
`fixed left-1/2 transition-all duration-500 ease-in-out overflow-y-auto sm:max-w-[500px] sm:mx-auto`, | ||
`dark:bg-gray-800 bg-gray-200 p-4 border dark:bg-slate-800 dark:border-slate-700`, | ||
'top-1/2 w-[calc(100%-32px)] -translate-x-1/2 -translate-y-1/2 rounded-lg animate-modalShow max-h-[90vh]', | ||
); | ||
|
||
const closeBtnClasses = `absolute bg-transparent -top-8 right-0 text-gray-50 ${displayClose === 'none' ? 'hidden' : 'block'}`; | ||
|
||
return ( | ||
<div className={overlayClasses} onClick={onClose}> | ||
<div | ||
ref={modalRef} | ||
className={modalClasses} | ||
onClick={(e) => e.stopPropagation()} | ||
> | ||
{children} | ||
<button | ||
className={`${closeBtnClasses} material-symbols-rounded`} | ||
onClick={onClose} | ||
> | ||
close | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ModalView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useCallback, useState, ReactNode, useEffect } from 'react'; | ||
import ModalContainer from '@/components/Modal/ModalContainer'; | ||
import ModalPortal from '@/components/Modal/ModalPortal'; | ||
|
||
interface UseModalProps { | ||
useBlur?: boolean; | ||
} | ||
|
||
const useModal = ({ useBlur = true }: UseModalProps = {}) => { | ||
const [modals, setModals] = useState<string[]>([]); | ||
|
||
const open = useCallback((modalId: string) => { | ||
document.body.style.overflow = 'hidden'; | ||
setModals((prevModals) => [...prevModals, `${modalId}Modal`]); | ||
}, []); | ||
|
||
const close = useCallback((modalId: string) => { | ||
const deleteModal = document.getElementById(`${modalId}Modal`); | ||
if (deleteModal) { | ||
document.body.style.overflow = 'visible'; | ||
setModals((prevModals) => | ||
prevModals.filter((id) => id !== `${modalId}Modal`), | ||
); | ||
document.body.removeChild(deleteModal); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
return () => { | ||
document.body.style.overflow = 'visible'; | ||
}; | ||
}, []); | ||
|
||
const Modal = useCallback( | ||
({ | ||
children, | ||
modalKey, | ||
displayClose, | ||
}: { | ||
children: ReactNode; | ||
modalKey: string; | ||
displayClose?: string; | ||
}) => { | ||
return modals.includes(`${modalKey}Modal`) ? ( | ||
<ModalPortal modalKey={`${modalKey}Modal`}> | ||
<ModalContainer | ||
isOpen | ||
onClose={useBlur ? () => close(modalKey) : () => {}} | ||
displayClose={displayClose} | ||
> | ||
{children} | ||
</ModalContainer> | ||
</ModalPortal> | ||
) : null; | ||
}, | ||
[modals, useBlur, close], | ||
); | ||
|
||
return { | ||
Modal, | ||
open, | ||
close, | ||
}; | ||
}; | ||
|
||
export default useModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.