-
Notifications
You must be signed in to change notification settings - Fork 77
Investigate support for media-query scoped properties #9
Comments
I've something in a stash that can do the following @media screen and (min-width: 320px) {
:root {
--size: 1em;
}
@media (min-height: 320px) {
:root {
--size: 0.5em;
}
}
}
:root {
--size: 2em;
}
body {
font-size: var(--size);
}
@media screen and (min-width: 320px) {
p {
font-size: var(--size);
}
} body {
font-size: 2em;
}
@media screen and (min-width: 320px) {body {font-size: 1em;}}
@media screen and (min-width: 320px) {@media (min-height: 320px) {body {font-size: 0.5em;}}}
@media screen and (min-width: 320px) {
p {
font-size: 2em;
}
@media screen and (min-width: 320px) {p {font-size: 1em;}}
@media screen and (min-width: 320px) {@media (min-height: 320px) {p {font-size: 0.5em;}}}
} Obviously this is producing a lot of code (don't look mq values, there are stupid), but I think it's doing what user can expect. Note: we might want to optimize mq output after that. |
That's the point of this issue & why we should handle that. I'm not sure what the problem here.
Do we really need to handle that ? If user is writing weird css, he can only get weird output :)
With great powers came great responsabilities ?
I think in the example above, the cascade is respected (& in this case of my local wip - sourcemap is referring to the custom prop definition). |
The point isn't about handling it, but about holes in the model because of incomplete information.
In my example, the resulting style resolution is different to what it would be with native custom properties. These kinds of divergences are unavoidable when you inject rules or move them around. All the media query packer plugins have the same caveat. |
Well I'm so focus on shipping something that I miss these kind of issues. It's a dangerous game indeed. |
Probably not. Anyway, I think we've done some investigation so this can be closed. It was always going to be a low-priority anyway. |
Can we add an option to allow this type of media scoped properties? Input :root { --fontSize: 2em; }
.One { font-size: var(--fontSize); }
.Two { font-size: 6em; }
@media (min-width: 64em) {
:root { --font-size: 3em; }
.One { font-size: var(--fontSize); }
} => .One { font-size: 2em; }
.Two { font-size: 6em; }
@media (min-width: 64em) {
.One { font-size: 3em; }
} In this case, the author manually add the rule to And with this option enabled, if they give an input: :root { --fontSize: 2em; }
.One { font-size: var(--fontSize); }
.Two { font-size: 6em; }
@media (min-width: 64em) {
:root { --font-size: 3em; }
} we only produce :root { --fontSize: 2em; }
.One { font-size: var(--fontSize); }
.Two { font-size: 6em; } This isn't spec compliant, but this is because the author gave an incorrect input (for this option). She should know how the option works before enabling it. Although the option itself isn't spec compliant, giving it the correct input, it will generate spec compliant code. The reason I want this behavior is that if I put all variables in the top level, naming becomes very painful: :root { --fontSize: 2em; --wideViewFontSize: 3em }
.One { font-size: var(--fontSize); }
.Two { font-size: 6em; }
@media (min-width: 64em) {
.One { font-size: var(--wideViewFontSize); }
} Tons of variables need to be prefixed with |
But there is probably one problem: passing js defined custom properties and extracting custom properties is not as simply as it current does. I can think of a few solutions:
|
Both solutions are dangerous. I don't think we should implement partial & weird solutions. Maybe you can "just" use a shorter convention for size like I do :root {
--fontSize: 2em;
--fontSize--XL: 3em; /* i use xs, s, m, l, xl */
}
.One { font-size: var(--fontSize); }
.Two { font-size: 6em; }
@media (min-width: 64em) {
.One { font-size: var(--fontSize--XL); }
} |
A bit cryptic IMHO. But I agree the option is dangerous. I'm looking at shadow dom now to see if it can help. |
@necolas what do you think about the solution below (injecting rules after the selector using the custom prop) ? <div class="One Two">Text</div> Input: :root { --fontSize: 2em; }
.One { font-size: var(--fontSize); }
.Two { font-size: 6em; }
@media (min-width: 64em) {
:root { --font-size: 3em; }
} Output: .One { font-size: 2em; }
@media (min-width: 64em) { /* this do not override selector below */
.One { font-size: 3em; }
}
.Two { font-size: 6em; } |
This is pretty smart. I think it will work. |
As you can see, a solution has been proposed, it's just nobody implemented it. Feel free to make a PR. |
is there any way to set variables for a given media-query from the variables: {
'--color': 'blue',
'@media only screen and (min-width : 600px)': {
'--color': 'green'
}
} |
I understand the proposed solution, so I’ll accept a PR that accomplishes this. Otherwise, I’ll be closing out this issue next week. |
See: reworkcss/rework-vars#17. I still think this is inherently problematic (like plugins that consolidate media queries), but worth exploring to get an answer.
Example input:
Example output:
Complications could include:
@import
statements in media queries@media screen (min-width:0)
?)Input:
Output (notice
One
now overridesTwo
, which it would not with a native solution):The text was updated successfully, but these errors were encountered: