Event listeners
dompack improves on the standard addEventListener APIs by providing a stricter typed API if you're binding to (and only using) HTMLElements.
By setting event.target
to a stricter type than EventTarget your code doesn't need to work around various "'target' is possibly 'null'" and "Propery 'closest' does not exist on type 'EventTarget'"
errors. We do this by proving a DocEvent<>
wrapper for Event
types that redefines (current)target and adding a addDocEventListener
API to register these handlers.
eg
this.node.addEventListener("click", evt => this._checkClick(evt));
_checkClick(evt: MouseEvent) { .. };
should become
addDocEventListener(this.node, "click", evt => this._checkClick(evt));
_checkClick(evt: DocEvent<MouseEvent>) { .. };
addDocEventListener
also adds a listenerSet
argument which you can point to a EventListenerSet
which allows you to remove all associated listeners in one call.