Last active
February 25, 2020 15:17
-
-
Save sbatson5/81dfd628748b506c6cd2bb8c5a4db3f1 to your computer and use it in GitHub Desktop.
Step 4
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 CandidateImage extends HTMLElement { | |
connectedCallback() { | |
this.shadow = this.attachShadow({ mode: 'open' }); | |
// Create an image | |
const image = new Image(); | |
// Find the right image for the provided name | |
if (this.getAttribute('name') === 'pete') { | |
image.src = 'https://cdn-candidates.com/pete.jpg'; | |
} else if (this.getAttribute('name') === 'bernie') { | |
image.src = 'https://cdn-candidates.com/bernie.jpg'; | |
} | |
// We can use appendChild just like we do the normal document | |
this.shadow.appendChild(image); | |
} | |
} | |
/** | |
* Register our custom element | |
*/ | |
window.addEventListener('DOMContentLoaded', () => { | |
customElements.define('candidate-image', CandidateImage); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment