Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the new html <dialog> tag by clicking on its ::backdrop

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.

like image 743
mantramantramantra Avatar asked Sep 16 '14 08:09

mantramantramantra


People also ask

How to close dialog tag HTML?

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.

What is dialog in HTML?

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.


1 Answers

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();     } }); 
like image 80
Seralo Avatar answered Sep 20 '22 19:09

Seralo