npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@devicechain/widget-sdk

v1.9.0

Published

The DeviceChain widget SDK: the typed contract for writing a dashboard widget as a React component, and the sealed registry that makes it available to a host. Declare a widget once (portable descriptor plus trusted component) and register it beside the bu

Downloads

5,764

Readme

@devicechain/widget-sdk

The typed contract for writing a DeviceChain dashboard widget as a React component, and the sealed registry that makes it available to a host application. The built-in widgets are declared through exactly this interface.

npm install @devicechain/widget-sdk @devicechain/dashboards react

@devicechain/dashboards and react are peer dependencies: the SDK defines the React contexts a widget reads (selection, candidate lookup, time zone, theme root, time range, density, freshness), and a context is identified by the object that created it, so every widget package and the host must resolve the same single copy of this package. Declare it as a peer in your own widget package too, never as a dependency.

What is in the box

The root entry is chart-free and map-free: the card frame (WidgetFrame), theme-variable references (css), formatters, the change-flash cue, option readers (optString, optNumber, ...) and the React hooks that bind a hub channel to widget state. A widget that only draws DOM imports from here and pulls in neither Apache ECharts nor MapLibre nor the built-in widgets.

A stream hook never returns one subscription's snapshot for another. Since 0.22, useAlarmStream and useLocationStream return loading: true with no rows from the very render whose subscription changed (a re-pointed slot, a new filter), until the new subscription answers. Before 0.22 that one render still carried the previous subscription's alarms or positions.

Chart helpers live on a separate entry, @devicechain/widget-sdk/echarts: the EChart wrapper, buildLineOption / buildGaugeOption and the chart colour theme. echarts is an optional peer dependency, needed only by a widget that imports that entry.

The chart engine itself is not part of that entry's static imports. EChart loads it on demand, as one chunk, the first time a chart mounts, so a host bundler keeps ECharts out of the first download. Until the engine arrives, the chart's box is empty. If the engine cannot be fetched, EChart throws the error to the nearest error boundary, and the next mount fetches it again. A host that wants charts drawn on its first frame awaits loadChartEngine() before rendering. A widget that draws a chart can set MyWidget.preload = loadChartEngine on its component, and then preloadWidgets from @devicechain/widgets fetches the engine for any board that contains it.

The other public subpaths are:

  • @devicechain/widget-sdk/empty-state: WidgetEmptyState, widgetEmptyStrings, WIDGET_EMPTY_STRINGS and their types. Render <WidgetEmptyState kind="no-data" /> inside a frame; kinds are unbound, not-reported, no-data, forbidden, error. Optional measurement, hint, detail and locale customize the explanation.
  • @devicechain/widget-sdk/strict-csp: configureStrictCsp, strictCspEnabled and plainTooltipText. These are also root exports. Call configureStrictCsp(true) before mounting under a strict style policy; use plainTooltipText(text) to escape braces for canvas tooltips.

Declare a widget

import { defineWidget } from '@devicechain/widget-sdk';

export const tankLevelWidget = defineWidget({
  descriptor: {
    id: 'acme/tank-level',
    version: '1.0.0',
    sdkApiVersion: 1,
    optionsVersion: 1,
    display: { title: 'Tank level', category: 'Telemetry' },
    channel: 'measurement',
    datasource: { mode: 'required', selectors: ['device', 'slot'] },
    binding: { window: { option: 'window', min: 1 } },
    options: {
      unit: { kind: 'string', doc: 'Unit shown beside the level.' },
      window: { kind: 'number', integer: true, min: 1, doc: 'Samples retained.' },
    },
    defaults: { unit: '%' },
    interactions: {},
  },
  implementation: {
    kind: 'trusted-react',
    // `data` is typed from the channel: a measurement widget receives measurement data.
    component: ({ data }) => <span>{Object.values(data.latest)[0]?.value}</span>,
  },
});

The descriptor is plain JSON-serializable data: no component, callback or URL. A descriptor that disagrees with its channel, or whose defaults or bindings disagree with its option schema, fails type checking, and createWidgetRegistry checks the same rules again at runtime.

Register it

import { createWidgetRegistry } from '@devicechain/widget-sdk';

const registry = createWidgetRegistry([tankLevelWidget]);

A registry is validated and sealed when it is created: duplicate ids, malformed descriptors, an unsupported sdkApiVersion and invalid bindings or defaults are all reported together in one WidgetRegistryError. There is no global registration; two registries on one page are independent. Extension ids are namespaced (scope/name); the un-namespaced ids are reserved for the built-in widgets.

Trusted widget code runs with the host page's privileges. The declarations in a descriptor shape the API; they are not a security boundary.

Component and host contract

A WidgetProps<D> component receives widget, channel-specific data and optional actions. useWidgetSelect() and useWidgetCandidates() read optional host interaction callbacks; handle undefined in a host without those services. useWidgetTimeRange() and useWidgetTimeZone() read the board's range and zone. Use pickSample(latest, name) for a single measurement (pass undefined for the first one), and optString(options, key), optNumber(options, key) or optBoolean(options, key) for defensive option reads.

useDashboardHub(resolver, authorities) creates and disposes a live hub; pass undefined for authorities in a read-only host. Standalone widgets can bind channel state with useMeasurementStream, useAlarmStream, useCommandStream and useLocationStream; a component registered with defineWidget already receives its data from the host. WidgetFrame supplies body-size context for useWidgetScale(); useElementSize<T>(step?) returns [ref, { width, height }].

The registry exposes catalog for parseDashboardDefinition(raw, { catalog }); pass that same registry to the renderer. An optional implementation.editor receives WidgetEditorProps: options, setOptions(patch), descriptor and services (slots, optional device token and optional commandChoices).

Offering a download from the frame

A widget can let the viewer save the data it shows. Pass exportRows to WidgetFrame: a function that returns a WidgetExportTable (a file name without its extension, headings, and rows of strings, numbers, booleans or nulls), or a promise of one. The frame then draws a download button in its title bar, and Download CSV calls your function when the viewer picks it, so the table is built on demand from what the widget is showing. The prop is optional and feature-detected: a widget that passes none is unchanged, and the frame draws no button when its title bar is hidden.

The CSV encoder is the frame's: RFC 4180 quoting, a leading apostrophe on a text cell that begins with =, +, -, @, tab or carriage return (so a spreadsheet never runs it as a formula), and UTF-8 with a byte-order mark so Excel reads units such as °C. Put the unit in the heading, write instants as ISO 8601 with a zone, and say in the file name what the file covers (the span and the bucket on screen). A chart drawn with <EChart> also gets Download image (PNG) with no code of yours.

Upgrading from 0.21

0.22 removes no export. A TypeScript build can notice three changes:

  • ChartTheme has a required severity field. This matters only if you build a theme by hand instead of calling useChartTheme or resolveChartTheme.
  • The unions WidgetChannel (now with 'aggregate'), WidgetId, BuiltinWidgetId, ThemeVar and OptionSpec (now with ListOptionSpec) gained members, so an exhaustive switch over one of them needs a new case.
  • formatValue with no precision rounds to 3 decimals, or 4 significant digits below 1, where it used to print the number unrounded.

Additions, which need no change:

  • WidgetFrame takes flush, which removes the content padding for a widget that fills its box edge to edge, and sampleTime, the time of the one sample a single-value widget shows, so its age readout ages that sample rather than the newest one the host holds.
  • useElementSize takes an optional step that rounds each dimension down to a multiple of it, so a caller that needs only a coarse size does not re-render for every pixel.
  • New exports for size and density tokens (fontVar, spaceVar, tokenStyle, useWidgetScale, fitValueFont), sample age (useFreshness, Freshness, StaleBadge, formatAge), alarm severity (SEVERITY_LEVELS, severityVar, useSeverityColors), thresholds (parseThresholds, thresholdBands, bandAt), the board's time range (useWidgetTimeRange) and the hub connection (useHubConnectionState, HubConnectionBadge).

SDK_API_VERSION is unchanged. See If you build against the npm packages in the release's upgrade guide.