push-based-widget-module-pattern
できること
このスキルは、呼び出し元からデータを受け取り、変わった部分だけを更新する、シンプルなウィジェットの作り方を示します。頻繁に更新されるサイドバーやパネルを作る開発者向けです。
使う場面
- ✓サイドバーやウィジェットが頻繁に新しいデータを受け取り、全体を描き直さずに更新すべきとき。
- ✓データがウィジェットの外側にあるコントローラーやイベントの仕組みから届くとき。
- ✓クラスやフレームワークのコンポーネントより、シンプルなモジュールを好むとき。
スキルのリンク
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/push-based-widget-module-pattern/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“アプリが新しいデータを送るたびに更新される、サイドバーのウィジェットを作ってもらえますか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
ウィジェットは、主要なページの要素への参照を保持します。新しいデータが届くと、ウィジェット全体を作り直す代わりに、それらの要素だけを変更します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Push-Based Widget Module Pattern
Use this pattern when a UI sidebar or widget component needs to be updated by its caller (push-based) rather than fetching data itself. Instead of a class or framework component, implement the widget as a plain module with singleton state, a factory function to create the DOM, and an update function to efficiently patch it.
When to Use
- A sidebar, panel, or widget is rendered once but updated frequently with new data.
- The data source lives outside the component (e.g., a parent controller, event bus, or store).
- You want lightweight partial re-renders without rebuilding the entire DOM tree.
- You prefer a simple module interface over a class-based or framework-based component.
Core Structure
widgets/
MyWidget.ts # The module (singleton state + exports)
MyWidget.types.ts # (Optional) Exported data interface
Step-by-Step Instructions
1. Define the Data Interface
Export a typed interface so callers know exactly what data to push.
// MyWidget.types.ts (or inline at top of MyWidget.ts)
export interface MyWidgetData {
title: string;
items: string[];
status: "idle" | "active" | "error";
// ... all fields the widget needs to render
}
2. Declare Module-Level Singleton Refs
At module scope, keep references to the container and any frequently updated child nodes. This avoids querying the DOM on every update.
// MyWidget.ts
import type { MyWidgetData } from "./MyWidget.types";
// Singleton DOM refs
let containerEl: HTMLElement | null = null;
let titleEl: HTMLElement | null = null;
let itemListEl: HTMLElement | null = null;
let statusEl: HTMLElement | null = null;
// Current state snapshot (optional — useful for diffing)
let currentData: MyWidgetData | null = null;
3. Export a Factory Function (createMyWidget)
The factory builds the full DOM tree once, stores refs to mutable nodes, and returns the root element to be mounted by the caller.
export function createMyWidget(): HTMLElement {
// Build the container
containerEl = document.createElement("aside");
containerEl.className = "my-widget";
// Build static structure; keep refs to dynamic parts
titleEl = document.createElement("h2");
titleEl.className = "my-widget__title";
itemListEl = document.createElement("ul");
itemListEl.className = "my-widget__list";
statusEl = document.createElement("span");
statusEl.className = "my-widget__status";
// Compose the tree
const header = document.createElement("header");
header.appendChild(titleEl);
header.appendChild(statusEl);
containerEl.appendChild(header);
containerEl.appendChild(itemListEl);
return containerEl;
}
Rule:
createMyWidgetshould be callable only once per page. If called again, consider returning the existingcontainerEl(idempotent factory).
4. Export an Update Function (updateMyWidget)
The update function receives the new data object and performs surgical DOM mutations on the stored refs. It does NOT rebuild the whole tree.
export function updateMyWidget(data: MyWidgetData): void {
if (!containerEl) {
console.warn("updateMyWidget called before createMyWidget");
return;
}
currentData = data;
// Update only what changed
if (titleEl) titleEl.textContent = data.title;
if (statusEl) {
statusEl.textContent = data.status;
statusEl.dataset.status = data.status; // for CSS hooks
}
// Rebuild only dynamic list items
if (itemListEl) {
itemListEl.innerHTML = ""; // clear previous items
data.items.forEach((item) => {
const li = document.createElement("li");
li.textContent = item;
itemListEl!.appendChild(li);
});
}
}
Optimization tip: For large lists, compare
currentDatatodatabefore clearing and rebuilding, or use a keyed reconciliation approach.
5. Caller Usage Pattern
The caller mounts the widget once and pushes updates whenever its data changes.
import { createMyWidget, updateMyWidget } from "./widgets/MyWidget";
import type { MyWidgetData } from "./widgets/MyWidget.types";
// Mount once
const sidebar = document.getElementById("sidebar")!;
sidebar.appendChild(createMyWidget());
// Push updates (e.g., from a store subscription, WebSocket, timer)
function onDataChange(newData: MyWidgetData) {
updateMyWidget(newData);
}
// Example: push an initial state immedi
よくある質問
ここでのプッシュ型とはどういう意味ですか?
ウィジェットが自分でデータを取得せず、アプリの残りの部分が新しいデータを送ってくるのを待つことを意味します。
フレームワークは必要ですか?
いいえ。このパターンは、プレーンTypeScriptのモジュールを使います。
ウィジェットの一部だけを更新するのはなぜですか?
部分的な更新によって、ページを速く保ち、データが頻繁に変わるときの画面のちらつきを防げるからです。
詳細
作者による元の説明
How to implement sidebar/widget UI components that receive pushed data updates from a caller, using a singleton module pattern with exported factory and update functions plus module-level DOM refs for efficient partial re-renders.
repo: HKUDS/AgentPilot
人気
- GitHubのスター
- 7.8k
- ランキング
- #13077
- ライセンス
- MIT
- 更新日
- 2026/11/1
コメント
- …
このリポジトリの他のスキル
agentpilot
7.8kエージェントが、作業を準備し、変更案を提案してテストし、評価するAgentPilotのループを実行することで、プロジェクトを段階的に改善できるよう支援します。
openspace
7.8kエージェントが、スキルを保存し取り出すツールであるOpenSpaceをセットアップし、MCPを通じてAIアプリに接続できるよう支援します。
rsi-arena
7.8kエージェントが、AIMEやTerminal-Benchなどの標準的なAIベンチマークテストをセットアップして実行し、異なる改善方法を比較できるよう支援します。
experiment-guide
7.8kエージェントが、プログラムを改善する方法を調べる間に、小さな実験を計画し、その結果を読み、発見を記録できるよう支援します。
似ているスキル
angular-developer
280kエージェントがAngularのコードを作成し、プロジェクト構成についてアドバイスできるようにします。新しいプロジェクトの開始、コード部品の作成、パフォーマンスやアクセシビリティの改善に役立ちます。
frontend-a11y
280kエージェントが Web インターフェースを誰にとっても使いやすくするよう支援します。スクリーンリーダーやキーボード操作に対応し、サイトやアプリが適切に動作するようにします。
frontend-patterns
280kエージェントがフロントエンド開発の最新パターンを適用できるようにします。React と Next.js において、パフォーマンスの最適化、状態管理、ベストプラクティスの遵守を行います。