-
Notifications
You must be signed in to change notification settings - Fork 2
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 #509 from sparcs-kaist/dev
Main branch update from Dev branch
- Loading branch information
Showing
22 changed files
with
237 additions
and
161 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ModalElemProps } from "components/Modal/ModalElem"; | ||
|
||
import { atom } from "recoil"; | ||
|
||
export type ModalType = { | ||
id: string; | ||
props: ModalElemProps; | ||
}; | ||
|
||
const modalsAtom = atom<Array<ModalType>>({ | ||
key: "modalsAtom", | ||
default: [], | ||
}); | ||
|
||
export default modalsAtom; |
This file was deleted.
Oops, something went wrong.
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,126 @@ | ||
import { | ||
MouseEvent, | ||
ReactNode, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
|
||
import { useDelayBoolean } from "hooks/useDelay"; | ||
import useDisableScrollEffect from "hooks/useDisableScrollEffect"; | ||
import useKeyboardOperationEffect from "hooks/useKeyboardOperationEffect"; | ||
|
||
import RLayout from "components/RLayout"; | ||
|
||
import theme from "tools/theme"; | ||
|
||
import CloseRoundedIcon from "@mui/icons-material/CloseRounded"; | ||
|
||
export type ModalElemProps = { | ||
isOpen: boolean; | ||
onChangeIsOpen?: (isOpen: boolean) => void; | ||
onEnter?: () => void; | ||
displayCloseBtn?: boolean; | ||
width?: PixelValue; | ||
padding?: Padding; | ||
children: ReactNode; | ||
isAlert?: boolean; | ||
}; | ||
|
||
const ModalElem = ({ | ||
isOpen, | ||
onChangeIsOpen, | ||
onEnter, | ||
displayCloseBtn = true, | ||
width = theme.modal_width, | ||
padding = "0px", | ||
children, | ||
isAlert = false, | ||
}: ModalElemProps) => { | ||
const [display, setDisplay] = useState(false); | ||
const shouldMount = useDelayBoolean(isOpen, theme.duration_num); | ||
const modalRef = useRef<HTMLDivElement>(null); | ||
const clickRef = useRef(false); | ||
|
||
const closeHandler = useCallback( | ||
onChangeIsOpen ? () => onChangeIsOpen(false) : () => {}, | ||
[onChangeIsOpen] | ||
); | ||
const onMouseDown = useCallback(({ target }: MouseEvent) => { | ||
if (!modalRef.current?.contains(target as Node)) { | ||
clickRef.current = true; | ||
} | ||
}, []); | ||
const onMouseUp = useCallback( | ||
({ target }: MouseEvent) => { | ||
if (clickRef.current && !modalRef.current?.contains(target as Node)) { | ||
closeHandler(); | ||
} | ||
clickRef.current = false; | ||
}, | ||
[closeHandler] | ||
); | ||
|
||
useDisableScrollEffect(isOpen); | ||
useKeyboardOperationEffect({ | ||
onEnter: isOpen ? onEnter : undefined, | ||
onEscape: isOpen ? closeHandler : undefined, | ||
}); | ||
useEffect(() => { | ||
setDisplay(shouldMount && isOpen); | ||
}, [shouldMount, isOpen]); | ||
|
||
const styleBgd = { | ||
position: "fixed" as any, | ||
display: "flex", | ||
top: "0px", | ||
left: "0px", | ||
width: "100%", | ||
height: "calc(100% + 1px)", // useDisableScrollEffect 로 감소된 1px을 보정 | ||
zIndex: isAlert ? theme.zIndex_alert : theme.zIndex_modal, | ||
background: isAlert ? theme.black_40 : theme.black_60, | ||
opacity: display ? 1 : 0, | ||
transition: `opacity ${theme.duration} ease-in-out`, | ||
pointerEvents: (isOpen ? "auto" : "none") as any, | ||
}; | ||
const styleBody = { | ||
position: "relative" as any, | ||
background: theme.white, | ||
borderRadius: "15px", | ||
padding: padding, | ||
minHeight: "148px", | ||
maxHeight: "720px", | ||
display: "flex", | ||
flexDirection: "column" as any, | ||
boxSizing: "border-box" as any, | ||
}; | ||
const styleBtn: CSS = useMemo( | ||
() => ({ | ||
color: theme.gray_text, | ||
position: "absolute", | ||
top: "10px", | ||
right: "10px", | ||
fontSize: "24px", | ||
cursor: "pointer", | ||
}), | ||
[] | ||
); | ||
|
||
if (!shouldMount) return null; | ||
return ( | ||
<div css={styleBgd} onMouseDown={onMouseDown} onMouseUp={onMouseUp}> | ||
<RLayout.Popup width={parseFloat(width.replace("px", ""))}> | ||
<div ref={modalRef} css={styleBody}> | ||
{children} | ||
{displayCloseBtn && ( | ||
<CloseRoundedIcon style={styleBtn} onClick={closeHandler} /> | ||
)} | ||
</div> | ||
</RLayout.Popup> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ModalElem; |
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,11 @@ | ||
import ModalElem from "./ModalElem"; | ||
|
||
import modalsAtom from "atoms/modals"; | ||
import { useRecoilValue } from "recoil"; | ||
|
||
const ModalProvider = () => { | ||
const modals = useRecoilValue(modalsAtom); | ||
return modals.map(({ id, props }) => <ModalElem key={id} {...props} />); | ||
}; | ||
|
||
export default ModalProvider; |
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,28 @@ | ||
import { useEffect } from "react"; | ||
|
||
import useDateToken from "hooks/useDateToken"; | ||
|
||
import { ModalElemProps } from "./ModalElem"; | ||
|
||
import modalsAtom from "atoms/modals"; | ||
import { useSetRecoilState } from "recoil"; | ||
|
||
const Modal = (props: ModalElemProps) => { | ||
const [id] = useDateToken(); | ||
const setModals = useSetRecoilState(modalsAtom); | ||
|
||
useEffect(() => { | ||
setModals((prev) => [...prev, { id, props }]); | ||
return () => setModals((prev) => prev.filter((modal) => modal.id !== id)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setModals((prev) => | ||
prev.map((modal) => (modal.id === id ? { id, props } : modal)) | ||
); | ||
}, Object.values(props)); | ||
|
||
return null; | ||
}; | ||
|
||
export default Modal; |
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
Oops, something went wrong.