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
As a user, you could avoid using a selector to style children (e.g: & > div) and just pass down class names as props. In this case, a child component that is re-usable but it should be styled differently depending on where it is used. For example, a re-usable Heading component which could have different color.
// A.jsconstComponentA=({ color })=><divclassName={css({ color })}/>// What is color??// B.jsconstComponentB=()=><ComponentAcolor="#fff"/>
However, this is better and makes it possible to make static analysis of the code:
// A.jsconstComponentA=({ color })=><divclassName={color}/>// B.jsconststyles=css({color: '#fff'})// Static analysis is possibleconstComponentB=<ComponentAcolor={styles.color}/>
Details
stylex was shown in ReactConf by Facebook, so the original idea comes from there:
otion could export a create method (or choose better name) that could allow to create a styles object. This could basically just be (overly simplified version):
functioncreate(styleObj: Record<string,CSSPropsThing>){// << Generic type is betterconstkeys=Object.keys(styleObj)conststyles={}keys.forEach(k=>styles[k]=css(styleObj[k]))// << Take into account nested stuff (selectors)styles.toString=function(){return'....'/* get all classnames somehow */}}
So, similarly as with css:
conststyles=create({square: {color: 'hotpink',border: '1px solid blue',},})// Returned value should look likeconststyles={square: {color: 'c0',border: 'c1',toString(){...}},toString(){returncombineAllStyles(...)}// See next 👇 }
otion could export a combine method (or choose better) to combine style objects and deduplicate styles:
conststyle1=css({color: 'red',background: 'blue'})// { color: 'c0', background: 'c1' }conststyle2=css({color: 'blue'})// { color: 'c2' }constfinalStyle=combineOrDedupeOrFancyName(style1,style2)// Return should beconstfinalStyle={color: 'c2',background: 'c1'}// color is blue, background is blue
TL;DR
It's a breaking change but I believe this could allow for:
It allows for composable class names that could be passed down as props in React apps (or similarly outside of React) and can potentially allow the user to avoid writing complex selectors that style children
Summarizing how the new API could look like and it's usage:
// A.js - unchanged (because `toString()`)constMyComponentA=()=>(<divclassName={css({color: 'red'})}/>)// B.js - `toString` of `create` return would combine & serialize all class namesconstMyComponentB=()=>(<divclassName={css.create({box: {border: '1px solid red'},other: {color: 'blue'}})}/>)// C.js - composableconstMyComponentC=({ className })=><divclassName={className}/>conststyles=css.create({box: {border: '1px solid green',background: 'grey'},child: {color: 'blue'},})constMyComponentD=({ inheritStyles })=>(<divclassName={css.combine(styles.box,inheritStyles)}> // <Combineownstyles&optionallyinheritfromprops<MyComponentCclassName={styles.child}/> // pass down single class name
</div>)constsectionStyles=css.create({div: { ... }box: {border: '2px solid lime'},})constMyComponentE=()=>(<divclassName={sectionStyles.div}><MyComponentDinheritStyles={sectionStyles.box}/> // <<<overrideMyComponentD `border`
</div>
)
Let me know what do you think 😅 and if it's within the scope of otion to support this. I can help with the implementation 😄
Motivation & Examples
Currently
cssreturns a single string with all class names, so it isn't possible to get a single class name for a specific property. For example:It'd be great if the result of
csswas like:An example use case:
This would offer many benefits:
& > div) and just pass down class names as props. In this case, a child component that is re-usable but it should be styled differently depending on where it is used. For example, a re-usableHeadingcomponent which could have different color.Static analysis is difficult or impossible
However, this is better and makes it possible to make static analysis of the code:
Details
stylexwas shown in ReactConf by Facebook, so the original idea comes from there:Based on that:
csscould return an object instead of serializing all classnames:This could allow to access any classname:
Note: also add
valueOf()? 🤔otioncould export acreatemethod (or choose better name) that could allow to create a styles object. This could basically just be (overly simplified version):So, similarly as with
css:otioncould export acombinemethod (or choose better) to combine style objects and deduplicate styles:TL;DR
It's a breaking change but I believe this could allow for:
Summarizing how the new API could look like and it's usage:
Let me know what do you think 😅 and if it's within the scope of
otionto support this. I can help with the implementation 😄