This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
91 lines (75 loc) · 2.75 KB
/
index.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
import { LitElement } from 'lit';
import { useStore } from '../../common/utils/use-store';
import config from '../../services/config';
import { variableStyle, typography, svHr, iconButtonStyle } from './styles';
import { Constructor, WebComponentsBaseInterface } from './types';
export const WebComponentsBase = <T extends Constructor<LitElement>>(superClass: T) => {
class WebComponentsBaseClass extends superClass {
private unsubscribeFrom: Array<(id: unknown) => void> = [];
protected useStore = useStore.bind(this) as typeof useStore;
static styles = [
variableStyle,
typography,
svHr,
iconButtonStyle,
(superClass as unknown as typeof LitElement).styles ?? [],
];
public connectedCallback() {
setTimeout(() => {
const rootStyleElement = document.getElementById('superviz-style');
const colorsStyleElement = this.createCustomColors();
const style = document.createElement('style');
style.innerHTML = rootStyleElement?.innerHTML || '';
this.shadowRoot?.appendChild(style);
if (colorsStyleElement) {
this.shadowRoot?.appendChild(colorsStyleElement);
}
});
super.connectedCallback();
}
/**
* @function disconnectedCallback
* @description Unsubscribes from all the subjects
* @returns {void}
*/
public disconnectedCallback() {
super.disconnectedCallback();
this.unsubscribeFrom.forEach((unsubscribe) => unsubscribe(this));
}
/**
* @function createCustomColors
* @description Creates a custom style tag with the colors from the configuration
* @returns {HTMLStyleElement} - The style tag with the colors
*/
private createCustomColors(): HTMLStyleElement {
if (!config.get('colors')) return;
const tag = document.createElement('style');
const readyColors = Object.entries(config.get('colors'))
.map(([key, value]) => `--${key}: ${value} !important;`)
.join(' ');
tag.innerHTML = `
* {
${readyColors}
}
`;
return tag;
}
/**
* @function emitEvent
* @description Emits a custom event with the given name, detail and optional configuration
* @param {string} name - The name of the custom even
* @param {object} detail - The detail of the custom event
* @param {object} configs - The configuration of the custom event
* @returns {void}
*/
protected emitEvent(
name: string,
detail: object,
configs: object = { composed: true, bubbles: true },
): void {
const event = new CustomEvent(name, { detail, ...configs });
this.dispatchEvent(event);
}
}
return WebComponentsBaseClass as unknown as Constructor<WebComponentsBaseInterface> & T;
};