I am building a widget for third-party websites, using shadow DOM to prevent their CSS from interfering with ours. I am using the ShadyDOM and ShadyCSS polyfills to make it work in Edge and IE, but it is not transforming the CSS for the shadow DOM as I would expect.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Shadow DOM test</title>
</head>
<body>
<div id="container">container is here</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/2.3.0/webcomponents-bundle.js"></script>
<script>
const shadow = document.getElementById("container").attachShadow({ mode: "open" });
const style = document.createElement("style");
style.innerHTML = `
:host .stuff {
background: #ff00ff;
}
`;
shadow.appendChild(style);
const div = document.createElement("div");
div.classList.add("stuff");
div.innerHTML = "stuff inside shadow dom";
shadow.appendChild(div);
</script>
</body>
</html>
In Chrome (which supports shadow DOM natively), the stuff
div has a pink background, as I would expect. But in Edge (which does not support shadow DOM natively), I see the "stuff inside shadow dom" text (meaning my script ran and the ShadyDOM functions worked), but I don't see the pink background.
Why is this happening? I am attaching a shadow root to a plain old div, instead of using custom elements as the example in the ShadyCSS README does, but does that matter? If so, how can I make this work? I am working on a big, existing app, and not wanting to make too many changes at once, so I would strongly prefer to use the standard HTML elements I am already using (div
s, button
s, etc.) instead of coming up with my own elements or templates, although I would be willing to consider templates and/or custom elements if it can be done easily, without having to make a lot of big changes.
style
element directly to the innerdiv
instead of the shadow root, appending thestyle
element after appending the innerdiv
, and setting theinnerHTML
of thestyle
element after appending it. But none of those things helped. – Calumny:host
from the CSS selector, then the styles get applied. But they also get applied to things outside the shadow root, which I don't want. – CalumnyShadyCSS.styleElement(element)
andShadyCSS. styleSubtree(element)
and they didn't work. – Calumnystyle
tags in the right place and adjust the dimensions appropriately. It is slow at rendering and cumbersome to deal with, so I changed it to use shadow DOM. Now, it works beautifully in all non-Microsoft browsers. I am trying to make it work in Edge and IE using the polyfill, but it is not working. – Calumny@webcomponents/shadydom
polyfill, and also@webcomponents/shadydom
with@webcomponents/shadycss
, and that didn't seem to do anything helpful. – Calumny