@fiberfx/hyperscript
v4.1.1
Published
Mercury Hyperscript (MHS) Virtual DOM library.
Readme
@fiberfx/hyperscript
Mercury Hyperscript (MHS): the virtual DOM runtime of the @fiberfx/* packages. It renders function and class components to the DOM with native DOM semantics (class, for, native events), ships hooks, a server renderer and a JSX runtime, and has no React compatibility layer.
Install
npm install @fiberfx/[email protected]This package is version 4.0.0, coordinated with @fiberfx/core and
@fiberfx/ui 4.0.0. It declares no runtime dependencies or peer dependencies.
For a native mx app, preserve its module files and use mx install; the npm
command above is for an npm-managed consumer.
Use
In a native Mercury app, mx selects the Hyperscript JSX runtime. Do not add a JSX pragma comment: the native compiler fixes the runtime and refuses pragmas.
import { render } from '@fiberfx/hyperscript';
import { useState } from '@fiberfx/hyperscript/hooks';
function Counter() {
const [count, setCount] = useState(0);
return (
<button class="counter" onClick={() => setCount(count + 1)}>
{count}
</button>
);
}
render(<Counter />, document.getElementById('app'));Without JSX, h(type, props, ...children) builds the same tree. Browsers do
not parse JSX or TypeScript; use plain JavaScript h calls and an import map
for the browser-only route.
Entry points
| Import | Provides |
| --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| @fiberfx/hyperscript | render, hydrate, h/createElement, Fragment, createRef, cloneElement, isValidElement, Component, createContext, createPortal, toChildArray, memo |
| @fiberfx/hyperscript/hooks | useState, useReducer, useEffect, useLayoutEffect, useRef, useImperativeHandle, useMemo, useCallback, useContext, useDebugValue, useErrorBoundary, useId, useSyncExternalStore |
| @fiberfx/hyperscript/client | createRoot, hydrateRoot |
| @fiberfx/hyperscript/server | renderToString, renderToStringAsync, renderToStaticMarkup |
| @fiberfx/hyperscript/jsx-runtime, /jsx-dev-runtime | The automatic JSX runtime (jsx, jsxs, jsxDEV, Fragment) |
| @fiberfx/hyperscript/test-utils | act, setupRerender, teardown, onCommit, onDiff (test-only: a throw from its listener stops the render) |
| @fiberfx/hyperscript/inspect | inspect, inspectFailures: the renderer observed as detached records, root by root, effector's ./inspect twin (mercury.hyperscript.2) |
| @fiberfx/hyperscript/debug | Development warnings |
There is no ./compat entry and no compat source: React compatibility was deleted (mercury.compat-removal.1). Every sub-path imports the runtime by its own name, so an application holds one runtime. The renderer's hook slots (options) are internal to the module; observe it through ./inspect.
DOM contract
Mercury Hyperscript follows the DOM, not React. Five rules are pinned by probes in a real browser: test/browser/dom-contract.test.jsx in the source repository, run against core in headless Chromium. Each probe has a control that must fail.
classandforare the authoring names, on HTML and SVG:<label class="a" for="x">. (classNamealso works, because it's a DOM property, butclassis the name to write.)- Native input and change. On a text input or textarea,
onInputfires on every keystroke andonChangefires on the DOM'schangeevent (at least on blur). On a select or checkbox,onChangefires per selection or toggle. There is no React-style mapping ofonChangeto input events. - SVG attributes keep their SVG names:
stroke-width,fill-rule,clip-path.hrefworks withoutxlink. Core doesn't translatestrokeWidth. - Focus that bubbles uses
onFocusInandonFocusOut. A parent'sonFocusdoesn't fire for a descendant, becausefocusdoesn't bubble in the DOM. - Custom elements receive an object prop as a property and a string as an attribute. Custom events attach through
on<name>:onmy-eventlistens tomy-event, andonionChangetoionChange. A name that starts with a capital afteronis lowercased, as for built-in events.
The packed-consumer gate
pnpm run check:consumer (after pnpm run build) packs this package and installs it into a fresh app outside the workspace, with no aliases and no mercury-source condition. It checks what a consumer gets (mercury.64):
- the install has no warning, and its closure holds no npm
preact,@preact/*, React or compat package; - in Node,
.,./client,./hooks,./serverand both JSX runtimes import, and./compat,./compat/clientand./compat/jsx-runtimethrowERR_PACKAGE_PATH_NOT_EXPORTEDand each fail a Vite build; renderToStringkeepsclass,forandstroke-width;tscaccepts a hook component with the contract's spellings and refuses the compat imports;- the production build takes the runtime only from the installed
dist, one root module, nothing foreign; - in Chromium (dev and the production preview) the component renders, follows each keystroke, updates on a click and cleans up on unmount, and root, hooks, jsx-runtime, test-utils and inspect share one renderer.
--mutant drop-export (a packed export's file removed), --mutant npm-preact (the packed hooks rebound to npm preact) --mutant compat-export (./compat re-exported) and --mutant compat-browser-dangling (a browser-only ./compat whose module fails for another reason) must each fail it. --work <dir> chooses the scratch directory, --keep keeps it, and --offline forbids the registry.
Provenance
Mercury Hyperscript is vendored from Preact, MIT.
- Runtime (
src,hooks,jsx-runtime,debug,devtools,test-utils): Preact'smainbranch after the 11.0.2 tag. As imported in4cb7abce(2026-10-25), all 49 source files are byte-identical to Preactmainatb21ad22e(2026-10-25T07:25:58Z), the last upstream change to them before the import. 46 of them also match the11.0.2tag; the other three changed onmainafter it: - Server renderer (
src/server): vendored from preact-render-to-string, upstream after 6.8.1, assrc/serverrecords it. Upstream's license is MIT, Copyright (c) 2015 Jason Miller. - Mercury's changes since the import are in this repository's history: the package name and version, the native APIs and one runtime identity (H0, H0b), compat removed from what ships and then deleted (mercury.compat-removal.1), the donor server suite, the numeric-style unit, controlled inputs, and the manifests (mercury.62). The two runtime departures are listed below.
Mercury's departures from Preact 11
This package vendors Preact 11. Where Mercury's runtime differs on purpose, it says so here.
Numeric style lengths are in px.
style={{ width: 10 }}writeswidth: 10px, on the client and in./server's HTML alike; unitless properties (opacity,zIndex,flex,lineHeight, grid line numbers…) and custom properties (--gap) are written as given. Preact 11 dropped the client's px rule; Mercury's server renderer never did, and Mercury's own components and React-era code write numeric lengths, so the client agrees with the server. The list and the rule live in one module,src/style.js, which both renderers import. (H0e)A controlled input shows its prop. When an
<input>,<textarea>or<select>has avalueorcheckedprop, a handler that refuses an edit leaves the element as the prop says. After the element's ownonInputoronChangehandler runs and every render it queued has run, avalueorcheckedthat no render wrote is written back. So<input value="a" onInput={() => {}} />staysa, and<input type="checkbox" checked={false} onChange={() => {}} />stays unchecked.- An accepted edit is untouched, caret included, because its render already wrote the new value.
- A refused text edit puts the caret back where the edit was made.
- A refused radio click restores the group's controlled radios, so the checked one stays checked.
- No write-back during composition. An input event between
compositionstartandcompositionendisisComposing, and an IME keeps its text until it commits. - No cost to an uncontrolled element: an element with
defaultValue,defaultCheckedor neither prop never queues a write-back.
Preact 11 compares
valueandcheckedwith the DOM only during a render, so a refused edit stays on screen. Mercury's components and React-era code are written for controlled semantics. The write-back isrestoreControlledinsrc/diff/props.js; it runs in the render queue's flush,processinsrc/component.js. (H0f)
License
MIT. See LICENSE: Copyright (c) 2015-present Jason Miller, Preact's author. The server renderer is MIT too, Copyright (c) 2015 Jason Miller (preact-render-to-string).
