1
- import { h , render } from 'preact' ;
2
-
3
- const Empty = ( ) => null ;
1
+ import { h , cloneElement , render } from 'preact' ;
4
2
5
3
export default function register ( Component , tagName , propNames ) {
6
4
function PreactElement ( ) {
@@ -10,27 +8,35 @@ export default function register(Component, tagName, propNames) {
10
8
}
11
9
PreactElement . prototype = Object . create ( HTMLElement . prototype ) ;
12
10
PreactElement . prototype . constructor = PreactElement ;
13
- PreactElement . prototype . connectedCallback = renderElement ;
14
- PreactElement . prototype . attributeChangedCallback = renderElement ;
15
- PreactElement . prototype . detachedCallback = unRenderElement ;
16
- PreactElement . observedAttributes = propNames ;
11
+ PreactElement . prototype . connectedCallback = connectedCallback ;
12
+ PreactElement . prototype . attributeChangedCallback = attributeChangedCallback ;
13
+ PreactElement . prototype . detachedCallback = detachedCallback ;
14
+ PreactElement . observedAttributes = propNames || Component . observedAttributes || Object . keys ( Component . propTypes || { } ) ;
17
15
18
- return window . customElements . define (
19
- tagName || Component . displayName || Component . name ,
16
+ return customElements . define (
17
+ tagName || Component . tagName || Component . displayName || Component . name ,
20
18
PreactElement
21
19
) ;
22
20
}
23
21
24
- function renderElement ( ) {
25
- this . _root = render ( toVdom ( this , this . _vdomComponent ) , this , this . _root ) ;
22
+ function connectedCallback ( ) {
23
+ this . _vdom = toVdom ( this , this . _vdomComponent ) ;
24
+ render ( this . _vdom , this ) ;
25
+ }
26
+
27
+ function attributeChangedCallback ( name , oldValue , newValue ) {
28
+ const props = { } ;
29
+ props [ name ] = newValue ;
30
+ this . _vdom = cloneElement ( this . _vdom , props ) ;
31
+ render ( this . _vdom , this ) ;
26
32
}
27
33
28
- function unRenderElement ( ) {
29
- render ( h ( Empty ) , this , this . _root ) ;
34
+ function detachedCallback ( ) {
35
+ render ( this . _vdom = null , this ) ;
30
36
}
31
37
32
38
function toVdom ( element , nodeName ) {
33
- if ( element . nodeType === 3 ) return element . nodeValue ;
39
+ if ( element . nodeType === 3 ) return element . data ;
34
40
if ( element . nodeType !== 1 ) return null ;
35
41
let children = [ ] ,
36
42
props = { } ,
0 commit comments