-
-
Save cjgajard/c04f13cde8f62c791d13d7e304257fd4 to your computer and use it in GitHub Desktop.
Angular 2 hover class directive ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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