vue3-touch-events

repository·master·Indexed 43 days ago

https://github.com/robinrodricks/vue3-touch-events

A library for Vue.js 3.x that enables declarative touch, swipe, tap, hold, and drag events on HTML DOM elements. It provides a unified syntax for both mobile touchscreens and desktop mouse interactions via the v-touch directive, supporting events like v-touch:swipe, v-touch:longtap, and mobile-only zoom gestures. Version 5.0.15.

Tokens
4.8K
Snippets
18
Records
21
Agent score
6%

What's inside vue3-touch-events

  1. Pass parameters to v-touch event handlers

    master

    To pass custom parameters to a v-touch handler, the handler method must return a delegate function. The delegate function receives the event direction (for swipes) and the mouseEvent object.

    <div v-for="(item, i) in items">
    	<div v-touch:swipe="onSwipeItem(item, i)">Button</div
    </div
    methods: {
    	onSwipeItem(item, i) {
    		return function (direction, mouseEvent) {
    			console.log("Swiped item ", i, ": ", item, " in direction ", direction);
    		};
    	},
    }
  2. Configure v-touch options and classes

    master

    You can customize event behavior for specific elements using the v-touch-options directive or apply CSS classes during interaction using v-touch-class.

    • v-touch-options: Pass an object to override global settings like touchClass, swipeTolerance, or touchHoldTolerance.
    • v-touch-class: Automatically adds a CSS class to the element during the interaction.

    Example of overriding options:

    <span v-touch:tap="tapHandler" v-touch-options="{touchClass: 'active', swipeTolerance: 80, touchHoldTolerance: 300}">Different options</span>
    <!-- customize touch effects by CSS class -->
    <span v-touch:tap="tapHandler" v-touch-class="active">Customize touch class</span>
  3. Register vue3-touch-events in Nuxt 3+

    master

    For Nuxt 3 projects, create a plugin file in your plugins directory to register the library with the Nuxt app.

    import Vue3TouchEvents from 'vue3-touch-events';
    
    export default defineNuxtPlugin((nuxtApp) => {
      nuxtApp.vueApp.use(Vue3TouchEvents)
    });
  4. Migrate from Vue 2.x

    master

    If you are upgrading from the original vue2-touch-events library, note that several event names have changed for consistency:

    Old EventNew Event
    v-touch:touchholdv-touch:hold
    v-touch:startv-touch:press
    v-touch:endv-touch:release
    v-touch:movedv-touch:drag.once
    v-touch:movingv-touch:drag
  5. Register vue3-touch-events in Vue 3 (TypeScript)

    master

    To use the library in a Vue 3 application with TypeScript, register the plugin in your main application file. You can optionally provide global configuration using Vue3TouchEventsOptions.

    import Vue3TouchEvents, {
      type Vue3TouchEventsOptions,
    } from "vue3-touch-events";
    
    app.use<Vue3TouchEventsOptions>(Vue3TouchEvents, {
      disableClick: false
      // any other global options...
    })
  6. Register vue3-touch-events using UMD (JavaScript)

    master

    For pure JavaScript projects without a build step, include the UMD script in your HTML. No manual registration with Vue is required when using the UMD version.

    <script src="libs/vue.js"></script>
    <script src="libs/vue3-touch-events.js"></script>
  7. Use the v-touch directive for touch events

    master

    Use the v-touch directive on any HTML element to bind touch or mouse events. The syntax follows v-touch:[event].[modifier]="handler".

    Common Event Examples:

    • Tap: v-touch:tap="handler" or simply v-touch="handler" (tap is the default).
    • Swipe: v-touch:swipe="handler" (any direction) or v-touch:swipe.left="handler" (specific direction).
    • Long Tap: v-touch:longtap="handler".
    • Press/Release: v-touch:press="startHandler" v-touch:release="endHandler".
    • Drag: v-touch:drag="movingHandler" (continuous) or v-touch:drag.once="movedHandler" (triggers once when movement exceeds tolerance).
    • Hold: v-touch:hold="handler".
    • Zoom: v-touch:zoom.in="handler" or v-touch:zoom.out="handler".
    <!-- bind a tap event -->
    <span v-touch:tap="touchHandler">Tap Me</span>
    
    <!-- only when swipe left can trigger the callback -->
    <span v-touch:swipe.left="swipeHandler">Swipe Left Here</span>
    
    <!-- bind a long tap event -->
    <span v-touch:longtap="longtapHandler">Long Tap Event</span>
    
    <!-- zoom in -->
    <span v-touch:zoom.in="zoomInHandler">Use multi-touch to zoom in</span>
  8. Configure Drag and Drop settings

    master

    You can customize drag behavior using tapTolerance, dragFrequency, and dragOutside. These can be set via Global Configuration or the v-touch-options directive.

    SettingUnitsDescription
    tapTolerancepixelsPixels required to move before a tap is no longer registered. Default: 10
    dragFrequencymsInterval between drag events. Set to 10 (100 FPS) or 16 (60 FPS) to prevent lag. Default: 10
    dragOutsidebooleanIf true, drag events fire even when the mouse moves outside the element. Default: false
  9. Configure Global Settings

    master

    Set global defaults for the library during Vue installation. This affects all elements unless overridden by v-touch-options.

    NameUnitsDescription
    disableClickbooleanIf true, disables desktop events. Recommended for mobile-only apps to prevent touch pass-through issues. Default: false
    touchClassstringThe global CSS class added during press/hover. Default: ''
    namespacestringCustomizes the Vue namespace. Default is touch (directives: touch, touch-class, touch-options). If set to yolo, directives become yolo, yolo-class, etc.
    import Vue3TouchEvents from 'vue3-touch-events';
    
    Vue.use(Vue3TouchEvents, {
      disableClick: false,
      touchClass: 'my-active-class',
      namespace: 'touch'
    });
  10. Install and configure the vue3-touch-events plugin

    master

    To use vue3-touch-events in your Vue 3 application, install the package and register it as a plugin in your main entry file (e.g., main.ts). You can pass a configuration object to the install method to set global options for all touch events.

    By default, the plugin uses the namespace touch. This means the primary directive will be v-touch and helper directives will be v-touch-class and v-touch-options.

    import { createApp } from 'vue'
    import Vue3TouchEvents from 'vue3-touch-events'
    
    const app = createApp(App)
    
    // Register the plugin with global options
    app.use(Vue3TouchEvents, {
      touchClass: 'is-touching',
      namespace: 'touch'
    })
    
    app.mount('#app')
  11. Use Drag and Drop events

    master

    The library provides several directives for handling drag-and-drop interactions on both Desktop and Mobile.

    • v-touch:press: Triggered on mouse down (Desktop) or tap without release (Mobile).
    • v-touch:drag.once: Triggered the moment the user first drags the element.
    • v-touch:drag: Triggered continuously while dragging. This event is throttled by dragFrequency.
    • v-touch:release: Triggered on mouse up (Desktop) or tap and release (Mobile).

    To prevent stuttering during drag operations, you can increase the dragFrequency in your configuration.

    <!-- Example usage -->
    <div 
      v-touch:press="onPress"
      v-touch:drag="onDrag"
      v-touch:release="onRelease"
    >
      Drag me
    </div>