Warning
This package is no longer maintained. Consider clsx, equally tiny and more powerful utility that does the same thing.
A function that merges given class names, no matter their format. Filters out invalid class names as well.
- Install by executing
npm install merge-class-names
oryarn add merge-class-names
. - Import by adding
import mergeClassNames from 'merge-class-names'
. - Use it in
className
like so:<div className={mergeClassNames('foo', condition && 'bar', arrayOfClasses)} />
- Strings with one or multiple class names:
a
,a b
- Array of strings with one or multiple class names:
['a', 'b']
,['a b', 'c d']
.
> mergeClassNames('a', 'b', 'c');
< 'a b c'
> mergeClassNames('a b', 'c d', 'e f');
< 'a b c d e f'
> mergeClassNames(['a', 'b'], ['c', 'd']);
< 'a b c d'
> mergeClassNames(['a b', 'c d'], ['e f', 'g h']);
< 'a b c d e f g h'
> mergeClassNames('a', 'b', falsyCondition && 'c');
< 'a b'
> mergeClassNames('a', 'b', 'c', null, ['d', null], () => {}, 'e', undefined);
< 'a b c d e'
The MIT License.
Wojciech Maj kontakt@wojtekmaj.pl https://wojtekmaj.pl |