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
hammerjspackage:shnpm install hammerjs --save -
Import the
HammerModulein theapp.module.tsfile:tsimport { HammerModule } from '@angular/platform-browser'; @NgModule({ imports: [ HammerModule ] }); -
(Optional) Use the
HammerGestureConfigclass to configure the Hammer.JS touch gestures to include the all directions for theswipegesture: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
divelement container and handle theswipeLeftandswipeRightevents 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
swipeUpandswipeBottomgestures 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:
Due to site limitations, you need to open the examples in an external code editor, such as StackBlitz or CodeSandBox, to view and test the integration properly.
The following example demonstrates how to integrate Hammer.JS touch gestures with the Kendo UI for Angular Drawer component: