Giter Site home page Giter Site logo

Comments (3)

Diveafall avatar Diveafall commented on June 29, 2024 1

@cezaraugusto Would be a great addition to the documentation!

Also, the sample looks great and pretty straightforward. Thank you for making it!

Should be more than enough for my use case. Our extension uses MUI as the UI library, but adding it shouldn't be an issue. Below is the code for people who'd be interested:

import createCache from "@emotion/cache"
import { CacheProvider } from "@emotion/react"
import {
  createTheme,
  Stack,
  ThemeProvider,
  Typography
} from "@mui/material"

const styleElement = document.createElement("style")

const styleCache = createCache({
  key: "chatbot-cache",
  prepend: true,
  container: styleElement
})

export const getStyle = () => styleElement

const theme = createTheme({
  typography: {
    button: {
      textTransform: "none"
    }
  }
})

const Chatbot = () => {
  return (
    <CacheProvider value={styleCache}>
      <ThemeProvider theme={theme}>
        <Stack>
          <Typography>This should work!</Typography>
        </Stack>
      </ThemeProvider>
    </CacheProvider>
  )
}

export default Chatbot

Again, thank you for your help!

from extension.js.

cezaraugusto avatar cezaraugusto commented on June 29, 2024

hi @Diveafall! thanks a lot!

I don't have a short-term plan to create an abstraction for that, but seems something worth adding to the documentation for future reference.

I created a sample running Tailwind.css in a content_script via Shadow DOM, let me know if it fits your use-case. Link https://github.com/cezaraugusto/content-shadow-dom-tailwind:

npx extension@latest dev https://github.com/cezaraugusto/content-shadow-dom-tailwind

Here's the code I used.

import ReactDOM from "react-dom/client";
import ContentApp from "./ContentApp";
import("./base.css");
import("./content.css");

setTimeout(initial, 1000);

const TAILWIND_URL =
  "https://cdn.jsdelivr.net/npm/tailwindcss@^2.0/dist/tailwind.min.css";

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 = "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 = "extension-root";
  shadowRoot.appendChild(rootDiv);

  // You can use either `appendTailwindStyleData` or `appendTailwindStyleLink`
  // to inject Tailwind CSS into the shadow DOM.

  // Option 1: Inject Tailwind CSS using a <style> tag
  // appendTailwindStyleLink(shadowRoot);

  // Option 2: Inject Tailwind CSS using a <link> tag
  appendTailwindStyleData(shadowRoot);

  // Use `createRoot` to create a root, then render the <ContentApp /> component
  const root = ReactDOM.createRoot(rootDiv);
  root.render(<ContentApp />);
}

from extension.js.

cezaraugusto avatar cezaraugusto commented on June 29, 2024

Here's a screenshot of the above

Screenshot 2024-05-01 at 18 15 09

from extension.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.