Package Exports
- react-swipeable
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-swipeable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
React Swipeable
React swipe event handler hook
Api
Use the hook and set your swipe(d) handlers.
const handlers = useSwipeable({
onSwiped: (eventData) => console.log("User Swiped!", evenData),
...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, // Fired after any swipe
onSwipedLeft, // Fired after LEFT swipe
onSwipedRight, // Fired after RIGHT swipe
onSwipedUp, // Fired after UP swipe
onSwipedDown, // Fired after DOWN swipe
onSwiping, // Fired during any swipe
onTap, // Fired after a tap
}Event data
All Event Handlers are called with the below event data.
{
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)
}Configuration Props
{
delta: 10, // min distance(px) before a swipe starts
preventDefaultTouchmoveEvent: false, // preventDefault on touchmove, *See Details*
trackTouch: true, // track touch input
trackMouse: false, // track mouse input
rotationAngle: 0, // set a rotation angle
}None of the props/config options are required.
Hook details
- Hook use requires react >= 16.8.0
- The props contained in
handlersare currentlyrefandonMouseDown- Please spread
handlersas the props contained in it could change as react improves event listening capabilities
- Please spread
preventDefaultTouchmoveEvent Details
preventDefaultTouchmoveEvent prevents the browser's touchmove event.
Use this to stop scrolling in the browser while a user swipes.
You can additionally try
touch-actioncss property, [see below]e.preventDefault()is only called when:preventDefaultTouchmoveEvent: truetrackTouch: true- the users current swipe has an associated
onSwipingoronSwipedhandler/prop
Example:
- If a user is swiping right with
<Swipable onSwipedRight={this.userSwipedRight} preventDefaultTouchmoveEvent={true} >thene.preventDefault()will be called, but if the user was swiping left thene.preventDefault()would not be called.
Please experiment with the example to test preventDefaultTouchmoveEvent.
passive listener
With v6 we've added the passive event listener option by default, setting to it to false only when preventDefaultTouchmoveEvent is `true.
When preventDefaultTouchmoveEvent is:
true=>{ passive: false }false=>{ passive: true }
React's long running passive event issue.
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
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
const { ref } = useSwipeable({
...
}) as { ref: RefCallback<Document> };
useEffect(() => {
ref(document);
});How to use & share ref from `useSwipeable?
More details in issue 189.
Example ref passthrough:
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} />
}touch-action and preventing 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 here.
Local Development
Initial install & setup, with node 10+ & yarn v1, run yarn.
Make changes/updates to the src/index.ts file.
Please add/update tests if PR adds/changes functionality.
Verify updates with the examples
Build, run, and test examples locally:
yarn run start:examples
After the server starts you can then view the examples page with your changes at http://localhost:3000.
You can now make updates/changes to src/index.ts and webpack will rebuild, then reload the page so you can test your changes!
License
MIT