forked from caracal-js/Incognito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.js
39 lines (33 loc) · 1.06 KB
/
component.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
36
37
38
39
function createComponent(element) {
return new Proxy({}, {
get: (target, prop) => {
if (prop === 'target') return element;
if (prop === 'text') return element.textContent;
if (prop === 'style') return element.style;
if (prop === 'clear') return clear;
return target[prop];
},
set: (target, prop, val) => {
if (prop === 'text') return (element.textContent = val, true);
if (typeof val === 'string') val = document.createElement(val);
if (prop in target) {
target[prop].remove();
delete target[prop];
};
element.append(val);
target[prop] = val
return true;
},
deleteProperty: (target, prop) => {
target[prop].remove();
return delete target[prop];
},
});
function clear() {
this.text = '';
for (const key in this) {
delete this[key];
};
};
};
export { createComponent };