Created
August 6, 2020 01:17
-
-
Save tomhodgins/1c2d3c432f253204253fb8e9106674c0 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
function evaluateTemplate(template = '', html = '') { | |
const tag = Object.assign( | |
document.createElement('shadow-dom'), | |
{innerHTML: html} | |
) | |
tag.attachShadow({mode: 'open'}) | |
tag.shadowRoot.innerHTML = template | |
// Replace <slot> with contents (flatten) | |
tag.shadowRoot.querySelectorAll('slot').forEach(tag => | |
tag.replaceWith(...tag.assignedNodes()) | |
) | |
// Strip slot="" attributes from previously slotted content | |
tag.shadowRoot.querySelectorAll('[slot]').forEach(tag => | |
tag.removeAttribute('slot') | |
) | |
return tag.shadowRoot | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment