ClassNames Go is a VS Code extension designed to assist developers in wrapping classname strings or to classNames() or clsx() or adding className attribute with classnames or clsx in React files.
It simplifies the process of wrapping class strings with the classnames or clsx library, allowing you to easily combine static Tailwind classes with dynamic CSS Module classes.
- 🔄 One-Command Conversion: Convert
className="..."toclassName={classNames("...")}orclassName={clsx("...")}instantly. - 📦 Auto Import: Automatically detects if
classnamesorclsxis imported. If not, it addsimport classNames from 'classnames'to the top of your file. - 🎯 Migration Ready: Perfect for projects introducing Tailwind CSS into an existing CSS Modules architecture.
-
Open a React file (
.tsxor.jsx). -
Select the
classNameattribute or place your cursor on it.// Before <div className="container flex-row"></div>
-
Open the Command Palette (
Ctrl+Shift+P/Cmd+Shift+P). -
Search and select
ClassNames Go: Convert to ClassNames(Command ID:classnames-go.go). -
The code will be transformed, and the import will be added if missing.
// After import classNames from 'classnames'; // ... <div className={classNames('container flex-row')}></div>;
| Command | Description |
|---|---|
classnames-go.go |
Convert selected className string to classNames function wrapper |
-
Make sure the
classnamespackage is installed in your project:npm install classnames # or yarn add classnames
ClassNames Go 是一款 VS Code 插件,旨在帮助开发者为 React 文件添加 className 属性,将 classname 字符串包裹为 classNames() 或者 clsx() 或者添加 className 并使用 classnames 或者 clsx。
它简化了使用 classnames 库包裹类名字符串的过程,让您可以轻松地将静态 Tailwind 类与动态 CSS Module 类结合起来。
- 🔄 一键转换:瞬间将
className="..."转换为className={classNames("...")}或者 ``className={clsx("...")}`。 - 📦 自动导入:自动检测是否已导入
classnames。如果没有,它会自动在文件顶部添加import classNames from 'classnames'。 - 🎯 迁移就绪:非常适合在现有的 CSS Modules 架构中引入 Tailwind CSS 的项目。
-
打开一个 React 文件 (
.tsx或.jsx)。 -
选中
className属性或将光标放在该属性上。// 转换前 <div className="container flex-row"></div>
-
打开命令面板 (
Ctrl+Shift+P/Cmd+Shift+P)。 -
搜索并选择
ClassNames Go: Convert to ClassNames(命令 ID:classnames-go.go)。 -
代码将被转换,如果缺少导入语句,插件会自动添加。
// 转换后 import classNames from 'classnames'; // ... <div className={classNames('container flex-row')}></div>;
| 命令 | 描述 |
|---|---|
classnames-go.go |
将选中的 className 字符串转换为 classNames 函数包裹形式 |
-
请确保您的项目中已安装
classnames包:npm install classnames # 或 yarn add classnames