David Wolf
David Wolf

Reputation: 1738

Pass event options to on:event directive

Is it possible to pass event options to a svelte on:* directive?

To register an event listener with options I currently use:

<script lang="ts">
  import { onMount } from "svelte"
  
  let root: HTMLElement

  onMount(() => {
    const touchHandlerOptions = {
      passive: true,
    }

    root.addEventListener("touchstart", handleTouchStart, touchHandlerOptions)
  })
</script>

<div bind:this={root} />

Wondering if this could be refactored to pass the options in an on:* directive directly?

Upvotes: 0

Views: 43

Answers (1)

brunnerh
brunnerh

Reputation: 185300

There is no way to pass options directly, but you can use event-modifiers:

<div on:touchstart|passive={handleTouchStart}>

See docs

Upvotes: 2

Related Questions