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
> 💡 This approach is recommended when the value of a prop is **unknown** ahead of time or possibly unlimited values, e.g. styling based on the user's input.
Zero-runtime will replace the callback with a CSS variable and inject the value through inline style. This makes it possible to create a static CSS file while still allowing dynamic styles.
253
+
Pigment CSS will replace the callback with a CSS variable and inject the value through inline style. This makes it possible to create a static CSS file while still allowing dynamic styles.
Zero-runtime can generate CSS variables from the theme values when you wrap your theme with `extendTheme` utility. For example, in a `next.config.js` file:
358
+
Pigment CSS can generate CSS variables from the theme values when you wrap your theme with `extendTheme` utility. For example, in a `next.config.js` file:
Emotion and styled-components are runtime CSS-in-JS libraries. What you write in your styles is what you get in the final bundle, which means the styles can be as dynamic as you want with bundle size and performance overhead trade-offs.
503
+
504
+
On the other hand, Pigment CSS extracts CSS at build time and replaces the JS code with hashed class names and some CSS variables. This means that it has to know all of the styles to be extracted ahead of time, so there are rules and limitations that you need to be aware of when using JavaScript callbacks or variables in Pigment CSS's APIs.
505
+
506
+
Here are some common patterns and how to achieve them with Pigment CSS:
507
+
508
+
1.**Fixed set of styles**
509
+
510
+
In Emotion or styled-components, you can use props to create styles conditionally:
511
+
512
+
```js
513
+
constFlex=styled('div')((props) => ({
514
+
display:'flex',
515
+
...(props.vertical// ❌ Pigment CSS will throw an error
516
+
? {
517
+
flexDirection:'column',
518
+
paddingBlock:'1rem',
519
+
}
520
+
: {
521
+
paddingInline:'1rem',
522
+
}),
523
+
}));
524
+
```
525
+
526
+
But in Pigment CSS, you need to define all of the styles ahead of time using the `variants` key:
527
+
528
+
```js
529
+
constFlex=styled('div')((props) => ({
530
+
display:'flex',
531
+
variants: [
532
+
{
533
+
props: { vertical:true },
534
+
style: {
535
+
flexDirection:'column',
536
+
paddingBlock:'1rem',
537
+
},
538
+
},
539
+
{
540
+
props: { vertical:false },
541
+
style: {
542
+
paddingInline:'1rem',
543
+
},
544
+
},
545
+
],
546
+
}));
547
+
```
548
+
549
+
> 💡 Keep in mind that the `variants` key is for fixed values of props, for example, a component's colors, sizes, and states.
550
+
551
+
2.**Programatically generated styles**
552
+
553
+
For Emotion and styled-components, the styles will be different on each render and instance because the styles are generated at runtime:
554
+
555
+
```js
556
+
functionrandomBetween(min:number, max:number) {
557
+
returnMath.floor(Math.random() * (max - min +1) + min);
However, in Pigment CSS with the same code as above, all instances will have the same styles and won't change between renders because the styles are extracted at build time.
578
+
579
+
To achieve the same result, you need to move the dynamic logic to props and pass the value to CSS variables instead:
580
+
581
+
```js
582
+
functionrandomBetween(min:number, max:number) {
583
+
returnMath.floor(Math.random() * (max - min +1) + min);
0 commit comments