-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to inject CSS files with using shadow dom for content scripts. #609
Comments
this is similar to what I have in my content-script.js. You should be able to adjust it to work at the component level too. Still has some HMR issues though.
|
@willjohnathan would that only add the root css? what about the css for like individual components? I usually import those within the component files themselves |
I'm actually not too familiar with react, but couldn't you just manually add a style tag. I suppose it would leak from compoenent to compenent though.
|
This is not working because, as far as I understand CRXJS Vite setup injects CSS import to the page header. If you work inside shadow dom, those styles are not accessible. That was my point, how can we make the vite add those styles in the component body when we use shadow dom? |
I tried this and couldn't make it work tbh. What I end up using is removing the shadow dom and using tailwind with config that adds auto important to all styles. I would love to have a solution where we can use Tailwind with shadow dom. I think it's a matter of a new vite config, but I have no idea how to do it. I don't know how to handle bundlers :D |
I use tailwind in the shadow dom similar to as mentioned above, but with Vue. I tried to do it with Svelte, but ran into a similar issue you are describing with vite still applying to the head as well. I couldn't do the prefix or important method, because it still would reset all the base html element styles (i.e. input, h2, etc.). Also, you might find this helpful, he modifies the vite file to look in the shadow-dom to update the css instead of the head (but I believe it was for HMR): |
I will check this out, thanks. I also disabled to base styles for tailwind. I simply use prefix, important and disabled base styles atm. I want to use shadow-dom but my lack of vite knowledge is preventing me from doing it atm :) |
It's not a bug. Vite sees If you add import styles from 'index.css?inline';
render(
<StrictMode>
<style type="text/css">{styles}</style>
<div><Component /></div>
</StrictMode>
); |
This works, but how do you handle HMR? With this approach, dev server doesn't see tailwind classes until you restart the dev server. |
Most of the time re-saving the |
in this case, how to handle 3rd lib css? |
It depends on the lib. If the lib comes with its own CSS that you need to import, you can do the same (add That's one of the reasons why headless libraries are getting more popular. They provide all the functionality, but 0 CSS and it is up to you and your design requirements how to style it. Examples: TanStack Table, Headless UI, Downshift |
Hey y'all, I found a work around that unblocks me in development. What I found is that if you
However, it is enough to get a little hack going. In my app i'm using svelte in the content script that is mounted in a closed shadow root. Ignoring the mounting code, I have a svelte component that looks something like <script lang="ts">
import css from "./../app.css?inline";
let styleTag = `<${""}style>${css}</style>`;
// WARNING: This is a hack
import "./../app.css";
let tag = document.querySelector('[data-vite-dev-id]')
if(tag instanceof HTMLStyleElement) {
tag.media = 'max-width: 0px';
styleTag = `<${""}style>${tag.innerText}</style>`;
}
</script>
{@html styleTag}
<h1 class="text-3xl bg-green-400">hello world</h1> Okay so to solve the first issue, I find the style tag that vite injects into the head, and I nullify it's effects on the page by setting Then for the second issue, I create a VIM autocmd It's obviously not ideal, but it gets HMR working with shadow root CSS with Tailwind in development which is good enough for me for now. |
Any chance you could show me the snippet of config to do the above @demirelio - I tried adding prefix and important but it still affects the base site. I'm probably missing this |
Thanks @thmsmlr for the suggestion. I'm currently using this in my nvim config: local augroup = vim.api.nvim_create_augroup("Tailwind HMR for crxjs", { clear = true })
local filetypes_to_check = { "typescriptreact", "javascriptreact", "svelte", "vue" }
vim.api.nvim_create_autocmd({ "BufWritePost" }, {
pattern = "*",
group = augroup,
callback = function()
if vim.tbl_contains(filetypes_to_check, vim.bo.ft) then
local result = vim.fn.filereadable("manifest.json")
if result == 1 then
vim.cmd("silent! !touch src/index.css")
end
end
end,
}) I don't really need to configure anything crazy in my project at all. Just auto reload the Edit: I ran into the problem of leaking Tailwind CSS into the main DOM. I'm using this in my export default {
plugins: {
"postcss-nested": {},
tailwindcss: {},
autoprefixer: {},
},
}; My #crx-root {
@tailwind base;
@tailwind components;
@tailwind utilities;
} Tailwind compiler will complain, but everything works fine for me atm. |
Thank you, @ziontee113, you saved my day!!!! |
Imo crxjs should have a feature specifically for injecting a react component into a shadow dom. I spent the entire day today figuring out how to handle css in shadowdom with crxjs. Right now what I’m doing is running building once, placing the bundled css into my public folder and then building it again. If anyone here had a better solution it would be very helpful. If there was a way to just include the bundled css in the manifest file directly under web accessible resources, that would not solve everything but it would certainly help. |
Another solution I have figured out right now is by using "vite-plugin-css-injected-by-js". I have the plugin inject the bundled css as a style tag with an id, whose css I then add to another style tag I create in the shadow dom. This fixes most of the issues with working with shadow dom and crxjs but sadly it's not compatible with the vite dev server some reason, so no hmr. However, I still think the tradeoff is worth it. I am now running vite build --watch and web-ext run to emulate a speedy dev environment. It's not quite hmr but is good enough considering I absolutely needed the css to work in a shadow dom. @IamFive this method also works with 3rd party libs. |
Build tool
Vite
Where do you see the problem?
Describe the bug
I created a shadow dom for my content scripts to prevent css leak from host website. I have my react component that import css file but it doesn't work inside shadow dom. When I control the network tab, I see that css file is actually accessible in host website but it doesn't work inside shadow dom. The css file is imported to component sits inside shadow dom.
CONTENT-ENTRY
APP
CSS
.big-title { color: purple; } .test { color: green; }
Reproduction
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: