I didn't find any built in solution or workaround for closing the html5 element by clicking on its background(::backdrop), although it's clearly a basic functionality.
If you use click it will close the dialog even if you drag from inside to outside the dialog, which can be annoying. You can use pointerdown and pointerup to detect outside clicks more accurately.
The <dialog> tag defines a dialog box or subwindow. The <dialog> element makes it easy to create popup dialogs and modals on a web page.
Backdrop clicks can be detected using the dialog bounding rect.
var dialog = document.getElementByTagName('dialog'); dialog.showModal(); dialog.addEventListener('click', function (event) { var rect = dialog.getBoundingClientRect(); var isInDialog=(rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width); if (!isInDialog) { dialog.close(); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With