diff --git a/.changeset/big-pets-nail.md b/.changeset/big-pets-nail.md new file mode 100644 index 00000000..fc786357 --- /dev/null +++ b/.changeset/big-pets-nail.md @@ -0,0 +1,5 @@ +--- +"runed": patch +--- + +allow undefined | null in useEventListener diff --git a/packages/runed/src/lib/functions/useEventListener/useEventListener.svelte.ts b/packages/runed/src/lib/functions/useEventListener/useEventListener.svelte.ts index 36150e53..45143c4e 100644 --- a/packages/runed/src/lib/functions/useEventListener/useEventListener.svelte.ts +++ b/packages/runed/src/lib/functions/useEventListener/useEventListener.svelte.ts @@ -3,14 +3,14 @@ import { boxed } from "$lib/internal/utils/boxed.svelte.js"; import { addEventListener } from "$lib/internal/utils/event.js"; export function useEventListener( - target: ValueOrGetter, + target: ValueOrGetter, event: TEvent, handler: (this: Window, event: WindowEventMap[TEvent]) => unknown, options?: boolean | AddEventListenerOptions ): void; export function useEventListener( - target: ValueOrGetter, + target: ValueOrGetter, event: TEvent, handler: (this: Document, event: DocumentEventMap[TEvent]) => unknown, options?: boolean | AddEventListenerOptions @@ -20,26 +20,29 @@ export function useEventListener< TElement extends HTMLElement, TEvent extends keyof HTMLElementEventMap, >( - target: ValueOrGetter, + target: ValueOrGetter, event: TEvent, handler: (this: TElement, event: HTMLElementEventMap[TEvent]) => unknown, options?: boolean | AddEventListenerOptions ): void; export function useEventListener( - target: ValueOrGetter, + target: ValueOrGetter, event: string, handler: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions ): void; export function useEventListener( - _target: ValueOrGetter, + _target: ValueOrGetter, event: string, handler: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions ) { const target = boxed(_target); - $effect(() => addEventListener(target.value, event, handler, options)); + $effect(() => { + if (target.value === undefined || target.value === null) return; + return addEventListener(target.value, event, handler, options); + }); } diff --git a/sites/docs/content/functions/use-active-element.md b/sites/docs/content/functions/use-active-element.md index 133e0c4d..9970278c 100644 --- a/sites/docs/content/functions/use-active-element.md +++ b/sites/docs/content/functions/use-active-element.md @@ -1,5 +1,5 @@ --- -title: UseActiveElement +title: useActiveElement description: A function that returns the currently active element. --- diff --git a/sites/docs/content/functions/use-event-listener.md b/sites/docs/content/functions/use-event-listener.md index aea64e15..87750006 100644 --- a/sites/docs/content/functions/use-event-listener.md +++ b/sites/docs/content/functions/use-event-listener.md @@ -1,5 +1,5 @@ --- -title: UseEventListener +title: useEventListener description: A function that attaches an automatically disposed event listener. --- @@ -22,8 +22,11 @@ import { UseEventListenerDemo } from '$lib/components/demos'; count++; } - useEventListener(() => document, "click", increment); + let wrapper = $state(); + useEventListener(() => wrapper, "click", increment); -

You've clicked {count} {count === 1 ? "Time" : "Times"}

+
+

You've clicked {count} {count === 1 ? "time" : "times"}

+
``` diff --git a/sites/docs/src/lib/components/demos/use-event-listener.svelte b/sites/docs/src/lib/components/demos/use-event-listener.svelte index 9bc5543c..20fe7469 100644 --- a/sites/docs/src/lib/components/demos/use-event-listener.svelte +++ b/sites/docs/src/lib/components/demos/use-event-listener.svelte @@ -6,9 +6,10 @@ count++; } - useEventListener(() => document, "click", increment); + let wrapper = $state(); + useEventListener(() => wrapper, "click", increment); -
-

You've clicked {count} {count === 1 ? "Time" : "Times"}

+
+

You've clicked {count} {count === 1 ? "time" : "times"}