Skip to content

Commit

Permalink
(#0) 모달 오픈 시 스크롤 방지
Browse files Browse the repository at this point in the history
  • Loading branch information
baegofda committed Dec 6, 2023
1 parent 7d306bc commit 5b74be0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/core/components/Modal/ModalPopUp/index.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import clsx from "clsx";
import { PropsWithChildren, forwardRef } from "react";

import { useBlockScrollingEffect } from "@/hooks/effects/useBlockScrollingEffect";
import Section from "../../Section";
import ModalBase from "../ModalBase";
import { ModalPopUpProps } from "./types";

const ModalPopUp = forwardRef((
{
isOpen,
children,
...props
}: PropsWithChildren<ModalPopUpProps>,
ref: React.Ref<HTMLDialogElement>,
) => {
const { target, className, ...rest } = props;

useBlockScrollingEffect(isOpen);

return (
<ModalBase
target = {target ?? "modal"}
ref = {ref}
variants = "modal"
isOpen = {isOpen}
{...rest}
>
<Section
Expand Down
13 changes: 13 additions & 0 deletions src/hooks/effects/useBlockScrollingEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useEffect } from "react";

export const useBlockScrollingEffect = (deps: boolean) => {
useEffect(() => {
if (deps) {
document.body.classList.add("overflow-hidden");
} else {
document.body.classList.remove("overflow-hidden");
}

return () => document.body.classList.remove("overflow-hidden");
}, [deps]);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RefObject, useEffect } from "react";

const useEffectClickOutSide = (ref: RefObject<Element>, onClose?: () => void) => {
const useClickOutSideEffect = (ref: RefObject<Element>, onClose?: () => void) => {
useEffect(() => {
const onClickOutSide = (e: MouseEvent) => {
if (!ref.current || ref.current.contains(e.target as Node)) return;
Expand All @@ -14,4 +14,4 @@ const useEffectClickOutSide = (ref: RefObject<Element>, onClose?: () => void) =>
}, [ref.current]);
};

export default useEffectClickOutSide;
export default useClickOutSideEffect;
4 changes: 2 additions & 2 deletions src/hooks/useClickOutSide.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useRef } from "react";
import useEffectClickOutSide from "./useEffectClickOutSide";
import useClickOutSideEffect from "./effects/useClickOutSideEffect";

const useClickOutside = <T extends Element>(onClose?: () => void) => {
const contentRef = useRef<T | null>(null);

useEffectClickOutSide(contentRef, onClose);
useClickOutSideEffect(contentRef, onClose);

return { contentRef };
};
Expand Down

0 comments on commit 5b74be0

Please sign in to comment.