-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomMenuItem.ts
115 lines (92 loc) · 2.81 KB
/
CustomMenuItem.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
export interface MenuState {
label: string;
icon?: string;
iconClass?: string;
labelClass?: string;
}
export default class CustomMenuItem extends HTMLElement {
shadow: ShadowRoot;
state = {
label: null,
icon: null,
iconClass: null,
labelClass: null,
} as MenuState;
icon: string = '';
label: string;
styles: string;
participant: any;
_onClick: (participantId: string) => void;
_onStateChange: (participantId: string, callback: (state: MenuState) => void) => void;
constructor() {
super();
this.shadow = this.attachShadow({ mode: "open" });
}
set onClick(onClick: (participantId: string) => void) {
this._onClick = onClick;
}
get onClick() {
return this._onClick;
}
set onStateChange(method) {
this._onStateChange = method;
}
get onStateChange() {
return this._onStateChange;
}
static get observedAttributes() {
return ["label", "icon", "styles"];
}
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
if (oldValue === newValue) return;
if (name === "label") {
this.label = newValue;
}
if (name === "icon") {
this.icon = newValue;
}
if (name === "styles") {
this.styles = newValue;
}
}
createStyles() {
if (!this.styles || this.styles.length === 0) return;
const styleEl = document.createElement("style");
styleEl.innerHTML = this.styles;
this.shadow.appendChild(styleEl);
}
updateLabel(container: any) {
container.innerHTML = "";
if (this.icon || this.state.icon) {
const icon = document.createElement("dyte-icon");
icon.setAttribute("icon", this.state.icon || this.icon);
icon.className = this.state.iconClass || "";
icon.setAttribute("size", "md");
icon.setAttribute("slot", "start");
container.appendChild(icon);
}
if (this.state.labelClass) {
container.className = this.state.labelClass;
}
const textNode = document.createTextNode(this.state.label ?? this.label);
container.appendChild(textNode);
}
render() {
const container = document.createElement("dyte-menu-item");
container.onclick = () => {
if (this.participant) {
this._onClick(this.participant.id);
}
};
this.updateLabel(container);
this.onStateChange(this.participant.id, (state) => {
this.state = state;
this.updateLabel(container);
});
this.shadow.appendChild(container);
}
connectedCallback() {
this.render();
this.createStyles();
}
}