clsx is a compact (239B gzipped) and highly efficient utility for conditionally constructing `className` strings in JavaScript and TypeScript applications. It offers a faster and smaller alternative to similar libraries like `classnames`. The package is actively maintained, with recent releases like v2.1.1, suggesting a steady cadence of minor features and patches. Key differentiators include its minimal footprint, support for various argument types (strings, objects, arrays, and nested structures), and the `clsx/lite` submodule (140B gzipped) for string-only use cases, particularly beneficial in environments like Tailwind CSS. It ships with TypeScript types and is supported across all Node.js versions and browsers supporting `Array.isArray` (IE9+).
npm install clsxVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates `clsx`'s ability to combine strings, objects, and arrays conditionally to generate a final CSS class string, handling various data types and falsey values.
Ensure your build tools (e.g., Webpack, Rollup, Vite) and Node.js version are configured to handle native ESM. You might need to update `tsconfig.json` for TypeScript to `"moduleResolution": "node16"` or `"nodenext"`.
If IE8 support is critical, use `clsx@1.0.x`. Otherwise, ensure your target browser list (browserslist) reflects IE9+ compatibility.
Only use `clsx/lite` when you are certain all arguments will be strings. For mixed argument types, use the full `clsx` module.
Understand that `clsx(true, false, '', null, undefined, 0, NaN)` will return `''`. Only truthy string or object/array values will contribute to the output string.
Feel free to use `import { clsx } from 'clsx';` if preferred, but be aware it's functionally identical to `import clsx from 'clsx';`. Choose one style for consistency within your project.Ensure you are using the correct import/require syntax for your project's module system. For ES Modules, use `import clsx from 'clsx'` or `import { clsx } from 'clsx'`. For CommonJS, use `const clsx = require('clsx');`. If using a bundler, verify its configuration for module resolution.If you need to pass objects or arrays for conditional class logic, use the full `clsx` module: `import { clsx } from 'clsx';` instead of `import { clsx } from 'clsx/lite';`.Add a `tailwindCSS.experimental.classRegex` configuration to your VS Code `settings.json` to include `clsx` patterns. Refer to the `clsx` README for the recommended configuration.
No dependency data recorded yet.