-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathweb-component-wrapper.ts
More file actions
72 lines (62 loc) · 2.45 KB
/
web-component-wrapper.ts
File metadata and controls
72 lines (62 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { OnInit, Component, ElementRef, Input, OnChanges, SimpleChanges, ViewChild, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { loadRemoteModule } from '@angular-architects/native-federation';
import { WebComponentWrapperOptions } from './model/web-component-wrapper-options';
import { CommonModule } from '@angular/common';
type EventHandlers = { [event: string]: (event: Event) => void };
@Component({
selector: 'web-component-wrapper',
standalone: true,
imports: [CommonModule],
template: `<div #host></div>`,
})
export class WebComponentWrapper implements OnChanges, OnInit {
@ViewChild('host', { read: ElementRef, static: true }) host: ElementRef | undefined;
@Input() config: WebComponentWrapperOptions | undefined;
@Input() props: { [prop: string]: unknown } | undefined;
@Input() handlers: EventHandlers| undefined;
webComponent: HTMLElement | undefined;
protected route = inject(ActivatedRoute);
async ngOnInit(): Promise<void> {
await this.loadWebComponent();
}
async ngOnChanges(changes: SimpleChanges): Promise<void> {
if (!this.webComponent) return;
const { config, handlers } = changes;
if (config?.previousValue !== config?.currentValue && config?.currentValue) {
await this.loadWebComponent();
}
if (handlers?.previousValue !== handlers?.currentValue && handlers?.previousValue) {
this.unbindEventHandlers(handlers.previousValue);
}
this.bindEventHandlers();
this.bindProps();
}
protected async loadWebComponent(): Promise<void> {
const config = this.config || (this.route.snapshot.data as WebComponentWrapperOptions);
if (!config) return;
await loadRemoteModule(config);
this.webComponent = document.createElement(config.elementName);
this.bindProps();
this.bindEventHandlers();
this.host!.nativeElement.appendChild(this.webComponent);
}
protected bindProps(): void {
if (!this.webComponent) return;
for (const prop in this.props) {
(this.webComponent as any)[prop] = this.props[prop];
}
}
protected bindEventHandlers(): void {
if (!this.webComponent) return;
for (const event in this.handlers) {
this.webComponent.addEventListener(event, this.handlers[event]);
}
}
protected unbindEventHandlers(handlers: EventHandlers): void {
if (!this.webComponent) return;
for (const event in handlers) {
this.webComponent.removeEventListener(event, handlers[event]);
}
}
}