Skip to content

How to insert <style>...</style> into <head> of Shadow Root #1600

Answered by mengxi-ream
mengxi-ream asked this question in Q&A
Discussion options

You must be logged in to vote

solve by:

onMount: (container, shadow) => {
        const wrapper = document.createElement("div");
        container.appendChild(wrapper);

        const root = ReactDOM.createRoot(wrapper);
        document.head.querySelectorAll("style").forEach((styleEl) => {
          if (styleEl.textContent?.includes("[data-sonner-toaster]")) {
            const shadowHead = shadow.querySelector("head");
            if (shadowHead) {
              shadowHead.append(styleEl);
            } else {
              shadow.append(styleEl);
            }
            console.log("styleEl.textContent", styleEl);
          }
        });

        ...
      },
      ```

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mengxi-ream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant