Skip to content

Instantly share code, notes, and snippets.

@AndrewIngram
Created October 21, 2014 12:51
Show Gist options
  • Save AndrewIngram/752fa3ac35682c27a401 to your computer and use it in GitHub Desktop.
Save AndrewIngram/752fa3ac35682c27a401 to your computer and use it in GitHub Desktop.
var namespace = 'mediacat'
function cx(classNames, options) {
var classes;
var states;
var bemClasses = [];
var baseClassName;
var stateClassName;
if (typeof classNames == 'object') {
classes = Object.keys(classNames).filter(function(className) {
return classNames[className];
});
} else {
classes = classNames;
}
classes.forEach(function(className) {
baseClassName = namespace + '-' + className;
bemClasses.push(baseClassName);
if (options && options['theme']) {
bemClasses.push(baseClassName + '--theme-' + options['theme']);
}
if (options && options['states']) {
states = options['states'];
if (typeof states == 'object') {
states = Object.keys(states).filter(function(stateName) {
return states[stateName];
});
}
states.forEach(function(state) {
stateClassName = baseClassName + '--is-' + state;
bemClasses.push(stateClassName);
if (options.theme) {
bemClasses.push(stateClassName + '--theme-' + options.theme);
}
});
}
});
return bemClasses.join(' ');;
}
module.exports = cx;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment