Created
September 16, 2017 18:18
-
-
Save Graniron/ec6c948c173ef2c9a15943088edb2f80 to your computer and use it in GitHub Desktop.
Angular tooltip directive(with exportAs)
This file contains 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 { Component } from '@angular/core'; | |
@Component({ | |
selector: 'form-component', | |
template: ` | |
<div> | |
<label> | |
Name | |
<input | |
name="name" | |
type="text" | |
placeholder="Enter your name"> | |
</label> | |
<label | |
tooltip="Your surname name bro" | |
#myTooltip="tooltip"> | |
Enter surname | |
<span | |
(mouseover)="myTooltip.show()" | |
(mouseout)="myTooltip.hide()"> | |
(?) | |
</span> | |
<input type="text"> | |
</label> | |
</div> | |
` | |
}) | |
export class AppComponent { | |
} |
This file contains 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 { Input, Directive, ElementRef, OnInit } from '@angular/core'; | |
@Directive({ | |
selector: '[tooltip]', | |
exportAs: 'tooltip' | |
}) | |
export class TooltipDirective implements OnInit { | |
tooltipElement = document.createElement('div'); | |
visible = false; | |
@Input() | |
set tooltip(value) { | |
this.tooltipElement.textContent = value; | |
} | |
hide() { | |
this.tooltipElement.classList.remove('tooltip--active'); | |
} | |
show() { | |
this.tooltipElement.classList.add('tooltip--active'); | |
} | |
constructor( | |
private element: ElementRef | |
) {} | |
ngOnInit() { | |
this.tooltipElement.className = 'tooltip'; | |
this.element.nativeElement.appendChild(this.tooltipElement); | |
this.element.nativeElement.classList.add('tooltip-container'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment