Last active
September 9, 2016 20:22
-
-
Save jemgold/3cd0c4db0889ba76a730c78d7ead8dc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stylez.js | |
// colors :: Style | |
export const colors = { | |
black: '#000', | |
white: '#fff', | |
} | |
// or | |
// bgBlack :: Style | |
export const bgBlack = { | |
backgroundColor: colors.black, | |
} | |
// spacing :: [number] | |
const spacing = [ | |
0, | |
0.25, | |
0.5, | |
1, | |
2, | |
4, | |
8, | |
16, | |
]; | |
// addRem :: number -> string | |
const addRem = n => `${n}rem`; | |
// sp :: [string] | |
// [ '0rem', '0.25rem', …] | |
export const sp = map(addRem, spacing); | |
// or | |
// pa2 :: Style | |
export const pa2 = { | |
padding: sp[2], | |
} // etc | |
// or | |
// pa :: int -> Style | |
const pa = n => { | |
padding: sp[n], | |
} // etc | |
// WhateverComponent.jsx | |
const sx = { | |
// this is too verbose | |
padding: sp[2], | |
// but I also really dislike all these spreads | |
...pa2, | |
...pa(2), | |
// and again | |
color: colors.white, | |
backgroundColor: colors.black, | |
...bgBlack, | |
}; | |
const WhateverComponent = ({ children }) => | |
<div style={sx}>{ children }</div> | |
// these all feels really convoluted | |
// when we can just do | |
const WhateverComponent = ({ children }) => | |
<div className="pa2 white bg-black">{ children }</div> |
Yeah, that bothers me a little as well but it's a trade off, right?
Huge benefit about using this approach, for example, is that you will only load the styles that you actually need, instead of a bundle full of stuff that you don't need.
Also, what @jxnblk mentioned above is very interesting... By relying on good abstractions - like Rebass - you can simplify all this ES2015 mumbo jumbo into a nice and declarative API.
Side note: have you looked at CSS Modules plus functional CSS?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To do this with Rebass...
You can do:
Or if it's something reusable, turn that into another component: