Skip to content

Instantly share code, notes, and snippets.

@brenogcota
Last active May 30, 2022 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenogcota/1242f784ed72649892c36119c4937de0 to your computer and use it in GitHub Desktop.
Save brenogcota/1242f784ed72649892c36119c4937de0 to your computer and use it in GitHub Desktop.
make dynamic css classes
function cssHandles(...arguments) {
   return arguments.reduce((acc, cur) => {
     if (typeof cur === 'string') return acc + ` ${cur}`;
     if (typeof cur === 'object') {
        return Object.keys(cur).map((key) => {
            if(cur[key]) return acc + ` ${key}`
            return acc
        }).filter(Boolean).join('').trim()
         
     }  
   }, '')
}

cssHandles({ 'button--active': true, 'button--disabled': false, 'primary': true }, { 'is--open': false })
> 'button--active primary'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment