@desource/context7-widget-vue

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

Native Vue 3 component and composable for a customizable Context7 documentation chat widget.

原始使用文档
文档目录15 节
  1. Why Use This Widget?
  2. What You Get
  3. Before You Start
  4. Install
  5. Component
  6. Composable
  7. Examples
  8. Plugin
  9. Controlled Open State
  10. Trigger Modes
  11. Customization
  12. Props And Events
  13. Data Flow And Privacy
  14. Multiple Widgets And Packaging
  15. Related packages

@desource/context7-widget-vue

Vue Coverage SonarCloud Ask Context7 Ask DeepWiki License

Context7 docs chat, styled for your Vue app. Use your fonts, colors, and help button, with a native component and a composable for opening chat from your UI.

Why Use This Widget?

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

A docs assistant should fit the site around it. This package expands the official Context7 widget’s basic visual options with presets, CSS variables, custom triggers, and flexible layouts. Visitors get multiline questions, copyable code, Stop and Retry controls, and chat that lets them scroll back and read.

Already using the official widget? Switch the script URL or use the Vue component below with your existing Context7 library. New to Context7? Add documentation answers where visitors evaluate your library and start their first integration.

What You Get

  • A native Context7Widget component with typed props and events.
  • A useContext7Widget composable and plugin for controls and app defaults.
  • A trigger slot for your own button content, plus external trigger support.
  • Shared widget styles in styles.css and the same core behavior as every package.

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-vue

Import the stylesheet once in your application entry:

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

In Nuxt, the equivalent global setup is:

export default defineNuxtConfig({
  css: ['@desource/context7-widget-vue/styles.css']
});

Component

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

function trackQuestion(detail: Context7WidgetQuestionEventDetail) {
  console.log(detail.library, detail.question);
}
</script>

<template>
  <Context7Widget library="/owner/repo" position="anchor" preset="glass" theme="auto" @question="trackQuestion" />
</template>

Composable

<script setup lang="ts">
import { useContext7Widget } from '@desource/context7-widget-vue';
import '@desource/context7-widget-vue/styles.css';

const docs = useContext7Widget({
  autoMount: true,
  library: '/owner/repo',
  position: 'center',
  preset: 'terminal',
  widgetId: 'docs'
});

async function ask() {
  await docs.send('How do I customize the widget?');
  console.log(docs.isOpen.value, docs.isBusy.value, docs.messages.value);
}
</script>

<template>
  <button type="button" @click="ask">Ask documentation</button>
</template>

Automatic mounting happens after the owner mounts. Call send, open, and other controls from an event handler or after the widget is ready.

The composable exposes reactive widget, isOpen, isBusy, and messages refs plus mount, unmount, open, close, toggle, send, cancel, retry, reset, and getMessages. mount(overrides) also updates an existing owned widget, and those overrides remain in effect when reactive source options change. Owned widgets are removed with their owner by default; set removeOnUnmount: false only when another part of the app will own cleanup. Call the composable during component setup; imperative mount() is browser-only. Programmatically rendered widgets inherit the owner app context and defaults provided by createContext7WidgetPlugin.

Without autoMount, the composable uses a package-level registry rather than Vue or DOM ancestry. It resolves the newest registration for widgetId, which defaults to default; if no default registration exists, that lookup falls back to the first available widget. Duplicate ids form a stack, so unmounting the newest registration restores the previous one.

Examples

Use v-model:open when a parent owns visibility:

<script setup lang="ts">
import { ref } from 'vue';
import { Context7Widget } from '@desource/context7-widget-vue';
import '@desource/context7-widget-vue/styles.css';

const open = ref(false);
</script>

<template>
  <Context7Widget v-model:open="open" library="/owner/repo" position="center" preset="glass" backdrop custom-trigger />
</template>

Use useContext7Widget({ autoMount: true }) for route actions, command palettes, or other imperative flows. More runnable patterns are available in the demo gallery.

Plugin

Register the native component under a custom name and provide app-wide defaults:

import { createApp } from 'vue';
import { createContext7WidgetPlugin } from '@desource/context7-widget-vue';

createApp(App)
  .use(
    createContext7WidgetPlugin({
      componentName: 'DocsWidget',
      defaults: {
        preset: 'glass',
        theme: 'auto'
      }
    })
  )
  .mount('#app');

Defaults are inherited by rendered Vue components and composable-owned widgets; the plugin does not create a second widget. Plugin options are captured when the plugin is created, so mutating the original options object later does not alter installed application behavior.

Controlled Open State

Omit open for state initialized by defaultOpen. Use v-model:open when the parent owns visibility:

<Context7Widget v-model:open="docsOpen" library="/owner/repo" />

The component emits update:open as the controlled-state request. open and close remain lifecycle notifications emitted only after an actual transition.

Trigger Modes

<!-- Built-in floating launcher -->
<Context7Widget library="/owner/repo" />

<!-- Vue renders a package-managed button -->
<Context7Widget library="/owner/repo" custom-trigger launcher-label="Ask docs" />

<!-- Vue renders the button, you control its markup -->
<Context7Widget library="/owner/repo" custom-trigger>
  <template #trigger="{ label }">
    <span class="docs-dot" />
    <span>{{ label }}</span>
  </template>
</Context7Widget>

<!-- Bind to a button anywhere by id, with or without # -->
<button id="docs-help">Ask docs</button>
<Context7Widget library="/owner/repo" custom-trigger="docs-help" />

<!-- Full CSS selectors are supported too -->
<button class="docs-help">Ask docs</button>
<Context7Widget library="/owner/repo" custom-trigger=".docs-help" />

<!-- Vue refs and direct Elements are supported for external triggers -->
<button ref="docsHelp">Ask docs</button>
<Context7Widget library="/owner/repo" :custom-trigger="docsHelp" />

External custom triggers hide the Vue floating launcher only after they bind. Missing or late-rendered selectors keep the launcher available and bind automatically when the target appears.

Customization

The Vue component renders native Vue DOM under .context7-widget; it does not mount the core custom element. Customize it with the shared CSS variables:

.context7-widget[widget-id='docs'] {
  --c7-accent: #7cffb2;
  --c7-panel-background: #101513;
  --c7-panel-color: #f7f2e8;
  --c7-border-color: rgba(247, 242, 232, 0.18);
  --c7-panel-radius: 8px;
}

.context7-widget [part~='send-button'] {
  min-width: 5rem;
  text-transform: uppercase;
}
.context7-widget-trigger {
  --c7-trigger-background: #111827;
  --c7-trigger-border: rgba(255, 255, 255, 0.16);
  --c7-trigger-color: #f8fafc;
  --c7-trigger-focus: rgba(124, 255, 178, 0.42);
  --c7-trigger-radius: 8px;
  --c7-trigger-shadow: none;
}

See the live customization guide for every public token and part.

Props And Events

The component accepts the same public widget options as the core package: library, theme, preset, position, color, customTrigger, backdrop, closeOnOutsideClick, defaultOpen, initialMessage, labels, launcherLabel, launcherVariant, linkBaseUrl, panelHeight, panelWidth, placeholder, title, and widgetId.

Use labels for partial localization of every visible and assistive string, including attribution and library fallbacks. Context7WidgetLabels is exported for typed dictionaries. Relative Markdown links resolve against the Context7 library page unless linkBaseUrl supplies a documentation origin.

Defaults are shared with core: position="bottom-right", preset="default", theme="auto", launcher-variant="icon", and widget-id="default". A centered widget enables its backdrop unless :backdrop="false" is explicit.

Vue’s only prop-level difference is customTrigger:

ValueBehavior
omittedRender the built-in floating launcher
trueRender the Vue-managed trigger and expose the trigger slot
id stringBind an external trigger by id, with or without #
CSS selectorBind the first matching external trigger
Element/refBind the provided external trigger element

Vue events: ready, open, close, cancel, question, first-token, answer, answer-complete, tool-call, tool-result, error, and update:open.

Each handler receives the typed event detail as its only argument.

Component refs expose open, close, toggle, send, cancel, retry, reset, isOpen, isBusy, getMessages, and subscribe. The composable adds owned mount/unmount operations and reactive widget, isOpen, isBusy, and messages refs. State listeners registered through subscribe are isolated: if one throws, the error is reported without corrupting the request or skipping the remaining listeners. Cancelling after answer tokens arrive preserves the visible partial assistant message in getMessages() with status: 'cancelled'. send() resolves with a status result such as complete, cancelled, error, busy, or empty.

While a response streams, the send action becomes an enabled Stop action. The composer accepts multiline/code-paste input: Enter sends and Shift+Enter inserts a newline. Completed answers and fenced code blocks can be copied, and transport errors expose a retry action without duplicating the question. Both built-in and external triggers receive aria-controls, aria-haspopup="dialog", and synchronized aria-expanded; attributes owned by an external trigger are restored when it is unbound. Centered dialogs trap focus, make outside content inert, and lock page scrolling, while non-modal corner and anchored panels do not.

The component is SSR-safe. useContext7Widget can also be created during SSR, but its imperative mount() method requires a browser document.

Data Flow And Privacy

The browser posts the configured library id and current 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; conversation state remains in the mounted widget until reset() or unmount. Emitted event payloads expose questions and answers to the host application, so any logging, analytics, or persistence added there is the integrator's data flow. Do not send secrets or sensitive personal data, and review Context7's policies for backend processing and retention.

Multiple Widgets And Packaging

Use a unique widgetId for each independently controlled widget. When duplicate ids are mounted intentionally, the most recently mounted instance is resolved and the previous instance becomes active again if the newer one unmounts.

The package exposes one JavaScript entry, @desource/context7-widget-vue, containing the component, composable, plugin, and public types. Styles are intentionally separate at @desource/context7-widget-vue/styles.css; there are no component or composable JavaScript subpaths. Vue and @desource/context7-widget/kit remain external module dependencies, allowing the consuming app to deduplicate Vue and tree-shake unused kit modules. The ESM entry and declarations are SSR-import safe and validated with modern Node ESM and TypeScript bundler resolution.

版本与发布记录

@desource/context7-widget-vue 共发布 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-vue 的常见问题

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

Native Vue 3 component and composable for a customizable Context7 documentation chat widget.

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

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

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

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

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

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

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

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

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

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

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

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