@desource/context7-widget-svelte

中文文档
0.2.3发布于 2026/11/01MITTypeScriptES Module构建来源已验证近 30 天下载 1.7万

Svelte 5 component and reactive controller for the customizable Context7 documentation chat widget.

原始使用文档
文档目录11 节
  1. Why Use This Widget?
  2. Before You Start
  3. Install
  4. Examples
  5. Component
  6. Reactive controller
  7. Customization
  8. UX and accessibility
  9. Data flow and privacy
  10. Packaging
  11. Related packages

@desource/context7-widget-svelte

Svelte Coverage SonarCloud Ask Context7 Ask DeepWiki License

Context7 documentation answers in a widget you can style for your Svelte app. Use your own colors, type, and trigger content, with Svelte 5 snippets, bindable state, and reactive controls.

Why Use This Widget?

Context7 provides its docs widget and hosted AI answers for free. This project adds customizable interface around that service.

The official Context7 widget offers basic visual settings. This version lets you fit the assistant to a carefully designed site and adds the chat details visitors use every day: multiline questions, copyable code, Stop, Retry, and scrolling that leaves them in control.

Upgrade an existing Context7 script or use the native component below with the same library and domain settings. For a new site, give visitors a way to explore your library’s capabilities and ask how to get started. Svelte handles rendering; the core kit shares the chat engine, transport, Markdown, and layout helpers.

Before You Start

Use a library you have claimed on Context7. In its Admin → Chat settings, enable the widget, add your site's domain to the allowed domains, and save. Replace /owner/repo in the examples with that library's id. A library being indexed alone does not enable chat on your site. See Context7's widget setup.

Install

npm install @desource/context7-widget-svelte

Import the widget stylesheet once:

import '@desource/context7-widget-svelte/styles.css';

Examples

Component

<script lang="ts">
  import { Context7Widget, type Context7WidgetHandle } from '@desource/context7-widget-svelte';
  import '@desource/context7-widget-svelte/styles.css';

  let widget: Context7WidgetHandle;
  let open = $state(false);
</script>

<Context7Widget
  bind:this={widget}
  bind:open
  library="/owner/repo"
  position="anchor"
  preset="glass"
  customTrigger
  launcherLabel="Ask docs"
  onQuestion={(detail) => console.log(detail.question)}
/>

<button onclick={() => widget.send('Show the recommended setup')}>Ask programmatically</button>

customTrigger supports four modes: omit it for the built-in launcher, pass true for a Svelte-managed button, pass an element id or CSS selector for an external trigger, or pass an Element. Use a Svelte 5 snippet to customize the managed trigger:

<Context7Widget library="/owner/repo" customTrigger>
  {#snippet trigger({ label })}
    <span aria-hidden="true">?</span> {label}
  {/snippet}
</Context7Widget>

Component bindings expose open, close, toggle, send, cancel, retry, reset, isOpen, isBusy, getMessages, and subscribe.

Reactive controller

createContext7Widget can own a programmatic widget or control a registered component by widgetId:

<script lang="ts">
  import { onMount } from 'svelte';
  import { createContext7Widget } from '@desource/context7-widget-svelte';
  import '@desource/context7-widget-svelte/styles.css';

  const docs = createContext7Widget({
    library: '/owner/repo',
    position: 'center',
    preset: 'terminal'
  });

  onMount(() => {
    docs.mount();
    return docs.unmount;
  });
</script>

<button onclick={() => docs.open()}>Open docs</button><p>{docs.messages.length} messages</p>

The controller exposes reactive element, isOpenState, isBusyState, and messages getters plus the full imperative API. mount(overrides) creates or updates one owned widget; update(options) applies live options without replacing it.

Customization

Every shared option is supported: library, theme, preset, position, color, customTrigger, backdrop, closeOnOutsideClick, defaultOpen, initialMessage, labels, launcherLabel, launcherVariant, linkBaseUrl, panelHeight, panelWidth, placeholder, title, and widgetId.

Use labels for localized visible and assistive text. Relative Markdown links resolve against the Context7 library page unless linkBaseUrl supplies another documentation origin.

The component renders native Svelte light DOM. Style public part attributes below .context7-widget, and set CSS variables on that root:

.context7-widget {
  --c7-accent: #7c3aed;
  --c7-font-family: Inter, system-ui, sans-serif;
}

.context7-widget [part~='panel'] {
  border-color: color-mix(in srgb, var(--c7-accent), transparent 65%);
}

.context7-widget-trigger {
  --c7-trigger-background: #7c3aed;
  --c7-trigger-radius: 10px;
}

Callback props are onReady, onOpen, onClose, onCancel, onQuestion, onFirstToken, onAnswer, onAnswerComplete, onToolCall, onToolResult, and onError. Consumer callback failures are reported without corrupting widget state or skipping state listeners.

UX and accessibility

The composer supports multiline input: Enter sends and Shift+Enter inserts a newline. During streaming, Send becomes an enabled Stop action. Completed answers and code blocks have explicit copy actions, errors expose retry, and cancelling preserves partial answers. Reader-aware scrolling follows new output only while the conversation stays near the bottom.

Triggers synchronize aria-controls, aria-expanded, and aria-haspopup. Centered dialogs trap focus, isolate background content, and lock page scrolling; corner and anchored panels remain non-modal. External trigger attributes are restored after unmount.

Data flow and privacy

The browser sends the library id and conversation messages directly to https://context7.com/api/v2/widget/chat. DeSource Labs does not proxy chat content. The package adds no analytics, cookies, or persistent browser storage. Do not send secrets or sensitive personal data; review Context7 policies for backend processing and retention.

Packaging

The package uses Svelte 5 runes and the official svelte export condition. Imports are SSR-safe; DOM work starts only on component mount or explicit controller mount(). Svelte and @desource/context7-widget/kit remain external dependencies for consumer deduplication. Styles are separate, and tree-shaking metadata marks only CSS as side-effectful.

版本与发布记录

@desource/context7-widget-svelte 共发布 5 个版本,首次发布于 2026/09/10;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.3latest2026/11/0111 天前
0.2.22026/10/101 个月前
0.2.12026/10/011 个月前
0.2.02026/09/141 个月前
0.1.02026/09/102 个月前

关于 @desource/context7-widget-svelte 的常见问题

@desource/context7-widget-svelte 是什么?

Svelte 5 component and reactive controller for the customizable Context7 documentation chat widget.

如何安装 @desource/context7-widget-svelte?

在项目目录运行 npm install @desource/context7-widget-svelte,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:svelte(^5.20.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@desource/context7-widget-svelte 支持 TypeScript 吗?

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

@desource/context7-widget-svelte 支持 ESM 还是 CommonJS?

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

@desource/context7-widget-svelte 可以商用吗?

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

@desource/context7-widget-svelte 的下载量有多少?

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

@desource/context7-widget-svelte 还在维护吗?

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