npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

classjoin

v0.2.1

Published

A bit simpler utility for conditionally joining classNames together

Downloads

118

Readme

classjoin

A bit simpler utility for conditionally joining classNames together.

It’s simplified version of classnames package. The original package is not very lage in size, but it can be a bit simpler. Also, it has no ES-modules verison, and TypeScript types should be installed separately. A lot of problems for so small and simple package, so I write my own in TypeScript.

There is only two options:

  • You can specify classes with conditions in object as first argument.
    If the value of the key is falsy, class won't be included in the output.
  • You can specify classes without conditions in array as second argument.

If you want to take classes from several objects, just combine them useing Object.assign or spread operator and use resulting object.

Installation

For bundlers and other NPM-based environments:

npm install --save-dev classjoin

Usage

The first argument of the function is object and it’s required (because it’s the main purpose of this package, if you want just to combine classes from array, you can use .join( ' ' )).

You can use second argument to specify classes, that should be added to the output. This argument is not required and can be omitted. Classes from this array added to the output string before classes from the object (because they always exists).

function MyComponent( {active, disabled, items} )
{
	const className = classJoin(
		{
			active,
			disabled,
			multiple: ( items.length > 1 ),
		},
		[
			'my-component',
		]
	);
	
	return (
		<div className={className}></div>
	);
}

// <MyComponent active items={['one', 'two']} />
// → <div class="my-component active multiple"></div>

UMD

UMD is default for this package, so just use something like:

import classJoin from 'classjoin';
// or
const classJoin = require( 'classjoin' );

For using directly in browser (import with <script> tag in HTML-file):

You can use AMD or classJoin global variable.

ES2015 module systems

Package contain module property for use with ES2015 module bundlers (like Rollup and Webpack 2).

ES2015 code base

If you don’t want to use transplitted to ES5 code, you can use included ES2015 version.

You can directly import this version:

import classJoin from 'classjoin/es2015';

Or specify alias in Webpack config:

{
	// …
	resolve: {
		extensions: ['.ts', '.tsx', '.js'],
		alias: {
			'classjoin': 'classjoin/es2015',
		},
	},
};

License

MIT.