HTML Dialog Modal Accessible

Use the dialog element for accessible modals with focus trapping and close on backdrop.

HtmlForm Validation

Code

snippet
<dialog id="modal">
  <form method="dialog">
    <p>Are you sure?</p>
    <button value="cancel">Cancel</button>
    <button value="confirm">Confirm</button>
  </form>
</dialog>
<button onclick="modal.showModal()">Open</button>
<script>
  modal.addEventListener('close', () => {
    console.log(modal.returnValue);
  });
  modal.addEventListener('click', (e) => {
    if (e.target === modal) modal.close();
  });
</script>