Skip to content

meowtec/react-auto-classnames

Repository files navigation

react-auto-classnames

JSX runtime for applying clsx() to className prop automatically.

NOT a babel plugin.

Installation

npm i react-auto-classnames

Usage

If you use TypeScript, edit your tsconfig.json like:

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react-auto-classnames",
  }
}

If you use @babel/preset-react to transform jsx:

// .babelrc / babel.config.json
{
  "presets": [
    [
      "@babel/preset-react",
      {
        "runtime": "automatic",
        "importSource": "react-auto-classnames"
      }
    ]
  ]
}

If you use esbuild (take vite for example):

{
  esbuild: {
    jsxFactory: 'createElement',
    jsxFragment: 'Fragment',
    jsxInject: 'import { createElement, Fragment } from \'react-auto-classnames\'',
  },
}

Then you can write React code like this:

function App() {
  return (
    <div className={['class-a', 'class-b', {
      'class-disabled': disabled,
    }]}>
  )
}

clsx will be used to handle the className prop.

About

JSX runtime for applying `clsx()` automatically

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published