9. dialog

๐Ÿ”—MDN dialog ์š”์†Œ

<dialog> ๋Š” ๋ชจ๋‹ฌ ํŒ์—… ์„ ํƒœ๊ทธ ํ•œ ์ค„๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” HTML ์š”์†Œ์ž…๋‹ˆ๋‹ค. ESC ํ‚ค๋กœ ์ฐฝ ๋‹ซ๊ธฐ, ํฌ์ปค์Šค ํŠธ๋žฉ, ๋ฐฐ๊ฒฝ ์–ด๋‘ก๊ฒŒ ์ฒ˜๋ฆฌ(::backdrop) ๋“ฑ์„ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ธฐ๋ณธ ์ œ๊ณต ํ•ฉ๋‹ˆ๋‹ค.

Info: โœ… 2026๋…„ ํ˜„์žฌ ๋ชจ๋“  ๋ฉ”์ด์ € ๋ธŒ๋ผ์šฐ์ €(Chrome / Edge / Firefox / Safari) ๊ฐ€ <dialog> ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค (Baseline).

1. ๊ธฐ๋ณธํ˜• ๊ตฌํ˜„

<dialog open>
  <p>์Šคํฌ๋ฆฝํŠธ ์•ˆ์จ๋„ ์ •๋ง ๋œ๋‹ค๊ตฌ?</p>
  <form method="dialog">
    <button>OK</button>
  </form>
</dialog>

์‹คํ–‰ํ™”๋ฉด
ok๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์ฐฝ์ด ๋‹ซํžŒ๋‹ค.

2. ์˜ค๋ฒ„๋ ˆ์ด ๋ฐฐ๊ฒฝ์ถ”๊ฐ€

<dialog>
  <button autofocus>Close</button>
  <p>์ด๋ฒˆ์—” ๋ฐฐ๊ฒฝ์ƒ‰ ๋„ฃ๊ธฐ</p>
</dialog>
<button>์–์–</button>
::backdrop {
  background-image: linear-gradient(
    45deg,
    magenta,
    rebeccapurple,
    dodgerblue,
    green
  );
  opacity: 0.75;
}

::backdrop ์„ ํƒ์ž๋Š” dialog ์š”์†Œ์˜ ๋ฐฐ๊ฒฝ์ƒ‰์„ ์„ ํƒํ• ์ˆ˜ ์žˆ๋‹ค.
์•„์ฃผ ์•ฝ๊ฐ„์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

const dialog = document.querySelector("dialog");
const showButton = document.querySelector("dialog + button");
const closeButton = document.querySelector("dialog button");

// "Show the dialog" button opens the dialog modally
showButton.addEventListener("click", () => {
  dialog.showModal();
});

// "Close" button closes the dialog
closeButton.addEventListener("click", () => {
  dialog.close();
});

showModal(), close() ๋Š” dialog API ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
์‹คํ–‰ํ™”๋ฉด

3. dialog API ์ •๋ฆฌ

๋ฉ”์„œ๋“œ / ์†์„ฑ ์„ค๋ช…
dialog.show() ๋น„๋ชจ๋‹ฌ ๋กœ ์—ด๊ธฐ (๋ฐฐ๊ฒฝ ํด๋ฆญ ๊ฐ€๋Šฅ, ํฌ์ปค์Šค ํŠธ๋žฉ ์—†์Œ)
dialog.showModal() ๋ชจ๋‹ฌ ๋กœ ์—ด๊ธฐ (๋ฐฐ๊ฒฝ ํด๋ฆญ ๋ง‰ํž˜, ํฌ์ปค์Šค ํŠธ๋žฉ, ESC ๋‹ซ๊ธฐ)
dialog.close(returnValue?) ๋‹ซ๊ธฐ. ํผ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ ๊ฐ€๋Šฅ
dialog.returnValue ๋งˆ์ง€๋ง‰ close() ์— ์ „๋‹ฌ๋œ ๊ฐ’
<form method="dialog"> ์•ˆ์˜ submit ๋ฒ„ํŠผ์ด dialog ๋ฅผ ๋‹ซ์Œ (์Šคํฌ๋ฆฝํŠธ ๋ถˆํ•„์š”)
::backdrop ์˜์‚ฌ ์š”์†Œ showModal() ์ผ ๋•Œ๋งŒ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฐฐ๊ฒฝ โ€” CSS ๋กœ ์Šคํƒ€์ผ๋ง

1. ์ด๋ฒคํŠธ

  • close โ€” dialog ๊ฐ€ ๋‹ซํž ๋•Œ
  • cancel โ€” ESC ํ‚ค๋กœ ๋‹ซํž ๋•Œ (์ด ๋‹จ๊ณ„์—์„œ preventDefault() ๋กœ ๋ง‰์„ ์ˆ˜ ์žˆ์Œ)

4. 2025๋…„ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ โ€” Light Dismiss

closedby ์†์„ฑ์ด ์ถ”๊ฐ€๋˜์–ด ๋ฐ”๊นฅ ์˜์—ญ ํด๋ฆญ์œผ๋กœ ๋‹ซ๊ธฐ ๋ฅผ ์„ ์–ธ์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<dialog closedby="any">โ€ฆ</dialog>
closedby ๊ฐ’ ๋™์ž‘
any ๋ฐ”๊นฅ ํด๋ฆญ / ESC / close() ๋ชจ๋‘๋กœ ๋‹ซํž˜
closerequest ESC ๋˜๋Š” close request ๋กœ๋งŒ ๋‹ซํž˜ (๊ธฐ๋ณธ ๋ชจ๋‹ฌ๊ณผ ์œ ์‚ฌ)
none ๋ช…์‹œ์  close() ํ˜ธ์ถœ๋กœ๋งŒ ๋‹ซํž˜

HTMLDialogElement.requestClose() ๋ฉ”์„œ๋“œ๋„ ํ•จ๊ป˜ ์ถ”๊ฐ€๋จ โ€” cancel ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉฐ ๋‹ซ๊ธฐ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

Info: โš ๏ธ closedby / requestClose() ๋Š” ๋น„๊ต์  ์‹ ๊ทœ ๊ธฐ๋Šฅ โ€” ์‚ฌ์šฉ ์ „ Can I Use ์—์„œ ๋Œ€์ƒ ๋ธŒ๋ผ์šฐ์ € ์ง€์› ํ™•์ธ ๊ถŒ์žฅ.

๋Œ“๊ธ€ ๋‚จ๊ธฐ๊ธฐ