111k

TanStack Start

创建一个支持 RTL 的 TanStack Start 项目。

创建项目

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

如果你已经使用 shadcn/create 创建过项目,则可以跳过这一步。

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

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

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

添加 DirectionProvider

html 标签上添加 dir="rtl"lang="ar" 属性。将 lang="ar" 改成你的目标语言。

然后在你的 __root.tsx 中使用 direction="rtl" 属性将 DirectionProvider 组件包裹在应用外层:

src/routes/__root.tsx
import { DirectionProvider } from "@/components/ui/direction"
 
export const Route = createRootRoute({
  component: RootComponent,
})
 
function RootComponent() {
  return (
    <html lang="ar" dir="rtl">
      <head>
        <Meta />
      </head>
      <body>
        <DirectionProvider direction="rtl">{children}</DirectionProvider>
        <Scripts />
      </body>
    </html>
  )
}

添加字体

为了获得最佳的 RTL 体验,我们建议使用对目标语言支持良好的字体。Noto 是一个很好的字体家族,而且它和 Inter、Geist 搭配得很好。

通过 Fontsource 安装该字体:

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

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

src/styles.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

添加组件

现在你已经可以向项目中添加组件了。CLI 会自动处理 RTL 支持。

pnpm dlx shadcn@latest add item