Skip to content

Instantly share code, notes, and snippets.

@pinkhominid
Last active June 5, 2022 13:15
Show Gist options
  • Save pinkhominid/21128775619699dc26df152479949cd0 to your computer and use it in GitHub Desktop.
Save pinkhominid/21128775619699dc26df152479949cd0 to your computer and use it in GitHub Desktop.
Web Component starter
customElements.define('my-component', class extends HTMLElement {
static get observedAttributes() {
return [
// attrs
];
}
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.render();
}
attributeChangedCallback(name, oldValue, newValue) {
switch(name) {
// type converters
default:
this[name] = newValue;
}
this.render();
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
}
:host([hidden]) {
display: none !important;
}
</style>
<slot></slot>
`;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment