Created
January 2, 2020 08:32
-
-
Save nirlanka/bc188e91fb0db52fd3f18303948e1e8b to your computer and use it in GitHub Desktop.
Web component experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<x-one> | |
<p>Lorem ipsum</p> | |
Dolor sit amet | |
</x-one> | |
<br> | |
<button is="x-button">Haha</button> | |
<script src="./main.js"></script> | |
<script src="./x-one.js"></script> | |
<script src="./x-button.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
if ('customElements' in window) { | |
customElements.whenDefined('x-one').then(() => { | |
console.log('x-one defined'); | |
}); | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
const template = document.createElement('template'); | |
template.innerHTML = ` | |
<style></style> | |
<strong> | |
<slot></slot> | |
</strong> | |
`; | |
class El extends HTMLElement { | |
static get observedAttributes() { | |
return ['color']; | |
} | |
get color() { return this.getAttribute('color'); } | |
set color(val) { this.setAttribute('color', val); } | |
constructor() { | |
super(); | |
const shadowRoot = this.attachShadow({ mode: 'open' }); | |
shadowRoot.appendChild(template.content.cloneNode(true)); | |
this.addEventListener('click', e => { | |
console.log(e); | |
}); | |
} | |
connectedCallback() { | |
console.log('x-one connected'); | |
} | |
disconnectedCallback() {} | |
attributeChangedCallback(attrName, oldVal, newVal) { | |
console.log(attrName, oldVal, newVal); | |
} | |
} | |
if ('customElements' in window) { | |
customElements.define('x-one', El); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment