λ°μν
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
λ°μν