[whatwg/dom] addEventListener defer option to call that handler after all other handlers run (#730)

I'm adding a key handler to a behaviour that a consumer adds to an element to virtualise scrolling.  The handler intercepts the keys that normally scroll the element and adjusts the scroll value in certain situations.  I can't guarantee that the consumer will not add their handler before the behaviour is attached, which means that, if the consumer adds a handler and wants to cancel the behaviour action then I can't easily know that.  You also don't know whether the consumer might use event delegation to handle events further up the tree, so you'd have to use some unpalatable options (event delegation at the root, overriding `addEventListener()` to remove and re-add yours whenever another handler is added for that type) to do your default action.

I'm proposing that an option, `defer`, that tells the browser to execute that handler after the bubbling phase has run.  This lets custom elements define their own default actions without having to use custom events:

```javascript
element.addEventListener('keydown', event => {
    if (event.defaultPrevented) {
        return;
    }
 
    event.preventDefault();
    // do the custom behaviour
}, { defer: true });
```

The handler always executes even if `event.stopPropagation()` or `event.stopImmediatePropagation()` was called. 

*Note: I'm thinking this is mostly useful for custom elements, in fact my behaviour would be implemented as a custom element if it weren't for it being too small to require the consumers use a web components polyfill. I could propose a more custom element-y solution over at the web components repo, but I feel this may be useful for devs who want to make lightweight behaviours for elements, hence suggesting here first.*

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/730

Received on Monday, 21 January 2019 10:11:02 UTC