Skip to content

babel plugin for automatically adding `classnames()` to `className` attribute.

License

Notifications You must be signed in to change notification settings

meowtec/babel-plugin-jsx-classnames

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-jsx-classnames

babel plugin for automatically adding classnames() to className attribute.

transform

<div className={expression}></div>

to

<div className={classNames(expression)}></div>

Why

classNames

In React, when you need to render conditionally classNames, you can write code like this:

<button className={`${active ? 'btn-active' : ''} ${disabled ? 'btn-disabled' : ''}`}>
</button>

classnames can help you write clear conditionally classNames:

<button
  className={classNames({
    'btn-active': active,
    'btn-disabled': disabled,
  })}>
</button>

babel-plugin-jsx-classnames

This babel plugin will keep you away from writing many classNames, make the code clean and easy to read.

Usage

Install:

npm i babel-plugin-jsx-classnames --save-dev

Use plugin (example .babelrc):

{
  "presets": [
    "env",
    "react"
  ],
  "plugins": [
    ...
    "jsx-classnames"
  ]
}

Write your jsx:

<button
  className={{
    'btn-active': active,
    'btn-disabled': disabled,
  }}>
</button>

It will be transformed to:

import classNames from 'classnames'

<button
  className={classNames({
    'btn-active': active,
    'btn-disabled': disabled,
  })}>
</button>

About

babel plugin for automatically adding `classnames()` to `className` attribute.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published