@nibcss/runtime
The browser layer: one runtime per page, a MutationObserver, dark mode, diagnostics events and SSR collection.
The runtime is the engine plus a DOM. It scans a root, writes the rules it needs into its own
<style id="nib">, and keeps watching so anything added later is styled too. It is what resolves every
value the build could not read.
Install#
npm i @nibcss/runtimeStarting it#
import { init, getRuntime, shareRuntime, resetRuntime, NibRuntime } from "@nibcss/runtime";
import config from "@nibcss/config";
const rt = init(config); // create, start and share — idempotentExport | What it is |
|---|---|
| create, start and share the page's runtime (idempotent) |
| the shared runtime, or |
| register an instance you created yourself, without starting it |
| forget the shared instance (tests) |
| the class — |
| the dark-mode decision as a string, to inline in |
|
|
| the config discovery the CDN build uses |
| the pieces, if you are building something unusual |
An instance#
Member | What it does |
|---|---|
| scan the root and observe it / stop and remove the stylesheet |
| the class string for an attribute, generating the CSS |
| merge new config in — one token, a recipe, |
| apply what the observer has queued, now (tests, or after bulk DOM changes) |
| the |
| the resolved config, the CSS so far, rule and class counts |
| re-scan a subtree, collect rules no element uses any more |
Construction-only options: root (default document.documentElement), watch (observe mutations —
false for one-shot SSR), gc, nativeValidation (validate values with CSS.supports).
Diagnostics in the browser#
document.addEventListener("nib:warning", (e) => {
console.warn(e.detail.attr, e.detail.messages, e.detail.diagnostics);
});document also receives nib:config whenever configure() runs, with the resolved config as its detail.
Server-side rendering#
const rt = new NibRuntime({ ...config, root: dom.window.document, watch: false });
rt.start();
const css = rt.engine.css();A second runtime for a widget#
const rt = new NibRuntime({ root: document.querySelector("#widget")!, prefix: "wgt-", watch: true });
rt.start();