@emanuele-em/nuxt-swipe

Development
1,490 9
Install
npm install @emanuele-em/nuxt-swipe
Latest Version:1.0.2
Vue:3
License:MIT
Last Updated:Aug 5, 2024
Links: GitHub  ·  npm
Maintainer: emanuele-em

Nuxt-Swipe

This Nuxt 3 module allows you to easily add swipe gestures to your Vue 3 App. With just a few lines of code, you can enable swiping on your website or web application.

Installation

To add this module to your Nuxt.js project, run the following command:

  npm i @emanuele-em/nuxt-swipe

Then, add nuxt-swipe to the modules section of your nuxt.config.js file:

  export default {
    modules: [
        '@emanuele-em/nuxt-swipe'
    ]
  }

Usage

To use the module, simply add <Swipe> component, it will be the component that will intercept the swipe gesture

For example:

<template>
    <Swipe>
        <slot />
    </Swipe>
</template>

The module will create a plugin that will emit the Swipe event only after some checks to make sure that the gesture is really a swipe gesture.

You can handle that event in the script section of your component, remember to import nuxtApp:

<script setup>

import { useNuxtApp } from '#app'

const nuxtApp = useNuxtApp()

nuxtApp.$bus.$on('swipe', (direction) => {
    switch (direction) {
        case 'left': 
            // swiped left, do things
            break;
        case 'right':
            // swiped right, do things
            break;
        case 'up':
            // swiped up, do things
            break;
        case 'down':
            // swiped down, do things
            break;
        default:
            break;
    }
})

</script>

Examples

Swipe navigation with Swipe component as Default Layout

layouts/default.vue

<template>
    <Swipe>
        <slot />
    </Swipe>
</template>
  
<script setup>
import { useNuxtApp, useRoute, navigateTo } from '#app'

const nuxtApp = useNuxtApp()
const routes = ['/', '/about']

nuxtApp.$bus.$on('swipe', (direction) => {
    let indexCurrentRoute = routes.indexOf(useRoute().path)
    if (direction === 'left' && routes[indexCurrentRoute + 1]) {
        indexCurrentRoute += 1
    }
    if (direction === 'right' && routes[indexCurrentRoute - 1]) {
        indexCurrentRoute -= 1
    }
    return navigateTo(routes[indexCurrentRoute])
})

</script>
  

Demo

demo-nuxt-swipe.pages.dev

Roadmap

  • Typescript correct syntax

  • Swipe handling during the touchEvent and not just at touchend

License

MIT

Related Packages

@nathanchase/nuxt-dayjs-module

This is a Nuxt 3 compatible module built with the official Nuxt 3 (module-builde...

19,376 31
cap-socket

Functions developed by Cap to establish real-time and persistent communication w...

968
@brc-dd/nuxt-i18n

Minimal module for i18n with locale auto-imports & localized routing

898 12
@unocss/nuxt

Nuxt module for UnoCSS

3,181,743 18,982
nuxt-vercel-analytics

Nuxt module to integrate Vercel Analytics

12,952 22

Version History

Version Released Vue License
1.0.2 ^3.0 MIT
1.0.1 ^3.0 MIT
1.0.0 ^3.0 MIT