alpinejs-swipe
v1.0.2
Published
AlpineJS custom directive to detect swipe gestures on an element.
Downloads
2,594
Readme
alpinejs-swipe
AlpineJS custom directive to detect swipe gestures on an element.
Installation
CDN
Include the following <script>
tag in the <head>
of your document:
<script src="https://unpkg.com/alpinejs-swipe"></script>
Important: This must be added before loading Alpine.js when using CDN links.
NPM
You can also install using a package manager.
npm install alpinejs-swipe
yarn add alpinejs-swipe
And then register the plugin before Alpine.start()
:
import swipePlugin from "alpinejs-swipe";
Alpine.plugin(swipePlugin);
Alpine.start();
Usage
Just use the "x-swipe" directive with an expression and you'll be called back when the user swipes in the direction you want.
<div x-swipe:down="console.log('SWIPED DOWN!')"></div>
<div x-swipe:right="console.log('SWIPED RIGHT!')"></div>
You can also define a threshold with modifiers:
<div x-swipe:down.threshold.200px="console.log('SWIPED DOWN!')"></div>
The default threshold is 50.
If you want, it's also possible to be called back when the user swipes to any side, just omit the direction:
<div x-swipe="console.log('SWIPED!')"></div>
In this case the direction will be passed to the function in the expression through the first argument.
Future ideas
- Allow other gestures detection (like pan, doubletap and longpress) with https://github.com/sciactive/tinygesture.
License
Licensed under the MIT license, see LICENSE.md for details.