Event Listener
Safely listen for events.
Usage
<script>
import { eventListener } from '@sveu/browser'
eventListener(document, 'visibilitychange', (evt) => {console.log(evt)})
</script>
<script>
import { eventListener } from '@sveu/browser'
eventListener(document, 'visibilitychange', (evt) => {console.log(evt)})
</script>
svelte
Example
<script lang="ts">
// import { eventListener } from "@sveu/browser"
import { on } from "@sveu/browser"
let key = ""
const cleanup = on("keydown", (e: KeyboardEvent) => {
console.log(e.key)
key = e.key
if (e.key === "Escape") cleanup()
})
</script>
<p>{key}</p>
<script lang="ts">
// import { eventListener } from "@sveu/browser"
import { on } from "@sveu/browser"
let key = ""
const cleanup = on("keydown", (e: KeyboardEvent) => {
console.log(e.key)
key = e.key
if (e.key === "Escape") cleanup()
})
</script>
<p>{key}</p>
svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Click fold/expand code
API
Arguments
Name | Description | Type | Required |
---|---|---|---|
target | The target to listen to. | EventTarget or undefined | No |
event | The event to listen to. | ListAble<string > | Yes |
listeners | The event handler. | ListAble<AnyFn > | Yes |
options | The event listener options. | Boolean or AddEventListenerOptions | No |