Skip to content

Instantly share code, notes, and snippets.

@smitroshin
Created November 13, 2020 15:34
Show Gist options
  • Save smitroshin/62cfd72cd142ca53bb0cfec9ef49917e to your computer and use it in GitHub Desktop.
Save smitroshin/62cfd72cd142ca53bb0cfec9ef49917e to your computer and use it in GitHub Desktop.
Easy way to put conditions into class names
/**
* Filter a list of expressions to final string.
*
* @param args
* @returns {string}
*/
const classNames = (...args) =>
args
.reduce((accumulator, itm) => {
if (itm) {
accumulator.push(`${itm}`.trim());
return accumulator;
}
return accumulator;
}, [])
.join(' ');
export default classNames;
const isSelected = true;
const isDisabled = false;
classNames('option', isSelected && 'selected', isDisabled && 'disabled') // will return 'option selected'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment