-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
171 additions
and
16 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./useActiveElement/index.js"; | ||
export * from "./useDebounce/index.js"; | ||
export * from "./useElementSize/index.js"; | ||
export * from "./useEventListener/index.js"; |
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 @@ | ||
export * from "./useEventListener.js"; |
45 changes: 45 additions & 0 deletions
45
packages/runed/src/lib/functions/useEventListener/useEventListener.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ValueOrGetter } from "$lib/internal/types.js"; | ||
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>, | ||
event: TEvent, | ||
handler: (this: Window, event: WindowEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): void; | ||
|
||
export function useEventListener<TEvent extends keyof DocumentEventMap>( | ||
target: ValueOrGetter<Document>, | ||
event: TEvent, | ||
handler: (this: Document, event: DocumentEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): void; | ||
|
||
export function useEventListener< | ||
TElement extends HTMLElement, | ||
TEvent extends keyof HTMLElementEventMap, | ||
>( | ||
target: ValueOrGetter<TElement>, | ||
event: TEvent, | ||
handler: (this: TElement, event: HTMLElementEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): void; | ||
|
||
export function useEventListener( | ||
target: ValueOrGetter<EventTarget>, | ||
event: string, | ||
handler: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
): void; | ||
|
||
export function useEventListener( | ||
target: ValueOrGetter<EventTarget>, | ||
event: string, | ||
handler: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
) { | ||
const $target = boxed(target); | ||
|
||
$effect(() => addEventListener($target.value, event, handler, options)); | ||
} |
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,58 @@ | ||
/** | ||
* Overloaded function signatures for addEventListener | ||
*/ | ||
export function addEventListener<TEvent extends keyof WindowEventMap>( | ||
target: Window, | ||
event: TEvent, | ||
handler: (this: Window, event: WindowEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
export function addEventListener<TEvent extends keyof DocumentEventMap>( | ||
target: Document, | ||
event: TEvent, | ||
handler: (this: Document, event: DocumentEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
export function addEventListener< | ||
TElement extends HTMLElement, | ||
TEvent extends keyof HTMLElementEventMap, | ||
>( | ||
target: TElement, | ||
event: TEvent, | ||
handler: (this: TElement, event: HTMLElementEventMap[TEvent]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
): VoidFunction; | ||
|
||
export function addEventListener( | ||
target: EventTarget, | ||
event: 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, | ||
event: string | string[], | ||
handler: EventListenerOrEventListenerObject, | ||
options?: boolean | AddEventListenerOptions | ||
) { | ||
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)); | ||
|
||
// Return a function that removes the event listener from the target element(s). | ||
return () => { | ||
events.forEach((_event) => target.removeEventListener(_event, handler, options)); | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: UseEventListener | ||
description: A function that attaches an automatically disposed event listener. | ||
--- | ||
|
||
<script> | ||
import { UseEventListenerDemo } from '$lib/components/demos'; | ||
</script> | ||
|
||
## Demo | ||
|
||
<UseEventListenerDemo /> | ||
|
||
## Usage | ||
|
||
```svelte | ||
<script lang="ts"> | ||
import { useEventListener } from "runed"; | ||
const activeElement = useActiveElement(); | ||
const text = $derived( | ||
`Currently active element: ${ | ||
activeElement.value !== nulla | ||
? activeElement.value.localName | ||
: "No active element found" | ||
}`a | ||
); | ||
</script> | ||
<div class="rounded-md bg-card p-8"> | ||
<p>{text}</p> | ||
</div> | ||
``` |
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,3 +1,4 @@ | ||
export { default as UseActiveElementDemo } from "./use-active-element.svelte"; | ||
export { default as UseDebounceDemo } from "./use-debounce.svelte"; | ||
export { default as UseElementSizeDemo } from "./use-element-size.svelte"; | ||
export { default as UseEventListenerDemo } from "./use-event-listener.svelte"; |
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
13 changes: 13 additions & 0 deletions
13
sites/docs/src/lib/components/demos/use-event-listener.svelte
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,13 @@ | ||
<script lang="ts"> | ||
import { useEventListener } from "runed"; | ||
let count = $state(0); | ||
$effect(() => { | ||
useEventListener(document, "click", () => count++); | ||
}); | ||
</script> | ||
|
||
<div class="rounded-md bg-card p-8"> | ||
<p>You've clicked {count} {count === 1 ? "Time" : "Times"}</p> | ||
</div> |
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