Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AhsanAyaz/f28893e90b71cc03812287016192d294 to your computer and use it in GitHub Desktop.
Save AhsanAyaz/f28893e90b71cc03812287016192d294 to your computer and use it in GitHub Desktop.
Pointy Little Popovers PopoverPositionalClassDirective
import { AfterViewInit, ChangeDetectorRef, Directive, Input, Renderer2, SimpleChanges } from '@angular/core';
@Directive({
selector: '[appPopoverPositionalClass]'
})
export class PopoverPositionalClassDirective implements AfterViewInit {
@Input() originY: string;
@Input() targetSelector: string;
@Input() inverseClass;
@Input() initialDirection = 'bottom';
constructor(private renderer: Renderer2, private cdRef: ChangeDetectorRef) {}
ngAfterViewInit() {
const requiredProps = ['originY', 'targetSelector'];
requiredProps.forEach(prop => {
if (this[prop] === undefined) {
throw new Error(`${prop} is required`);
}
})
this.inverseClass = this.inverseClass || `${this.targetSelector}-inverse`;
}
ngOnChanges(changes: SimpleChanges) {
if (changes && changes.originY) {
if (changes.originY.currentValue !== changes.originY.previousValue) {
// let the popover appear
setTimeout(() => {
this.applyClass(changes.originY.currentValue);
}, 0);
}
}
}
applyClass(originY) {
const target = document.querySelector(this.targetSelector);
if (!target) {
return;
}
if (originY !== this.initialDirection) {
this.renderer.addClass(target, this.inverseClass);
} else {
this.renderer.removeClass(target, this.inverseClass);
}
this.cdRef.markForCheck();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment