Integrating HammerJS Touch Gestures with Kendo UI for Angular
Environment
Product | Progress® Kendo UI® for Angular |
Description
Not all of the Kendo UI for Angular components support touch gestures. How can I integrate the Hammer.JS touch gestures with Kendo UI for Angular components?
Solution
To achieve the desired scenario:
-
Install the
hammerjs
package:shnpm install hammerjs --save
-
Import the
HammerModule
in theapp.module.ts
file:tsimport { HammerModule } from '@angular/platform-browser'; @NgModule({ imports: [ HammerModule ] });
-
(Optional) Use the
HammerGestureConfig
class to configure the Hammer.JS touch gestures to include the all directions for theswipe
gesture:tsimport { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser'; export class MyHammerConfig extends HammerGestureConfig { overrides = <any>{ 'swipe': { direction: Hammer.DIRECTION_ALL } } } @NgModule({ providers: [ { provide: HAMMER_GESTURE_CONFIG, useClass: MyHammerConfig } ] });
-
Create a
div
element container and handle theswipeLeft
andswipeRight
events in the component:tsimport { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <div (swipeLeft)="onSwipeLeft()" (swipeRight)="onSwipeRight()"> Swipe left or right </div> ` }) export class AppComponent { public onSwipeLeft() { console.log('swipe left'); } public onSwipeRight() { console.log('swipe right'); } }
If the optional step 3 is implemented, the
swipeUp
andswipeBottom
gestures will be available. Hammer Swipe options and events are described in the Hammer.JS recognizer documentation. -
Handle the swipe gesture events, and execute methods or change the component properties depending on the direction of the swipe.
The following example demonstrates how to integrate Hammer.JS touch gestures with the Kendo UI for Angular MultiViewCalendar component:
The following example demonstrates how to integrate Hammer.JS touch gestures with the Kendo UI for Angular Drawer component: