Comments (3)
@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.
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.
Here's a screenshot of the above
![Screenshot 2024-05-01 at 18 15 09](https://private-user-images.githubusercontent.com/4672033/327251711-6756b0d3-0948-4028-a38c-ae6a758d0641.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk2NzA0OTYsIm5iZiI6MTcxOTY3MDE5NiwicGF0aCI6Ii80NjcyMDMzLzMyNzI1MTcxMS02NzU2YjBkMy0wOTQ4LTQwMjgtYTM4Yy1hZTZhNzU4ZDA2NDEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYyOSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MjlUMTQwOTU2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDliNTY1YjhmZmI5NzZjYzM2NDFhNzJiZjEzNTcwMDA0ZTFmOTA4M2QxNDIyMDAxODlhN2QyODU0MzQ0NjlhOCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.rD3ufN7L_JYjUpbRmYcjAVWBUivq8AECOiq9ZAz_G0o)
from extension.js.
Related Issues (20)
- How to include polyfills for node apis? HOT 3
- Can extension.js add some common utility methods? HOT 1
- Consider custom support for authentication
- Allow to develop on any Chromium-based Browser HOT 1
- Live reloading with custom JS HOT 3
- Feature: Outlook extension HOT 4
- Setting the execution environment for content script to `MAIN` throws an error HOT 1
- Could not resolve path ./scripts/content.js. Either add it to the "public" directory or create a script in the "scripts" directory. HOT 1
- Custom Tailwindcss theme with Extension.js HOT 6
- Mainfest file have issue on icons path HOT 3
- Can't load "content_scripts/content-0.js" ,Because it's not UTF-8 encoded HOT 8
- The styles in the vue-typescript template are missing.
- After running the build command, both the images and styles are missing. HOT 1
- Import styles in development mode do not take effect HOT 3
- Improve monorepo release strategy
- Files imported from a "public/" path should not be emitted elsewhere
- Creating window with more than one url leads to error HOT 1
- Zipped artifacts should not be bound to the browser
- Is there a method to avoid style lack? HOT 2
- Support non-standard cross-browser manifest fields HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from extension.js.