Skip to content

Commit

Permalink
docs(module): improve Tailwind CSS usage guide (#4668)
Browse files Browse the repository at this point in the history
* docs(module): improve Tailwind CSS usage guide

* docs: update
  • Loading branch information
chenjiahan authored Sep 18, 2023
1 parent 9dd3151 commit eff3fb1
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function createCompiler({
? context.target[index]
: context.target;
const name = TARGET_ID_MAP[target || 'web'];
logger.log(color(`✔ ${name} succeed in`, time));
logger.log(color(`✔ ${name} compiled in`, time));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultOption: Props = {
width: 25,
buildIcon: '◯',
finishIcon: '✔',
finishInfo: 'succeed',
finishInfo: 'compiled',
errorIcon: '✖',
errorInfo: 'compile failed',
message: '',
Expand Down
48 changes: 34 additions & 14 deletions packages/document/main-doc/docs/en/guides/basic-features/css.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,31 @@ If you need to use other CSS-in-JS libraries such as [styled-jsx](https://www.np

## Using Tailwind CSS

[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles. To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, simply run `pnpm run new` in the project root directory and enable it.
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.

Follow the steps below to make a selection:
To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you can follow the steps below:

1. Run `pnpm run new` in the root directory of your project and make the following selections:

```text
? Please select the operation you want: Enable features
? Please select the feature name: Enable Tailwind CSS
```

Register the Tailwind plugin in `modern.config.ts`:
After successful initialization, you will see the following additions to the `package.json` file:

```json title="./package.json"
{
"dependencies": {
"tailwindcss": "^3.0.0"
},
"devDependencies": {
"@modern-js/plugin-tailwindcss": "^2.0.0"
}
}
```

2. Register the Tailwind plugin in `modern.config.ts`:

```ts title="modern.config.ts"
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
Expand All @@ -80,29 +95,34 @@ export default defineConfig({
});
```

To use, add the following code to the root component (such as `src/App.jsx`) of the entry:
3. Create a `index.css` file and add the following code:

```css title="index.css"
@tailwind base;
@tailwind components;
@tailwind utilities;
```

:::info
Depending on your needs, you can selectively import the CSS styles provided by Tailwind CSS. Please refer to the [`@tailwind` documentation](https://tailwindcss.com/docs/functions-and-directives#tailwind) for detailed usage of the `@tailwind` directive.
:::

4. Import the `index.css` file, for example, add the following code in the root component `src/App.jsx`:

```js
import 'tailwindcss/base.css';
import 'tailwindcss/components.css';
import 'tailwindcss/utilities.css';
import './index.css';
```

Then you can use the Utility Class provided by Tailwind CSS in each component:
5. Now you can use the Utility Classes provided by Tailwind CSS in your components:

```tsx
const App = () => (
const Hello = () => (
<div className="h-12 w-48">
<p className="text-xl font-medium text-black">hello world</p>
</div>
);
```

:::info Additional Information
Depending on your needs, you can selectively import the CSS files provided by Tailwind CSS. Since using `@tailwind` is equivalent to directly importing CSS files, you can refer to the comments in the [`@tailwind` usage](https://tailwindcss.com/docs/functions-and-directives#tailwind) documentation for the purpose of the CSS files provided by Tailwind CSS.

:::

### Configuring Tailwind CSS

In Modern.js, you have two ways to configure Tailwind CSS:
Expand Down
48 changes: 34 additions & 14 deletions packages/document/main-doc/docs/zh/guides/basic-features/css.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,31 @@ Modern.js 内部集成了 Babel 的 [babel-plugin-styled-components](https://git

## 使用 Tailwind CSS

[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),只需要在项目根目录下执行 `pnpm run new` 并开启。
[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。

按照如下进行选择:
在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),你只需要按照以下步骤操作:

1. 在项目根目录下执行 `pnpm run new`,按照如下进行选择:

```text
? 请选择你想要的操作 启用可选功能
? 请选择功能名称 启用 「Tailwind CSS」 支持
```

`modern.config.ts` 中注册 Tailwind 插件:
成功开启后,会看到 `package.json` 中新增了依赖:

```json title="./package.json"
{
"dependencies": {
"tailwindcss": "^3.0.0"
},
"devDependencies": {
"@modern-js/plugin-tailwindcss": "^2.0.0"
}
}
```

2.`modern.config.ts` 中注册 Tailwind 插件:

```ts title="modern.config.ts"
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
Expand All @@ -80,29 +95,34 @@ export default defineConfig({
});
```

使用时在入口的根组件(如 `src/App.jsx`)添加如下代码:
3. 创建 `index.css` 文件,添加以下代码:

```css title="index.css"
@tailwind base;
@tailwind components;
@tailwind utilities;
```

:::info
根据需求不同,你可以选择性地导入 Tailwind CSS 提供的 CSS 样式。请参考 [`@tailwind` 文档](https://tailwindcss.com/docs/functions-and-directives#tailwind) 来了解 `@tailwind` 指令的详细用法。
:::

4. 引用 `index.css` 文件,比如在入口的根组件 `src/App.jsx` 添加如下代码:

```js
import 'tailwindcss/base.css';
import 'tailwindcss/components.css';
import 'tailwindcss/utilities.css';
import './index.css';
```

然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:
5. 然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:

```tsx
const App = () => (
const Hello = () => (
<div className="h-12 w-48">
<p className="text-xl font-medium text-black">hello world</p>
</div>
);
```

:::info 补充信息
根据需求不同,你可以选择性的导入 Tailwind CSS 提供的 CSS 文件。由于使用 `@tailwind` 与直接导入 CSS 文件的作用等价,因此关于 Tailwind CSS 提供的 CSS 文件的用途,可以参考 [`@tailwind` 的使用](https://tailwindcss.com/docs/functions-and-directives#tailwind) 文档中注释里的内容。

:::

### 配置 Tailwind CSS

在 Modern.js 中,你有两种方式来配置 Tailwind CSS:
Expand Down
Loading

0 comments on commit eff3fb1

Please sign in to comment.