@viewstem/dom
v0.1.0
Published
Browser renderer and DOM component connector for Viewstem
Readme
@viewstem/dom
English | 한국어
Register component contracts and implementations through defineResourceSet in @viewstem/core. For vanilla DOM, use DOM_CONNECTOR_ID and supply a DomComponent. Pass the Runtime to createRenderer({ runtime }). Additional connectors can be supplied through connectors.
renderer.mount({ target, source }) owns its View. await mounted.ready returns the View when it is prepared and connected. await mounted.whenRendered() observes DOM completion on that connection. Use renderer.attach(view, target) to display an existing View and detach() to release the display.
A fallback({ parent, anchor, node, error }) can insert static DOM before anchor in parent. Return a handle with dispose() to clean up resources such as timers. Without update(context), a returned handle is disposed and the fallback is recreated when the node's status or error changes. Implement update(context) to use the latest snapshot in place. Optional whenRendered() signals completion.
A connector places its DOM before context.anchor in context.parent, then returns a MountedComponent. Register each named content host with provideSlot. Its cleanup callback withdraws that registration. The browser renderer places Viewstem children in registered hosts and handles movement and disposal. An implementation should stop its work when the supplied instance or update signal is aborted.
A synchronous DOM component returns update(), setPending(), and dispose().
import type { DomComponent } from '@viewstem/dom';
export const label: DomComponent = {
mount({ parent, anchor, node }) {
const element = parent.ownerDocument.createElement('span');
element.textContent = String(node.inputs.label);
parent.insertBefore(element, anchor);
return {
update(next) { element.textContent = String(next.inputs.label); },
setPending(pending) { element.toggleAttribute('aria-busy', pending); },
dispose() { element.remove(); },
};
},
};If the connector applies its DOM asynchronously, provide whenRendered(signal) to report when its own DOM and slots are ready. The renderer waits for that result, then renders children into the slots provided so far and runs afterChildren if supplied. The display's whenRendered() covers both the connector and those children.
Styles for a component set
Use the same package-specific style ID for the CSS build and for the DOM your components create. The ID starts with a lowercase letter and uses lowercase letters, digits, and hyphens.
import { createStyleScope } from '@viewstem/dom/styles';
const forms = createStyleScope('acme-forms');
const input = forms.createElement(document, 'input');Use createElementNS(document, namespace, tag) for owned SVG and mark(element) for an element created by a library on your behalf. Keep the host and borrowed slot elements in their original scopes.
import postcss from 'postcss';
import { scopeUiCss } from '@viewstem/dom/styles/postcss';
const result = await postcss([scopeUiCss('acme-forms')]).process(css, { from: 'styles.css' });Resolve CSS imports before this step and publish the resulting stylesheet. The plugin scopes the final target of each selector and prefixes internal custom properties, layers, and animation names. In .theme { .field { … } }, an owned .field can match inside a host or another set's .theme. Declarations on .theme itself apply to owned .theme elements. Use publicProperties for custom properties the app can set by name.
The plugin accepts @media, @supports, @container, @scope, @starting-style, @layer, @keyframes, and private @property registrations. Top-level @charset and @namespace directives are also supported. Expand build-tool directives before this step. Other at-rules fail the build.
Declare document-wide rules such as @font-face and @counter-style in a host stylesheet. The host also registers public custom properties and defines names used by document View Transitions, named anchors, or named scroll and view timelines. Set styles can reference a host-provided font, counter style, or public custom property in their own rules.
When a component updates a local stylesheet property from JavaScript, pass forms.localProperty('--progress') to element.style.setProperty(). This resolves the same name that the build emits for --progress.
To bundle styles with the set, make the package’s browser export import the generated CSS and re-export the set from its JavaScript entry. Put the browser condition before the normal JavaScript entry in exports, and include the browser entry and the CSS in sideEffects. Consumers register the set as usual. Export the stylesheet separately for hosts that load CSS explicitly.
