forked from polylib/polylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-mixin.js
35 lines (33 loc) · 1.06 KB
/
template-mixin.js
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
import {TemplateInstance} from "./template.js";
const PlTemplateMixin = s => class plTplMixin extends s {
/**
* @constructor
* @param {object} [config]
* @param {boolean} [config.lightDom] - Использование LightDom вместо ShadowDom
*/
constructor(config) {
super();
this.root = config?.lightDom ? this : this.attachShadow({ mode: 'open' });
}
connectedCallback() {
super.connectedCallback();
let tpl = this.constructor.template;
if (tpl) {
let inst = new TemplateInstance(tpl);
this._ti = inst;
inst.attach(this);
}
// append styles
if (this.constructor.css) {
if (this.constructor.css instanceof CSSStyleSheet) {
if (this.root.adoptedStyleSheets) this.root.adoptedStyleSheets = [this.constructor.css];
} else {
this.root.append(this.constructor.css)
}
}
}
disconnectedCallback() {
this._ti?.detach();
}
}
export {PlTemplateMixin};