> ## Documentation Index
> Fetch the complete documentation index at: https://docs.blobhub.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Libraries and Theme

> The platform libraries a custom widget loads by name, building one module, and the light and dark look.

A [custom widget](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widgets.md) is one ES module that stands alone: everything it
runs travels in its definition, within the definition's [size
limit](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widgets.md#limits). Heavy libraries are the exception. The platform serves a
catalog of named libraries on every widget's origin, and a widget names in its `libraries`, at exact versions, the ones
it loads from there instead of bundling them; anything it does not name, it bundles. A library loaded from the catalog
adds nothing to the definition, and loading React from the catalog keeps one React across `react-dom` and
`react-bootstrap`, where two copies would break every component that uses hooks.

This page lists the catalog, says how the frame resolves a widget's imports through it, states the rules a build follows
to make one module that loads, and gives BlobHub's light and dark colors. Everything here is what a widget needs
[without the kit](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widgets.md#the-kit).

## The catalog

The catalog is `https://blobhub.io/libs/catalog.json`, also served at `/libs/catalog.json` on every widget origin, and
it is the complete, current list of libraries and their versions. Each version's files sit at an immutable path,
`/libs/<name>/<version>/`, on every widget origin, so a widget loads its libraries from its own origin. A published
version is never changed or removed, so a widget that names one keeps loading it; a later version of a library is added
beside the earlier ones, never in their place. The browser caches each library file as immutable, once per widget
origin: on a warm cache a library costs nothing, and on a cold one a download per widget.

These are the libraries and versions the catalog holds:

| Library | Version | Import-map entries | Stylesheets | Peers |
| - | - | - | - | - |
| `react` | 18.3.1 | `react`, `react/jsx-runtime` | | |
| `react-dom` | 18.3.1 | `react-dom`, `react-dom/client` | | `react` |
| `react-bootstrap` | 1.6.8 | `react-bootstrap`, and `react-bootstrap/<Component>` for each of its components | | `react`, `react-dom` |
| `bootstrap` | 4.6.2 | none | `bootstrap.min.css` | |
| `@xyflow/react` | 12.1.1 | `@xyflow/react` | `style.css` | `react`, `react-dom` |
| `dagre` | 0.8.5 | `dagre` | | |
| `ag-grid-community` | 32.1.0 | `ag-grid-community` | `ag-grid.css`, `ag-theme-quartz.css` | |
| `ag-grid-react` | 32.1.0 | `ag-grid-react` | | `react`, `react-dom`, `ag-grid-community` |
| `ace-builds` | 1.35.4 | `ace-builds`, and `ace-builds/src-noconflict/<file>` for every file but the workers | | |
| `react-ace` | 12.0.0 | `react-ace` | | `react`, `ace-builds` |
| `marked` | 14.0.0 | `marked` | | |
| `marked-highlight` | 2.2.4 | `marked-highlight` | | |
| `dompurify` | 3.4.10 | `dompurify` | | |
| `highlight.js` | 11.11.1 | `highlight.js`, `highlight.js/lib/core`, `highlight.js/lib/common` | | |
| `mermaid` | 11.16.1 | `mermaid` | | |
| `react-icons` | 5.3.0 | `react-icons/lu`, `react-icons/vsc`, `react-icons/go`, `react-icons/cg` | | `react` |
| `react-select` | 5.8.0 | `react-select`, `react-select/creatable`, `react-select/async`, `react-select/async-creatable`, `react-select/base` | | `react`, `react-dom` |
| `timeago.js` | 4.0.2 | `timeago.js` | | |
| `d3-selection` | 3.0.0 | `d3-selection` | | |
| `d3-zoom` | 3.0.0 | `d3-zoom` | | `d3-selection` |
| `tradingview` | 29.6.0 | `tradingview` | | |

* **Peers.** A peer is a catalog library that a library imports by its bare name, so that one instance is shared: React
  across `react-dom` and `react-bootstrap`, the grid's module registry across `ag-grid-react` and `ag-grid-community`,
  Ace across `react-ace` and Ace's modes, and `d3-selection` across `d3-zoom`'s transitions. **A widget that declares a
  library declares its peers too: the frame resolves only what the widget declares.** A library whose peer is not
  declared cannot import it, and the widget does not start. `catalog.json` does not list peers, and neither the CLI nor
  the server checks them, so this table is where they are found. Everything else a library needs is bundled inside it.
* **Ace.** `ace-builds` holds Ace's minified files under `src-noconflict/`, without its workers, since the frame refuses
  a worker loaded from a URL. A widget sets Ace's `basePath` to `/libs/ace-builds/1.35.4/src-noconflict`, so that Ace
  loads a mode, theme or snippet itself, and `useWorker: false`, so Ace runs without its background syntax checker. Each
  `ace-builds/src-noconflict/<file>` entry loads `ace-builds` first, so a mode imported before Ace still finds it.
* **TradingView.** `tradingview` is TradingView's charting library. It draws its chart in a frame nested in the widget
  and loads its files from its directory in the catalog, both of which the frame allows, so it works in a widget
  unchanged; a widget passes `library_path: "/libs/tradingview/29.6.0/"` when it creates a chart.

### The format

```json theme={null}
{
  "catalog": 1,
  "libraries": {
    "react": {
      "18.3.1": {
        "imports": {"react": "index.js", "react/jsx-runtime": "jsx-runtime.js"},
        "css": [],
        "integrity": {"index.js": "sha384-…", "jsx-runtime.js": "sha384-…"}
      }
    },
    "bootstrap": {
      "4.6.2": {"imports": {}, "css": ["bootstrap.min.css"], "integrity": {"bootstrap.min.css": "sha384-…"}}
    }
  }
}
```

* **`catalog`** is the format's version, `1`. A reader refuses a version it does not know: the frame does not load the
  widget, and `blobhub workflow deploy` refuses it with `WIDGET_CATALOG_UNAVAILABLE`.
* **`libraries`** maps each library's name to every published version of it, each `MAJOR.MINOR.PATCH`.
* **`imports`** maps each import-map entry the version answers to onto a file, and **`css`** lists its stylesheets. Both
  hold paths relative to `/libs/<name>/<version>/`, so `@xyflow/react` 12.1.1's entry `@xyflow/react` resolves to
  `/libs/@xyflow/react/12.1.1/index.js`.
* **`integrity`** holds the Subresource Integrity value of every file in the version's module graph: the files `imports`
  and `css` name, and the files they import. A file a library loads itself, outside the import map, is not checked
  against `integrity`: TradingView's bundles have no value, and a mode, theme or snippet Ace loads from its `basePath`
  is fetched as a classic script, which an import map's integrity does not cover. A mode, theme or snippet is checked
  only when the widget imports it through its `ace-builds/src-noconflict/<file>` entry.

## The import map

When a widget loads, the frame's bootstrap page reads `/libs/catalog.json` from the widget's own origin and, before it
imports the entry, writes an import map for exactly the libraries the widget declares: every import-map entry each
declared version answers to, mapped to its file under `/libs/<name>/<version>/`, with the integrity value of every file
the version lists. It links each declared library's stylesheets, with their integrity values, and then the widget's own,
so the widget's rules come after the catalog's and can restyle them. Then it imports the entry.

A bare import of a declared library's entry resolves through the map, static or dynamic, in the widget's code and in the
catalog's libraries alike, which is how a library reaches its peers. A static import loads with the entry; a dynamic
one, such as `import("mermaid")`, loads only when the code reaches it, so a heavy library a widget seldom needs costs
nothing until then. An entry ending `/` answers to every specifier that starts with it, as an import map resolves it.

Nothing else resolves. A declared library or version the catalog lacks stops the load, and the playground shows
`This widget needs <library> <version>, which the catalog does not have.` in place of the widget. A bare name no
declared library answers to fails the import, and the widget does not start.
[`blobhub workflow deploy`](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widgets#deploying) catches both before it uploads
anything: it refuses a library or version the catalog lacks with `WIDGET_LIBRARY_UNKNOWN`, and an entry that imports a
relative path, a URL, or a bare name no declared library answers to with `WIDGET_IMPORT_INVALID`. A computed
`import(expr)`, which it cannot judge, draws the advisory `WIDGET_IMPORT_UNVERIFIED`.

## Building one module

A widget's own build writes the directory `blobhub workflow build` packs, as
[Deploying](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widgets.md#deploying) shows. Whatever bundler makes it, the output
follows these rules; a build that breaks one makes a widget that does not load, or loads broken:

1. **One ES module.** The entry is an ES module that exports `mount`, with the widget's own dynamic imports inlined: no
   code splitting and no chunks. The frame loads every file from a `blob:` URL, which cannot resolve a relative import,
   so a chunk is never found, and `build`, `deploy` and `diff` refuse a relative import.
2. **Declared libraries stay bare.** Every import of a declared library's import-map entry is left as a bare import, for
   the import map to resolve, and a declared library is imported only through its entries. Bundled, a library is a
   second copy beside the one the catalog's libraries import: with two Reacts, a component that uses hooks fails. Nor
   does a widget import a path into a declared library that is not one of its entries, such as `react-dom/server`: left
   bare it resolves to nothing, and the CLI refuses it.
3. **A CommonJS `require()` of a declared library becomes an import.** A CommonJS dependency that calls
   `require("react")` must receive the library the import map loads, through an import. Left as a `require`, it throws
   when the module loads, since a browser has no `require`, and the widget does not start. The CLI cannot see this: a
   `require` is not an import.
4. **A declared library's stylesheet is left out.** The bootstrap links the catalog's stylesheet of every declared
   library that has one, so the build drops any import of it, from a script or from a stylesheet's `@import`. Left bare,
   the import resolves to nothing, and the CLI refuses it in a script; bundled, it is a second copy in the definition. A
   stylesheet the catalog does not serve, such as a highlight.js theme, is bundled.
5. **Images and fonts inlined.** Every image and font the code or a stylesheet uses is inlined as a `data:` URL. A
   stylesheet loaded from a `blob:` URL cannot resolve a relative `url()`, and the widget's origin serves no file of the
   widget's, so an image or font left as a file never shows.
6. **`index.js` and `index.css`.** The output is the entry module, `index.js`, and, when the widget has styles, one
   stylesheet, `index.css`. `widget.yaml`'s `entry` names the module, and the bootstrap links every stylesheet in the
   build after the catalog's.

With esbuild, a `build.mjs` in the widget's project, run with `node build.mjs` before `blobhub workflow build`, for the
Order Board's two libraries:

```javascript theme={null}
import * as esbuild from "esbuild";

// The libraries widget.yaml declares, at the same versions.
const libraries = { react: "18.3.1", "react-dom": "18.3.1" };

const catalog = await (await fetch("https://blobhub.io/libs/catalog.json")).json();
const external = Object.entries(libraries).flatMap(([name, version]) => {
  const published = catalog.libraries[name]?.[version];
  if (!published) {
    throw new Error(`The catalog has no ${name} ${version}.`);
  }
  return Object.keys(published.imports);
});

await esbuild.build({
  entryPoints: ["src/index.js"],
  outdir: "dist",
  bundle: true,
  format: "esm",
  splitting: false,
  external,
  jsx: "automatic",
  loader: { ".js": "jsx", ".png": "dataurl", ".svg": "dataurl", ".woff": "dataurl", ".woff2": "dataurl" },
  define: { "process.env.NODE_ENV": '"production"' },
  minify: true,
});
```

* **`external`** holds every entry of the declared versions, read from the catalog. esbuild also leaves every path under
  a listed package bare, its stylesheets included, so the widget imports no stylesheet of a declared library. One the
  catalog does not serve is imported by its path relative to the importing file, such as
  `../node_modules/highlight.js/styles/github.css`, which esbuild bundles.
* **`jsx: "automatic"`** compiles JSX into imports of `react/jsx-runtime`, an entry the catalog's React answers to.
* **`define`** replaces `process.env.NODE_ENV`, which many packages read and a browser does not have.
* **The CommonJS case.** esbuild uses a package's ES module build when the package ships one. When a dependency ships
  only CommonJS and calls `require()` on a declared library, esbuild keeps the call, and the module throws
  `Dynamic require of "react" is not supported` when it loads. Such a dependency needs a plugin that resolves that
  `require` to a module importing the entry and handing its exports back, or a replacement that ships ES modules.

## Light and dark

The bootstrap sets `<html data-theme>` to the page's theme, `light` or `dark`, before the entry loads.
[`hello`](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widget-bridge#handshake) answers the same `theme`, and when the page
switches between light and dark the host pushes `theme.changed` ([Watches and
events](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widget-bridge.md#watches-and-events)), on which the widget sets the
attribute itself: the bootstrap sets it only once. A stylesheet keys its dark colors on `:root[data-theme="dark"]`, and
a library that draws from script, such as a chart or an editor, is given the theme as an option: from `hello` on load,
and again on each `theme.changed`.

None of the catalog's stylesheets follows `data-theme`. Bootstrap's is light-only, and AG Grid's and React Flow's stay
light unless switched by a class or option of their own. A widget that uses them restyles them for dark in its own
stylesheet, which the bootstrap links after theirs. Here a widget declares a few of the [tokens](#tokens) for both
themes and draws with them:

```css theme={null}
:root {
  color-scheme: light;
  --bh-canvas: #ffffff;
  --bh-surface: #ffffff;
  --bh-text: #212529;
  --bh-text-muted: #6a737d;
  --bh-border: #e5e7eb;
  --bh-accent: #007bff;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bh-canvas: #171719;
  --bh-surface: #202024;
  --bh-text: #e4e4e7;
  --bh-text-muted: #a1a1aa;
  --bh-border: #2e2e33;
  --bh-accent: #93bdf8;
}

body {
  background: var(--bh-canvas);
  color: var(--bh-text);
}

/* The catalog's Bootstrap is light-only: restate the colors of what the widget uses. */
.card {
  background-color: var(--bh-surface);
  border-color: var(--bh-border);
}
```

`color-scheme: dark` darkens the frame's own backdrop, scrollbars and form controls. In `mount`, the widget follows the
page's theme after load:

```javascript theme={null}
port.addEventListener("message", (event) => {
  const message = event.data;
  if (message && message.event === "theme.changed") {
    document.documentElement.dataset.theme = message.data.theme;
  }
});
```

## Tokens

BlobHub's own widgets draw with `--bh-*` custom properties. The frame declares none of them: a widget that takes
BlobHub's look declares the ones it uses, with these values. The core tokens:

| Token | Light | Dark | Use |
| - | - | - | - |
| `--bh-canvas` | `#ffffff` | `#171719` | the page ground |
| `--bh-surface` | `#ffffff` | `#202024` | a card, bubble, node or input |
| `--bh-surface-overlay` | `#ffffff` | `#2c2c33` | a dropdown, popover or modal |
| `--bh-surface-sunken` | `#f8fafc` | `#131315` | an input well, code plate or table header |
| `--bh-fill` | `#f2f4f6` | `#26262b` | a chip or object on a container |
| `--bh-fill-strong` | `#e8ebee` | `#35353c` | the selected or emphasized neutral |
| `--bh-text` | `#212529` | `#e4e4e7` | body text |
| `--bh-text-strong` | `#111827` | `#f4f4f5` | headings and selected labels |
| `--bh-text-muted` | `#6a737d` | `#a1a1aa` | muted text |
| `--bh-text-subtle` | `#9ca3af` | `#88888f` | placeholder and disabled text |
| `--bh-border` | `#e5e7eb` | `#2e2e33` | a hairline |
| `--bh-border-strong` | `#ced4da` | `#45454d` | the edge of a control or of floating chrome |
| `--bh-border-subtle` | `#eaecef` | `#2a2a2f` | the faintest structural hairline |
| `--bh-accent` | `#007bff` | `#93bdf8` | the accent blue, as text or a mark |
| `--bh-accent-text` | `#084298` | `#a9c8f3` | text on the accent tint |
| `--bh-accent-tint` | `#e7f1ff` | `#272d39` | the accent's pale plate, such as a selected item |
| `--bh-link` | `#223355` | `#93bdf8` | a link |
| `--bh-link-hover` | `#909090` | `#b7d3fb` | a link under the pointer |
| `--bh-success` | `#28a745` | `#5cc37c` | green as a mark |
| `--bh-success-text` | `#155724` | `#8ed3a4` | text on the success tint |
| `--bh-success-tint` | `#e8f6ec` | `#25322d` | the success plate |
| `--bh-danger` | `#dc3545` | `#ec6a63` | red as a mark |
| `--bh-danger-text` | `#842029` | `#f39c96` | text on the danger tint |
| `--bh-danger-tint` | `#fdecee` | `#38292c` | the danger plate |
| `--bh-warning` | `#ffc107` | `#d9a441` | amber as a mark and as a fill |
| `--bh-warning-text` | `#7a5b00` | `#e2bd75` | text on the warning tint |
| `--bh-warning-tint` | `#fff6e0` | `#363027` | the warning plate |
| `--bh-info` | `#17a2b8` | `#4cc0cf` | the info mark |
| `--bh-info-text` | `#0c5460` | `#85d6e0` | text on the info tint |
| `--bh-info-tint` | `#e7f6f8` | `#253339` | the info plate |
| `--bh-focus-ring` | `rgba(0, 123, 255, 0.25)` | `rgba(147, 189, 248, 0.50)` | the focus ring's color |
| `--bh-code` | `#e83e8c` | `#f48fb8` | inline code |
| `--bh-code-bg` | `rgba(15, 23, 42, 0.08)` | `rgba(228, 228, 231, 0.09)` | inline code's plate |
| `--bh-shadow-sm` | `0 1px 3px rgba(20, 30, 45, .10)` | `0 0 0 1px var(--bh-border), 0 1px 3px rgba(0, 0, 0, .5)` | a resting card or node |
| `--bh-shadow-md` | `0 4px 14px rgba(20, 30, 45, .16)` | `0 0 0 1px var(--bh-border-strong), 0 4px 14px rgba(0, 0, 0, .6)` | a hovered node or a raised panel |

* **Status colors.** Each of `success`, `danger`, `warning` and `info` has a mark color, a pale `-tint` plate, and a
  `-text` color for text on that plate.
* **`--bh-focus-ring`** is a color only: a focus ring is `box-shadow: 0 0 0 0.2rem var(--bh-focus-ring)`.
* **The shadows** hold a whole `box-shadow` value. In dark, where a shadow barely shows, each also draws an edge from
  `--bh-border` or `--bh-border-strong`, so a widget that uses a shadow declares that token too.

## Storage

A widget keeps a viewer's preferences, such as a chosen tab or a column's width, in `localStorage` on its own origin. No
other widget and no page on blobhub.io can read it, and the bridge has no storage call. The origin belongs to the
widget's alias in its blob, so every placement of the widget on the blob's playgrounds, every build deployed under that
alias in any revision, and every person who opens it in the same browser share that storage. A widget therefore keys
each entry by the viewer's `id`, from `hello` and `viewer.changed`, and reads what an earlier build wrote without
assuming its shape. What it stores stays in that browser.

## See also

* [Custom Widgets](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widgets.md): `widget.yaml` and its `libraries`, the definition,
  permissions, and deploying.
* [Bridge Protocol](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widget-bridge.md): the entry, the handshake, every call, event
  and refusal.
* [Security Model](https://docs.blobhub.io/blob-types/workflow/playgrounds/custom-widget-security.md): the frame and its origin, the headers that
  cut it off from the network, and the risks that remain.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.