-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
<style bind:styles={styles}> and <style styles={styles}> - CSS Binding and Passing scoped styles between components and scoped svelte CSS import. #6422
Comments
@non25 Yes, I mentioned What do you think about my entire proposal? I admit that the lack of any reaction worries me a bit. |
I don't think it is productive to discuss something like this now. I recommend using svelte-preporcess-cssmodules, as I don't see anything like this will ever be implemented, and |
I see it like this:
And it's easy to expand on my proposal to handle CSS import and class passing - but that is another matter, it is not my basic proposal, but a possible extension of the proposal. So "limitation with svelte-scoping" is more about passing classes, and passing a block of styles is different and much simpler. Well, in fact, it makes no sense to write about it until some person in charge of svelte gives a signal. |
@lukaszpolowczyk thank you for such an elaborate proposal, here are some of my thoughts on the proposal, and why I think it is not feasible to implement this in Svelte at the moment. before that, to understand how scoped styles work in Svelte, when you write: <h1>Heading 1</h1>
<h2>Heading 2</h2>
<style>
h1 { color: blue; }
p { color: yellow; }
</style> it get compiled to something that looks like this: <h1 class="svelte-xxx">Heading 1</h1>
<h2>Heading 2</h2>
<style>
h1.svelte-xxx { color: blue; }
</style> the style
So, if you understand how the scope style works, now to pass
if you think hard enough, you may come up with some solution to this problems, which most likely undo all the optimisations comes with Svelte. on that note, I'm going to close this issue for now. |
@tanhauhau I will try to answer as far as I can: 1. The A indicator to the style copy is passed to the Answer: There would be no redundant removal at this point. It would be generated individually. 2. 3. 2. and 3.
So
Additional thoughts: Overall, the goal is:
I have no idea how to pass the indicator less dynamically. Maybe using setContext or something like that? I do not know. |
maybe can elaborate more on this "air" ?
me too. and i dont think it's possible at the current state of Svelte |
In the form of a simple string or inside an additional
Just to understand it well - in general, it is possible to implement the whole mechanism. I just wrote that if I did it less dynamically, it could all be prepared on the compiler side, and now it can be done, but doing some extra runtime side operations. |
Is your feature request related to a problem? Please describe.
I want to have a common part of the CSS style, at the same time also I don't want to have to throw every little bit of CSS out of the
*.svelte
files and I don't want to use global CSS.Describe the solution you'd like
CSS Binding and Passing.
It is the most intuitive, transparent and sveltish solution, at the same time referring to the CSS assert in custom element proposal.
Style set in
App.svelte
instyle
tag withbind:styles
attribute, passed to theComponent.svelte
andComponent2.svelte
components instyle
withstyles
atribute. Scoped.Example:
Style should not be global, but scoped at the point of use.
I can see that in every component with
<style styles={styles} />
, the same hash likesvelte-n323vl
is used for classes.The variable
styles
is an object that cannot be modified, it is used only to pass the preset style to the component.Questions:
It should be possible to use multiple
style bind:styles={styles}
(both declaration and use) and plain<style>
at the same time.Is something in the way?
OR:
<style bind:styles={ [styles, styles2] } />
or<style bind:styles={ {...styles, ...styles2} } />
- and only the declaration could be multiple.Should the binded
style
inApp.svelte
also be applied to the HTML elements inApp.svelte
?Or should it be optional? Attribute of type
onlydeclaration
(I don't have a good idea of the name).I don't know if it should work in
App.svelte
by default and need to be turned off with the attribute, or vice versa?Another option - better? - at the same time simpler, but also a bit circular (OF COURSE, this is not usually used, only in exceptional cases):
or even just:
Probably yes, but maybe there will be some obstacles?
Maybe in order for
<style bind:styles={styles}></style>
to not confuse with the standard<style></style>
behavior (i.e. applying styles toApp.svelte
), you need to use<svelte:style bind:styles={styles}></svelte:style>
?Other idea...:
Instead of requiring separate tags (
<style bind:styles>
or<svelte:style bind:styles>
), I figured out a way to keep the number of<style>
tags constant.Where:
:expose {}
has a separate hash:expose {}
<style styles={style}>
causes the hash to be used inComponent.svelte
elements - Component.svelte has its own hash, and the one passed is an additional hash, e.g .:svelte-15kka16
is the hash added withstyles={styles}
andsvelte-edszo3
is the hashComponent.svelte
.:expose {}
default are NOT applied toApp.svelte
. To make them, you have to use:I think it would be nice to be able to name the displayed block styles and use more than one:
Each of them would have a separate hash.
Most importantly, it is not global, and is controlled where this
:expose {}
block is to be used.Summary:
The whole thing seems clear, refers to the upcoming standard, and at the same time is "sveltish".
Describe alternatives you've considered
<Component --css-var={var}>
Passing CSS custom properties to components rfcs#13svelte-css-in-js
modular-css
Some do not work, none offer the type of possibilities I suggest, nor are they so sveltish.
If anyone wants to do RFC, please do.
How important is this feature to you?
The point is not to use global CSS or do some redundant CSS.
In fact, such something is needed all the time.
This is where my basic proposition ends.
FURTHER THINKING - Import CSS file and Passing Classes:
Import CSS file
Seems like can be used to... import CSS.
There is a proposition
import styleSheet from "./styles.css" assert { type: "css" };
for WebComponents.Such an imported svelte style would also be scoped.
I can see an easy use of this in
<svelte:options tag="my-element" />
.I will not be surprised if such placing of imported CSS will be interesting for someone, and passing between components less...
Question:
Do you allow
import ("./styles.css", {assert: {type: "css"}});
? Probably not.Passing Classes
Binding classes with the
style
tag and passing to components. Extracting classes from the binding variablestyle
.Example:
...or as in
css-modules
?:The text was updated successfully, but these errors were encountered: