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 🙏

© 2021 – Pkg Stats / Ryan Hefner

react-swipeable

v6.1.2

Published

React Swipe event handler hook

Downloads

1,264,998

Readme

React Swipeable

React swipe event handler hook

npm downloads npm version build status gzip size maintenance status

Edit react-swipeable image carousel

Github Pages Demo

Api

Use the hook and set your swipe(d) handlers.

const handlers = useSwipeable({
  onSwiped: (eventData) => console.log("User Swiped!", eventData),
  ...config,
});
return <div {...handlers}> You can swipe here </div>;

Spread handlers onto the element you wish to track swipes on.

Props / Config Options

Event handler props

{
  onSwiped,       // After any swipe   (SwipeEventData) => void
  onSwipedLeft,   // After LEFT swipe  (SwipeEventData) => void
  onSwipedRight,  // After RIGHT swipe (SwipeEventData) => void
  onSwipedUp,     // After UP swipe    (SwipeEventData) => void
  onSwipedDown,   // After DOWN swipe  (SwipeEventData) => void
  onSwipeStart,   // Start of swipe    (SwipeEventData) => void *see details*
  onSwiping,      // During swiping    (SwipeEventData) => void
  onTap,          // After a tap       ({ event }) => void
}

Details

  • onSwipeStart - called only once per swipe at the start and before the first onSwiping callback
    • The first property of the SwipeEventData will be true

Configuration props and default values

{
  delta: 10,                            // min distance(px) before a swipe starts
  preventDefaultTouchmoveEvent: false,  // call e.preventDefault *See Details*
  trackTouch: true,                     // track touch input
  trackMouse: false,                    // track mouse input
  rotationAngle: 0,                     // set a rotation angle
}

Swipe Event Data

All Event Handlers are called with the below event data, SwipeEventData.

{
  event,          // source event
  initial,        // initial swipe [x,y]
  first,          // true for first event
  deltaX,         // x offset (current.x - initial.x)
  deltaY,         // y offset (current.y - initial.y)
  absX,           // absolute deltaX
  absY,           // absolute deltaY
  velocity,       // √(absX^2 + absY^2) / time - "absolute velocity" (speed)
  vxvy,           // [ deltaX/time, deltaY/time] - velocity per axis
  dir,            // direction of swipe (Left|Right|Up|Down)
}

None of the props/config options are required.

Hook details

  • Hook use requires react >= 16.8.3
  • The props contained in handlers are currently ref and onMouseDown
    • Please spread handlers as the props contained in it could change as react improves event listening capabilities

preventDefaultTouchmoveEvent details

This prop allows you to prevent the browser's touchmove event default action, mostly "scrolling".

Use this to stop scrolling in the browser while a user swipes.

  • You can additionally try touch-action css property, see below

e.preventDefault() is only called when:

  • preventDefaultTouchmoveEvent: true
  • trackTouch: true
  • the users current swipe has an associated onSwiping or onSwiped handler/prop

Example scenario:

If a user is swiping right with props { onSwipedRight: userSwipedRight, preventDefaultTouchmoveEvent: true } then e.preventDefault() will be called, but if the user was swiping left then e.preventDefault() would not be called.

Please experiment with the example app to test preventDefaultTouchmoveEvent.

passive listener

With v6 we've added the passive event listener option, by default, to internal uses of addEventListener. We set the passive option to false only when preventDefaultTouchmoveEvent is true.

When preventDefaultTouchmoveEvent is:

  • true => el.addEventListener(event, cb, { passive: false })
  • false => el.addEventListener(event, cb, { passive: true })

React's long running passive event issue.

We previously had issues with chrome lighthouse performance deducting points for not having passive option set.

Browser Support

The release of v6 react-swipeable we only support browsers that support options object for addEventListener, Browser compatibility. Which mainly means react-swipeable does not support ie11 by default, you need to polyfill options. For example using event-listener-with-options.

Version 6 Updates and migration

If upgrading from v5 or later please refer to the release notes and the v6 migration doc

v6 now only exports a hook, useSwipeable.

If would like something similar to the old <Swipeable> component you can recreate it from the hook. There are examples in the migration doc.

FAQs

How can I add a swipe listener to the document?

Example by @merrywhether #180

const { ref } = useSwipeable({
  ...
}) as { ref: RefCallback<Document> };

useEffect(() => {
  ref(document);
});

How to share ref from useSwipeable?

Example ref passthrough, more details #189:

const MyComponent = () => {
  const handlers = useSwipeable({ onSwiped: () => console.log('swiped') })

  // setup ref for your usage
  const myRef = React.useRef();

  const refPassthrough = (el) => {
    // call useSwipeable ref prop with el
    handlers.ref(el);

    // set myRef el so you can access it yourself
    myRef.current = el;
  }

  return (<div {...handlers} ref={refPassthrough} />
}

How to use touch-action to prevent scrolling?

Sometimes you don't want the body of your page to scroll along with the user manipulating or swiping an item.

You might try to prevent the event default action via preventDefaultTouchmoveEvent, which calls event.preventDefault(). But there may be a simpler, more effective solution, which has to do with a simple CSS property.

touch-action is a CSS property that sets how an element's region can be manipulated by a touchscreen user.

const handlers = useSwipeable({
  onSwiped: (eventData) => console.log("User Swiped!", evenData),
  ...config,
});
return <div {...handlers} style={{ touchAction: 'pan-y' }}> Swipe here </div>;

This explanation and example borrowed from use-gesture's wonderful docs.

License

MIT

Contributing

Please see our contributions guide.

Maintainers

Project Maintenance

Maintenance Status

Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.