Skip to content

Instantly share code, notes, and snippets.

@bdelacretaz
Created January 19, 2023 10:23
Show Gist options
  • Save bdelacretaz/ef6d21086fdf2382299b50cc0f9c137a to your computer and use it in GitHub Desktop.
Save bdelacretaz/ef6d21086fdf2382299b50cc0f9c137a to your computer and use it in GitHub Desktop.
Declarative Shadow DOM Polyfill
// From https://web.dev/declarative-shadow-dom/
// TODO do nothing in a browser that already supports declarative shadow DOM?
(function attachShadowRoots(root) {
root.querySelectorAll("template[shadowroot]").forEach(template => {
const mode = template.getAttribute("shadowroot");
const shadowRoot = template.parentNode.attachShadow({ mode });
shadowRoot.appendChild(template.content);
template.remove();
attachShadowRoots(shadowRoot);
});
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment