Skip to content

Instantly share code, notes, and snippets.

@nirlanka
Created January 2, 2020 08:32
Show Gist options
  • Save nirlanka/bc188e91fb0db52fd3f18303948e1e8b to your computer and use it in GitHub Desktop.
Save nirlanka/bc188e91fb0db52fd3f18303948e1e8b to your computer and use it in GitHub Desktop.
Web component experiment
<!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>
(() => {
if ('customElements' in window) {
customElements.whenDefined('x-one').then(() => {
console.log('x-one defined');
});
}
})();
(() => {
const template = document.createElement('template');
template.innerHTML = `
<style>
</style>
<slot>
</slot>
`;
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-button connected');
}
disconnectedCallback() {}
attributeChangedCallback(attrName, oldVal, newVal) {
console.log(attrName, oldVal, newVal);
}
}
if ('customElements' in window) {
customElements.define('x-button', El);
}
})();
(() => {
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