# @salla\.sa/twilight-components-react 中文文档

规范地址：https://npmcn.com/package/%40salla.sa/twilight-components-react/

版本：3\.0\.21
npm 元数据更新时间：2026-11-02T09:08:53.829Z
中文译文更新时间：2026-11-02T09:50:11.065Z
软件包许可：GPL-3\.0

本文由上游 README 机器翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40salla.sa/twilight-components-react
原文对照：https://npmcn.com/package/%40salla.sa/twilight-components-react/?lang=original
项目主页：https://salla.dev/
源码仓库：https://github.com/sallaapp/twilight-components

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：792 kB，731 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：4 个
- peer 依赖：react \>=17\.0\.0 \|\| ^19，react-dom \>=17\.0\.0 \|\| ^19，@salla\.sa/twilight-components ^3\.0\.0
- 首次发布：2026-07-13T19:35:06.897Z
- 最新版本发布：2026-11-02T09:08:53.630Z
- 已发布版本数：23（近 12 个月 23 个）

## 中文简介

Salla Twilight Web组件的React包装器

## 安装

```sh
npm install @salla.sa/twilight-components-react
```

也可以使用：`pnpm add @salla.sa/twilight-components-react`、`yarn add @salla.sa/twilight-components-react`、`bun add @salla.sa/twilight-components-react`

## README

# @salla.sa / twilight-components-react

Salla暮光Web组件的React包装器，完全支持SSR。

## 安装

```bash
npm install @salla.sa/twilight-components-react
# or
pnpm add @salla.sa/twilight-components-react
```

## 使用

### 客户端(CSR)

```tsx
import { SallaButton, SallaModal, SallaAlert } from '@salla.sa/twilight-components-react';

function App() {
  return (
    <div>
      <SallaButton fill="solid" color="primary">
        Click Me
      </SallaButton>
      
      <SallaModal isOpen={false}>
        Modal Content
      </SallaModal>
    </div>
  );
}
```

### 摇树（优化导入）

生成的组件包括 `'use client'` 指令用于Next.js应用路由器兼容性。所有导入都已经针对摇树进行了优化：

```tsx
// ✅ GOOD: Tree-shaking works automatically
import { SallaButton, SallaModal } from '@salla.sa/twilight-components-react';

// ❌ BAD: Imports everything (avoid this)
import * as Twilight from '@salla.sa/twilight-components-react';
```

## 服务器端呈现（SSR）

### 1\. Next.js设置

#### 选项A：使用 `next/dynamic` (推荐)

```tsx
// components/SallaButton.tsx
import dynamic from 'next/dynamic';

export const SallaButton = dynamic(
  () => import('@salla.sa/twilight-components-react').then(mod => mod.SallaButton),
  { ssr: false }
);

// Usage
import { SallaButton } from '@/components/SallaButton';

export default function Page() {
  return <SallaButton>Add to Cart</SallaButton>;
}
```

#### 选项B：使用 `'use client'` 指令（应用路由器）

```tsx
// components/SallaProvider.tsx
'use client';

export { SallaButton, SallaModal, SallaAlert } from '@salla.sa/twilight-components-react';

// app/page.tsx (Server Component)
import { SallaButton } from '@/components/SallaProvider';

export default function Page() {
  return <SallaButton>Server Rendered Button</SallaButton>;
}
```

#### 选项C：与水合模板完全水合

```tsx
// app/layout.tsx or pages/_document.tsx
import { hydrate } from '@salla.sa/twilight-components/hydrate';

// For App Router (layout.tsx)
export default async function RootLayout({ children }) {
  // Hydrate on server
  return (
    <html lang="en">
      <head>
        <script
          dangerouslySetInnerHTML={{
            __html: `
              import('@salla.sa/twilight-components/hydrate').then(m => m.hydrate());
            `
          }}
        />
      </head>
      <body>{children}</body>
    </html>
  );
}
```

### 2\. 自定义SSR设置

```tsx
// server.ts
import express from 'express';
import { hydrateToString } from '@salla.sa/twilight-components/hydrate';

const app = express();

app.get('*', async (req, res) => {
  const html = `
    <!DOCTYPE html>
    <html>
      <body>
        <salla-button>Add to Cart</salla-button>
      </body>
    </html>
  `;
  
  // Hydrate on server
  const hydratedHtml = await hydrateToString(html, '/');
  
  res.send(hydratedHtml);
});
```

### 3\. Vite SSR设置

```tsx
// entry-server.ts
import { hydrateToString } from '@salla.sa/twilight-components/hydrate';

export async function render(url: string) {
  const html = `
    <div id="app">
      <salla-button>Click Me</salla-button>
    </div>
  `;
  
  return await hydrateToString(html, url);
}
```

## 完整的SSR示例与Next.js

### 第一步：制作补水产品

```tsx
// providers/SallaProvider.tsx
'use client';

import { useEffect } from 'react';

export function SallaProvider({ children }: { children: React.ReactNode }) {
  useEffect(() => {
    // Import and hydrate on client
    import('@salla.sa/twilight-components/hydrate').then(({ hydrate }) => {
      hydrate();
    });
  }, []);

  return <>{children}</>;
}
```

### 步骤2：在布局中使用

```tsx
// app/layout.tsx
import { SallaProvider } from '@/providers/SallaProvider';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>
        <SallaProvider>
          {children}
        </SallaProvider>
      </body>
    </html>
  );
}
```

### 步骤3：使用组件

```tsx
// app/page.tsx
import { SallaButton, SallaProductCard } from '@salla.sa/twilight-components-react';

export default function Home() {
  return (
    <main>
      <SallaButton fill="solid" color="primary">
        Add to Cart
      </SallaButton>
      
      <SallaProductCard 
        product-id="123"
        show-quick-add
      />
    </main>
  );
}
```

## 避免水合不匹配

当使用SSR与定制元素，包裹组件，以避免水合误差：

```tsx
// components/SafeSalla.tsx
'use client';

import { useEffect, useState } from 'react';
import { SallaButton } from '@salla.sa/twilight-components-react';

export function SafeSallaButton(props: any) {
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
  }, []);

  if (!mounted) {
    // Return placeholder during SSR
    return <button {...props}>{props.children}</button>;
  }

  return <SallaButton {...props} />;
}
```

## 可用的组件

所有组件 `@salla.sa/twilight-components` 都可以作为React组件使用：

-   `SallaButton` -可自定义按钮
-   `SallaModal` -模态对话框
-   `SallaDrawer` -侧抽屉
-   `SallaAlert` -警报通知
-   `SallaProductCard` -产品卡片显示
-   `SallaCartSummary` -购物车摘要
-   `SallaComments` -评论部分
-   `SallaReviews` -产品评论
-   和更多的……

## 束的优化

### 推荐：摇树与个人进口

```tsx
// ✅ Best - Tree-shake unused components
import { SallaButton } from '@salla.sa/twilight-components-react';
```

### 组件组（包）

为了更好地缓存，这些组件被捆绑在一起：

| 包 | 组件 |
| --- | --- |
| UI的核心 | 小按钮，小装载，小模式，小抽屉，小手风琴，小面包屑 |
| 形式 | sall\_file -upload, sall\_date - time-picker, sall\_color -picker, sall\_condition -fields, sall\_tel\_input, sall\_quantity -input, sall\_book\_field |
| 评论 | 萨拉-评论，萨拉-评论表单，萨拉-评论项目，萨拉-评论，萨拉-评论页面，萨拉-评论摘要，萨拉-星级，萨拉-评论卡 |
| 产品 | Salla-add-product-button, salla-product-options, salla-product-availability, salla-quick-buy |
| 车 | salla-cart-summary, salla-cart-item-offers |
| 警报 | salla-alert, salla-bottom-alert |

## 打印稿的支持

包含完整的TypeScript支持：

```tsx
import type { JSX } from '@salla.sa/twilight-components';

interface ButtonProps extends JSX.SallaButton {
  onClick?: () => void;
}
```

## 相关的包

-   `@salla.sa/twilight-components` -本地web组件
-   `@salla.sa/twilight-components/hydrate` - SSR水化设施

## 常见问题

### @salla\.sa/twilight-components-react 是什么？

Salla Twilight Web组件的React包装器

### 如何安装 @salla\.sa/twilight-components-react？

在项目目录运行 npm install @salla\.sa/twilight-components-react，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（\>=17\.0\.0 \|\| ^19）、react-dom（\>=17\.0\.0 \|\| ^19）、@salla\.sa/twilight-components（^3\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @salla\.sa/twilight-components-react 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @salla\.sa/twilight-components-react 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @salla\.sa/twilight-components-react 可以商用吗？

@salla\.sa/twilight-components-react 使用 GPL-3\.0 许可。强 Copyleft：分发包含该代码的软件时，整个作品需以 GPL-3\.0 兼容许可开源，并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括，不构成法律意见，请以许可原文为准。

### @salla\.sa/twilight-components-react 还在维护吗？

最新版本 3\.0\.21 发布于 2026/11/02（1 天前）；至今共发布 23 个版本，其中过去 12 个月发布了 23 个；首次发布于 2026/07/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

