@react2svelte/swipeable

An action to emit swipe and tap events on an element, based on react-swipeable v7.0.0. Many thanks to all the contributors of that package for their hard work!
This package provides a Svelte Action called swipeable
to attach to any DOM element to react to swipe events.
Quickstart
Install the library
npm i @react2svelte/swipeable
And add it to your component
<script lang="ts">
import { swipeable } from '@react2svelte/swipeable';
import type { SwipeEventData } from '@react2svelte/swipeable';
function handler(e: CustomEvent<SwipeEventData>) {
console.log('User swiped!', e.detail);
}
</script>
<div
use:swipeable <!-- use the action -->
on:swiped={handler}
/>
Type hints on the events
Add the following line to your app.d.ts
file
(There should already be a line with /// <reference types="@sveltejs/kit" />
)
Emitted events
The swipeable
action emits 10 new events:
General swipe events
swipedstart
- emitted once, at the beginning of a swipe
swiping
- emitted continuously as the user is swiping
swiped
- emitted once the swipe is complete
Directional swipe events. These are like swiped
but for specific directions only
swipedup
- User swiped up.
swipeddown
- User swiped down.
swipedleft
- User swiped left.
swipedright
- User swiped right.
Tap events
Passthrough events
touchstartormousedown
touchendormouseup
Configuration
This library is based on react-swipeable
, and all the same configuration options and default values apply. Configration can be set by passing an object to use
declation:
<div
use:swipeable={{
delta: 10,
preventScrollOnSwipe: false,
trackTouch: true,
trackMouse: false,
rotationAngle: 0,
swipeDuration: Infinity,
touchEventOptions: { passive: true },
}}
/>
Please have a look at the react-swipeable documentation for additional information.
Swipe event data
All Event Handlers are called with the below event data, SwipeEventData
.
{
event,
initial,
first,
deltaX,
deltaY,
absX,
absY,
velocity,
vxvy,
dir,
}
svelte-gestures
provides pinch
, pan
and rotate
gestures besides swiping and tapping. However, there is no support for the swiping
functionality of swipeable
, which provides continuous updates as the user is swiping, rather than just a final event ones the swipe is complete.
For the user it can be helpful to get visual feedback as they are swiping - for example and image in a gallery - and see the image move as they are swiping, not just once at the end of their swipe.
License
MIT