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

vue-throttle-event

v3.1.0

Published

rAF-throttled DOM event handling for Vue 3 composable

Readme

vue-throttle-event

rAF-throttled DOM event handling for Vue 3

npm version npm downloads CI License: MIT

Collapses high-frequency DOM events (scroll, mousemove, resize, …) into at most one handler call per animation frame using requestAnimationFrame.

Features

  • Zero-dependency composable (Vue peer dep only)
  • Auto-cleanup via onScopeDispose — no manual teardown in components
  • Full TypeScript support with generic event types
  • ESM + CJS dual output, tree-shakeable

Install

npm install vue-throttle-event

Usage

Composable (recommended)

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useThrottledEvent } from 'vue-throttle-event'

const scrollY = ref(0)

onMounted(() => {
  // Listener is auto-removed when the component unmounts
  useThrottledEvent(window, 'scroll', () => {
    scrollY.value = window.scrollY
  })
})
</script>

Custom event type

useThrottledEvent<MouseEvent>(el.value, 'mousemove', (event) => {
  // event is typed as MouseEvent
  console.log(event.clientX, event.clientY)
})

Manual stop

const stop = useThrottledEvent(window, 'resize', onResize)

// Call later to remove the listener and cancel any pending rAF
stop()

API

useThrottledEvent(target, type, handler, options?)

| Parameter | Type | Description | |---|---|---| | target | EventTarget \| null \| undefined | The DOM target to listen on. null/undefined returns a no-op. | | type | string | Event type (e.g. 'scroll'). | | handler | (event: E) => void | Called once per rAF frame with the most-recent event. | | options | ThrottledEventOptions | Optional. AddEventListenerOptions plus leading (see below), forwarded to addEventListener. |

Returns () => void — a stop function that removes the listener and cancels any pending rAF. Called automatically on scope dispose when used inside a Vue component or effect scope.

Caveat: preventDefault()

Your handler runs rAF-throttled — up to one frame after the original event — so it fires outside that event's synchronous dispatch. Calling event.preventDefault() inside it will not reliably cancel the event (e.g. a throttled touchmove/wheel handler cannot block scrolling). For cancelation, attach a separate, non-throttled listener — or use leading below, which gives you a synchronous first call for free.

leading option

useThrottledEvent(el.value, 'touchmove', (event) => {
  event.preventDefault() // works: this call runs synchronously
}, { leading: true })

With leading: true, the first event of an idle period invokes the handler synchronously, inside that event's own dispatch — so preventDefault() inside it behaves normally. Further events that arrive before the next animation frame still coalesce into a single trailing call, same as the default behaviour. Once the trailing frame fires, the next event is treated as leading again. Defaults to false (unchanged behaviour).


Migrating from v1 (Vue 2)

v2 is a full rewrite for Vue 3. The public API changed completely.

Before (v1, Vue 2 Options API)

// main.js
import VueThrottleEvent from 'vue-throttle-event'
Vue.use(VueThrottleEvent)

// Component
created() {
  this.$throttle('scroll', 'scroll-throttled')     // dispatches CustomEvent
  this.$on('scroll-throttled', this.onScroll)
}

After (v2, Vue 3 Composition API)

import { useThrottledEvent } from 'vue-throttle-event'

onMounted(() => {
  useThrottledEvent(window, 'scroll', onScroll)    // calls handler directly
})

Key differences:

| v1 (Vue 2) | v2 (Vue 3) | |---|---| | Vue.use(plugin) global install required | Import composable directly | | Re-dispatches a CustomEvent; listen with this.$on | Calls your handler with the latest Event object | | Targets vm.$el by default | You provide the target explicitly | | Returns the raw listener (for manual removeEventListener) | Returns a stop() function; auto-removed on scope dispose |


License

MIT © Marco Boffo