Utility function to register multiple event listeners
In other words, utility function that multiple execute addEventListener/removeEventListener
with yarn
yarn add register-event-listeners
or
with npm
npm install register-event-listeners
import { registerEventListeners } from 'register-event-listeners'
const eventTarget = document.body
const onTouchStart = (event) => console.log(`${event.type} event dispatched.`)
const onTouchMove = (event) => console.log(`${event.type} event dispatched.`)
const onEnd = (event) => console.log(`${event.type} event dispatched.`)
const { register, unRegister } = registerEventListeners(eventTarget, [
['touchstart', onTouchStart],
['touchmove', onTouchMove, { passive: false }],
['touchend', onEnd],
['touchcancel', onEnd]
])
// Execute element.addEventListener(s) passed to the argument.
register()
// Now added event listeners. 🔈
// Execute element.removeEventListener(s) passed to the argument.
unRegister()
// Now removed event listeners. 🔇
name | require | type | default | decstiption |
---|---|---|---|---|
eventTarget | ✓ | EventTarget | - | MDN - EventTarget |
listeners | ✓ | Array(EventListeners) | - | MDN - addEventListener |
Element is same as the format of the argument of addEventListener
That is this 👉 [type, listener[, options])]
type
is event.type.listener
is commonly called a handleroptions
is listenerOptions
[
['touchstart', onTouchStart, {capture: true, once: true}],
['touchmove', onTouchMove, { passive: false }],
['touchend', onEnd],
['touchcancel', onEnd]
]
register event listeners. In other words addEventListeners.
unRegister event listeners. In other words removeEventListeners.
with Jest.
yarn test
or
npm run test
Use SemVer for versioning. For the versions available, see the tags on this repository.
MIT © 1natsu172