Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Created August 6, 2020 01:17
Show Gist options
  • Save tomhodgins/1c2d3c432f253204253fb8e9106674c0 to your computer and use it in GitHub Desktop.
Save tomhodgins/1c2d3c432f253204253fb8e9106674c0 to your computer and use it in GitHub Desktop.
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