Skip to content

Commit 625818a

Browse files
committed
fix(tooltip): do not show the tooltip for empty content, refactor with input()
1 parent 60f62de commit 625818a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

projects/coreui-angular/src/lib/tooltip/tooltip.directive.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
ComponentRef,
55
DestroyRef,
66
Directive,
7+
effect,
78
ElementRef,
89
HostBinding,
910
inject,
1011
Inject,
12+
input,
1113
Input,
1214
OnChanges,
1315
OnDestroy,
@@ -39,7 +41,7 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
3941
* Content of tooltip
4042
* @type {string | TemplateRef}
4143
*/
42-
@Input('cTooltip') content: string | TemplateRef<any> = '';
44+
readonly content = input<string | TemplateRef<any>>('', { alias: 'cTooltip' });
4345

4446
/**
4547
* Optional popper Options object, takes precedence over cPopoverPlacement prop
@@ -110,6 +112,11 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
110112
private intersectionService: IntersectionService
111113
) {}
112114

115+
contentEffect = effect(() => {
116+
this.destroyTooltipElement();
117+
this.content() ? this.addTooltipElement() : this.removeTooltipElement();
118+
});
119+
113120
ngAfterViewInit(): void {
114121
this.intersectionServiceSubscribe();
115122
}
@@ -197,13 +204,17 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
197204
}
198205

199206
private addTooltipElement(): void {
207+
if (!this.content()) {
208+
return;
209+
}
210+
200211
if (!this.tooltipRef) {
201212
this.createTooltipElement();
202213
}
203214

204215
this.tooltipId = this.getUID('tooltip');
205216
this.tooltipRef.instance.id = this.tooltipId;
206-
this.tooltipRef.instance.content = this.content;
217+
this.tooltipRef.instance.content = this.content();
207218

208219
this.tooltip = this.tooltipRef.location.nativeElement;
209220
this.renderer.addClass(this.tooltip, 'd-none');
@@ -227,9 +238,9 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
227238
this.changeDetectorRef.markForCheck();
228239

229240
setTimeout(() => {
230-
this.tooltipRef.instance.visible = this.visible;
231-
this.popperInstance.forceUpdate();
232-
this.changeDetectorRef.markForCheck();
241+
this.tooltipRef && (this.tooltipRef.instance.visible = this.visible);
242+
this.popperInstance?.forceUpdate();
243+
this.changeDetectorRef?.markForCheck();
233244
}, 100);
234245

235246
}

0 commit comments

Comments
 (0)