Developers
Writing web parts
Build web parts for dashboards and forms with the web part SDK.
Web parts are the building blocks of dashboards. The web part SDK,
@omnicore/webpart-sdk, has the contract parts are written against, a base class, a test host and a harness that runs a
package in the browser. OmniCore's own charts and basic parts are written with it.
Uploading your own web part packages to a server (as
.owpfiles) is planned. Today you can build, test and run packages with the SDK and its harness.
A web part
A web part is a class that extends HTMLElement and has a context property. The host creates the element, sets
context once and adds the element to the page. The module exports the class; the host defines the element under
the name in the manifest. Don't call customElements.define yourself.
The OmniWebPart base class gives the part an open shadow root (this.root), removes subscriptions when the part leaves
the page, and re-renders when the configuration changes:
import { OmniWebPart, escapeHtml, type Table } from "@omnicore/webpart-sdk";
interface Config { datasource: string; title?: string }
export class CountryList extends OmniWebPart<Config> {
#table: Table | null = null;
protected override connected() {
this.track(this.context.data(this.config.datasource!).subscribe((table) => {
this.#table = table;
this.render();
}));
}
protected render() {
const rows = this.#table?.rows ?? [];
this.root.innerHTML = `<h3>${escapeHtml(this.config.title ?? "Countries")}</h3>
<ul>${rows.map((r) => `<li>${escapeHtml(r.country)}</li>`).join("")}</ul>`;
this.root.querySelectorAll("li").forEach((li, i) =>
li.addEventListener("click", () =>
this.context.provide("selected", { name: "country", type: "Text", values: [rows[i]!.country] })));
}
}Any custom element with a context works, whether it's written with Lit, React or plain JavaScript. The host also calls
configChanged(config), resized({ width, height }) and static migrateConfig(config, fromVersion) when a part has
them.
Rules for parts
- Render inside your own shadow root. Don't read or change the page's DOM, globals or storage.
- Use the theme's custom properties, such as
var(--omni-accent)andvar(--omni-text). - Escape data before putting it in HTML (
escapeHtml). - The portal's Content Security Policy refuses inline
<style>: link a stylesheet from your package instead. - Never put secrets in configuration: everyone who can open the dashboard can read it.
The context
| Member | |
|---|---|
instanceId, mode | This instance's id; mode is edit in the designer. |
config | This instance's saved configuration. |
data(name) | A data source on the dashboard. |
provide(endpoint, value) | Share a table, row or field through a provider endpoint. null clears it. |
emit(endpoint, payload) | Raise an event. |
consume(endpoint, handler) | Receive values for a consumer endpoint. |
isConnected(endpoint) | Whether anything is connected to the endpoint. |
theme, locale, user | Theme tokens, the user's locale, and who is signed in. Parts never get the user's token. |
navigate, notify, log | Open an entity, view or URL; show a message; write to the log. |
The context checks how it's used: providing through an undeclared endpoint, or a value of the wrong shape, throws with the part's and endpoint's names.
Data sources
const sales = this.context.data(this.config.datasource);
sales.columns; // known without running it
const page = await sales.query({ order: "-total", take: 10 });
const stop = sales.subscribe((table) => draw(table), { take: 10 }); // re-runs when parameters changeThe host runs queries with the signed-in user's permissions. Parts that read the same data source with the same options share one query.
Connections
A part declares its endpoints in the manifest; the dashboard connects them, and parts never refer to each other.
| Kind | Value | Example |
|---|---|---|
table | { columns, rows, total? } | A grid sharing its filtered rows |
row | { columns, values, entityId? } | A grid's selected row |
field | { name, type, values: [] } | A chart's selected category |
event | Any JSON | "Slice clicked" |
Table, row and field values are state: a consumer that connects later gets the latest value at once. Events aren't kept. Delivery is asynchronous and coalesced. Field types are the database's own, and Integer and Decimal are interchangeable.
The manifest
webparts.json is at the root of the package:
{
"id": "contoso.charts",
"version": "1.2.0",
"publisher": "contoso",
"webParts": [
{
"id": "contoso.donut-chart",
"name": "Donut chart",
"module": "charts.js",
"export": "DonutChart",
"element": "contoso-donut-chart",
"configSchema": "donut.config.schema.json",
"size": { "defaultColumns": 4, "defaultRows": 3 },
"provides": [{ "name": "selectedCategory", "kind": "field", "type": "Text" }],
"consumes": [{ "name": "highlight", "kind": "field", "type": "Text" }]
}
]
}- Web part ids start with
<publisher>., and element names with<publisher>-. configSchemais a JSON Schema the designer turns into a settings form. Hints such as"x-omnicore-widget": "datasource"or"column"give it data source and column pickers.- For a richer set-up, a part can export a set-up element (
configExport,configElement). - Raise
configVersionand addmigrateConfigwhen the shape of the configuration changes.
Check a package with npx omnicore-webparts validate ..
Testing
@omnicore/webpart-sdk/testing runs a part without the portal, under happy-dom or jsdom:
import { staticDataSource, testWebPart } from "@omnicore/webpart-sdk/testing";
const t = await testWebPart(BarChart, manifest, {
config: { datasource: "byCountry", category: "country", value: "total" },
datasources: [staticDataSource({ name: "byCountry", columns, rows })],
});
t.element.shadowRoot!.querySelector("button")!.click();
expect(t.provided("selectedCategory")).toEqual({ name: "country", type: "Text", values: ["KE"] });
t.dispose();feed drives consumer endpoints, provided and events read what the part shared, and setConfig and
setParameter change things as the designer or another part would. testConfigElement tests set-up elements.
The harness
cd webparts
npm install
npm run harness # builds the SDK, then serves the sample at http://localhost:5175The harness runs a package with static data sources and connections from its harness.json, in view and edit modes,
with a Bus panel showing every message. For your own package, npx omnicore-webparts serve ..