All Projects → FormidableLabs → React Swipeable

FormidableLabs / React Swipeable

Licence: mit
React swipe event handler hook

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Swipeable

Vuetify Swipeout
👆 A swipe out example built with Vue CLI 3 + Vuetify + Swiper.
Stars: ✭ 117 (-91.32%)
Mutual labels:  mobile, swipe, touch
Swiper
Most modern mobile touch slider with hardware accelerated transitions
Stars: ✭ 29,519 (+2089.84%)
Mutual labels:  mobile, swipe, touch
React Easy Swipe
Easy handler for common swipe operations
Stars: ✭ 85 (-93.69%)
Mutual labels:  mobile, swipe, touch
Cupertino Pane
🎉📱Multi-functional panes and boards for next generation progressive applications
Stars: ✭ 267 (-80.19%)
Mutual labels:  mobile, swipe, touch
Slider
Touch swipe image slider/slideshow/gallery/carousel/banner mobile responsive bootstrap
Stars: ✭ 2,046 (+51.78%)
Mutual labels:  mobile, swipe, touch
react-gallery-carousel
Mobile-friendly gallery carousel 🎠 with server side rendering, lazy loading, fullscreen, thumbnails, touch, mouse emulation, RTL, keyboard navigation and customisations.
Stars: ✭ 178 (-86.8%)
Mutual labels:  touch, swipe
Hover On Touch
A pure Javascript Plugin for an alternative hover function that works on mobile and desktop devices. It triggers a hover css class on »Taphold« and goes to a possible link on »Tap«. It works with all html elements.
Stars: ✭ 256 (-81.01%)
Mutual labels:  mobile, touch
Framework7
Full featured HTML framework for building iOS & Android apps
Stars: ✭ 16,560 (+1128.49%)
Mutual labels:  mobile, touch
React Native Swipe Gestures
4-directional swipe gestures for react-native
Stars: ✭ 471 (-65.06%)
Mutual labels:  swipe, touch
Keen Slider
The HTML touch slider carousel with the most native feeling
Stars: ✭ 3,097 (+129.75%)
Mutual labels:  mobile, touch
Vue Gallery
📷 Responsive and customizable image and video gallery, carousel and lightbox, optimized for both mobile and desktop web browsers.
Stars: ✭ 405 (-69.96%)
Mutual labels:  mobile, touch
React Fastclick
Fast Touch Events for React
Stars: ✭ 476 (-64.69%)
Mutual labels:  mobile, touch
Mars
腾讯移动 Web 前端知识库
Stars: ✭ 9,255 (+586.57%)
Mutual labels:  mobile, touch
react-imageViewer
React component for image displaying in full screen
Stars: ✭ 61 (-95.47%)
Mutual labels:  hook, touch
gestures
A library for normalized events and gesture for desktop and mobile.
Stars: ✭ 31 (-97.7%)
Mutual labels:  touch, swipe
Better Gesture
A gesture library use for pc, mobile, vue, and mini programs
Stars: ✭ 419 (-68.92%)
Mutual labels:  mobile, touch
Any Touch
👋 手势库, 按需2kb~5kb, 兼容PC / 移动端
Stars: ✭ 567 (-57.94%)
Mutual labels:  swipe, touch
Swipe
Swipe is the most accurate touch slider. Support both React and Angular.
Stars: ✭ 850 (-36.94%)
Mutual labels:  mobile, swipe
React Drag Drawer
A responsive mobile drawer that is draggable on mobile, and falls back to a modal on desktop
Stars: ✭ 135 (-89.99%)
Mutual labels:  mobile, touch
Trip
移动前端开发经验指南
Stars: ✭ 550 (-59.2%)
Mutual labels:  mobile, touch

React Swipeable

React swipe event handler hook

build status Coverage Status npm version npm downloads gzip size

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.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].