brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: closedby="any" lets people close a dialog by clicking outside it

Web-Development
~/tips/tip-dialog-closedby-any-light-dismiss

cat ~/tips/tip-dialog-closedby-any-light-dismiss.md

A modal closes on a backdrop click or Escape without a custom click handler.

Users expect a modal to close when they click the dimmed area around it. The dialog element did not do that, so everyone wrote a handler that checked if the click landed on the backdrop. The closedby attribute makes that built in.

<dialog id="settings" closedby="any">
  <h2>Display settings</h2>
  <button onclick="this.closest('dialog').close()">Done</button>
</dialog>

There are three values. any allows light dismiss (a click or tap outside the dialog), the Escape key or a close request like the Android back gesture, and your own close button. closerequest allows Escape and close requests but not clicks outside. none means only your own code can close it. MDN notes that without a valid value, a dialog opened with showModal() behaves like closerequest and a non modal one like none.

I would use any for settings and pickers, and keep the default for dialogs where a stray click should not throw away what someone typed.

Support: Chrome 134 and Firefox 141 have it. MDN lists Safari only from 27.3, which is still in beta, so Safari users currently get the old behavior and the dialog stays open on a backdrop click. That is harmless as long as there is a visible close button, which you want anyway.

Further reading: MDN: The dialog element

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link