Non-React Components: defineJsComponent¶
defineJsComponent lets you write slot and page contributions without React. The host hands your factory a DOM node, and you own everything inside it.
Which surfaces accept a JsComponent¶
| Surface | Accepted |
|---|---|
Slot tags (toolbar.main, file-row.actions, ...) |
Yes |
Page tags (page:*) |
Yes |
For pages, the factory receives empty props (EmptySlotProps), so update is optional. See Page contributions for registration.
Signature¶
import { defineJsComponent } from "@lore-vcs/desktop-plugins-sdk";
defineJsComponent<Props, Contract>(factory): BrandedJsComponent<Props, Contract>
Both type parameters are required. Props must match the slot tag's props type. Contract must match your plugin's contract (the same type you pass to definePluginRenderer<C>).
The factory has this shape:
type JsComponent<P, C extends PluginContract> = (
el: HTMLElement,
props: P,
ctx: JsComponentContext<C>
) => JsInstance<P>;
| Parameter | Description |
|---|---|
el |
A host-owned <div> that is your mount point. Append children to it. |
props |
The slot's current props (for example { filePath, nodeType } for file-row.actions). |
ctx |
Mount-scoped context: data, commands, events, logging, disposal. |
The factory is called once per mount. Each row in a list gets its own factory call, its own el, its own ctx, and its own instance.
The factory must be synchronous. An async factory is a compile error (the return type Promise<JsInstance> has a then property that conflicts with JsInstance's then?: never guard). If you need async work, do it inside the factory body and update the DOM when it resolves. Check a disposed flag so a late resolution after unmount is a no-op.
JsInstance¶
The factory returns a JsInstance<P>:
interface JsInstance<P> {
dispose?(): void;
update(props: P): void; // required when P has keys
// update?(props: P): void; // optional when P is EmptySlotProps
}
update(props): called by the host when the slot's props change (for example a file row re-renders with a newfilePath). Required for row-level tags (file-row.actions,branch-row.actions,revision-row.actions) where props carry per-item data. Optional for chrome tags (toolbar.*,sidebar.*,content.header) and pages, where props are always empty.dispose(): called when the host unmounts the instance. Clean up DOM mutations, timers, or other resources. Optional, because the host also auto-disposes everything registered viactx.onDispose()and allctx.data/ctx.eventssubscriptions.
JsComponentContext¶
The ctx argument is the JsComponent equivalent of useBridge() in React. It carries the same three namespaces (data, commands, events) plus logging and disposal. The full interface:
interface JsComponentContext<C extends PluginContract> {
readonly pluginId: string;
readonly log: PluginLog;
readonly data: JsComponentData<C>;
readonly commands: JsComponentCommands;
readonly events: JsComponentEvents;
onDispose(disposable: Disposable): void;
}
Events, Data and Commands covers each namespace in detail, with code for both React and JsComponent contexts side by side. The rest of this section covers JsComponent-specific usage.
ctx.log¶
Scoped logger. It has the same shape as reg.log and ctx.log elsewhere:
ctx.log.info("mounted");
ctx.log.warn("something unexpected");
ctx.log.error("failed to fetch", err);
ctx.log.debug("props:", props);
ctx.data¶
Read and subscribe to plugin data keys. Key resolution is the same as for the React hooks: own keys, host keys, and foreign plugin:<id>/<key> keys all work.
// One-shot read (returns a Promise)
const stats = await ctx.data.get("stats");
// Subscribe (replay-1: fires immediately with current value, then on change)
ctx.data.on("theme", (value) => {
el.style.background = value === "dark" ? "#1a1a1a" : "#fff";
});
Subscriptions made through ctx.data are auto-disposed by the host on unmount. You do not need to register them via ctx.onDispose().
ctx.commands¶
Call and stream commands:
const result = await ctx.commands.call("plugin:@acme/my-plugin/analyze", { path: filePath });
const handle = ctx.commands.stream("plugin:@acme/my-plugin/watch", { path: filePath }, {
next(item) { /* ... */ },
error(err) { /* ... */ },
complete() { /* ... */ },
});
// handle.cancel() to stop early
ctx.events¶
Subscribe to and emit events:
ctx.events.on("repository.opened", (payloads) => {
for (const repo of payloads) {
ctx.log.info("opened:", repo);
}
});
ctx.events.emit("analysisComplete", { count: 42 });
Subscriptions made through ctx.events are auto-disposed on unmount.
ctx.onDispose(disposable)¶
Register a resource whose lifetime is tied to the mount. Use it for resources you create (timers, animation frames, DOM listeners) that the host cannot track automatically:
const timer = setInterval(() => refresh(), 5000);
ctx.onDispose({ dispose: () => clearInterval(timer) });
Dormant-state behaviour¶
When the owning plugin is dormant (not yet activated), the ctx surfaces degrade individually rather than throwing. The full table is in Events, Data and Commands.
One nuance worth calling out: ctx.data.on behaves differently depending on the key type. For the plugin's own keys, the subscription parks and re-attaches when the store arrives, so replay-1 semantics are preserved. For host keys, it delivers undefined once and then attaches when the plugin's bridge is published. ctx.events.on follows the bridge the same way, without replaying events that fired earlier. If you need state at activation, use ctx.data with own keys, which have replay-1 semantics even across the dormant to active transition.
Curried factory: createJsComponent<C>()¶
If you have multiple JsComponents, bind the contract type once:
import { createJsComponent } from "@lore-vcs/desktop-plugins-sdk";
import type { MyContract } from "./contract";
import type { EmptySlotProps, FileRowSlotProps } from "@lore-vcs/desktop-plugins-sdk";
const jsComponent = createJsComponent<MyContract>();
export const myToolbarWidget = jsComponent<EmptySlotProps>((el, _props, ctx) => {
el.textContent = "Hello";
return {};
});
export const myFileAction = jsComponent<FileRowSlotProps>((el, props, ctx) => {
const btn = document.createElement("button");
btn.textContent = `Analyze ${props.filePath}`;
el.appendChild(btn);
return {
update(nextProps) {
btn.textContent = `Analyze ${nextProps.filePath}`;
},
dispose() {
btn.remove();
},
};
});
Complete working example¶
A slot contribution that shows a live sparkline in the sidebar footer:
import { defineJsComponent } from "@lore-vcs/desktop-plugins-sdk";
import type { MyContract } from "./contract";
import type { EmptySlotProps } from "@lore-vcs/desktop-plugins-sdk";
export const Sparkline = defineJsComponent<EmptySlotProps, MyContract>(
(el, _props, ctx) => {
const canvas = document.createElement("canvas");
canvas.width = 120;
canvas.height = 24;
el.appendChild(canvas);
const points: number[] = [];
const renderCtx = canvas.getContext("2d")!;
function draw() {
renderCtx.clearRect(0, 0, canvas.width, canvas.height);
if (points.length < 2) return;
renderCtx.beginPath();
const step = canvas.width / (points.length - 1);
const max = Math.max(...points, 1);
for (let i = 0; i < points.length; i++) {
const x = i * step;
const y = canvas.height - (points[i] / max) * canvas.height;
if (i === 0) renderCtx.moveTo(x, y);
else renderCtx.lineTo(x, y);
}
renderCtx.stroke();
}
// Subscribe to plugin data — auto-disposed on unmount
ctx.data.on("stats", (value) => {
if (typeof value === "number") {
points.push(value);
if (points.length > 30) points.shift();
draw();
}
});
return {
dispose() {
canvas.remove();
},
};
}
);
Register it:
import { definePluginRenderer } from "@lore-vcs/desktop-plugins-sdk/renderer";
import { Sparkline } from "./Sparkline";
import type { MyContract } from "./contract";
export default definePluginRenderer<MyContract>(() => ({
register(reg) {
reg.provide("sidebar.footer", Sparkline);
},
}));
Next¶
Both component types talk to the backend through the same bridge: data, events, and commands. The next page is a quick reference for all three namespaces.