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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-swipe-detector

v1.0.0

Published

react-swipe-detector is a React library to detect Touch Swipes and Directions.

Downloads

8

Readme

React Swipe Detector

react-swipe-detector is a React library to detect Touch Swipes and Directions.

npm i react-swipe-detector

After installing react-swipe-detector, you can import useSwipe hook in your React components like this:

import useSwipe from 'react-swipe-detector' and cteate a ref connect it to div element that you want swipe happen in it and pass it to useSwipe hook:

//1 import 
import { useSwipe } from "react-swipe-detector";

function MyComponent() {
    //2 create the ref using useRef
    const myRef = useRef(null);
    //4 pass ref to useSwipe hook and get primary and secondary value from it!
    const [primary,secondary] = useSwipe(myRef);
    return (
        //3 pass that ref to div you want to swipe in
        <div ref={myRef}>
            Swipe me!
        </div>
    );
}

whenever you touch, move your touch and then end touch if this process pass this tests it will return primary as direction of swipe and secondary as unwanted swipe direction(it's actually used as detail).

  • Allowed time for move is 250ms
  • Minimum of movement distance should be more than 130px

react-swipe-detector-tests


The returned values are strings of swipe directions and distance of their swipes:

  • vertical swipe:
primary:{ 
    primaryDir: "up" or "down", 
    primaryDist: //distance of swipe(px) 
    },
secondary:{ 
    secondaryDir: "right" or "left",
    secondaryDist: //distance of swipe(px) 
    },
  • horizontal swipe:
primary:{ 
    primaryDir: "right" or "left", 
    primaryDist: //distance of swipe(px) 
    },
secondary:{ 
    secondaryDir: "up" or "down",
    secondaryDist: //distance of swipe(px) 
    },

react-swipe-detector-returned-values


if primary is vertical secondary will be horizontal and vice versa.

you can use it for your animation or any anything you want happen on swipe.


NOTEs:

  • it only work on touchable devices that support usual touch events
  • better to cancel user-select and user-drag for that div using:
/* css */
    user-select: none;
    -webkit-user-drag: none;

and

//javascript
    <div onDragStart={(event)=>{
        event.preventDefault();
        event.target.setAttribute ('draggable', false);
    }}>