Getting hammered in an angular way

Adding HammerJS to Angular 6 — right? we’re at 6?

‘Three Drunken Women’, painting by Torii Kiyonaga, c. 1787 (wikipedia/commons)

Draft notes:

I needed to implement a HammerJS solution (without Material) in Angular 6 and there’s not a consolidated article of how to implement an event plugin, so I thought I’d share how I did it. Basically, you add the plugin, you have to import it globally, then create your directive and add it to your HTML object.

Here’s the general outline for now, which I’m publishing in draft form, in the hopes it’s enough information to save someone else the time to cobble the solution together. I’ll come back and edit when I have time:

>npm install — save hammerjs

Also, either install platform-browser or see Ryan’s article: https://blog.angularindepth.com/gestures-in-an-angular-application-dde71804c0d0.

import ‘hammerjs’;

You have to load the plugin globally, because “currently all event plugins must be loaded at bootstrap” (https://github.com/angular/angular/issues/19874). Alternatively, include the script in your index.html file as noted by Edric: https://stackoverflow.com/questions/52584700/could-not-find-hammerjs-in-angular-6.

ng g directive directives/hammerd — selector rspnsv-hammer — export

This adds the hammerd directive files to a directives directory under your app. It also updates your app.modules.ts file.

<div rspnsv-hammer class=”kindGesture”></div>
import { Directive, ElementRef, AfterViewInit, Output, EventEmitter, Input } from '@angular/core';
import * as Hammer from 'hammerjs';
import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
@Directive({
selector: '[rspnsv-hammer]'
})
export class HammerdDirective implements AfterViewInit {
@Output() onGesture = new EventEmitter();
@Input() id;
static hammerInitialized = false;
constructor(private el: ElementRef) { }ngAfterViewInit(): void {
if (!HammerdDirective.hammerInitialized) {
let hammertime = new Hammer(this.el.nativeElement);
hammertime.get('swipe').set({ direction: Hammer.DIRECTION_HORIZONTAL });
hammertime.on("swipeleft", (ev) => {
this.onGesture.emit(this.id + " swiped left");
});
hammertime.on("swiperight", (ev) => {
this.onGesture.emit(this.id + " swiped right");
});
HammerdDirective.hammerInitialized = true;
}
}
}

Thanks to https://github.com/CG0323 for sharing how to set the horizontal direction.

Here, we’re creating a left-right swipe directive. If an HTML element has that rspnsv-hammer attribute, this directive will emit it’s ID and direction.

The HammerJS documentation does point you to RyanMullin’s directive (which I haven’t tried yet, as I was optimizing to a teenie-sub-set).

There you have it for now.

--

--

...a queer man, Captain Ahab- so some think- but a good one. Oh, thou'lt like him well enough; no fear, no fear. He's a grand, ungodly, god-like man...

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
julee

...a queer man, Captain Ahab- so some think- but a good one. Oh, thou'lt like him well enough; no fear, no fear. He's a grand, ungodly, god-like man...