Skip to content

Instantly share code, notes, and snippets.

@smpnjn
Created May 5, 2021 12:23
Show Gist options
  • Save smpnjn/d9268bb3c9b92819f286edfcebd150a4 to your computer and use it in GitHub Desktop.
Save smpnjn/d9268bb3c9b92819f286edfcebd150a4 to your computer and use it in GitHub Desktop.
class Paragraph extends HTMLElement {
constructor() {
super()
// Attach shadow DOM
let shadow = this.attachShadow({mode: 'open'});
// Append our Paragraph
shadow.innerHTML = `
<slot name="paragraph-text"><p>Default Text</p></slot>
<p>Another, unchangeable paragraph</p>
`;
// Our custom color
let color = 'red';
if(this.getAttribute('color') !== null) {
color = this.getAttribute('color');
}
// Add in our CSS
let style = document.createElement('style');
let elCss = style.textContent = `
p {
color: ${color};
font-size: 1.25rem;
}
`;
// Append our CSS
shadow.appendChild(style);
}
}
customElements.define('alpha-paragraph', Paragraph);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment