Skip to content

Commit 6cf180a

Browse files
committed
feat: add support to access host component instance
1 parent ca449fd commit 6cf180a

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

projects/toppy/src/lib/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface ComponentType<T> {
7373

7474
export type TID = string;
7575

76-
export type ToppyEventName = 't_open' | 't_close' | 't_dynpos' | 't_detach' | 't_posupdate';
76+
export type ToppyEventName = 't_open' | 't_close' | 't_dynpos' | 't_detach' | 't_posupdate' | 't_compins';
7777

7878
export interface ToppyEvent {
7979
from: TID;

projects/toppy/src/lib/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<ng-container *ngSwitchCase="'t'">
99
<ng-container *ngTemplateOutlet="content.data; context: { $implicit: content.props }"></ng-container>
1010
</ng-container>
11-
<ng-container *ngSwitchCase="'c'"> <ng-container *ngComponentOutlet="content.data"></ng-container> </ng-container>
11+
<ng-container *ngSwitchCase="'c'"> <ng-template #compOutlet></ng-template> </ng-container>
1212
</ng-container>
1313
</div>

projects/toppy/src/lib/toppy.component.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import {
33
ChangeDetectionStrategy,
44
ChangeDetectorRef,
55
Component,
6+
ComponentFactoryResolver,
67
ElementRef,
78
Injector,
89
OnDestroy,
9-
OnInit
10+
OnInit,
11+
ViewChild,
12+
ViewContainerRef
1013
} from '@angular/core';
1114
import { Observable, Subject } from 'rxjs';
1215
import { startWith, takeUntil, tap } from 'rxjs/operators';
@@ -22,6 +25,7 @@ import { Bus, cssClass, toCss } from './utils';
2225
changeDetection: ChangeDetectionStrategy.OnPush
2326
})
2427
export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
28+
@ViewChild('compOutlet', { read: ViewContainerRef }) compOutlet: ViewContainerRef;
2529
content: Content = {
2630
type: ContentType.STRING,
2731
data: '',
@@ -33,9 +37,18 @@ export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
3337
el: HTMLElement | any;
3438
wrapperEl: HTMLElement | any;
3539
extra: string;
40+
pinj: any;
41+
compInstance;
3642
private die: Subject<1> = new Subject();
3743

38-
constructor(private inj: Injector, private cd: ChangeDetectorRef, private elRef: ElementRef) {}
44+
constructor(
45+
public inj: Injector,
46+
private cd: ChangeDetectorRef,
47+
private compResolver: ComponentFactoryResolver,
48+
private elRef: ElementRef
49+
) {
50+
this.pinj = Injector;
51+
}
3952

4053
ngOnInit() {
4154
this.el = this.elRef.nativeElement;
@@ -47,13 +60,23 @@ export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
4760
this.el.setAttribute('data-tid', this.tid);
4861
cssClass('add', cls, `[data-tid='${[this.tid]}']`);
4962
cssClass('add', [this.config.bodyClass]);
50-
if (this.content.type === ContentType.COMPONENT) {
51-
Object.assign(this.content.data['prototype'], this.content.props);
52-
}
5363
}
5464

5565
ngAfterViewInit() {
5666
this.listenPos().subscribe();
67+
if (this.content.type === ContentType.COMPONENT) {
68+
this.compInstance = this.setComponent(this.content.props);
69+
Bus.send(this.tid, 't_compins', this.compInstance);
70+
}
71+
}
72+
73+
setComponent(props) {
74+
const compRef = this.compOutlet.createComponent(
75+
this.compResolver.resolveComponentFactory(this.content.data as any)
76+
);
77+
Object.assign(compRef.instance, props);
78+
compRef.changeDetectorRef.detectChanges();
79+
return compRef.instance;
5780
}
5881

5982
updateTextContent(data: string): void {

0 commit comments

Comments
 (0)