-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
:global(...) not working in custom elements #2969
Comments
I was trying to build some custom elements with Svelte and encountered the same issue. I have found out that |
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. |
Until this is fixed you can use svelte-preprocess. the modifier does get compiled when using this |
I tried to use preprocess on a custom element but couldn't get it to work using :global(... |
I agree. But in that sense, any selector inside However, svelte currently does not touch I think it should at least be unwrapped. |
It seems :global style removing happens in the Stylesheet.transform (second arg) And that transformation explicitly disabled for custom elements https://github.com/sveltejs/svelte/blob/master/src/compiler/compile/render_dom/index.ts#L32 |
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! |
Is there already a solution for global styles with "customElement: true"? 😕 |
Cant you use :host in custom-elements web components ? |
+1 to this, Until this is fixed, I used content-replace-webpack-plugin To replace plugins: [
new ContentReplacePlugin({
rules: {
'**/*.js': content => content.replace(/:global\((.*?)\)/g, '$1')
}
})
] Update: is better to use this regex: Update 2: (Vite) Can you use plugins: [
replaceCodePlugin({
replacements: [
{
from: /:global\(([\[\]\(\)\-\.\:\*\w]+)\)/g,
to: "$1",
}
]
}),
] Dirty but as temp fix. |
I also ran into the issue that svelte kept removing css because it thought it was unused. Instead of using import css from './App.scss'; Then I used a <svelte:element this="style">{@html css}</svelte:element> This way svelte will not remove "unused" css. See the full example below: // App.scss
@use "@material/card";
@include card.core-styles; <!-- App.svelte -->
<script lang="ts">
import { onMount } from 'svelte';
import { MDCRipple } from '@material/ripple';
import logo from './assets/svelte.png';
import css from './App.scss';
let primaryAction: HTMLDivElement;
onMount(() => {
MDCRipple.attachTo(primaryAction);
});
</script>
<svelte:options tag="my-card"/>
<svelte:element this="style">{@html css}</svelte:element>
<div class="mdc-card">
<div
bind:this={primaryAction}
class="mdc-card__primary-action"
tabindex="0"
>
<div class="my-card__media mdc-card__media mdc-card__media--16-9" style="background-image: url({logo});"></div>
<div class="mdc-card__ripple"></div>
</div>
</div>
<style>
:host .my-card__media {
background-size: contain;
}
</style> |
`:global(…)` doesn't work on custom elements. Here's a workaround suggested in sveltejs/svelte#2969 (comment)
`:global(…)` doesn't work on custom elements. Here's a workaround suggested in sveltejs/svelte#2969 (comment)
It's not just <style lang="scss">
@import 'maplibre-gl/dist/maplibre-gl.css';
</style> This results in the |
I don't know this will be correct way to do or not, but I had managed to get workaround by this
Example
components/normal.svelte <svelte:options tag={null} />
<span class=".container">
<SomeNestedComponent class="nested"/>
</span>
<style>
.container :global(.nested){
background: red
}
</style> web-components/normal.svelte <svelte:options tag="normal" />
<script>
import Normal from '../components/normal.svelte'
</script>
<Normal/> vite.config.js export default defineConfig({
build: {
},
plugins: [
svelte({
include: './src/web-components/*',
compilerOptions: {
customElement: true
}
}),
svelte({
exclude: './src/web-components/*',
emitCss: false,
compilerOptions: {
customElement: false
}
}),
svg(),
]
}) |
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 wrapper is introduced which wraps a Svelte component constructor and returns a HTML element constructor. This has a couple of advantages: - component can be used both as a custom element as well as a regular component. This allows creating one wrapper custom element and using regular Svelte components inside. Fixes #3594, fixes #3128, fixes #4274, fixes #5486, fixes #3422, fixes #2969, helps with sveltejs/kit#4502 - all components are compiled with injected styles (inlined through Javascript), fixes #4274 - the wrapper instantiates the component in `connectedCallback` and disconnects it in `disconnectedCallback` (but only after one tick, because this could be a element move). Mount/destroy works as expected inside, fixes #5989, fixes #8191 - the wrapper forwards `addEventListener` calls to `component.$on`, which allows to listen to custom events, fixes #3119, closes #4142 - some things are hard to auto-configure, like attribute hyphen preferences or whether or not setting a property should reflect back to the attribute. This is why `<svelte:options customElement={..}>` can also take an object to modify such aspects. This option allows to specify whether setting a prop should be reflected back to the attribute (default `false`), what to use when converting the property to the attribute value and vice versa (through `type`, default `String`, or when `export let prop = false` then `Boolean`), and what the corresponding attribute for the property is (`attribute`, default lowercased prop name). These options are heavily inspired by lit: https://lit.dev/docs/components/properties. Closes #7638, fixes #5705 - adds a `shadowdom` option to control whether or not encapsulate the custom element. Closes #4330, closes #1748 Breaking changes: - Wrapped Svelte component now stays as a regular Svelte component (invokeing it like before with `new Component({ target: ..})` won't create a custom element). Its custom element constructor is now a static property named `element` on the class (`Component.element`) and should be regularly invoked through setting it in the html. - The timing of mount/destroy/update is different. Mount/destroy/updating a prop all happen after a tick, so `shadowRoot.innerHTML` won't immediately reflect the change (Lit does this too). If you rely on it, you need to await a promise
Closed via #8457, to be released in Svelte 4 |
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
The text was updated successfully, but these errors were encountered: