registerEventListener
functionAdd an event listener to `target` and return a function that removes it. This is a thin, strongly typed wrapper around EventTarget.addEventListener that mirrors its overloads but returns a dispose function instead of `void`. It removes the `addEventListener`/`removeEventListener` boilerplate that every DOM subscription would otherwise duplicate, and composes cleanly with mergeRegister or as the return value of an effect. The same `options` value is forwarded to both `addEventListener` and `removeEventListener` so that the `capture` flag always matches, which is required for the listener to be removed correctly.
Import
import { registerEventListener } from '@modular/sdk/editor';Notes
- example
- ```ts // Returned directly from a React effect useEffect( () => registerEventListener(container, 'keydown', handler), [container], ); ```
- example
- ```ts // Composed with other teardown via mergeRegister return mergeRegister( registerEventListener(window, 'resize', onResize), registerEventListener(document, 'selectionchange', onSelectionChange), ); ```
- returns
- A function that removes the listener when called