thabti / react-native-css

Style React-Native components with css
MIT License
769 stars 66 forks source link

Adding --literal argument to generate js literal object #29

Closed alfonsodev closed 8 years ago

alfonsodev commented 8 years ago

This pull request adds a boolean --literal argument, false by default. When this argument is true, the utils.outputReactFriendlyStyle function will generate a javascript literal object instead of using the Stylesheet.create construct.

Why a literal object ?

From React docs: Stylesheet.create is optional ...

StyleSheet.create construct is optional but provides some key advantages. It ensures that the values are immutable and opaque by transforming them into plain numbers that reference an internal table. By putting it at the end of the file, you also ensure that they are only created once for the application and not on every render.

Having a literal object has it's own advantages, for instance in components that use string props to change its style, we can still use the generated styles as string.

<NavigationIOS tintColor={styles.mainColor.color} />

But Stylesheet.create turns the object keys into numbers making it not accesible anymore.
This pull request lets the user choose what construct they want for their styles.
I hope you like it :)

alexmick commented 8 years ago

This looks good ! I would merge but waiting on @sabeurthabti to confirm :+1:

thabti commented 8 years ago

done @alfonsodev @alexmick thanks both. Sorry for the delay.

alexmick commented 8 years ago

Ah there is one problem though @alfonsodev, the signature of the parse function has been changed which can break some stuff (eg. my grunt plugin 😄 ). Passing the callback as the fourth argument like before will not get called now. I don't think it's important enough to change back but a best practise in the future would be to add new arguments to the end and specifying a cb=false as default for the callback.