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 🙏

© 2026 – Pkg Stats / Ryan Hefner

clsx-react

v1.0.1

Published

Custom React JSX Runtime to support arrays and objects in className prop natively.

Readme

Cover Image

GitHub License npm GitHub Repo stars Run tests codecov Sponsor

clsx-react - JSX Super Power for className

Stop importing clsx or classnames manually.

clsx-react is a zero dependency, super tiny, custom React JSX runtime that natively supports arrays and objects in the className prop. It automatically applies clsx logic at the runtime level, keeping your code clean and your imports empty.

The Problem

You need conditional class names in your React components, but importing and using clsx or classnames everywhere leads to repetitive boilerplate code.

// ❌ Old way: Boilerplate everywhere
import clsx from 'clsx'; // or classnames

export const Button = ({ active, disabled }) => (
  <button className={clsx('btn', { 'btn-active': active, 'btn-disabled': disabled })}>Click me</button>
);

The Solution

No more imports or boilerplate. Just use arrays and objects directly in className. Strings still work as usual.

// ✅ New way: Zero imports, native syntax
export const Button = ({ active, disabled }) => (
  <button className={['btn', { 'btn-active': active, 'btn-disabled': disabled }]}>Click me</button>
);

Installation

npm install clsx-react
# or
yarn add clsx-react
# or
pnpm add clsx-react

Note: Requires react >= 17.0.0.

Configuration

To make this work, you need to tell your compiler to use this package as the JSX Import Source instead of the default react. Let me guide you through the setup for various environments.

1. TypeScript (tsconfig.json) / JavaScript (jsconfig.json) - Recommended

This handles both the compilation and the type definitions (so TS won't complain about arrays in className).

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "clsx-react"
  }
}

2. Vite (vite.config.ts) / Esbuild

If you are using Vite, you can set it explicitly in the config:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  esbuild: {
    jsxImportSource: 'clsx-react',
  },
});

3. Next.js / SWC / Turbopack

Next.js usually respects tsconfig.json or jsconfig.json. Ensure your compilerOptions are set as shown in step 1.


4. Babel / Webpack

If you are using Babel, you can set the jsxImportSource in your Babel config:

{
  "presets": [
    [
      "@babel/preset-react",
      {
        "runtime": "automatic",
        "importSource": "clsx-react"
      }
    ]
  ]
}

Usage Examples

Once configured, you can use className just like you would use the clsx function arguments.

Conditional Classes (Object)

<div className={{ hidden: isHidden, flex: isFlex }}>...</div>

Arrays

<div className={['text-lg', 'font-bold', isError && 'text-red-500']}>...</div>

Mixed & Nested

<div className={['p-4', { 'bg-gray-100': !dark }, ['shadow-md', 'rounded']]}>...</div>

Standard String (Still works)

<div className="just-a-string">...</div>

How it works

This package wraps the standard react/jsx-runtime and react/jsx-dev-runtime. It intercepts the creation of every JSX element:

  1. Checks if className prop exists.
  2. Checks if className is not a string (array or object).
  3. If so, it processes it with a bundled, lightweight version of clsx.
  4. Passes the processed props to the original React runtime.

It adds negligible overhead (bytes) and eliminates the need to manually import and call class utilities in every single component file.

TypeScript Support

This package includes a global augmentation for React.HTMLAttributes. Once you set "jsxImportSource": "clsx-react" in your tsconfig.json, TypeScript will automatically understand that className accepts arrays and objects. No extra .d.ts configuration needed!

Guidelines

See Code of Conduct, Contributing, and Security Policy.

License

MIT License © 2026 Zsolt Tövis

If you find this project useful, please consider sponsoring me on GitHub, PayPal, or give the repo a star.