styled-jsx: Expressions within template literal don't work
I would understand if dynamic properties didn’t work, but dynamic values don’t seem to be working either. Compiling succeeds, but the pieces with the expression seem to be omitted from the inserted script tag.
<div>
<div id={this.state.calendarId}></div>
<style jsx>{`
#${this.state.calendarId} .fc-button.fc-state-default {
border: 2px solid ${buttonColor};
}
`}</style>
</div>

const buttonColor = 'red';
return (
<div>
<div id="foo"></div>
<style jsx>{`
#foo .fc-button.fc-state-default {
border: 2px solid ${buttonColor};
}
`}</style>
</div>
);

<div>
<div id="foo"></div>
<style jsx>{`
#foo .fc-button.fc-state-default {
border: 2px solid red;
}
`}</style>
</div>

Thank you!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 23 (12 by maintainers)
Commits related to this issue
- :zap: use styled-jsx everywhere remove shared colors object unless https://github.com/zeit/styled-jsx/issues/25 is fixed — committed to agudulin/gorchichka.com by agudulin 7 years ago
@rauchg Yeah this would work and indeed add basic support for constants. Maybe we can allow anything except for inline (anonymous) functions? The following should all be valid:
I assume that any constant in the module scope is valid right? or were you thinking about restricting to the innermost scope? (the
renderone usually)I’m thinking code sharing and being static. One simple pattern that’s been possible for a while by using CSS preprocessors (and I guess post processors too) is the ability to f.ex. declare all used colors in one place and then use these colors across the app by referencing the variable name that holds the color code. As these colors are declared once and used all around the app it’s really easy to tweak them when necessary.
If you don’t follow this kind of pattern and you f.ex need to change a color you either need to go through all components on your app and update the color by hand or do a really careful search & replace. In bigger codebases without a centralized solution for things like colors, z-indexes etc. seeing the bigger picture becomes hard and it will lead to fragmentation.
Though the case here is very different I’ve always seen CSS pre and post processors as static code generators. Though I understand why you’re avoiding dynamic stuff I don’t understand why it should stand in way of code sharing. Or is there currently a way to achieve something close to what I’ve described above with styled-jsx?
Yes I realized the other day that we can support expressions from outside the closure (i.e.: not from props) for shared constants like colors and fonts
On Wed, Dec 21, 2016 at 11:50 AM Joonas Salovaara notifications@github.com wrote:
I tend to leverage css variables for dynamic values such as this.
Something like the following:
MyComponent.jsx
MyComonent.css
Yep but dynamic stuff in CSS has serious performance tradeoffs. it’s something I might consider but being static is also an advantage for predictable perf
On Sun, Dec 18, 2016 at 4:16 PM Bruno Lourenço notifications@github.com wrote:
See #26