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

swipe-right-js

v1.0.3

Published

A lightweight, high-performance, framework-agnostic swipe gesture library

Readme

swipe-right-js

npm version License: MIT TypeScript Bundle Size Tests

A lightweight, high-performance, framework-agnostic swipe gesture library for touch devices. Built with strict TypeScript and zero tolerance for any types.

Features

  • Lightweight: < 1KB minified (core only)
  • High Performance: Optimized event handling with passive listeners
  • Type Safe: Strict TypeScript with zero any types
  • Framework Agnostic: Works with React, Vue, Angular, or vanilla JavaScript
  • Touch Optimized: Designed specifically for mobile touch events

Installation

bun add swipe-right-js

Usage

Core API (Framework Agnostic)

import { createSwipeHandler } from 'swipe-right-js';

const element = document.getElementById('swipeable');

const handler = createSwipeHandler(element, (event) => {
  console.log(`Swiped ${event.direction}`);
  console.log(`Distance: ${event.distance}px`);
  console.log(`Velocity: ${event.velocity}px/ms`);
});

// Cleanup when done
handler.destroy();

React Hook (Custom Implementation)

Since this library is framework-agnostic, you can easily create your own React hook:

import { useEffect, useRef, type RefObject } from 'react';
import { createSwipeHandler, type SwipeCallback, type SwipeConfig } from 'swipe-right-js';

function useSwipe(
  ref: RefObject<HTMLElement>,
  callback: SwipeCallback,
  config?: SwipeConfig
): void {
  useEffect(() => {
    const element = ref.current;
    if (!element) return;

    const handler = createSwipeHandler(element, callback, config);

    return () => {
      handler.destroy();
    };
  }, [ref, callback, config]);
}

// Usage
function SwipeableComponent() {
  const elementRef = useRef<HTMLDivElement>(null);

  useSwipe(elementRef, (event) => {
    console.log(`Swiped ${event.direction}`);
  });

  return <div ref={elementRef}>Swipe me!</div>;
}

Configuration Options

import { createSwipeHandler } from 'swipe-right-js';

const handler = createSwipeHandler(
  element,
  (event) => {
    // Handle swipe
  },
  {
    threshold: 20,           // Minimum distance in pixels (default: 10)
    velocityThreshold: 0.5,  // Minimum velocity in px/ms (default: 0.3)
    preventDefault: false,   // Prevent default touch behavior (default: false)
  }
);

API Reference

createSwipeHandler(element, callback, config?)

Creates a swipe handler for the given DOM element.

Parameters:

  • element: HTMLElement - The DOM element to attach swipe detection to
  • callback: (event: SwipeEvent) => void - Function called when a swipe is detected
  • config?: SwipeConfig - Optional configuration options

Returns:

  • SwipeHandler - Object with a destroy() method

Types

type SwipeDirection = 'left' | 'right' | 'up' | 'down';

interface SwipeEvent {
  direction: SwipeDirection;
  distance: number;      // Distance in pixels
  duration: number;      // Duration in milliseconds
  velocity: number;      // Velocity in pixels per millisecond
  deltaX: number;        // Horizontal movement
  deltaY: number;        // Vertical movement
}

interface SwipeConfig {
  threshold?: number;           // Minimum distance (default: 10)
  velocityThreshold?: number;   // Minimum velocity (default: 0.3)
  preventDefault?: boolean;     // Prevent default (default: false)
}

Examples

Basic Swipe Detection

import { createSwipeHandler } from 'swipe-right-js';

const element = document.querySelector('.swipeable');

createSwipeHandler(element, (event) => {
  switch (event.direction) {
    case 'left':
      console.log('Swiped left');
      break;
    case 'right':
      console.log('Swiped right');
      break;
    case 'up':
      console.log('Swiped up');
      break;
    case 'down':
      console.log('Swiped down');
      break;
  }
});

React Carousel

import { useEffect, useRef, useState } from 'react';
import { createSwipeHandler } from 'swipe-right-js';

function Carousel() {
  const [index, setIndex] = useState(0);
  const carouselRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const element = carouselRef.current;
    if (!element) return;

    const handler = createSwipeHandler(element, (event) => {
      if (event.direction === 'left') {
        setIndex((i) => i + 1);
      } else if (event.direction === 'right') {
        setIndex((i) => i - 1);
      }
    });

    return () => handler.destroy();
  }, []);

  return (
    <div ref={carouselRef} className="carousel">
      {/* Carousel content */}
    </div>
  );
}

Custom Thresholds

// Require longer swipes
createSwipeHandler(element, callback, {
  threshold: 50,          // 50px minimum
  velocityThreshold: 0.5, // Faster swipes only
});

Performance

  • Uses passive event listeners by default for optimal scroll performance
  • Minimal object allocations in the hot path
  • Early exit conditions for invalid gestures
  • Single touch point tracking only

Browser Support

  • Modern browsers with touch event support
  • Mobile Safari (iOS)
  • Chrome Mobile (Android)
  • Firefox Mobile

License

MIT