-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathaddress-custom-element.html
45 lines (43 loc) · 1.3 KB
/
address-custom-element.html
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
<template id="address_custom_element_tpl">
<style>
ul {
list-style: none;
padding: 0;
margin: 0;
}
</style>
<ul></ul>
</template>
<script type="module">
class AddressCustomElement extends HTMLElement {
static get observedAttributes() {
return ['address'];
}
constructor() {
super();
this.attachShadow({mode: 'open'});
const tpl = document.getElementById('address_custom_element_tpl');
this.shadowRoot.appendChild(tpl.content.cloneNode(true));
}
render() {
const ul = this.shadowRoot.querySelector('ul');
const address = this.getAttribute('address')
.split('\n')
.map(line => {
const li = document.createElement('li');
li.textContent = line;
return li;
});
ul.replaceChildren(...address);
}
connectedCallback() {
this.render();
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
this.render();
}
}
}
customElements.define('address-custom-element', AddressCustomElement);
</script>