npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

svelte-event

v0.0.13

Published

svelte-event provides a set of wrapper functions for adding modifiers to event handlers and a versatile `event` action for comprehensive event listener management in Svelte.

Readme

⚡️ svelte-event

svelte-event provides a set of wrapper functions for adding modifiers to event handlers and a versatile event action for comprehensive event listener management in Svelte.

This package is primarily intended to address the upcoming changes to events in Svelte 5, though is entirely compatible with Svelte 3 and 4 as well.

🚧 NOTE: This plugin is still in early development, use at your own risk.

🚀 Installation

# Using npm
npm install svelte-event

# Using yarn
yarn add svelte-event

# Using pnpm
pnpm add svelte-event

# Using bun
bun add svelte-event

💡 Usage

Modifier Wrapper Functions

svelte-event provides several wrapper functions for modifying event behavior:

  • preventDefault
  • stopPropagation
  • stopImmediatePropagation
  • self
  • trusted
  • once

Note: passive requires use of the event action, as it requires access to the event listener options which is not possible using wrapper functions.

Using Wrapper Functions

Apply modifiers directly to event handlers:

<script>
  import { once, preventDefault } from 'svelte-event';

  function handleClick(event) {
    // Click event logic
  }
</script>

<div onclick={once(preventDefault(handleClick))} />

Combining Modifiers with withModifiers

Use withModifiers to apply multiple modifiers using a configuration object:

<script>
  import { withModifiers } from 'svelte-event';

  function handleClick(event) {
    // Click event logic
  }

  const modifiedHandler = withModifiers(handleClick, {
    preventDefault: true,
    stopPropagation: true
  });
</script>

<div onclick={modifiedHandler} />

Key Modifier

The key modifier allows you to specify a key that must be pressed for the event handler to execute:

<script>
  import { key } from 'svelte-event/key';

  function handleKeydown(event) {
    // Keydown event logic
  }
</script>

<div onkeydown={key(handleKeydown, 'Enter')} />

You can also specify the set of modifier keys (altKey, ctrlKey, metaKey, shiftKey) that must be pressed for the event handler to execute:

<script>
  import { key } from 'svelte-event/key';

  function handleKeydown(event) {
    // Keydown event logic
  }
</script>

<div onkeydown={key(handleKeydown, 'Enter', { ctrlKey: true, exact: true })} />

If the exact modifier is set to true, then the event handler will only execute if the specified key is pressed and no other modifier keys are pressed.

Mouse Modifiers

The package also provides left, right, and middle modifiers for mouse events, which only execute the event handler if the left, right, or middle mouse button is pressed, respectively:

<script>
  import { left, right, middle } from 'svelte-event/mouse';

  function handleClick(event) {
    // Click event logic
  }
</script>

<div onclick={left(handleClick)} />

You can also specify the set of modifier keys (altKey, ctrlKey, metaKey, shiftKey) that must be pressed for the event handler to execute, as well as the exact modifier in the same way as the key modifier.

Compose Function

The compose function allows you to combine multiple wrapper functions into a single function:

import { compose } from 'svelte-event';

const handler = compose(
  handler1,
  handler2,
);

// Use the composed handler in your Svelte component
<div onclick={handler} />

Event Action

The event action in svelte-event allows you to attach event listeners to DOM elements, enabling detailed control through various modifiers.

Basic Example

<script>
  import { event } from 'svelte-event';

  function handleClick() {
    // Click event logic
  }
</script>

<div use:event={{ click: handleClick }} />

Advanced Configuration

You can provide detailed configuration for event listeners, including multiple handlers, various modifiers, and specific event phases.

  • Multiple Handlers: Attach several handlers to the same event:

    <div use:event={{ click: { handlers: [handleClick1, handleClick2] } }} />
  • Event Modifiers: Customize event behavior with modifiers such as preventDefault, stopPropagation, passive, and more:

    <div use:event={{ click: { handler: handleClick, modifiers: { preventDefault: true } } }} />
  • Performance Optimization with passive: Improve scrolling performance for touch and wheel events:

    <div use:event={{ wheel: { modifiers: { passive: true } } }} />
  • Capture Phase with capture: Execute event handler during the capture phase:

    <div use:event={{ click: { modifiers: { capture: true } } }} />

📜 License

svelte-event is open source, licensed under the MIT License. For more information, see the LICENSE file.