Last active
March 23, 2017 19:48
-
-
Save goldhand/56509526435a90315bb7f1bfee0dc26e to your computer and use it in GitHub Desktop.
Format pseudo styles for a react component
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
/** | |
* Pseudo styles | |
* @param {Object} pseudoStates - state object with keys matching pseudo attributes | |
* @returns {Object} use(styles) | |
* | |
* @example | |
* const styles = {color: '#000', hover: {color: 'red'}, active: {color: 'green'}}; | |
* const state = {hover: true, active: true}; | |
* pseudoStyles(state) | |
* .use(styles.button) | |
* .set('hover') | |
* .set('active') | |
* .styles(); // {color: 'green'} | |
*/ | |
const pseudoStyles = pseudoStates => { | |
const setStyles = style => attr => { | |
const attrActive = ( | |
Object.prototype.hasOwnProperty.call(style, attr) && | |
Object.prototype.hasOwnProperty.call(pseudoStates, attr) && | |
pseudoStates[attr] | |
); | |
const nextStyles = attrActive | |
? {...style, ...style[attr]} | |
: {...style}; | |
return { | |
set: setStyles(nextStyles), | |
styles: () => nextStyles, | |
}; | |
}; | |
return { | |
use: style => setStyles(style)(), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment