Skip to content

Instantly share code, notes, and snippets.

@cjgajard
Forked from TigorC/hover-class.directive.ts
Last active March 20, 2017 21:06
Show Gist options
  • Save cjgajard/c04f13cde8f62c791d13d7e304257fd4 to your computer and use it in GitHub Desktop.
Save cjgajard/c04f13cde8f62c791d13d7e304257fd4 to your computer and use it in GitHub Desktop.
Angular 2 hover class directive ES6
import { Directive, ElementRef, Renderer } from '@angular/core';
/*
* Adds a class to a DOM element when the mouse is over its box (:hover).
* This DOESN'T support adding multiple classes separated by spaces.
* Usage:
* <button class="btn" hoverClass="btn-success">
*/
const HoverClassDirective = class {
constructor(elementRef, renderer) {
this.elementRef = elementRef;
this.renderer = renderer;
// @Input this.hoverClass
}
mouseover() {
this.renderer.setElementClass(this.elementRef.nativeElement, this.hoverClass, true);
}
mouseout() {
this.renderer.setElementClass(this.elementRef.nativeElement, this.hoverClass, false);
}
};
HoverClassDirective.annotations = [
new Directive({
selector: '[hoverClass]',
host: {
'(mouseover)': 'mouseover()',
'(mouseout)': 'mouseout()',
},
inputs: ['hoverClass'],
}),
];
HoverClassDirective.parameters = [
[ElementRef],
[Renderer],
];
export default HoverClassDirective;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment