์ฐธ๊ณ
https://react.dev/reference/react-dom/createPortal
createPortal – React
The library for web and native user interfaces
react.dev
https://abangpa1ace.tistory.com/entry/React-Portal-%ED%8F%AC%ED%83%88
[React] Portal (ํฌํ), Modal ๊ตฌํํ๊ธฐ
ํ์ฌ์์ ์ ๋ฐฐ์ React๋ก ๋ชจ๋ฌ ๊ตฌํ์ ๋ํด ๊ณ ๋ฏผํ๋ค๊ฐ ์๊ฒ๋ ๋ด์ฉ์ด์๋ค. ์ฐ๋ฆฌ ํ์ฌ ํ๋ก์ ํธ๋ Vue๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, ํ๋ฌ๊ทธ์ธ(this.$modal) ๋ฉ์๋๋ฅผ ํตํด ๋ฃจํธ์ ๋ชจ๋ฌ ์ปดํฌ๋ํธ๋ฅผ ์ฃผ์ , ์ญ์ ํด
abangpa1ace.tistory.com
https://dev-bomdong.tistory.com/21
React | Portal์ ์ด์ฉํ Modal ๊ตฌํ
ํ๊ตญ์ธ์๊ฒ portal์ด๋.. ๋ฅํฐ์คํธ๋ ์ธ์ง์ ๋ง๋ฒ์ง๊ฐ์ ํฌํ๋ง ๋ ์ค๋ฅผ ์ ์๋ค. ์ด๋ฏธ ์ถฉ๋ถํ ์ต์ํ ๋ค๋ฅธ ๊ณณ์ผ๋ก ์ด๋์ํจ๋ค๋ ๊ฐ๋ ์ ์ด์ด๋ฐ์, ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ชจ ์ปดํฌ๋ํธ์ ๋ฐ๊นฅ์ ๋ ๋๋งํด์ฃผ
dev-bomdong.tistory.com
https://jeonghwan-kim.github.io/2022/06/02/react-portal
๋ฆฌ์กํธ Portal
…
jeonghwan-kim.github.io
ํฌํ ์ฌ์ฉ ์ด์
ํฌํ์ ์ฌ์ฉํ๋ฉด ๋ถ๋ชจ/์์ ๊ด๊ณ๋ ์ ์งํ์ง๋ง ๋ ๋ฆฝ์ ์ธ ์์น์์ ์์ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ค.
→ CSS ์์ ๊ตฌ์กฐ์์ ๋ฒ์ด๋ ์ ์์
์ง์ ๊ตฌํํ๋ฉด์ ์ข๋ค๊ณ ๋๋ ์ ์ z-index์์ ์์ ๋ก์ธ ์ ์๋ค๋ ์ ์ด์๋ค.
ํ ์คํฌ๋ฆฐ ๋ชจ๋ฌ์ผ ๊ฒฝ์ฐ z-index๋ฅผ ์ถ๊ฐํ์ง ์์๋ ๋๊ณ ,
dimmed ๋ฐฐ๊ฒฝ์ด ๊น๋ฆด ๊ฒฝ์ฐ dimmed ๋ฐฐ๊ฒฝ์์๋ง ๊ณ ๋ คํด์ z-index๋ฅผ ์ค์ ํ๋ฉด ๋์ด์ ์ข์๋ค.
ํฌํ ์ฌ์ฉํด์ ๋ชจ๋ฌ ๊ตฌํํ๊ธฐ
1. div ํ๊ทธ ์ถ๊ฐ
2. ModalPotal ์ปดํฌ๋ํธ ์ถ๊ฐ
createPortal(๋ ๋๋ง ์์, ํ๊ฒ๋๋ ๋ถ๋ชจ์์)
import { createPortal } from 'react-dom'
const ModalPotal = ({ children }: { children: React.ReactElement }) => {
const modal = document.getElementById('modal')!
return createPortal(children, modal)
}
export default ModalPotal
3. Modal ์ถ๊ฐํ๊ธฐ
๋ชจ๋ฌ ์ปดํฌ๋ํธ๋ฅผ ๊ตฌํํ ๋ค ๋ง๋ค์ด๋ ModalPotal์ ๊ฐ์ธ๋ฉด ์๋ฃ
div ํ๊ทธ ์์์์๋ก ์ ๋ ๋๋ง๋๋ค.