Last active
April 26, 2017 22:18
-
-
Save C-Rodg/fb43188fcc94775507a404cf93b72039 to your computer and use it in GitHub Desktop.
An example of custom HTML elements and using the shadow DOM.
This file contains hidden or 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
customElements.define('my-element', class extends HTMLElement { | |
constructor() { | |
super(); | |
const shadow = this.attachShadow({mode: 'open'}); | |
shadow.innerHTML = ` | |
<style> #custom-shadow { color: red; } ::slotted(h1) { font-size: 38px; } :host { width: 600px; }</style> | |
<slot id="headerSlot" name="header"></slot> | |
<div id="custom-shadow">My custom shadow element</div> | |
<slot id="footerSlot" name="footer"></slot> | |
`; | |
this.addEventListener('click', e => { | |
this.toggleSomething(); | |
}); | |
} | |
toggleSomething() { | |
//... | |
} | |
get disabled() { | |
return this.hasAttribute('disabled'); | |
} | |
set disabled(val) { | |
if (val) { | |
this.setAttribute('disabled', ''); | |
} else { | |
this.removeAttribute('disabled'); | |
} | |
} | |
}); | |
// <my-element> | |
// <h1 slot="header">My Header Slot</h1> | |
// <button slot="footer">Logout</button> | |
// <a slot="footer">Copyright info</a> | |
// </my-element> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment