1
- class WebComponent extends HTMLElement {
1
+ export class WebComponent extends HTMLElement {
2
+
3
+ static tagName = 'web-component' ;
4
+
5
+ static eventNames = {
6
+ statechange : this . tagName + '.statechange'
7
+ } ;
2
8
3
9
static htmlString = null ;
4
10
@@ -21,7 +27,17 @@ class WebComponent extends HTMLElement {
21
27
this . shadowRoot . adoptedStyleSheets . push ( styles ) ;
22
28
23
29
// state
24
- this . state = { } ;
30
+ this . state = new Proxy ( { } , {
31
+ set : ( target , prop , value ) => {
32
+ target [ prop ] = value ;
33
+ const name = this . constructor . eventNames . statechange ;
34
+ const detail = { element : this , prop, value } ;
35
+ const payload = { detail, bubbles : true , composed : true } ;
36
+ const event = new CustomEvent ( name , payload ) ;
37
+ this . dispatchEvent ( event ) ;
38
+ return true ;
39
+ }
40
+ } ) ;
25
41
}
26
42
27
43
static get observedAttributes ( ) {
@@ -49,6 +65,11 @@ class WebComponent extends HTMLElement {
49
65
}
50
66
}
51
67
68
+ /**
69
+ * HTML template as string
70
+ *
71
+ * @type {string }
72
+ */
52
73
WebComponent . htmlString = `
53
74
<h1>
54
75
<slot></slot>
@@ -58,6 +79,11 @@ WebComponent.htmlString = `
58
79
</p>
59
80
` ;
60
81
82
+ /**
83
+ * CSS styles as string
84
+ *
85
+ * @type {string }
86
+ */
61
87
WebComponent . cssString = `
62
88
:host {
63
89
display: block;
@@ -80,5 +106,3 @@ WebComponent.cssString = `
80
106
color: blue;
81
107
}
82
108
` ;
83
-
84
- customElements . define ( 'web-component' , WebComponent ) ;
0 commit comments