@salla.sa/twilight-components-react

中文文档
3.0.21发布于 2026/11/02GPL-3.0TypeScriptESM + CJS

React wrapper for Salla Twilight Web Components

原始使用文档
文档目录19 节
  1. Installation
  2. Usage
  3. Client-Side (CSR)
  4. Tree-Shaking (Optimized Imports)
  5. Server-Side Rendering (SSR)
  6. 1. Next.js Setup
  7. 2. Custom SSR Setup
  8. 3. Vite SSR Setup
  9. Full SSR Example with Next.js
  10. Step 1: Create Hydration Provider
  11. Step 2: Use in Layout
  12. Step 3: Use Components
  13. Avoiding Hydration Mismatches
  14. Available Components
  15. Bundle Optimization
  16. Recommended: Tree-shaking with Individual Imports
  17. Component Groups (Bundles)
  18. TypeScript Support
  19. Related Packages

@salla.sa/twilight-components-react

React wrapper for Salla Twilight Web Components with full SSR support.

Installation

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

Usage

Client-Side (CSR)

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>
  );
}

Tree-Shaking (Optimized Imports)

The generated components include 'use client' directive for Next.js App Router compatibility. All imports are already optimized for tree-shaking:

// ✅ 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';

Server-Side Rendering (SSR)

1. Next.js Setup

// 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>;
}

Option B: Using 'use client' Directive (App Router)

// 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>;
}

Option C: Full Hydration with Stencil Hydrate

// 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. Custom SSR Setup

// 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 Setup

// 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);
}

Full SSR Example with Next.js

Step 1: Create Hydration Provider

// 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}</>;
}

Step 2: Use in Layout

// 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>
  );
}

Step 3: Use Components

// 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>
  );
}

Avoiding Hydration Mismatches

When using SSR with custom elements, wrap components to avoid hydration errors:

// 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} />;
}

Available Components

All components from @salla.sa/twilight-components are available as React components:

  • SallaButton - Customizable button
  • SallaModal - Modal dialog
  • SallaDrawer - Side drawer
  • SallaAlert - Alert notifications
  • SallaProductCard - Product card display
  • SallaCartSummary - Cart summary
  • SallaComments - Comments section
  • SallaReviews - Product reviews
  • And more...

Bundle Optimization

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

Component Groups (Bundles)

These components are bundled together for better caching:

BundleComponents
UI Coresalla-button, salla-loading, salla-modal, salla-drawer, salla-accordion, salla-breadcrumb
Formssalla-file-upload, salla-datetime-picker, salla-color-picker, salla-conditional-fields, salla-tel-input, salla-quantity-input, salla-booking-field
Commentssalla-comments, salla-comment-form, salla-comment-item, salla-reviews, salla-reviews-page, salla-reviews-summary, salla-rating-stars, salla-review-card
Productsalla-add-product-button, salla-product-options, salla-product-availability, salla-quick-buy
Cartsalla-cart-summary, salla-cart-item-offers
Alertssalla-alert, salla-bottom-alert

TypeScript Support

Full TypeScript support is included:

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

interface ButtonProps extends JSX.SallaButton {
  onClick?: () => void;
}
  • @salla.sa/twilight-components - Native web components
  • @salla.sa/twilight-components/hydrate - SSR hydration utilities

版本与发布记录

@salla.sa/twilight-components-react 共发布 23 个版本,首次发布于 2026/07/14;过去 12 个月发布了 23 个版本。

版本发布日期距今
3.0.21latest2026/11/021 天前
3.0.202026/11/021 天前
3.0.192026/11/021 天前
3.0.182026/11/012 天前
3.0.172026/10/295 天前
3.0.162026/10/285 天前

其他发布通道:beta → 3.0.0-beta.1。

关于 @salla.sa/twilight-components-react 的常见问题

@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 和提交记录判断。