-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tidy up addEventListener implementation * shut up prettier * restore array behavior * add jsdoc * Create olive-rivers-try.md --------- Co-authored-by: Thomas G. Lopes <[email protected]>
- Loading branch information
Showing
3 changed files
with
75 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"runed": patch | ||
--- | ||
|
||
Fix addEventListener overloads |
34 changes: 28 additions & 6 deletions
34
packages/runed/src/lib/functions/useEventListener/useEventListener.svelte.ts
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,58 +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(s) for the given event(s), and returns a function to remove it. | ||
* @param target The target element(s) 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 from the target element(s). | ||
*/ | ||
export function addEventListener( | ||
target: Window | Document | EventTarget, | ||
target: EventTarget, | ||
event: string | string[], | ||
handler: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
) { | ||
): VoidFunction { | ||
const events = Array.isArray(event) ? event : [event]; | ||
|
||
// Add the event listener to each specified event for the target element(s). | ||
events.forEach((_event) => target.addEventListener(_event, handler, options)); | ||
for (const event of events) { | ||
target.addEventListener(event, handler, options); | ||
} | ||
|
||
// Return a function that removes the event listener from the target element(s). | ||
return () => { | ||
events.forEach((_event) => target.removeEventListener(_event, handler, options)); | ||
for (const event of events) { | ||
target.removeEventListener(event, handler, options); | ||
} | ||
}; | ||
} |