Skip to content

Instantly share code, notes, and snippets.

@Graniron
Created September 16, 2017 18:18
Show Gist options
  • Save Graniron/ec6c948c173ef2c9a15943088edb2f80 to your computer and use it in GitHub Desktop.
Save Graniron/ec6c948c173ef2c9a15943088edb2f80 to your computer and use it in GitHub Desktop.
Angular tooltip directive(with exportAs)
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 {
}
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