Created
March 13, 2019 10:13
-
-
Save iankit3/d6ec25052f7fa2b2db820f9df0e044f2 to your computer and use it in GitHub Desktop.
JS Bin WebComponents Example // source https://jsbin.com/royufaquka
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> | |
<meta name="description" content="WebComponents Example"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<my-component></my-component> | |
<script id="jsbin-javascript"> | |
class MyComponent extends HTMLElement{ | |
constructor(){ | |
super(); | |
this.classList.add("my-component"); | |
this.addEventListener("click", (ev) => { | |
this.style.color = this.style.color == "red" ? "#000" : "red"; | |
}); | |
var shadow = this.attachShadow({mode: 'open'}); | |
var div = document.createElement('div'); | |
var text = document.createTextNode("A simple web component"); | |
div.appendChild(text); | |
var style = document.createElement('style'); | |
style.innerHTML = `:host{display: block;background : #ddd}`; | |
shadow.appendChild(style); | |
shadow.appendChild(div); | |
} | |
} | |
customElements.define('my-component', MyComponent); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">class MyComponent extends HTMLElement{ | |
constructor(){ | |
super(); | |
this.classList.add("my-component"); | |
this.addEventListener("click", (ev) => { | |
this.style.color = this.style.color == "red" ? "#000" : "red"; | |
}); | |
var shadow = this.attachShadow({mode: 'open'}); | |
var div = document.createElement('div'); | |
var text = document.createTextNode("A simple web component"); | |
div.appendChild(text); | |
var style = document.createElement('style'); | |
style.innerHTML = `:host{display: block;background : #ddd}`; | |
shadow.appendChild(style); | |
shadow.appendChild(div); | |
} | |
} | |
customElements.define('my-component', MyComponent); | |
</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
class MyComponent extends HTMLElement{ | |
constructor(){ | |
super(); | |
this.classList.add("my-component"); | |
this.addEventListener("click", (ev) => { | |
this.style.color = this.style.color == "red" ? "#000" : "red"; | |
}); | |
var shadow = this.attachShadow({mode: 'open'}); | |
var div = document.createElement('div'); | |
var text = document.createTextNode("A simple web component"); | |
div.appendChild(text); | |
var style = document.createElement('style'); | |
style.innerHTML = `:host{display: block;background : #ddd}`; | |
shadow.appendChild(style); | |
shadow.appendChild(div); | |
} | |
} | |
customElements.define('my-component', MyComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment