-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,81 @@ | ||
/** | ||
* Overloaded function signatures for addEventListener | ||
* Adds an event listener to the specified target element for the given event(s), and returns a function to remove it. | ||
* @param target The target element to add the event listener to. | ||
* @param event The event(s) to listen for. | ||
* @param handler The function to be called when the event is triggered. | ||
* @param options An optional object that specifies characteristics about the event listener. | ||
* @returns A function that removes the event listener(s) from the target element. | ||
*/ | ||
export function addEventListener<TEvent extends keyof WindowEventMap>( | ||
target: Window, | ||
event: TEvent, | ||
event: TEvent | TEvent[], | ||
handler: (this: Window, event: WindowEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
/** | ||
* Adds an event listener to the specified target element for the given event(s), and returns a function to remove it. | ||
* @param target The target element to add the event listener to. | ||
* @param event The event(s) to listen for. | ||
* @param handler The function to be called when the event is triggered. | ||
* @param options An optional object that specifies characteristics about the event listener. | ||
* @returns A function that removes the event listener(s) from the target element. | ||
*/ | ||
export function addEventListener<TEvent extends keyof DocumentEventMap>( | ||
target: Document, | ||
event: TEvent, | ||
event: TEvent | TEvent[], | ||
handler: (this: Document, event: DocumentEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
/** | ||
* Adds an event listener to the specified target element for the given event(s), and returns a function to remove it. | ||
* @param target The target element to add the event listener to. | ||
* @param event The event(s) to listen for. | ||
* @param handler The function to be called when the event is triggered. | ||
* @param options An optional object that specifies characteristics about the event listener. | ||
* @returns A function that removes the event listener(s) from the target element. | ||
*/ | ||
export function addEventListener< | ||
TElement extends HTMLElement, | ||
TEvent extends keyof HTMLElementEventMap, | ||
>( | ||
target: TElement, | ||
event: TEvent, | ||
event: TEvent | TEvent[], | ||
handler: (this: TElement, event: HTMLElementEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
/** | ||
* Adds an event listener to the specified target element for the given event(s), and returns a function to remove it. | ||
* @param target The target element to add the event listener to. | ||
* @param event The event(s) to listen for. | ||
* @param handler The function to be called when the event is triggered. | ||
* @param options An optional object that specifies characteristics about the event listener. | ||
* @returns A function that removes the event listener(s) from the target element. | ||
*/ | ||
export function addEventListener( | ||
target: EventTarget, | ||
event: string, | ||
event: string | string[], | ||
handler: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
/** | ||
* Adds an event listener to the specified target element for the given event, and returns a function to remove it. | ||
* @param target The target element to add the event listener to. | ||
* @param event The event to listen for. | ||
* @param handler The function to be called when the event is triggered. | ||
* @param options An optional object that specifies characteristics about the event listener. | ||
* @returns A function that removes the event listener from the target element. | ||
*/ | ||
export function addEventListener( | ||
target: EventTarget, | ||
event: string, | ||
event: string | string[], | ||
handler: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
) { | ||
// Add the event listener to each specified event for the target element. | ||
target.addEventListener(event, handler, options); | ||
const events = Array.isArray(event) ? event : [event]; | ||
|
||
for (const event of events) { | ||
target.addEventListener(event, handler, options); | ||
} | ||
|
||
// Return a function that removes the event listener from the target element(s). | ||
return () => { | ||
target.removeEventListener(event, handler, options); | ||
for (const event of events) { | ||
target.removeEventListener(event, handler, options); | ||
} | ||
}; | ||
} |