Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fdaciuk/strclass

A ~200b library to apply CSS classes as strings from objects
https://github.com/fdaciuk/strclass

classname classnames css hacktoberfest react reactjs strclass

Last synced: 17 days ago
JSON representation

A ~200b library to apply CSS classes as strings from objects

Awesome Lists containing this project

README

        

# strclass

> A tiny library to apply CSS classes as strings from objects

[![Build Status][travis-image]][travis-url]
[![Coveralls Coverage Status][coverage-image]][coverage-url]

## Installation

```
yarn add strclass
```

## Usage

`strclass` is an _Universal Module_, and can be used as a global,
with AMD, CommonJS or ES Module.

**Signature:**

```
strclass({ className: booleanConditionToShowThisClass }, [defaultClass1, defaultclass2, ..., defaultclassN])
```

**Usage:**

```js
import strclass from 'strclass'

console.log(strclass({ highlighted: true }))
// 'highlighted'

console.log(strclass({ '-active': true }, 'button'))
// 'button -active'

console.log(strclass({ '-active': true }, 'button', 'button-default'))
// 'button button-default -active'

console.log(strclass({ active: false }))
// ''
```

You can use with React, VanillaJS or any other lib/framework:

```js
const Button = ({ active, children }) => (

{children}

)

Click!
// will render: Click!

Click!
// will render: Click!
```

If you are using _CSS Modules_, or need to pass the class as a dynamic value, just do:

```js
const ac = 'active'
console.log(strclass({ [ac]: true })) // 'active'
```

## Differences from [`classnames module`](https://github.com/JedWatson/classnames)

**Size**

`strclass` has ~200bytes gzipped.

**Signature**

With `classnames`, you can pass any argument as `String`, `Object` or `Array`.
Otherwise, `strclass` has a signature that should be respected: `Object` as a first argument, with the conditionals,
and any other argument should be a `String`.

If you don't need conditionals, you don't need `strclass`. Just use `strings`.

That's it =)

## License

[MIT](license-url) © Fernando Daciuk

[travis-image]: https://img.shields.io/travis/fdaciuk/strclass.svg?style=flat-square
[travis-url]: https://travis-ci.org/fdaciuk/strclass
[coverage-image]: https://img.shields.io/coveralls/fdaciuk/strclass/master.svg?style=flat-square
[coverage-url]: https://coveralls.io/r/fdaciuk/strclass?branch=master
[license-url]: https://github.com/fdaciuk/licenses/blob/master/MIT-LICENSE.md