Skip to content

Commit 306ff32

Browse files
committed
design: 모달 활성화 시, 스크롤이 안되도록 수정
1 parent a7bb63a commit 306ff32

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/components/ModalOverlay.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
import { useEffect } from 'react';
2+
3+
import { getScrollbarWidth } from '@/utils/getScrollbarWidth';
4+
15
interface ModalOverlayProps {
26
closeOnOutsideClick?: boolean;
3-
children: React.ReactElement;
7+
children: React.ReactNode;
48
onClose?: () => void;
59
}
610

711
const ModalOverlay = ({ closeOnOutsideClick = false, children, onClose }: ModalOverlayProps) => {
12+
useEffect(() => {
13+
const scrollbarWidth = getScrollbarWidth();
14+
const header = document.querySelector('header');
15+
16+
document.documentElement.style.setProperty('--scrollbar-width', `${scrollbarWidth}px`);
17+
document.body.classList.add('modal-open');
18+
if (header) header.classList.add('modal-open');
19+
20+
return () => {
21+
document.documentElement.style.setProperty('--scrollbar-width', '0px');
22+
document.body.classList.remove('modal-open');
23+
};
24+
}, []);
25+
826
const handleClickOutside = () => {
927
if (closeOnOutsideClick && onClose) {
1028
onClose();

src/styles/utilities.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
/* Tailwind에서 기본적으로 제공하지 않는 추가 유틀리티 클래스 정의 */
2-
/* import congratThemeFrame from '@/assets/images/congrat-theme-frame.png';
3-
import fieldThemeFrame from '@/assets/images/field-theme-frame.png';
4-
import skyThemeFrame from '@/assets/images/sky-theme-frame.png';
5-
import vintageThemeFrame from '@/assets/images/vintage-theme-frame.png'; */
62

73
@layer utilities {
84
/* 편지지 줄 css */
@@ -63,4 +59,9 @@ import vintageThemeFrame from '@/assets/images/vintage-theme-frame.png'; */
6359
right: 8%; /* 수평 중앙 정렬 */
6460
transform: translateX(-50%);
6561
}
62+
63+
.modal-open {
64+
overflow: hidden;
65+
padding-right: var(--scrollbar-width, 0px);
66+
}
6667
}

src/utils/getScrollbarWidth.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let scrollbarWidth: number | undefined;
2+
3+
export const getScrollbarWidth = () => {
4+
if (scrollbarWidth !== undefined) return scrollbarWidth;
5+
6+
const container = document.createElement('div');
7+
8+
document.body.appendChild(container);
9+
container.style.overflow = 'scroll';
10+
11+
const inner = document.createElement('div');
12+
container.appendChild(inner);
13+
14+
scrollbarWidth = container.offsetWidth - inner.offsetWidth;
15+
document.body.removeChild(container);
16+
17+
return scrollbarWidth;
18+
};

0 commit comments

Comments
 (0)