class-name-prop
A lightweight utility function to create a React className
prop value for multiple classes.
- 📦 Tiny bundle size, tested.
- 💪 Supports ancient browsers.
- ⚡️ Simple and fast API.
- 🧠 Returns
undefined
if there are no classes, to prevent rendering a redundant class
attribute; unlike packages like classnames
.
Setup
To install with npm, run:
npm install class-name-prop
API
Table of contents
function classNameProp
Creates a React className
prop value for multiple classes.
Parameter | Type | Description |
---|
classes | …* | A parameter for each class; only non empty strings are added to the final string. |
Returns: string | undefined
— A className
prop value; either a string of classes or undefined
to prevent rendering an empty class
attribute.
Examples
How to import
.
import classNameProp from 'class-name-prop';
How to require
.
const classNameProp = require('class-name-prop');
A React component for a link that can be declared active, whilst supporting custom classes.
function Link({ className, active, ...props }) {
return (
<a className={classNameProp(className, active && 'active')} {...props} />
);
}