From 827d3903247692356d8eda55c55d043e114c238a Mon Sep 17 00:00:00 2001 From: Dhravya Date: Sat, 29 Jun 2024 16:24:37 -0500 Subject: [PATCH] tailwind shadow dom --- apps/extension/content/ContentApp.tsx | 18 ++++--------- apps/extension/content/base.css | 4 +-- apps/extension/content/content.tsx | 39 +++++++++++++++++++-------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/apps/extension/content/ContentApp.tsx b/apps/extension/content/ContentApp.tsx index fa3ae71b..ca1782c1 100644 --- a/apps/extension/content/ContentApp.tsx +++ b/apps/extension/content/ContentApp.tsx @@ -29,19 +29,11 @@ export default function ContentApp() { return (
-
- -
+
); } diff --git a/apps/extension/content/base.css b/apps/extension/content/base.css index 0e1e9c09..c8845aea 100644 --- a/apps/extension/content/base.css +++ b/apps/extension/content/base.css @@ -1,6 +1,6 @@ -@tailwind base; +/* @tailwind base; @tailwind components; -@tailwind utilities; +@tailwind utilities; */ #supermemory-extension-host { position: fixed; diff --git a/apps/extension/content/content.tsx b/apps/extension/content/content.tsx index a0879826..df66cab0 100644 --- a/apps/extension/content/content.tsx +++ b/apps/extension/content/content.tsx @@ -1,33 +1,50 @@ import ReactDOM from "react-dom/client"; import ContentApp from "./ContentApp"; +import("./base.css"); setTimeout(initial, 1000); -const appendTailwindStyleData = (shadowRoot: ShadowRoot) => { - const styleSheet = document.createElement("style"); +const TAILWIND_URL = + "https://cdn.jsdelivr.net/npm/tailwindcss@^2.0/dist/tailwind.min.css"; - const resourceUrl = chrome.runtime.getURL("../public/output.css"); - const tobeRendered = fetch(resourceUrl) - .then((res) => res.text()) - .then((data) => { - console.log(data); - styleSheet.textContent = data; +const appendTailwindStyleData = (shadowRoot: ShadowRoot) => { + // Load and inject the local Tailwind CSS file into the shadow DOM + const styleSheet = document.createElement("style"); + // Change this path to your actual Tailwind CSS file's location + fetch(TAILWIND_URL) + .then((response) => response.text()) + .then((css) => { + styleSheet.textContent = css; shadowRoot.appendChild(styleSheet); }); }; +const appendTailwindStyleLink = (shadowRoot: ShadowRoot) => { + // Import Tailwind CSS and inject it into the shadow DOM + const styleSheet = document.createElement("link"); + styleSheet.rel = "stylesheet"; + styleSheet.href = TAILWIND_URL; + shadowRoot.appendChild(styleSheet); +}; + function initial() { + // Create a new div element to host the shadow root. + // Styles for this div is in `content/content.css` const hostDiv = document.createElement("div"); - hostDiv.id = "supermemory-extension-host"; + hostDiv.id = "extension-host"; document.body.appendChild(hostDiv); + // Attach the shadow DOM to the hostDiv and set the mode to + // 'open' for accessibility from JavaScript. + // Ref https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow const shadowRoot = hostDiv.attachShadow({ mode: "open" }); + // Create a new div element that will be the root container for the React app const rootDiv = document.createElement("div"); - rootDiv.id = "supermemor-extension-root"; + rootDiv.id = "extension-root"; shadowRoot.appendChild(rootDiv); - appendTailwindStyleData(shadowRoot); + appendTailwindStyleLink(shadowRoot); const root = ReactDOM.createRoot(rootDiv); root.render();