Comments (23)
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?
from styled-jsx.
@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:
`p { color: ${myColor)}; }`
`p { color: ${darken(myColor))}; }`
`div { width: ${5*gridCellSize}px; }`
I assume that any constant in the module scope is valid right? or were you thinking about restricting to the innermost scope? (the render
one usually)
from styled-jsx.
from styled-jsx.
See #26
from styled-jsx.
from styled-jsx.
I tend to leverage css variables for dynamic values such as this.
Something like the following:
MyComponent.jsx
function MyComponent({ children, color, fontSize }){
return <p class='MyComponent' style={{
'--MyComponent-color': color,
'--MyComponent-fontSize': fontSize
}}>{children}</p>
}
MyComonent.css
.MyComponent {
color: var(--MyComponent-color);
font-size: var(--MyComponent-fontSize);
}
from styled-jsx.
Yeah this is something that could definitely work, but it's going to take some work. @hzoo, is it possible to create a babel template literal expression from text that includes ${}
? That way we can pass the raw text to the css compiler containing the ${}, and then pass it back to babel
from styled-jsx.
The problem is that the css gets added only once per component, so the CSS can't really be dynamic
from styled-jsx.
We'd have to think about a way of bypassing the deduping if properties are used, but the problem there is that it would really hurt performance
from styled-jsx.
This is a problem with styled-components
as well at the moment
from styled-jsx.
Ah I see, thank you for the explanation
from styled-jsx.
Would it be possible to insert the style tag where it's indicated instead of appending to the <head>
, and then scoping for each instantiated component instead? That could potentially increase the amount of outputted HTML/CSS substantially, but shouldn't hurt runtime performance extremely badly, unless I'm missing something.
from styled-jsx.
Er I guess not appending to the head doesn't really solve any problems, that would be just the same as not deduping at all, never mind.
from styled-jsx.
Essentially we need to dedupe based on the resulting CSS string as opposed to the hash. But then components could conflict. Therefore, we would need to figure out a way of targeting elements within a specific component
from styled-jsx.
In general, if you ask me, the power of CSS is in it not being dynamic, because it can be heavily optimized. For example, you declare a bunch of classes, and all your event handlers do is toggle them.
from styled-jsx.
I'm keeping this open in case we figure a workaround, but otherwise I'm keeping it open because we need to warn the user during compilation
from styled-jsx.
That's true. I'm mainly trying to do this to easily style a dom element I don't have control over, but I guess I can just as easily do it directly with JS instead.
That's reasonable, I think it would be nice to mention in the README somewhere that expressions aren't supported in addition to a compile-time warning. Having never used styled-components before I just assumed this would work and I imagine others might too. Thanks!
from styled-jsx.
Unless it is mentioned somewhere in the docs and I'm just blind, I apologize if that's the case.
from styled-jsx.
@rauchg actually you can with styled-components
.
You can do this:
import styled from 'styled-components';
const orangeColor = 'orange';
const align = 'right';
const Title = styled.h1`
font-size: 1.5em;
text-align: ${align};
color: ${orangeColor};
`;
To be able to do this is very important. Otherwise, we cannot use javascript to manipulate colors, units, custom mixins
like sass and other things like that...
Just toggle class is not enough.
from styled-jsx.
@rauchg So this is in the roadmap? π
from styled-jsx.
@rauchg how would you calculate the hash in that case? Are you thinking of resolving expressions by hand before hashing the css?
from styled-jsx.
Basically we'd do as much as we can to ensure people are using constants (by preventing usage of expressions that only live within the scope), and we'd hash with the name of the constant, not the value (to avoid complexity).
Obviously there are edge cases where people will have their expectations of dynamism not met, but if we prevent props / state from being used, we cover most potential surprises.
Would you agree?
from styled-jsx.
π€ I'm beginning to wonder if using CSS variables (especially for top level themes) would be the best choice for me. It does impose an implicit API on the components for css variables to be predefined.
from styled-jsx.
Related Issues (20)
- Unable to integrate with animated element of react-spring. HOT 1
- Security Vulnerability on dependency [email protected] - CVE-2022-37601
- styled-jsx/macro.d.ts typo HOT 1
- Document is not defined HOT 1
- Make <reference types="styled-jsx" /> include global.d.ts? HOT 4
- Scope type not being changed when using comment on external file
- scoping styles with brackets & double colons
- Flushing styles from an external component library into next js HOT 1
- Expected a template literal or String literal as the child of the JSX Style tag (eg: <style jsx>{`some css`}</style>), but got CallExpression
- Custom Font in Next13 App Dir HOT 1
- Container Queries HOT 2
- Support for vite + swc HOT 1
- CSS not working while migration from babel to swc compliler HOT 1
- Server Components HOT 1
- How to use styled-jsx with webpack and React only, without Babel and Next.js? HOT 2
- Bug: Received `true` for a non-boolean attribute `jsx` even with ["styled-jsx/babel-test"]
- ReactDOMServer.renderToPipeableStream
- Preventing the jsx-* class from being added on every element HOT 1
- loader-utils has security risks HOT 1
- Typo in issue template. "verce" should be "vercel"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from styled-jsx.