Skip to content

Commit

Permalink
feat: added config 'disableOnNilValue' to skip instance if value is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
florianjung79 committed Apr 6, 2021
1 parent 3a27fa0 commit 8c14a5e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions projects/ngneat/helipopper/src/lib/tippy.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
inView(this.host)
.pipe(takeUntil(this.destroyed))
.subscribe(() => {
this.createInstance();
this.createInstanceOnNonNilValue();
});
}
} else if (this.onlyTextOverflow) {
Expand All @@ -141,7 +141,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
this.checkOverflow(isElementOverflow);
});
} else {
this.createInstance();
this.createInstanceOnNonNilValue();
}
}

Expand Down Expand Up @@ -185,6 +185,12 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
return `${this.host.nativeElement.getBoundingClientRect().width}px`;
}

private createInstanceOnNonNilValue() {
if (this.globalConfig.disableOnNilValue && this.content !== null && this.content !== undefined) {
this.createInstance();
}
}

private createInstance() {
this.instance = tippy(this.host.nativeElement as HTMLElement, {
allowHTML: true,
Expand All @@ -202,16 +208,10 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
this.globalConfig.onCreate?.(instance);
},
onShow: instance => {
let resolvedContent = null;
this.zone.run(() => {
resolvedContent = this.resolveContent();
this.instance.setContent(resolvedContent);
this.instance.setContent(this.resolveContent());
});

if (this.globalConfig.disableOnNilValue && resolvedContent === null) {
return false;
}

if (this.useHostWidth) {
instance.popper.style.width = this.hostWidth;
instance.popper.style.maxWidth = this.hostWidth;
Expand Down Expand Up @@ -294,7 +294,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
private checkOverflow(isElementOverflow: boolean) {
if (isElementOverflow) {
if (!this.instance) {
this.createInstance();
this.createInstanceOnNonNilValue();
} else {
this.instance.enable();
}
Expand Down

0 comments on commit 8c14a5e

Please sign in to comment.