@alexgyver/component 1.0.25 → 1.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/component.js +34 -2
- package/example/script.js +24 -0
- package/package.json +1 -1
package/component.js
CHANGED
|
@@ -39,6 +39,36 @@ export class Component {
|
|
|
39
39
|
return Component.config(document.createElement(tag), data);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
44
|
+
* @param {string|Node} host html tag теневого элемента или Node
|
|
45
|
+
* @param {object} data параметры внешнего элемента
|
|
46
|
+
* @param {string} sheet css стили
|
|
47
|
+
* @returns {Node} host
|
|
48
|
+
*/
|
|
49
|
+
static makeShadow(host, data = {}, sheet = null) {
|
|
50
|
+
if (!host || typeof data !== 'object') return null;
|
|
51
|
+
|
|
52
|
+
let $host = (host instanceof Node) ? host : document.createElement(host);
|
|
53
|
+
$host.attachShadow({ mode: 'open' });
|
|
54
|
+
|
|
55
|
+
Component.config($host.shadowRoot, {
|
|
56
|
+
context: data.context,
|
|
57
|
+
children: [
|
|
58
|
+
{
|
|
59
|
+
tag: 'style',
|
|
60
|
+
textContent: sheet ?? '',
|
|
61
|
+
},
|
|
62
|
+
data.child ?? {},
|
|
63
|
+
...(data.children ?? []),
|
|
64
|
+
]
|
|
65
|
+
});
|
|
66
|
+
delete data.children;
|
|
67
|
+
delete data.child;
|
|
68
|
+
Component.config($host, data);
|
|
69
|
+
return $host;
|
|
70
|
+
}
|
|
71
|
+
|
|
42
72
|
/**
|
|
43
73
|
* Настроить элемент
|
|
44
74
|
* @param {Node} el элемент
|
|
@@ -46,7 +76,7 @@ export class Component {
|
|
|
46
76
|
* @returns {Node}
|
|
47
77
|
*/
|
|
48
78
|
static config(el, data) {
|
|
49
|
-
if (!(el instanceof Node)) return el;
|
|
79
|
+
if (!(el instanceof Node) || (typeof data !== 'object')) return el;
|
|
50
80
|
const context = data.context;
|
|
51
81
|
|
|
52
82
|
let addChild = (obj) => {
|
|
@@ -63,6 +93,7 @@ export class Component {
|
|
|
63
93
|
}
|
|
64
94
|
|
|
65
95
|
for (const [key, val] of Object.entries(data)) {
|
|
96
|
+
if (!val) continue;
|
|
66
97
|
switch (key) {
|
|
67
98
|
case 'tag':
|
|
68
99
|
case 'context':
|
|
@@ -74,7 +105,7 @@ export class Component {
|
|
|
74
105
|
case 'export': val[0] = el; break;
|
|
75
106
|
case 'var': if (context) context['$' + val] = el; break;
|
|
76
107
|
case 'events': for (let ev in val) if (val[ev]) el.addEventListener(ev, val[ev].bind(context)); break;
|
|
77
|
-
case 'parent': if (val instanceof
|
|
108
|
+
case 'parent': if (val instanceof Node || val instanceof DocumentFragment) val.append(el); break;
|
|
78
109
|
case 'attrs': for (let attr in val) el.setAttribute(attr, val[attr]); break;
|
|
79
110
|
case 'props': for (let prop in val) el[prop] = val[prop]; break;
|
|
80
111
|
case 'child': addChild(val); break;
|
|
@@ -95,6 +126,7 @@ export class Component {
|
|
|
95
126
|
* @returns {array} of Elements
|
|
96
127
|
*/
|
|
97
128
|
static makeArray(arr) {
|
|
129
|
+
if (!arr || !Array.isArray(arr)) return [];
|
|
98
130
|
return arr.map(x => Component.make(x.tag, x));
|
|
99
131
|
}
|
|
100
132
|
}
|
package/example/script.js
CHANGED
|
@@ -87,6 +87,30 @@ function Container(children) {
|
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
class ShadowComponent {
|
|
91
|
+
constructor() {
|
|
92
|
+
Component.makeShadow('div', {
|
|
93
|
+
context: this,
|
|
94
|
+
parent: document.body,
|
|
95
|
+
events: {
|
|
96
|
+
click: () => this.$div.dispatchEvent(new Event('kek', { bubbles: true, composed: true })),
|
|
97
|
+
},
|
|
98
|
+
children: [
|
|
99
|
+
{
|
|
100
|
+
tag: 'div',
|
|
101
|
+
text: 'Hello!',
|
|
102
|
+
class: 'myclass',
|
|
103
|
+
var: 'div',
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}, '.myclass{color:red;}'
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
document.addEventListener('kek', () => console.log('kek!'));
|
|
113
|
+
|
|
90
114
|
document.addEventListener("DOMContentLoaded", () => {
|
|
91
115
|
Component.make('h1', {
|
|
92
116
|
text: 'Hello!',
|