API Overview
Public surface on window.Dim after loading the CDN IIFE (dim.global.js or dim.wasm.global.js). For interactive demos, see Storybook.
const { html, css, define, useState, useStore, keyed, t } = Dim;
In components, prefer hooks and html/css from the second argument injected by define.
Renderer
| Export | Role |
|---|---|
html | Tagged template for markup (Dim’s own renderer) |
css / unsafeCSS | Style results for useStyle |
keyed | Stable list keys (Dim directive) |
unsafeHTML | Trusted HTML only |
renderChildren | Compose children; strings render as text |
const { html, css, keyed, unsafeHTML, renderChildren } = Dim;
define
const { define } = Dim;
define({
tag: "my-widget",
component: MyWidget,
inputs: null, // optional t.record schema — see Contracts
});
Registers a DimElement custom element, stamps component.tag = tag, and injects shared deps (html, hooks, keyed, …) as the second argument of component(props, deps).
Hooks
| Hook | Role |
|---|---|
useState | Local state [value, setValue] |
useEffect | Side effects + cleanup |
useMemo | Memoized value |
useRef | Mutable ref / DOM handle |
useStyle | Shadow-scoped CSS |
useScope | Register child components in scope |
useLazyScope | Experimental — loads via new Function; trusted sources only |
// Inside component(props, { useState, useEffect, useStyle, useScope, html, css })
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => setCount((c) => c + 1), 1000);
return () => clearInterval(id);
}, []);
useStyle(css`.x { color: blue; }`);
useScope({ "child-tag": ChildFn });
// or after define() stamped .tag:
useScope({ ChildFn });
useStore
Persists nested useState leaves to IndexedDB and syncs across components/tabs. Each leaf is a 5-tuple:
const {
notes: [notes, setNotes, isLoading, setIsLoading, isSaving],
} = useStore(
{ notes: useState([]) },
{ encryptionKey: "optional-secret" }, // omit for plaintext
);
// Shorthand: useStore({ notes: useState([]) }, "password");
| Index | Name | Role |
|---|---|---|
| 0 | value | Current state |
| 1 | setter | Update + persist |
| 2 | isLoading | true until initial IndexedDB read |
| 3 | setIsLoading | Managed by the async manager |
| 4 | isSaving | true while a debounced persist is in flight |
Encryption is opt-in. See Encrypted Storage.
File system & transitions
| Export | Role |
|---|---|
useFS | File System Access / OPFS with optional encryption |
useViewTransition | Manual view transitions |
viewTransitionStyles | Shared transition CSS helpers |
Declarative slides also work via a transitionId attribute/prop on the host. See View Transitions.
Contracts
const { define, t } = Dim;
define({
tag: "my-input",
component: Comp,
inputs: t.record({ label: t.string }),
});
Also on Dim: contract, fold, validateValue, getValidatorEngine. Rejected props render nothing and log an error. Browser verification: Contracts & DimVerify.
React interop (npm exception)
CDN apps do not need this. Mixed React + Dim trees use the package subpath:
import { useDimStore } from "@positive-intentions/dim/react";
See useDimStore.
Managers (advanced)
CryptoManager, StorageManager, AsyncronousStateManager, and debouncedDispatcher are on Dim for custom integrations. Prefer useStore in app code.