Skip to content

jaydenseric/class-name-prop

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

class-name-prop

A lightweight utility function to create a React className prop value for multiple class names.

  • 📦 Tiny bundle size, tested.
  • ⚡️ Simple and fast API.
  • 🧠 Returns undefined if there are no class names, to prevent rendering a redundant class attribute; unlike packages like classnames.

Installation

For Node.js, to install class-name-prop with npm, run:

npm install class-name-prop

For Deno and browsers, an example import map:

{
  "imports": {
    "class-name-prop": "https://unpkg.com/class-name-prop@6.0.0/classNameProp.mjs"
  }
}

Then, import and use the function classNameProp.

Examples

A React component for a link that can be declared active, whilst supporting custom class names:

import classNameProp from "class-name-prop";
import React from "react";

function Link({ className, active, ...props }) {
  return React.createElement("a", {
    className: classNameProp(className, active && "active"),
    ...props,
  });
}

Requirements

Supported runtime environments:

Non Deno projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check comment:

Exports

The npm package class-name-prop features optimal JavaScript module design. These ECMAScript modules are exported via the package.json field exports:

About

A lightweight utility function to create a React className prop value for multiple class names.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project