Skip to content

Instantly share code, notes, and snippets.

@C-Rodg
Last active April 26, 2017 22:18
Show Gist options
  • Save C-Rodg/fb43188fcc94775507a404cf93b72039 to your computer and use it in GitHub Desktop.
Save C-Rodg/fb43188fcc94775507a404cf93b72039 to your computer and use it in GitHub Desktop.
An example of custom HTML elements and using the shadow DOM.
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