Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/11/modal component #22

Merged
merged 15 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</head>
<body>
<div id="root"></div>
<div id="modal-root"></div>
<script type="module" src="/src/app.tsx"></script>
</body>
</html>
16 changes: 12 additions & 4 deletions src/app/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
import { useAtom } from "jotai";

import { messageAtom } from "@/store/messageAtom.tsx";
import Modal from "@/component/common/modal/Modal";
import useModal from "@/hooks/useModal";

function MainPage() {
const [message] = useAtom(messageAtom);
const { open, close, isOpen } = useModal();
return (
<div>
<span>welcome to layer ๐ŸŽ‡</span>
<div>{message}</div>
</div>
<>
<div>
<span>welcome to layer ๐ŸŽ‡</span>
<div onClick={open}>{message}</div>
</div>
<Modal onClose={close} isModalOpen={isOpen}>
<div>๋ƒ ๋ƒ </div>
</Modal>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<Modal onClose={close} isModalOpen={isOpen}>
<div>๋ƒ ๋ƒ </div>
</Modal>
{isOpen &&
<Modal onClose={close}>
<div>๋ƒ ๋ƒ </div>
</Modal>
}

์š”๋ ‡๊ฒŒ ํ•˜๊ณ  Modal ์•ˆ์—์„œ ์Šคํฌ๋กค ๋ฐฉ์ง€ํ•˜๋Š” ๋กœ์ง์„ useModal ํ›…์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด isModalOpen props๊ฐ€ ์—†์–ด์ ธ๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prop๋กœ ๋„˜๊ธฐ๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™์•„์š”~!! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค:)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isOpen์„ ๊ณ„์† ์จ์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค~!!

</>
);
}

Expand Down
69 changes: 69 additions & 0 deletions src/component/common/modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import ModalPortal from "@/component/common/modal/ModalPortal";
import { css } from "@emotion/react";
import { PropsWithChildren, useEffect, useRef } from "react";

type Props = PropsWithChildren<{
isModalOpen: boolean;
onClose: () => void;
}>;

function Modal({ onClose, isModalOpen, children }: Props) {
const modalRef = useRef<HTMLDivElement>(null);

// ๋ชจ๋‹ฌ ์˜คํ”ˆ ์‹œ ์Šคํฌ๋กค ๋ฐฉ์ง€
useEffect(() => {
document.body.style.overflow = isModalOpen ? "hidden" : "auto";
}, [isModalOpen]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋‚ด์šฉ์„ useModal ํ›… ์•ˆ์—์„œ isModalOpen ๋Œ€์‹  isOpen์œผ๋กœ ๋Œ€์ฒดํ•ด์„œ ์˜ฎ๊ธฐ๋ฉด ์–ด๋–จ๊นŒ์š” ??


// ๋ชจ๋‹ฌ ์˜์—ญ ์ด์™ธ ํด๋ฆญ ์‹œ ๋ชจ๋‹ฌ ๋‹ซ๊ธฐ
useEffect(() => {
const listener = (e: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
onClose();
}
};
document.addEventListener("mousedown", listener);
return () => {
document.removeEventListener("mousedown", listener);
};
}, [onClose, modalRef]);

if (!isModalOpen) return null;

return (
// FIXME: ์ถ”ํ›„ ๋””์ž์ธ ํ† ํฐ ์—ฐ๋™ ํ›„ ์ปฌ๋Ÿฌ ๊ฐ’ ๋ณ€๊ฒฝ
<ModalPortal>
<div
css={css`
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
display: flex;
justify-content: center;
align-items: center;
`}
>
<div
ref={modalRef}
css={css`
padding: 2rem;
min-width: 30rem;
min-height: 10rem;
background-color: #ffffff;
border: 1px solid #cbcbcb;
border-radius: 1rem;
`}
>
{children}
</div>
</div>
</ModalPortal>
);
}

export default Modal;
13 changes: 13 additions & 0 deletions src/component/common/modal/ModalPortal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ReactNode } from "react";
import ReactDom from "react-dom";

type Props = {
children: ReactNode;
};

const ModalPortal = ({ children }: Props) => {
const el = document.getElementById("modal-root") as HTMLElement;
return ReactDom.createPortal(children, el);
};

export default ModalPortal;
21 changes: 21 additions & 0 deletions src/hooks/useModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useCallback, useState } from "react";

const useModal = () => {
const [isOpen, setIsOpen] = useState(false);

const open = useCallback(() => {
setIsOpen(true);
}, []);

const close = useCallback(() => {
setIsOpen(false);
}, []);

return {
open,
close,
isOpen,
};
};

export default useModal;
Loading