Created
May 5, 2021 12:23
-
-
Save smpnjn/d9268bb3c9b92819f286edfcebd150a4 to your computer and use it in GitHub Desktop.
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 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