๋ฐ์ํ
Intro
๋ฆฌ์กํธ์์ Modal ํ์ ์ ๋ท๋ฐฐ๊ฒฝ์ด ์คํฌ๋กค ๋์ง ์๊ฒ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์๊ฐํ๊ณ ์ ํฉ๋๋ค.
How to do
import React, { useEffect } from 'react';
export const Modal = (props) => {
// ๋ชจ๋ฌ ์ค๋ฒ๋ ์ด์์ ์คํฌ๋กค ๋ฐฉ์ง
useEffect(() => {
document.body.style.cssText = `
position: fixed;
top: -${window.scrollY}px;
overflow-y: scroll;
width: 100%;`;
return () => {
const scrollY = document.body.style.top;
document.body.style.cssText = '';
window.scrollTo(0, parseInt(scrollY || '0', 10) * -1);
};
}, []);
...
};
useEffect๋ฅผ ์ฌ์ฉํ์ฌ Modal ํ์ ๋ ์ํ๋ผ๋ฉด ๋ค์์ CSS๋ฅผ ์ ์ฉ์ํค๊ณ , ๊ทธ๊ฒ ์๋๋ผ๋ฉด CSS๋ฅผ reset ์ํค๋ฉด ๋ฉ๋๋ค.
Reference
http://yoonbumtae.com/?p=3776
๋ฐ์ํ