🌐 Nodejs.cn

创建一个支持 RTL 的新 Vite 项目。

创建项目

🌐 Create Project

使用 --rtl 标志和 vite 模板创建一个新项目。

🌐 Create a new project using the --rtl flag and the vite template.

如果你已经使用 shadcn/create 创建了项目,可以跳过此步骤。

pnpm dlx shadcn@latest create --template vite --rtl

这将创建一个带有 rtl: true 标志的 components.json 文件。

🌐 This will create a components.json file with the rtl: true flag.

components.json
{
  "$schema": "https://shadcn.nodejs.cn/schema.json",
  "style": "base-nova",
  "rtl": true
}

添加 DirectionProvider

🌐 Add DirectionProvider

在你的 index.html 中,将 dir="rtl"lang="ar" 属性添加到 html 标签。将 lang="ar" 更新为你的目标语言。

🌐 Add the dir="rtl" and lang="ar" attributes to the html tag in your index.html. Update lang="ar" to your target language.

index.html
<!doctype html>
<html lang="ar" dir="rtl">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite App</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

然后在你的 main.tsx 中,用带有 direction="rtl" 属性的 DirectionProvider 组件封装你的应用:

🌐 Then wrap your app with the DirectionProvider component with the direction="rtl" prop in your main.tsx:

src/main.tsx
import { StrictMode } from "react"
import { createRoot } from "react-dom/client"
 
import { DirectionProvider } from "@/components/ui/direction"
 
import App from "./App"
 
import "./index.css"
 
createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <DirectionProvider direction="rtl">
      <App />
    </DirectionProvider>
  </StrictMode>
)

添加字体

🌐 Add Font

为了获得最佳的从右到左阅读体验,我们建议使用对目标语言有良好支持的字体。Noto 是一个很好的字体系列,它与 Inter 和 Geist 搭配得很好。

🌐 For the best RTL experience, we recommend using fonts that have proper support for your target language. Noto is a great font family for this and it pairs well with Inter and Geist.

使用 Fontsource 安装字体:

🌐 Install the font using Fontsource:

pnpm add @fontsource-variable/noto-sans-arabic

在你的 index.css 中导入字体:

🌐 Import the font in your index.css:

src/index.css
@import "tailwindcss";
@import "tw-animate-css";
@import "shadcn/tailwind.css";
@import "@fontsource-variable/noto-sans-arabic";
 
@theme inline {
  --font-sans: "Noto Sans Arabic Variable", sans-serif;
}

对于其他语言,例如希伯来语,你可以使用 @fontsource-variable/noto-sans-hebrew

🌐 For other languages, eg. Hebrew, you can use @fontsource-variable/noto-sans-hebrew.

添加组件

🌐 Add Components

你现在可以向你的项目添加组件了。CLI 将为你处理 RTL 支持。

🌐 You are now ready to add components to your project. The CLI will take care of handling RTL support for you.

pnpm dlx shadcn@latest add item