<button id="button">버튼</button>
<dialog class="dialog">
<button id="close" type="button">close</button>
</dialog>
const button = document.querySelector("#button");
const dialog = document.querySelector(".dialog");
button.addEventListener("click", () => {
dialog.showModal();
});
//close 버튼 클릭시 모달 닫기
const close = document.querySelector("#close");
close.addEventListener("click", () => {
dialog.close();
});
//모달 바깥 클릭시 모달 닫기
dialog.addEventListener("click", (event) => {
if (event.target.nodeName === "DIALOG") {
dialog.close();
}
});
https://developer.mozilla.org/ko/docs/Web/HTML/Element/dialog
<dialog>: 대화 상자 요소 - HTML: Hypertext Markup Language | MDN
HTML <dialog> 요소는 닫을 수 있는 경고, 검사기, 창 등 대화 상자 및 기타 다른 상호작용 가능한 컴포넌트를 나타냅니다.
developer.mozilla.org
https://ui.toast.com/posts/ko_20220518
dialog 요소가 가진 슈퍼 파워 알아보기
오래 전, 2014년에 dialog 요소가 Chromium 37에 착륙했다. 이 아이디어는 모달, 다이얼로그와 팝업을 쉽게 생성하도록 하는 것이다.
ui.toast.com