Skip to content

Commit

Permalink
restore array behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
abdel-17 committed Apr 26, 2024
1 parent 8560d5d commit a389077
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { addEventListener } from "$lib/internal/utils/event.js";

export function useEventListener<TEvent extends keyof WindowEventMap>(
target: MaybeBoxOrGetter<Window | null | undefined>,
event: TEvent,
event: TEvent | TEvent[],
handler: (this: Window, event: WindowEventMap[TEvent]) => unknown,
options?: boolean | AddEventListenerOptions
): void;

export function useEventListener<TEvent extends keyof DocumentEventMap>(
target: MaybeBoxOrGetter<Document | null | undefined>,
event: TEvent,
event: TEvent | TEvent[],
handler: (this: Document, event: DocumentEventMap[TEvent]) => unknown,
options?: boolean | AddEventListenerOptions
): void;
Expand All @@ -21,21 +21,21 @@ export function useEventListener<
TEvent extends keyof HTMLElementEventMap,
>(
target: MaybeBoxOrGetter<TElement | null | undefined>,
event: TEvent,
event: TEvent | TEvent[],
handler: (this: TElement, event: HTMLElementEventMap[TEvent]) => unknown,
options?: boolean | AddEventListenerOptions
): void;

export function useEventListener(
target: MaybeBoxOrGetter<EventTarget | null | undefined>,
event: string,
event: string | string[],
handler: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
): void;

export function useEventListener(
_target: MaybeBoxOrGetter<EventTarget | null | undefined>,
event: string,
event: string | string[],
handler: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
) {
Expand Down
61 changes: 43 additions & 18 deletions packages/runed/src/lib/internal/utils/event.ts
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);
}
};
}

0 comments on commit a389077

Please sign in to comment.