Skip to content

Commit

Permalink
Merge pull request #12 from svecosystem/event-listener-refactor
Browse files Browse the repository at this point in the history
update stuff
  • Loading branch information
TGlide authored Apr 22, 2024
2 parents e6b2b5e + 4d2a745 commit a0c0c15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 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);
});
}
2 changes: 1 addition & 1 deletion sites/docs/content/functions/use-active-element.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: UseActiveElement
title: useActiveElement
description: A function that returns the currently active element.
---

Expand Down
9 changes: 6 additions & 3 deletions sites/docs/content/functions/use-event-listener.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: UseEventListener
title: useEventListener
description: A function that attaches an automatically disposed event listener.
---

Expand All @@ -22,8 +22,11 @@ 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 a0c0c15

Please sign in to comment.