Hacker News new | ask | show | jobs
by ggregoire 921 days ago
> CloseWatcher API

> Chrome 120 brings the solution with CloseWatcher, a new API for directly listening and responding to close requests.

> watcher.addEventListener("close", () => document.querySelector("#sidebar").classList.remove("open"));

Couldn't they add an HTML attribute onCloseRequest?

Slightly related, I wish there was an attribute "onClickOutside" so we could close modals and dropdown menus without manually adding an event listener on the window and checking if the click wasn't inside the element.

1 comments

The issue with a single global event handler is discussed here: https://github.com/WICG/close-watcher#a-single-event

If you use popover="", you get the kind of functionality you're discussing for free. For <dialog>, the discussion is in progress and reaching a conclusion: https://github.com/whatwg/html/issues/9373