Spruce up CSS classes
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json

README.md

Spruce up

Build Status NPM Downloads pledge

Spruce up is a simple JavaScript function for conditionally and elegantly concatenate CSS classes. Spruce up supports:

  • Primitives: Returns a string that is the result of joining all primitives passed to the the function. Booleans and falsy values are exempted!
  • Arrays: Evaluate and join all elements of an array as classes.
  • Functions: Evaluate functions and concatenate their output as a class name.
  • Objects: Objects are looped through and used to conditionally add classes.

Spruce up is particularly useful with React and support all main stream browsers.

Usage

import classes from 'spruceup'

classes('hello', 'world')
// => 'hello world'

classes('hello', () => 'world')
// => 'hello world'

classes('hello', null, 0, '', 'world')
// => 'hello world'

classes('hello', ['world', null, 'and', 0], 'universe')
// => 'hello world and universe'

classes('hello', {
  world: false,
  universe: true
})
// => hello universe

Here's a simple example using a React component.

import classes from 'spruceup'

function Button (props) {
  const {label, ...styles} = props
  return ('
    <button className={classes('button', styles, ['primary', 'btn'])}></button>
  )
}

Installation

npm install spruceup --save

NPM

Question

For questions and feedback please use our twitter account. For support, bug reports and or feature requests please make sure to read our community guideline and use the issue list of this repo and make sure it's not present yet in our reporting checklist.

Contribution

This is an open source project and would not exist without its community. If you want to participate please make sure to read our guideline before making a pull request. If you have any spruceup related project, component or other let everyone know in our wiki.

License

The MIT License (MIT)

Copyright (c) 2016 Olivier Wietrich

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.