Skip to content

domisolo/reclsx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4fdc707 · Aug 23, 2024

History

8 Commits
Jul 6, 2022
Jul 6, 2022
Jul 6, 2022
May 7, 2024
Jul 6, 2022
Jun 9, 2022
Jun 9, 2022
May 7, 2024
Aug 23, 2024
May 7, 2024
Jul 6, 2022

Repository files navigation

Reclsx


NPM version NPM Weekly Downloads License

Dynamically classnames in React
Based on react/jsx-runtime
Using clsx

Example

example

Install

# via pnpm
$ pnpm i reclsx clsx

# via npm
$ npm i reclsx clsx

# via yarn
$ yarn add reclsx clsx

Usage

The constructing className conditionally using lukeed/clsx, For detailed API options, please refer to clsx

// basic
<div className="header navbar" /> // => <div class="header navbar"></div>

// conditionally
<div className={{ 'header': true, 'navbar': false, 'foo': true }} /> // => <div class="header foo"></div>
<div className={[ 'header', { 'navbar': false }, 'content', { 'foo': true } ]} /> // => <div class="header content foo"></div>
<div className={[ 'header', 'navbar', { 'navbar': false }, 'foo' ]} /> // => <div class="header foo"></div>

// dedupe
<div className={[ 'header', 'header', 'foo' ]} /> // => <div class="header foo"></div>

Configure

Use Reclsx's jsx functions instead of the default jsx-runtime when compiling JSX.

TypeScript support(Optional)

Make the Typescript compiler recognize Reclsx's className types in React.

{
  "compilerOptions": {
    "jsxImportSource": "reclsx"
  }
}

For Vite

// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react({
      jsxImportSource: "reclsx",
    }),
  ],
});

For Babel

Works with Webpack Rollup Taro etc.

// .babelrc
{
  "presets": [
    [
      "@babel/preset-react",
      {
        "runtime": "automatic",
        "importSource": "reclsx"
      }
    ]
  ]
}

For Esbuild

// via JS
require("esbuild").buildSync({
  entryPoints: ["app.jsx"],
  bundle: true,
  jsxFactory: "jsx",
  inject: ['./node_modules/reclsx/dist/jsx-runtime.js'],
  outfile: "out.js",
});

Support

@babel/preset-react >= v7.9.0
jsxRuntime: "automatic"

LICENSE MIT

Copyright © 2022 Daniel Zhao.