@desource/context7-widget-angular

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

Native Angular component and injectable controls for a customizable Context7 documentation chat widget.

原始使用文档
文档目录15 节
  1. Why Use This Widget?
  2. Before You Start
  3. Install
  4. Component
  5. Application defaults
  6. Injectable controls
  7. Examples
  8. Controlled visibility
  9. Managed trigger content
  10. External trigger
  11. Customization
  12. Inputs and outputs
  13. SSR and packaging
  14. Data flow and privacy
  15. Related packages

@desource/context7-widget-angular

Angular Coverage SonarCloud Ask Context7 Ask DeepWiki License

Bring Context7 documentation answers into your Angular app with a widget that matches its design. Customize fonts, colors, layout, and trigger content through a native standalone component.

Why Use This Widget?

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

You should be able to add docs chat without settling for a generic panel on a carefully designed site. This package expands the official Context7 widget’s visual controls and adds multiline input, answer and code copying, Stop, Retry, and scrolling that respects the reader’s position.

Existing Context7 users can replace the script URL or adopt this component with the same library and domain settings. New users can put documentation answers into product pages and onboarding, helping visitors understand the library and try their first integration.

The component uses OnPush, signal inputs, and typed outputs. Configure app defaults with provideContext7Widget and use the injectable service for programmatic controls. The shared core kit handles conversation state, transport, Markdown, and layout.

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

Import the stylesheet once in the application build:

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

Angular 22 is required.

Component

import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Context7Widget, type Context7WidgetQuestionEventDetail } from '@desource/context7-widget-angular';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [Context7Widget],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <context7-widget
      library="/owner/repo"
      position="anchor"
      preset="glass"
      theme="auto"
      (question)="trackQuestion($event)"
    />
  `
})
export class App {
  trackQuestion(detail: Context7WidgetQuestionEventDetail): void {
    console.log(detail.library, detail.question);
  }
}

The component exposes open, close, toggle, send, cancel, retry, reset, isOpen, isBusy, getMessages, and subscribe through a viewChild(Context7Widget) reference.

Application defaults

provideContext7Widget supplies defaults without creating another widget:

import { bootstrapApplication } from '@angular/platform-browser';
import { provideContext7Widget } from '@desource/context7-widget-angular';
import { App } from './app';

bootstrapApplication(App, {
  providers: [
    provideContext7Widget({
      launcherLabel: 'Ask docs',
      preset: 'glass',
      theme: 'auto'
    })
  ]
});

Explicit component inputs override provided defaults.

Injectable controls

Context7WidgetService resolves declarative widgets by widgetId and exposes reactive widget, widgetId, isOpen, isBusy, and messages signals.

import { Component, inject } from '@angular/core';
import { Context7WidgetService } from '@desource/context7-widget-angular';

@Component({
  selector: 'docs-actions',
  standalone: true,
  template: `<button type="button" (click)="ask()">Explain setup</button>`
})
export class DocsActions {
  readonly docs = inject(Context7WidgetService);

  async ask(): Promise<void> {
    this.docs.select('docs');
    this.docs.open();
    await this.docs.send('How do I configure the widget?');
  }
}

The service can own a dynamically mounted Angular component:

const widget = docs.mount({
  library: '/owner/repo',
  position: 'center',
  preset: 'terminal',
  target: '#widget-root',
  widgetId: 'docs'
});

await widget.send('Show the installation steps.');
docs.unmount('docs');

Calling mount again with the same widgetId updates and relocates the owned component. unmount never removes a component declared in a template. Programmatic mounting requires a browser document.

Examples

Controlled visibility

Use Angular two-way binding when the parent owns visibility:

import { Component, signal } from '@angular/core';
import { Context7Widget } from '@desource/context7-widget-angular';

@Component({
  selector: 'docs-shell',
  standalone: true,
  imports: [Context7Widget],
  template: `
    <button type="button" (click)="docsOpen.set(true)">Open docs</button>
    <context7-widget [(open)]="docsOpen" library="/owner/repo" position="center" [backdrop]="true" />
  `
})
export class DocsShell {
  readonly docsOpen = signal(false);
}

Omit open to initialize internal visibility from defaultOpen.

Managed trigger content

Import Context7WidgetTrigger when projecting a product-specific trigger:

import { Component } from '@angular/core';
import { Context7Widget, Context7WidgetTrigger } from '@desource/context7-widget-angular';

@Component({
  standalone: true,
  imports: [Context7Widget, Context7WidgetTrigger],
  template: `
    <context7-widget library="/owner/repo" [customTrigger]="true">
      <span context7WidgetTrigger>Ask product docs</span>
    </context7-widget>
  `
})
export class ProductDocs {}

External trigger

<button id="docs-help" type="button">Ask docs</button>
<context7-widget library="/owner/repo" customTrigger="docs-help" />

An id may include or omit #. Full CSS selectors, direct Element values, and ElementRef<Element> values are also supported. Missing selectors leave the built-in launcher visible and bind when the target enters the document. The package restores authored trigger ARIA attributes when a trigger changes or the widget is destroyed.

Customization

The component renders native Angular DOM under .context7-widget; it does not wrap the core custom element. Override shared CSS variables or public parts:

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

Inputs and outputs

The component accepts library, theme, preset, position, color, customTrigger, backdrop, closeOnOutsideClick, defaultOpen, initialMessage, labels, launcherLabel, launcherVariant, linkBaseUrl, panelHeight, panelWidth, placeholder, title, widgetId, and controlled open.

Outputs are ready, opened, closed, cancelled, question, firstToken, answer, answerComplete, toolCall, toolResult, error, and openChange. Each lifecycle or conversation output receives the same typed event detail used by the core package. openChange receives the requested boolean state.

Use labels for partial localization of visible and assistive strings. Relative Markdown links resolve against the Context7 library page unless linkBaseUrl sets a documentation origin.

While a response streams, the send action becomes an enabled Stop action. Enter sends, Shift+Enter inserts a newline, completed answers and fenced code blocks expose copy actions, and transport failures expose retry. Reader-aware autoscroll stops when someone moves up the conversation and resumes at the bottom. Centered widgets trap focus, isolate outside content, and lock document scrolling.

Duplicate widgetId registrations form a stack. Injectable controls resolve the newest widget, then restore the previous registration after the newest one is destroyed. A default lookup falls back to the first available widget when no widget uses the default id.

SSR and packaging

The ESM entry and declarations are safe to import during SSR. Declarative SSR does not access browser globals; browser-only listeners, modal state, focus, and trigger binding start after view initialization. Context7WidgetService.mount and unmount are client-side ownership APIs.

Angular and @desource/context7-widget/kit remain external dependencies, so applications can deduplicate Angular and tree-shake unused kit modules. Styles ship separately at @desource/context7-widget-angular/styles.css.

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. Emitted event details expose questions and answers to the host application, so logging or persistence added by the application becomes part of its data flow. Do not send secrets or sensitive personal data.

版本与发布记录

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

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

Native Angular component and injectable controls for a customizable Context7 documentation chat widget.

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

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

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

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

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

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

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

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

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

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

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

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