Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

classnames.macro

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

classnames.macro

Babel Macro for conditionally joining CSS class names together

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

classnames.macro

Conditionally join CSS class names at build time in Babel.

Preamble

The main goal of this package is to provide convenient API for CSS class names manipulation with the smallest performance hit.

Use with caution. The package is aiming to implement as many of classnames features as it is possible for a macro. classnames.macro is actively used as it is being developed but is not guaranteed to be problem-free.

Usage

Install and configure babel-plugin-macros if you haven’t yet. Then use classnames.macro almost the same way you use classnames.

Example

Given the following input:

import classNames from "classnames.macro"

function getClassName(options) {
  return classNames("block", options.dark && ["bg-black", "text-white"], {
    "font-bold": options.variant === "head",
    "text-sm": options.variant === "footnote",
  })
}

Babel will produce the following output:

function getClassName(options) {
  return (
    "block" +
    ((options.dark ? " bg-black text-white" : "") +
      ((options.variant === "head" ? " font-bold" : "") +
        (options.variant === "footnote" ? " text-sm" : "")))
  )
}

Performance

classnames.macro outperforms (20 to 100 times faster) packages with similar API:

PackageResultsNotes
classcat5 336 322 ops/s, ±0.37%95.06% slower
classnames1 962 814 ops/s, ±0.17%slowest, 98.18% slower
classnames.macro107 975 749 ops/s, ±0.27%fastest
clsx4 886 815 ops/s, ±0.27%95.47% slower
html-classes4 410 463 ops/s, ±0.17%95.92% slower

Benchmark source can be found in benchmark.js. To run the benchmark, clone the repo, install dependencies, build the package (yarn build) and run benchmark (yarn benchmark).

License

MIT

Keywords

FAQs

Package last updated on 19 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc