-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag-component.mjs
151 lines (138 loc) · 3.66 KB
/
tag-component.mjs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
A Multi Environment Component Wrapper / Creator
new Component('tag-tag')
tag, view, scope
*/
/*
Component.extend({
tag: "my-counter",
view: `
Count: <span>{{this.count}}</span>
<button on:click="this.increment()">+1</button>
`,
ViewModel: {
count: {default: 0},
increment() {
this.count++;
}
}
});
// Registers a Component for DOM and String usage
// when you create none arrpw functions you can use this in them
function render() {
// Test bind this.
this.innerHTML = this.view(this)
}
const comp = Component.define({
tag: 'my-counter',
view: ({ count }) => html`
Count: <span>{{count}}</span>
<button>+1</button>
`,
//viewModel: class extends HTMLElement {
viewModel: {
count = 0,
connectedCallback: function() {
this.querySelector('button').onclick = this.increment()
},
render,
increment() {
this.count++;
this.render()
}
}
})
// Returns component registers component
const myInst = new comp({
tag: 'my-counter',
view: ({ count = 0 }) => html`
Count: <span>{{count}}</span>
<button>+1</button>
`,
//viewModel: class extends HTMLElement {
viewModel: {
connectedCallback: function() {
this.querySelector('button').onclick = this.increment()
},
render,
increment() {
const count = Number(this.querySelector('span'))
count++
this.querySelector('span').innerText = count
}
}
})
tap(()=>{
// more here
},myInst.connected)
// returns component instance or string representation also registers the component if its not!
*/
//A ES Module that runs in the browser and any other environment that returns HTMLElement Conditional
const ifHTMLElement = typeof HTMLElement !== 'undefined' ? HTMLElement : class HTMLElement { }
export { ifHTMLElement as HTMLElement };
const MixinComponent = base=>class extends base {
// Used when not using extend or when calling super with arguments
constructor(tag = '', template = () => '', data = 'default') {
super()
this.tagName = tag
this.template = template.bind(this)
this.data = data
// Should Choose to register the element.
}
render() {
const { template, tagName, data } = this
const result = template(data)
this.innerHTML = result
return `<${tagName}>${result}</${tagName}>`
}
}
// minimum component can be also customElement
// customElements can trigger actions on insert
// customElements can encapsulate dom.
class myComponent extends MixinComponent(ifHTMLElement) {
tagName = 'my-tag'
template(data) {
return `${data}`
}
data = 'string'
}
/**
* About values
* they can come from user input
* they can come from external source
* they can be coded
* when a value changes it should call render or not
*/
class myListComponent extends MixinComponent(ifHTMLElement) {
tagName = 'my-tag'
//data should be a array
template(data) {
return `${data}`
}
data = 'string'
}
class pageComponent extends MixinComponent(ifHTMLElement) {
tagName = 'my-tag'
//data should be a array
template(data) {
return `${data}`
}
data = 'string'
}
// function pattern tag-html-component
function TagHtmlComponent(data) {
return html`<p></p>`
}
//class pattern tag-html-component-class
class TagHtmlComponentClass {
constructor(data) {
this.data = data
}
template() {
const { data } = this;
return html`<p>${data}`
}
render() {
return this.template()
}
}