You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
update 2016/03/14::root isn't a very good idea; we should just rely on JS vars instead. update 2016/11/16. scratch that, :root in moderate usage is a great idea, as long as a postprocessor can handle it (which it can now)
write tests for ES6 string interpolation
document variable passing using string interpolation
What do you think of making :root elements the transport for variables and such? Root would never be prefixed, and passed down to child elements so they can read values from it during compilation. Perhaps we should also do this for @custom-media selectors so breakpoints are passed down too? (example)
Just thought about one more thing. If you consider something like the above, it would be nice to be able to pass in some form of observable, preferably a stream,.
Maybe those streams could update css variables or use some polyfill for older browsers.
Talking to @hughsk he thought it was probably best not to pass :root around since it creates implicit state, which is very bad.
@serapath I think you're right and using ES6 ${var} is the way to go. We should probably add tests to check if this is going well and document it. I've updated the initial post to reflect this.
Re observables: that won't work since sheetify runs at compile time, and returns static values. Having values dynamically change during runtime would require a different approach; I don't feel extending sheetify's scope to support that would be wise.
@ahdinosaur oh yeah, passing in vars like that makes sense for external files I think - think interpolation would make sense for inline. Not sure I'm keen on having different methods for both, but given that { global: true } also is slightly different, this might be unavoidable - it's simple enough I reckon. Wonder what @hughsk's take is on this too
Oh, that's sad. I was hoping there will be runtime support. For me, that is a main point of using css-in-js, because changing styles dyanmically is the one thing that cannot be done with traditional solutions.
@ahdinosaur I like your approach :-) do you know any css-in-js project that does something similar?
I think you're right and using ES6 ${var} is the way to go. We should probably add tests to check if this is going well and document it. I've updated the initial post to reflect this.
hmm, seems this approach isn't working: the ${var} expressions aren't being evaluated correctly in the browserify transform and cause an error.
Is there any update on how to use vars with sheetify? Using the vars spec from cssnext isn't really working for me as I can't define them in one file and then use them somewhere else (in js).
${var} expression would be ideal, but not sure if this is in progress?
I can't define them in one file and then use them somewhere else (in js).
hey @thomhos, i recently tried to support this use case in sheetify, but ran into some fundamental barriers with doing things as static analysis (using a browserify transform).
if we do this as a browserify transform (even using static-module and a pull request i made to static-eval to statically evaluate template strings), styles is not defined as a value since it has not yet evaluated require('./styles'). it's theoretically possible to evaluate the require calls as necessary, but someone needs to be willing to spend the time and energy to do it right.
Thanks for the reply. So before we can actually use const styles, it should be evaluated first, but in doing that also the prefix styles get evaluated which then throws an error because the ${styles.textColor} is not valid syntax? Apologies if I'm not getting it right.
I was now using the sheetify-sass plugin so I could write sass but also able to use variables. I can then import them within the css. Not sure how much this impacts performance, and it feels a bit hack'ish. But it works for now.
So before we can actually use const styles, it should be evaluated first, but in doing that also the prefix styles get evaluated which then throws an error because the ${styles.textColor}
most of what sheetify does happens as a browserify transform, which means we receive a module filename and are expected to return a transform stream that receives source code and returns transformed source code. for the best way to do this, we can use static-module, which under the hood is using falafel which uses acorn which produces objects as described in estree.
when trying to transform the above snippet, expressions within the parsed AST include all the variables unevaluated.
this would be great to pass into static-eval, but we also need to pass in a context object with all Identifiers (in this case the context object is with { styles } where styles is an object with { textColor }).
to get this object, we need to find out what variables we need from the sheetify template string call, then go up the tree and try to de-reference them. (in the simplest case, which i think static-module already handles, the identifier is defined as a variable within the same module. the harder case, which we need, is when the identifier is defined as the output of a require call).
Thanks for the detailed explanation. I do see the problem (i think), and if i'm correct it's not unsolvable, but rather a complicated thing that needs work?
Anyways, for now I'm good to go using sheetify-sass, and unfortunately don't have the skills (yet) to work on a PR for the above issue.
Thanks for the detailed explanation. I do see the problem (i think), and if i'm correct it's not unsolvable, but rather a complicated thing that needs work?
Activity
serapath commentedon Feb 15, 2016
It's kind of interesting - implicit parameters, but in a real app it can become very intransparent i guess.
I would rather just use:
client-main.js
client-sidebar.js
serapath commentedon Feb 19, 2016
Just thought about one more thing. If you consider something like the above, it would be nice to be able to pass in some form of observable, preferably a stream,.
Maybe those streams could update css variables or use some polyfill for older browsers.
yoshuawuyts commentedon Mar 14, 2016
Talking to @hughsk he thought it was probably best not to pass
:rootaround since it creates implicit state, which is very bad.@serapath I think you're right and using ES6
${var}is the way to go. We should probably add tests to check if this is going well and document it. I've updated the initial post to reflect this.Re observables: that won't work since
sheetifyruns at compile time, and returns static values. Having values dynamically change during runtime would require a different approach; I don't feel extendingsheetify's scope to support that would be wise.ahdinosaur commentedon Mar 14, 2016
here's another possible way of passing vars around (not mutually exclusive with the template variable approach) using CSS variables:
style-vars.js
client-main.js
client-main.css
yoshuawuyts commentedon Mar 14, 2016
@ahdinosaur oh yeah, passing in vars like that makes sense for external files I think - think interpolation would make sense for inline. Not sure I'm keen on having different methods for both, but given that
{ global: true }also is slightly different, this might be unavoidable - it's simple enough I reckon. Wonder what @hughsk's take is on this tooserapath commentedon Mar 14, 2016
Oh, that's sad. I was hoping there will be runtime support. For me, that is a main point of using css-in-js, because changing styles dyanmically is the one thing that cannot be done with traditional solutions.
@ahdinosaur I like your approach :-) do you know any css-in-js project that does something similar?
ahdinosaur commentedon Aug 24, 2016
hmm, seems this approach isn't working: the ${var} expressions aren't being evaluated correctly in the browserify transform and cause an error.
thomhos commentedon Sep 16, 2016
Hello,
Is there any update on how to use vars with sheetify? Using the vars spec from cssnext isn't really working for me as I can't define them in one file and then use them somewhere else (in js).
${var} expression would be ideal, but not sure if this is in progress?
Cheers!
ahdinosaur commentedon Sep 17, 2016
hey @thomhos, i recently tried to support this use case in
sheetify, but ran into some fundamental barriers with doing things as static analysis (using a browserify transform).let's say we want to do:
if we do this as a browserify transform (even using
static-moduleand a pull request i made tostatic-evalto statically evaluate template strings),stylesis not defined as a value since it has not yet evaluatedrequire('./styles'). it's theoretically possible to evaluate therequirecalls as necessary, but someone needs to be willing to spend the time and energy to do it right.@thomhos for a different library that might work here, maybe try
css2objandfree-style.thomhos commentedon Sep 17, 2016
Hi @ahdinosaur,
Thanks for the reply. So before we can actually use
const styles, it should be evaluated first, but in doing that also the prefix styles get evaluated which then throws an error because the${styles.textColor}is not valid syntax? Apologies if I'm not getting it right.I was now using the sheetify-sass plugin so I could write sass but also able to use variables. I can then import them within the css. Not sure how much this impacts performance, and it feels a bit hack'ish. But it works for now.
I'm curious to see how this will evolve!
Cheers,
ahdinosaur commentedon Sep 18, 2016
most of what
sheetifydoes happens as a browserify transform, which means we receive a module filename and are expected to return a transform stream that receives source code and returns transformed source code. for the best way to do this, we can usestatic-module, which under the hood is usingfalafelwhich usesacornwhich produces objects as described inestree.when trying to transform the above snippet, expressions within the parsed AST include all the variables unevaluated.
for example:
is parsed as
this would be great to pass into
static-eval, but we also need to pass in a context object with all Identifiers (in this case the context object is with{ styles }wherestylesis an object with{ textColor }).to get this object, we need to find out what variables we need from the sheetify template string call, then go up the tree and try to de-reference them. (in the simplest case, which i think
static-modulealready handles, the identifier is defined as a variable within the same module. the harder case, which we need, is when the identifier is defined as the output of arequirecall).does that help explain things a bit more?
thomhos commentedon Sep 19, 2016
Hi @ahdinosaur,
Thanks for the detailed explanation. I do see the problem (i think), and if i'm correct it's not unsolvable, but rather a complicated thing that needs work?
Anyways, for now I'm good to go using sheetify-sass, and unfortunately don't have the skills (yet) to work on a PR for the above issue.
Cheers!
ahdinosaur commentedon Sep 19, 2016
hey
yep. 😄
yoshuawuyts commentedon Nov 16, 2016
yeah so
:rootvariables should now work in v6, forward - in modern browsers no compilation needed even - using it with moderation should be a-ok