Skip to content

Commit

Permalink
update stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TGlide committed Apr 22, 2024
1 parent e6b2b5e commit c770ac2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-pets-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"runed": patch
---

allow undefined | null in useEventListener
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { boxed } from "$lib/internal/utils/boxed.svelte.js";
import { addEventListener } from "$lib/internal/utils/event.js";

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

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

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

export function useEventListener(
_target: ValueOrGetter<EventTarget>,
_target: ValueOrGetter<EventTarget | null | undefined>,
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)
});
}
8 changes: 6 additions & 2 deletions sites/docs/content/functions/use-event-listener.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import { UseEventListenerDemo } from '$lib/components/demos';
count++;
}
useEventListener(() => document, "click", increment);
let wrapper = $state<HTMLElement>();
useEventListener(() => wrapper, "click", increment);
</script>
<p>You've clicked {count} {count === 1 ? "Time" : "Times"}</p>
<div bind:this={wrapper}>
<p>You've clicked {count} {count === 1 ? "time" : "times"}</p>
</div>
```
7 changes: 4 additions & 3 deletions sites/docs/src/lib/components/demos/use-event-listener.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
count++;
}
useEventListener(() => document, "click", increment);
let wrapper = $state<HTMLElement>();
useEventListener(() => wrapper, "click", increment);
</script>

<div class="select-none rounded-md bg-card p-8">
<p>You've clicked {count} {count === 1 ? "Time" : "Times"}</p>
<div class="select-none rounded-md bg-card p-8" bind:this={wrapper}>
<p>You've clicked {count} {count === 1 ? "time" : "times"}</p>
</div>

0 comments on commit c770ac2

Please sign in to comment.