svelte: :global(...) not working in custom elements
Whenever you use the :global() modifier in custom elements the css output will still contain the modifier.
To reproduce the issue you can use this repo: dylanblokhuis/svelte-global-modifier-issue
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 29
- Comments: 15 (1 by maintainers)
Commits related to this issue
- Fix for :global(...) styles in custom elements. Close #2969 — committed to kmmbvnr/svelte by kmmbvnr 5 years ago
- Fix for :global(...) styles in custom elements. Close #2969 — committed to kmmbvnr/svelte by kmmbvnr 5 years ago
- Fix for :global(...) styles in custom elements. Close #2969 — committed to kmmbvnr/svelte by kmmbvnr 5 years ago
- Fix ellipsis and mark style `:global(…)` doesn't work on custom elements. Here's a workaround suggested in https://github.com/sveltejs/svelte/issues/2969#issuecomment-1166306262 — committed to nuclia/frontend by mpellerin42 2 years ago
- Fix ellipsis and mark style (#242) `:global(…)` doesn't work on custom elements. Here's a workaround suggested in https://github.com/sveltejs/svelte/issues/2969#issuecomment-1166306262 — committed to nuclia/frontend by mpellerin42 2 years ago
- feat: custom elements rework (#8457) This is an overhaul of custom elements in Svelte. Instead of compiling to a custom element class, the Svelte component class is mostly preserved as-is. Instead a ... — committed to sveltejs/svelte by dummdidumm a year ago
Closed via #8457, to be released in Svelte 4
I also ran into the issue that svelte kept removing css because it thought it was unused. Instead of using
:globalI found a different workaround. I moved the css into a separate file and imported it inside the<script>block (I am using vite, it already comes with the necessary loaders).Then I used a
<svelte:element>to add the css to the shadow-root.This way svelte will not remove “unused” css. See the full example below:
+1 to this,
Until this is fixed, I used content-replace-webpack-plugin
To replace
:global()(used with <style global>)Update: is better to use this regex:
/:global\(([\[\]\(\)\-\.\:\*\w]+)\)/g(above was replacing some characters that shoud remain.Update 2: (Vite)
Can you use
vite-plugin-replaceDirty but as temp fix.
Is there already a solution for global styles with “customElement: true”? 😕
Any update on this or at least a workaround?
The idea of using :global is simply to be able to write CSS rules that are not in the component markup. Another solution could be to be able to disable globally the option to delete unused CSS.
Thanks for your work!
I tried to use preprocess on a custom element but couldn’t get it to work using :global(… Had a look through your other projects encase you had any examples but didn’t see anything in the rollup-config.
Native Web Components styles are isolated by the spec, aren’t they? May be that’s why Svelte doesn’t process their styles as they are applied only to a component by the platform itself.