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
Support conditional rule groups for named styles #46
Comments
The reason for a different syntax is that we need all rules to be accessible via sheet.getRule and sheet.classes.myRule ... because thats the point of named style sheets. |
FWIW, on my initial reading of the docs, I got the impression that you could use media queries with namespaces based on the first JSS syntax example. |
Whether that means the API is confusing, the readme is confusing, or I'm just generally confused due to my own issues, I don't really know. |
yep, this example was initially without using named style sheet. After I changed named style sheet to be default and changed the example, @media part of it became invalid, thats why I have removed it for now until we implement it right. |
So confusion is because of this wrong example. |
My completely uninformed assumption is that you should be able to write what basically looks like CSS and it should just work, even if you used namespaces. |
Ideally yes, but in the real world namespaces mean we need to have a common scope for them. In case of conditional group rules they kinda introduce a new scope. So we need to break CSS compatibility when using named styles in this case to have just one scope per sheet. |
another possibility would be to allow conditional group rules per style sheet. |
So that all rules within one style sheet are within @media condition. |
this would make 100% consistency with css but will force you to write separate sheets for this. Maybe not the worst option. |
The point I seem to have missed is that you can't override a namespace like you can with a selector when doing responsive design. Is this correct? |
Correct right now. But what if we allow this? And treat all declarations within a conditional group rule as an override if such a rule exist, if not - it will be added? May be also not bad at all. |
Makes a lot of sense to me. IMO the principle of least surprise would suggest you should be able to override just like normal CSS, otherwise this is just going to trip more people up. |
I assumed that namespaces just meant that JSS would generate class names for you, but otherwise allow you to just write CSS with a JSON syntax, at-rules and all. |
there are 2 basic ideas:
The last point could become less intuitive if we start overriding rules, but maybe its not that important there. |
In terms of implementation details: a rule can become a container rule with subrules which have conditions. But at the end it should render to css we expect. |
On the question of accessing the rules from JavaScript, maybe it would look something like this: sheet.getRule('myName');
//returns:
{
color: 'red',
'@media screen and (min-width: 768px)': {
color: 'green'
},
'@media screen and (min-width: 1024px)': {
color: 'blue'
}
} That would be the least surprising to me. Would that cause any issues? What are your thoughts on this API? |
this structure looks strange because @media conditional is on the same level as property names. |
getRule returns a Rule instance anyways ... I was thinking forward about getRule('...').toJSON() |
maybe toJSON should have an option for returning declarations only or with conditionals. |
I think this is the best syntax. {
button1: {
color: 'red'
},
'@media screen and (min-width: 768px)': {
button1: {
color: 'green'
},
button2: {
color: 'blue'
}
}
} Will try to implement something. |
Hmm another issue with this syntax is that its numeric values can't be easily constants, by nature of object properties. |
Is this actually an issue? var SOME_CONSTANT = 768
var rules = {
button1: {
color: 'red'
}
}
rules['@media screen and (min-width: '+ SOME_CONSTANT+'px)'] = {
button1: {
color: 'green'
},
button2: {
color: 'blue'
}
} |
I can definitely see that becoming a bit annoying. However, I still really appreciate the fact that it's a totally guessable format. If asked to use JSON to define CSS + media queries, I'm sure most people would produce something similar. Plus you could easily build some helpers to generate this format for you, if you feel the need. |
Also, I think that it should be possible to extend rules with an @-rule in it.
Would be achievable? It's something that has more to do with extend plugin, or it's better to discuss it here? |
@kof yes, it makes sense By the way, talking about @-selector rules, I believe in this case a special prop keyword, like Plus it would make the code more readable and smaller, avoiding repetitions (compare it to the alternative solution where you have to repeat the name of the rule every time within an @-rule). I'd definitely prefer a special keyword against an over-nested, repeating structure, but that's a matter of personal style. Eventually, I think we should also find a solution for using @-rules with named stylesheets. |
@kof How things going with this? I would really appreciate @media support in such form I know with 'jss' we can create several stylesheets with different media queries.
But I'm using 'react-jss' and we can create component with one stylesheet only, right?
|
I am on it now, should be ready next week. |
its finally landed, please try it out, would love your feedback |
@kof Great! JSS works as expected. But I have a question regarding to 'react-jss'.
And in My expectation is: I guess this happens because 'react-jss' do not know about subrules yet. |
@kof yep, do not see |
Yep, lets see if someone will stumble over it. |
closing for now, if something comes up feel free to reopen. |
Look whats possible with ES6 const WIDTH = 700
{
button: {
color: 'red'
},
[ `@media screen and (min-width: ${WIDTH}px)`]: {
button: {
color: 'green'
}
}
} |
As a result of #45
We need to support conditional rule groups like @media in named style sheet https://developer.mozilla.org/de/docs/Web/CSS/At-rule
One proposal would be to write it like this
Another proposal
The text was updated successfully, but these errors were encountered: