Skip to content

Commit 3e59a57

Browse files
committed
fix(popover): scrollbar flickering, refactor
1 parent 116766f commit 3e59a57

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { ComponentFactoryResolver, ElementRef, Renderer2, ViewContainerRef } from '@angular/core';
1+
import { ElementRef, Renderer2, ViewContainerRef } from '@angular/core';
22
import { ListenersService } from '../services/listeners.service';
33
import { PopoverDirective } from './popover.directive';
44

55
describe('PopoverDirective', () => {
66
let document: Document;
77
let renderer: Renderer2;
88
let hostElement: ElementRef;
9-
let componentFactoryResolver: ComponentFactoryResolver;
109
let viewContainerRef: ViewContainerRef;
1110

1211
it('should create an instance', () => {
1312
const listenersService = new ListenersService(renderer);
14-
const directive = new PopoverDirective(document, renderer, hostElement, componentFactoryResolver, viewContainerRef, listenersService);
13+
const directive = new PopoverDirective(document, renderer, hostElement, viewContainerRef, listenersService);
1514
expect(directive).toBeTruthy();
1615
});
1716
});

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

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
ComponentFactoryResolver,
32
ComponentRef,
43
Directive,
54
ElementRef,
@@ -95,7 +94,6 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
9594
@Inject(DOCUMENT) private document: any,
9695
private renderer: Renderer2,
9796
private hostElement: ElementRef,
98-
private componentFactoryResolver: ComponentFactoryResolver,
9997
private viewContainerRef: ViewContainerRef,
10098
private listenersService: ListenersService
10199
) {}
@@ -150,9 +148,8 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
150148

151149
private createPopoverElement(): void {
152150
if (!this.popoverRef) {
153-
const popoverComponent =
154-
this.componentFactoryResolver.resolveComponentFactory(PopoverComponent);
155-
this.popoverRef = popoverComponent.create(this.viewContainerRef.injector);
151+
this.popoverRef = this.viewContainerRef.createComponent<PopoverComponent>(PopoverComponent);
152+
// this.viewContainerRef.detach();
156153
}
157154
}
158155

@@ -162,8 +159,8 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
162159
// @ts-ignore
163160
this.popoverRef = undefined;
164161
this.popperInstance?.destroy();
165-
this.viewContainerRef.detach();
166-
this.viewContainerRef.clear();
162+
this.viewContainerRef?.detach();
163+
this.viewContainerRef?.clear();
167164
}
168165

169166
private addPopoverElement(): void {
@@ -172,36 +169,46 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
172169
}
173170
this.popoverRef.instance.content = this.content;
174171
this.popover = this.popoverRef.location.nativeElement;
172+
this.renderer.addClass(this.popover, 'd-none');
175173
this.renderer.addClass(this.popover, 'fade');
176174

175+
this.popperInstance?.destroy();
176+
177177
setTimeout(() => {
178178
this.popperInstance = createPopper(
179179
this.hostElement.nativeElement,
180180
this.popover,
181181
{ ...this.popperOptions }
182182
);
183183
this.viewContainerRef.insert(this.popoverRef.hostView);
184+
this.renderer.appendChild(this.document.body, this.popover);
185+
if (!this.visible) {
186+
this.removePopoverElement();
187+
return;
188+
}
184189
setTimeout(() => {
185190
this.popoverId = this.getUID('popover');
186191
this.popoverRef.instance.id = this.popoverId;
192+
if (!this.visible) {
193+
this.removePopoverElement();
194+
return;
195+
}
196+
this.renderer.removeClass(this.popover, 'd-none');
187197
this.popoverRef.instance.visible = this.visible;
188-
this.renderer.appendChild(this.document.body, this.popover);
189198
this.popperInstance.forceUpdate();
190-
// this.popoverRef.changeDetectorRef.markForCheck();
191199
}, 100);
192200
});
193201
}
194202

195203
private removePopoverElement(): void {
204+
this.popoverId = '';
196205
if (!this.popoverRef) {
197206
return;
198207
}
199208
this.popoverRef.instance.visible = this.visible;
200209
this.popoverRef.instance.id = undefined;
201210
setTimeout(() => {
202211
this.viewContainerRef.detach();
203-
this.popperInstance?.destroy();
204-
this.popoverId = '';
205212
}, 300);
206213
}
207214
}

0 commit comments

Comments
 (0)