-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Optionally embed styles in the DOM if the styles are in JS. #4039
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
Comments
Hey @kwangure im faceing the same issue did you found a workaround ? .. |
The easiest way to do it is to take full control of your styles, and use them in JS. I found myself doing this: <script>
const style = `
color: black;
background-color: red;
`
function setStyles(node) {
node.setAttribute("style", style);
}
</script>
<!-- Use either `use:action` or `style` attribute -->
<div use:setStyles {style}>My text</div> However, I'm not a fan of this work around as far as scaling is concerned. Mixing styles and JS in the What exactly is your usecase? That would make it easier to offer suggestions or help. In my case for example, I later switched to using the |
I had a custom element with custom elements inside so the style where inline but I changed so there is only one custom element and the child are normal svelte components.. <div>
<style>
.grid {}
</style>
... I lost the svelte scoping but since is a custom element it will not conflict with others styling |
If your custom element is the wrapper component, it makes it easier to manage. I recently discovered that you use can a <svelte:options tag="custom-component"/>
<link rel="stylesheet" href="path/to/exported/children/styles"/>
<Component>
<OtherComponent/>
</Component> will give you <custom-component>
#shadow-root
<!--Scoped styles for `Component` and `OtherCompoentent`-->
<link rel="stylesheet" href="path/to/exported/children/styles"/>
...
</custom-component> Browser support is also pretty good. Scoped |
I created a PR that makes it possible to use svelte inside a shadow dom. All styles and animations will work like in a normal svelte-application. You could install svelte from this branch to use it until it gets merged. |
Svelte seems to have a bunch of technical teething troubles when it comes to web components 😬... |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
The custom elements compiler option will inject CSS into JS, even for nested components now. Therefore closing as it seems this issue doesn't have real use cases outside of that, and a workaround for the PDF case exists via custom elements. If you feel different, please let us know and we can reconsider. Thank you. |
Is your feature request related to a problem? Please describe.
There is no way to control where Svelte embeds styles in JS that are not exported using the
css: true
compile option. Styles are currently embedded in thehead
tag, though it may not always be desirable. For example, if my Svelte component is used in a custom-element with a shadow dom, styles in the head are not applied due to encapsulation.Secondly, if my element is rendered in an environment without a
head
tag, Svelte throws an error. For example, the Chrome PDF viewer is an embedded page that doesn't have a head tag. It's frustrating when embedding Svelte components in the PDF viewer through Chrome extensions. If I expect such environments, I should be able to choose where the styles go.Describe the solution you'd like
The best solution I could come up with was using an option
Then when rendered will add the styles inline next to my component instead of the
head
:Describe alternative solutions you've tried
The alternative is to render all the nested components into custom elements to force Svelte into rendering the styles in-line. This is cumbersome especially when supporting both regular components and custom elements since svelte does not handle nested components for me as described in #3520.
So I'd need to create multiple versions of the same component if deeply nested.
The text was updated successfully, but these errors were encountered: