@cossistant/react

中文文档
0.3.0发布于 2026/08/05MITTypeScriptES Module近 30 天下载 1.2万

Headless React SDK for building AI-powered support/chat widgets. Hooks + primitives, WS-driven, TypeScript-first. Next.js-ready, Tailwind optional.

原始使用文档
文档目录10 节
  1. Install
  2. Import styles
  3. Quickstart
  4. Headless Primitives
  5. Feedback Quickstart
  6. Support Styling Hooks
  7. Feedback Styling Hooks
  8. Swap One Part with slots
  9. Full Composition with Support.Root
  10. More Docs

Cossistant React SDK

Build a ready-to-use support widget in React with good defaults, fast styling, and a composable API when you need to go further.

Install

bun add @cossistant/react

Import styles

Use one stylesheet at your app root:

import "@cossistant/react/styles.css";

Or, if your app already uses Tailwind CSS v4:

import "@cossistant/react/support.css";

Quickstart

Use the drop-in widget when you want Cossistant to own the runtime: client lifecycle, visitor context, websocket state, routing, open state, slots, and localization.

import { LazySupport } from "@cossistant/react/lazy-support";
import { SupportProvider } from "@cossistant/react/provider";
import "@cossistant/react/styles.css";
import { Suspense } from "react";

export function App() {
  return (
    <SupportProvider publicKey="pk_live_...">
      <Suspense fallback={null}>
        <LazySupport />
      </Suspense>
    </SupportProvider>
  );
}

LazySupport code-splits the batteries-included widget. It ships with the default trigger, router, home page, conversation page, timeline, composer, and styling hooks while keeping that UI out of the initial application chunk. Use Support from @cossistant/react/support when an eager component or compound subcomponents such as Support.Trigger are required.

Headless Primitives

Use hooks and primitives directly when you want to own the UI state and pass an explicit client. This does not require wrapping that subtree in SupportProvider.

import { CossistantClient } from "@cossistant/core";
import { SupportTrigger } from "@cossistant/react/primitives/trigger";
import { useSubmitFeedback } from "@cossistant/react/hooks/use-submit-feedback";
import { useState } from "react";

const client = new CossistantClient({ publicKey: "pk_live_..." });

export function CustomFeedbackButton() {
  const [open, setOpen] = useState(false);
  const feedback = useSubmitFeedback({ client });

  return (
    <div>
      <SupportTrigger
        className="rounded-md bg-black px-3 py-2 text-white"
        isOpen={open}
        onToggleOpen={() => setOpen((value) => !value)}
        unreadCount={0}
      >
        {({ isOpen }) => (isOpen ? "Close" : "Send feedback")}
      </SupportTrigger>

      {open ? (
        <button
          onClick={() =>
            feedback.mutate({
              rating: 5,
              source: "headless",
              visitorId: "visitor_123",
            })
          }
          type="button"
        >
          Send rating
        </button>
      ) : null}
    </div>
  );
}

Provider-optional hooks include useSubmitFeedback, useSendMessage, useCreateConversation, useFileUpload, and useFeedbackForm. If client is omitted they fall back to SupportProvider; if no client is available they fail with a clear missing-client error.

Feedback Quickstart

Use Feedback when you want a lightweight rating/comment widget backed by the same visitor context.

import { Feedback, SupportProvider } from "@cossistant/react";
import "@cossistant/react/styles.css";

export function App() {
  return (
    <SupportProvider publicKey="pk_live_...">
      <Feedback topics={["Bug", "Feature request", "UX", "Other"]} />
    </SupportProvider>
  );
}

Feedback supports the same controlled open pattern as the support widget:

import { Feedback } from "@cossistant/react/feedback";
import { useState } from "react";

export function ControlledFeedback() {
  const [open, setOpen] = useState(false);

  return (
    <Feedback.Root open={open} onOpenChange={setOpen}>
      <Feedback.Trigger asChild>
        <button type="button">Feedback?</button>
      </Feedback.Trigger>

      <Feedback.Content side="bottom" align="end">
        <div className="p-4">Build any feedback UI here.</div>
      </Feedback.Content>
    </Feedback.Root>
  );
}

Support Styling Hooks

Start with:

  • classNames.trigger
  • classNames.content
  • slotProps

The default support widget also exposes stable DOM hooks:

  • data-slot
  • data-state
  • data-page
  • data-support-mode

Feedback Styling Hooks

Start with:

  • classNames.trigger
  • classNames.content

The default feedback widget also exposes stable DOM hooks:

  • data-slot
  • data-state
  • data-feedback-*

Common slots include feedback-root, feedback-trigger, feedback-content, feedback-panel, feedback-form, feedback-rating-field, and feedback-submit.

Swap One Part with slots

Use slots when you want better DX than rebuilding the whole widget tree.

import {
  Support,
  type SupportHomePageSlotProps,
  type SupportTriggerSlotProps,
} from "@cossistant/react";
import * as React from "react";

function mergeClassNames(...classes: Array<string | undefined>) {
  return classes.filter(Boolean).join(" ");
}

const CustomBubble = React.forwardRef<
  HTMLButtonElement,
  SupportTriggerSlotProps
>(function CustomBubble({ isOpen, unreadCount, toggle, className, ...props }, ref) {
  return (
    <button
      {...props}
      className={mergeClassNames("fixed right-4 bottom-4 z-[9999]", className)}
      onClick={toggle}
      ref={ref}
      type="button"
    >
      {isOpen ? "Close" : "Need help?"} ({unreadCount})
    </button>
  );
});

function CustomHomePage({
  quickOptions,
  startConversation,
}: SupportHomePageSlotProps) {
  return (
    <div className="flex h-full flex-col gap-3 p-6">
      <h2 className="text-2xl font-semibold">Real support, instantly.</h2>
      {quickOptions.map((option) => (
        <button
          key={option}
          onClick={() => startConversation(option)}
          type="button"
        >
          {option}
        </button>
      ))}
    </div>
  );
}

<Support
  slots={{
    trigger: CustomBubble,
    homePage: CustomHomePage,
  }}
  slotProps={{
    content: {
      className: "rounded-3xl border shadow-2xl",
    },
  }}
/>;

Full Composition with Support.Root

Use Support.Root when you want a custom shell, explicit page registration, or inline placement. In this mode, you own the trigger layout classes.

import { Support } from "@cossistant/react";

function LaunchChecklistPage() {
  return <div className="p-6">Your custom home page</div>;
}

export function App() {
  return (
    <Support.Root open>
      <Support.Trigger asChild>
        <button className="fixed right-4 bottom-4 z-[9999]" type="button">
          Compose support
        </button>
      </Support.Trigger>

      <Support.Content className="rounded-3xl border shadow-2xl">
        <Support.Router>
          <Support.Page component={LaunchChecklistPage} name="HOME" />
        </Support.Router>
      </Support.Content>
    </Support.Root>
  );
}

More Docs

版本与发布记录

@cossistant/react 共发布 34 个版本,首次发布于 2025/10/22;过去 12 个月发布了 17 个版本。

版本发布日期距今
0.3.0latest2026/08/053 个月前
0.2.02026/04/216 个月前
0.1.22026/03/127 个月前
0.1.12026/02/168 个月前
0.1.02026/02/148 个月前
0.0.332026/02/108 个月前

关于 @cossistant/react 的常见问题

@cossistant/react 是什么?

Headless React SDK for building AI-powered support/chat widgets. Hooks + primitives, WS-driven, TypeScript-first. Next.js-ready, Tailwind optional.

如何安装 @cossistant/react?

在项目目录运行 npm install @cossistant/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18 <20)、react-dom(>=18 <20)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cossistant/react 支持 TypeScript 吗?

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

@cossistant/react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cossistant/react 可以商用吗?

@cossistant/react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cossistant/react 的下载量有多少?

根据 npm 官方下载统计,@cossistant/react 在 2026/10/11 – 2026/11/09 这 30 天内被下载 12,113 次,日均约 404 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cossistant/react 还在维护吗?

最新版本 0.3.0 发布于 2026/08/05(3 个月前);至今共发布 34 个版本,其中过去 12 个月发布了 17 个;首次发布于 2025/10/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。