meowjsx 0.1.1-alpha.1 → 0.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -7
- package/dist/src/context.d.ts +3 -6
- package/dist/src/context.js +1 -3
- package/dist/src/json/view.d.ts +2 -2
- package/dist/src/jsx-runtime.js +13 -6
- package/dist/src/parts.d.ts +29 -0
- package/dist/src/parts.js +102 -0
- package/dist/src/registry.d.ts +6 -5
- package/dist/src/registry.js +76 -48
- package/dist/src/renderer.d.ts +2 -2
- package/dist/src/renderer.js +8 -1
- package/dist/src/runtime.d.ts +10 -8
- package/dist/src/runtime.js +32 -43
- package/dist/src/style.d.ts +5 -1
- package/dist/src/style.js +92 -6
- package/dist/src/types.d.ts +49 -72
- package/dist/web/html.js +5 -0
- package/dist/web/index.d.ts +1 -0
- package/dist/web/observation.d.ts +1 -1
- package/dist/web/render.js +9 -4
- package/dist/web/shadows.d.ts +4 -2
- package/dist/web/shadows.js +1 -1
- package/dist/web/strategy.js +2 -1
- package/dist/web/types.d.ts +22 -0
- package/dist/web/types.js +1 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Synchronous HTML output follows the same ancestor rule, but still does not suppo
|
|
|
13
13
|
`useContext` reads environment only during the component's synchronous execution. Event handlers and code after `await` must use the value captured in render beforehand; do not treat it as asynchronous local storage. Provider does not add whole-tree re-render or dependency tracking; to share a dynamic value, pass a Signal object as the value and let consumers use the existing Signal binding. Replacing a Provider's ordinary value does not automatically re-run already mounted descendants. Context's internal scope is passed by the renderer and users must not modify it.
|
|
14
14
|
|
|
15
15
|
`RuntimePlugin.resolveComponent({ component, props, runtime })` can select a replacement function before the component is called.
|
|
16
|
-
It is called in the order `pre`, normal, `post`, and within each group's registration order; the first non-`undefined` result wins, and returning the original function also stops resolution; the result is not resolved recursively. Selection happens before the lifecycle scope is created, and the selected component uses its own
|
|
16
|
+
It is called in the order `pre`, normal, `post`, and within each group's registration order; the first non-`undefined` result wins, and returning the original function also stops resolution; the result is not resolved recursively. Selection happens before the lifecycle scope is created, and the selected component uses its own metadata, parts and cleanup logic while the original component does not execute. `props` is a read-only typed view and does not copy or freeze the object.
|
|
17
17
|
|
|
18
18
|
This hook belongs to the execution runtime: pre-created VNodes, Signal/Promise deferred mounts and synchronous HTML all apply. The cleanup returned by `runtime.use()` unmounts rules and does not rewrite already mounted instances; other runtimes are unaffected.
|
|
19
19
|
It does not intercept module side effects or direct function calls, and does not change the meaning of the `create` / `render` hooks.
|
|
@@ -21,7 +21,7 @@ It does not intercept module side effects or direct function calls, and does not
|
|
|
21
21
|
## Development metadata and instance observation
|
|
22
22
|
|
|
23
23
|
Component functions may carry `metadata?: ComponentMetadata`. `ComponentMetadata` is an open type-extension interface; development tools register their own namespaces through declaration merging, and core does not interpret or read their tool fields.
|
|
24
|
-
|
|
24
|
+
`Component<Props>` preserves this constraint, and ordinary functions can use
|
|
25
25
|
`satisfies ComponentMetadata` to check assignment. Metadata is not part of props or component runtime options.
|
|
26
26
|
|
|
27
27
|
`runtime.createRenderer(strategy, { observer })` enables target-neutral instance observation:
|
|
@@ -66,7 +66,7 @@ if that outlet is missing or throws again, the error is reported asynchronously
|
|
|
66
66
|
from an existing renderer are ignored. Usually one observation center is attached to one renderer; the implementation stores by instance identity and does not overwrite because different renderers have the same numeric id. `roots` may use a getter, but the scope is read and fixed when the renderer is created, suitable for tools that set the real child before creation; it is not a runtime dynamic-switch entry. Purely host or text roots
|
|
67
67
|
have no component instances, and external DOM/styles, font loading and browser size changes also do not belong to this observation contract.
|
|
68
68
|
|
|
69
|
-
MeowJSX core: the JSX/VNode factory, Signal/batch/effect, component and part
|
|
69
|
+
MeowJSX core: the JSX/VNode factory, Signal/batch/effect, component metadata and part bindings, shadow/plugin/theme
|
|
70
70
|
contracts, lifecycle and context, and the target-neutral renderer seam (`RenderStrategy<TNode>` /
|
|
71
71
|
`createRenderer()`). Core does not know the target platform.
|
|
72
72
|
|
|
@@ -79,9 +79,9 @@ meowjsx (this package: core + JSX contract)
|
|
|
79
79
|
|
|
80
80
|
## Exports
|
|
81
81
|
|
|
82
|
-
- `.`, `./runtime`: `h`, `Fragment`, `raw`, `
|
|
83
|
-
`createRuntime`, `defaultRuntime`, `
|
|
84
|
-
|
|
82
|
+
- `.`, `./runtime`: `h`, `Fragment`, `raw`, `signal`/`batch`/`mapSignal`/`derived`,
|
|
83
|
+
`createRuntime`, `defaultRuntime`, `defineShadow`, `createThemePlugin`, part descriptor helpers,
|
|
84
|
+
plus VNode/Signal/metadata, `Context` and generic ref types.
|
|
85
85
|
- `./renderer`: `RenderStrategy<TNode>`, `createRenderer()`, `Renderer`/`Rendered`/
|
|
86
86
|
`RendererOptions`, `ComponentObserver`/`ObservedComponent` and other target-seam types. `createRenderer()` injects component lifecycle,
|
|
87
87
|
render hooks, overrides and `key`/`componentPart` identity; the target only implements node and attribute placement.
|
|
@@ -111,9 +111,73 @@ The command above runs from the repository root and only checks the DOM-free cor
|
|
|
111
111
|
(defaults to `unknown`); the Web module narrows it to a real element. Theme contracts (`Theme`/`ThemeRule`/
|
|
112
112
|
`ThemeStyle` and their merge rules) stay in core as planned; they carry values only and touch no style API.
|
|
113
113
|
|
|
114
|
+
## Class lists
|
|
115
|
+
|
|
116
|
+
`className` accepts strings, stringifiable class values, nested readonly arrays, and Signals of
|
|
117
|
+
those values. Arrays keep their order, join with spaces, and omit `false`, `null`, `undefined` and
|
|
118
|
+
empty strings. They do not merge conflicting classes or interpret objects as class dictionaries.
|
|
119
|
+
The native `class` attribute follows the same runtime normalization.
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
const stateClass = signal("is-idle");
|
|
123
|
+
<div className={["panel", [false, stateClass], "host-panel"]} />;
|
|
124
|
+
stateClass.value = "is-active";
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Static HTML reads current values without subscribing. Mounted nodes subscribe to Signals at any
|
|
128
|
+
list depth, update their class attribute in place, and release replaced sources and unmount
|
|
129
|
+
subscriptions. Theme-provided class lists compose with the caller's classes in the same way. The
|
|
130
|
+
public `ClassValue` type is available to components that forward `className`.
|
|
131
|
+
|
|
132
|
+
## Components and refs
|
|
133
|
+
|
|
134
|
+
Components are ordinary functions. A static `meta` declares their stable name and finite parts;
|
|
135
|
+
`Context<typeof Component>` supplies the lifecycle and a typed `parts` map. Use
|
|
136
|
+
`Context<typeof Component, ComponentProps>` when setup also needs the declared types of
|
|
137
|
+
`context.props`. Setup runs once per mount, and Signals carry later changes.
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
import { h, type Context, type Props } from "meowjsx";
|
|
141
|
+
import type { WebRef } from "meowjsx/web";
|
|
142
|
+
|
|
143
|
+
interface PanelProps extends Props {
|
|
144
|
+
title: string;
|
|
145
|
+
ref?: WebRef<HTMLElement>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function Panel({ title, children, ref, ...props }: PanelProps, ctx: Context<typeof Panel>) {
|
|
149
|
+
return (
|
|
150
|
+
<section {...props} ref={[ctx.parts.root, ref]}>
|
|
151
|
+
<h2 ref={ctx.parts.heading}>{title}</h2>
|
|
152
|
+
<div ref={ctx.parts.body}>{children}</div>
|
|
153
|
+
</section>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
Panel.meta = { name: "panel", parts: ["root", "heading", "body"] } as const;
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`ref` accepts nested readonly arrays of part descriptors, node callbacks and `false`/nullish
|
|
160
|
+
omissions. Callbacks run in order at the existing host-property mount point; returned synchronous
|
|
161
|
+
cleanups run in reverse order. A failure still releases acquired cleanups. Static HTML extracts part
|
|
162
|
+
identity without running callbacks. A host node accepts one distinct part identity, and a descriptor
|
|
163
|
+
can mark several nodes. Component refs remain props and are forwarded explicitly to the intended
|
|
164
|
+
host node.
|
|
165
|
+
|
|
166
|
+
Generic ref types stay in the DOM-free root. Importing the explicit `meowjsx/web` type surface adds
|
|
167
|
+
HTML/SVG node inference for native JSX refs. Register component functions with `components: [Panel]`;
|
|
168
|
+
metadata does not register itself. The [parts and ref-binding design](../../docs/designs/2026-10-24-component-parts-ref-bindings.md)
|
|
169
|
+
records the breaking migration and theme adapter.
|
|
170
|
+
|
|
171
|
+
## Bundling
|
|
172
|
+
|
|
173
|
+
The package declares `sideEffects: false` and ships one ESM module per source file. The default
|
|
174
|
+
runtime, JSX runtime and Web renderer are created by `/* @__PURE__ */` calls, and their exported
|
|
175
|
+
members (`h`, `Fragment`, `jsx`, `render` and the rest) are pure reads, so a bundle that imports only
|
|
176
|
+
Signals, Context or types leaves the runtime and renderer out.
|
|
177
|
+
|
|
114
178
|
## Verification
|
|
115
179
|
|
|
116
|
-
- `bun test packages/meowjsx/tests/unit`: Signal, VNode, plugin,
|
|
180
|
+
- `bun test packages/meowjsx/tests/unit`: Signal, VNode, plugin, metadata, part/ref bindings, fake target seam and Web host tests.
|
|
117
181
|
- `tests/types/` checks core and Web type cases separately; the core type environment does not include the DOM.
|
|
118
182
|
|
|
119
183
|
## Web module
|
package/dist/src/context.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Child, Component } from "./runtime.js";
|
|
2
2
|
/** A render-tree environment. Targets carry it through deferred child regions. */
|
|
3
3
|
export type ContextScope = ReadonlyMap<symbol, unknown>;
|
|
4
|
-
export interface
|
|
4
|
+
export interface ContextValue<T> {
|
|
5
5
|
readonly key: symbol;
|
|
6
6
|
readonly defaultValue: T;
|
|
7
7
|
readonly Provider: Component<{
|
|
@@ -9,11 +9,8 @@ export interface Context<T> {
|
|
|
9
9
|
children?: Child;
|
|
10
10
|
}>;
|
|
11
11
|
}
|
|
12
|
-
/** Semantic identity for runtime observation, independent of function names. */
|
|
13
12
|
export declare function isContextProvider(component: Function): boolean;
|
|
14
|
-
export declare function createContext<T>(defaultValue: T):
|
|
15
|
-
|
|
16
|
-
export declare function useContext<T>(context: Context<T>): T;
|
|
17
|
-
/** Runtime/target seam, not a process-wide persistent provider stack. */
|
|
13
|
+
export declare function createContext<T>(defaultValue: T): ContextValue<T>;
|
|
14
|
+
export declare function useContext<T>(context: ContextValue<T>): T;
|
|
18
15
|
export declare function withContextScope<T>(scope: ContextScope | undefined, run: () => T): T;
|
|
19
16
|
export declare function contextScopeForProvider(component: Function, value: unknown, parent: ContextScope | undefined): ContextScope | undefined;
|
package/dist/src/context.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
const providers = new WeakMap();
|
|
2
2
|
let activeScope;
|
|
3
|
-
/** Semantic identity for runtime observation, independent of function names. */
|
|
4
3
|
export function isContextProvider(component) {
|
|
5
4
|
return providers.has(component);
|
|
6
5
|
}
|
|
6
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
7
7
|
export function createContext(defaultValue) {
|
|
8
8
|
const key = Symbol("meowjsx.context");
|
|
9
9
|
const Provider = ({ children }) => children ?? null;
|
|
10
10
|
providers.set(Provider, key);
|
|
11
11
|
return Object.freeze({ key, defaultValue, Provider });
|
|
12
12
|
}
|
|
13
|
-
/** Read synchronously while rendering; capture the value before an await/callback. */
|
|
14
13
|
export function useContext(context) {
|
|
15
14
|
return activeScope?.has(context.key) ? activeScope.get(context.key) : context.defaultValue;
|
|
16
15
|
}
|
|
17
|
-
/** Runtime/target seam, not a process-wide persistent provider stack. */
|
|
18
16
|
export function withContextScope(scope, run) {
|
|
19
17
|
const previous = activeScope;
|
|
20
18
|
activeScope = scope;
|
package/dist/src/json/view.d.ts
CHANGED
|
@@ -30,11 +30,11 @@ export type JsonPropsDefinition<P> = {
|
|
|
30
30
|
readonly [K in Exclude<KnownKeys<P>, "children">]?: NonNullable<P[K]> extends Callback ? JsonActionProperty<Extract<NonNullable<P[K]>, Callback>> : JsonValueProperty<P[K]>;
|
|
31
31
|
};
|
|
32
32
|
export interface JsonComponentDefinition<P extends object = any> {
|
|
33
|
-
readonly component: Component<P, any
|
|
33
|
+
readonly component: Component<P, any>;
|
|
34
34
|
readonly props: JsonPropsDefinition<P>;
|
|
35
35
|
}
|
|
36
36
|
export type JsonCatalog = ReadonlyMap<string, JsonComponentDefinition<any>>;
|
|
37
|
-
export declare function defineJsonComponent<P extends object>(component: Component<P, any
|
|
37
|
+
export declare function defineJsonComponent<P extends object>(component: Component<P, any>, props: JsonPropsDefinition<NoInfer<P>>): JsonComponentDefinition<P>;
|
|
38
38
|
export type JsonNode = string | number | boolean | null | readonly JsonNode[] | {
|
|
39
39
|
readonly component: string;
|
|
40
40
|
readonly props?: Readonly<Record<string, JsonValue>>;
|
package/dist/src/jsx-runtime.js
CHANGED
|
@@ -3,7 +3,9 @@ function createJSX(runtime, type, props, key) {
|
|
|
3
3
|
const { children, ...rest } = props ?? {};
|
|
4
4
|
if (key !== undefined)
|
|
5
5
|
rest.key = key;
|
|
6
|
-
return children === undefined
|
|
6
|
+
return children === undefined
|
|
7
|
+
? runtime.h(type, rest)
|
|
8
|
+
: runtime.h(type, rest, children);
|
|
7
9
|
}
|
|
8
10
|
export function createJSXRuntime(runtime) {
|
|
9
11
|
const jsx = (type, props, key) => createJSX(runtime, type, props, key);
|
|
@@ -15,8 +17,13 @@ export function createJSXRuntime(runtime) {
|
|
|
15
17
|
jsxDEV,
|
|
16
18
|
};
|
|
17
19
|
}
|
|
18
|
-
const defaultJSXRuntime = createJSXRuntime(defaultRuntime);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const defaultJSXRuntime = /* @__PURE__ */ createJSXRuntime(defaultRuntime);
|
|
21
|
+
// A pure read lets bundlers drop the default JSX runtime when none of these members is used.
|
|
22
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
23
|
+
function defaultMember(key) {
|
|
24
|
+
return defaultJSXRuntime[key];
|
|
25
|
+
}
|
|
26
|
+
export const Fragment = /* @__PURE__ */ defaultMember("Fragment");
|
|
27
|
+
export const jsx = /* @__PURE__ */ defaultMember("jsx");
|
|
28
|
+
export const jsxs = /* @__PURE__ */ defaultMember("jsxs");
|
|
29
|
+
export const jsxDEV = /* @__PURE__ */ defaultMember("jsxDEV");
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Cleanup } from "./types.js";
|
|
2
|
+
/** An opaque component and part identity carried by a host VNode. */
|
|
3
|
+
export interface PartDescriptor<Name extends string = string, Part extends string = string> {
|
|
4
|
+
readonly component: Name;
|
|
5
|
+
readonly part: Part;
|
|
6
|
+
readonly state?: Readonly<Record<string, unknown>>;
|
|
7
|
+
}
|
|
8
|
+
/** Create a descriptor by stable component and part names. */
|
|
9
|
+
export declare function createPartDescriptor<Name extends string, Part extends string>(component: Name, part: Part, state?: Readonly<Record<string, unknown>>): PartDescriptor<Name, Part>;
|
|
10
|
+
export declare function isPartDescriptor(value: unknown): value is PartDescriptor;
|
|
11
|
+
export type RefCleanup = () => void | {
|
|
12
|
+
readonly then?: never;
|
|
13
|
+
};
|
|
14
|
+
export type RefCallback<TNode> = (node: TNode) => void | RefCleanup;
|
|
15
|
+
export type RefBinding<TNode> = RefCallback<TNode> | PartDescriptor | false | null | undefined | readonly RefBinding<TNode>[];
|
|
16
|
+
declare const normalizedRef: unique symbol;
|
|
17
|
+
export interface NormalizedRef<TNode = unknown> {
|
|
18
|
+
readonly [normalizedRef]: true;
|
|
19
|
+
readonly callbacks: readonly RefCallback<TNode>[];
|
|
20
|
+
}
|
|
21
|
+
/** Flatten composed refs and resolve the single host part identity before hooks. */
|
|
22
|
+
export declare function normalizeRefBinding<TNode>(value: unknown): {
|
|
23
|
+
readonly ref?: NormalizedRef<TNode>;
|
|
24
|
+
readonly part?: PartDescriptor;
|
|
25
|
+
};
|
|
26
|
+
export declare function isNormalizedRef(value: unknown): value is NormalizedRef;
|
|
27
|
+
/** Invoke callbacks in order and release their successful cleanups in reverse order. */
|
|
28
|
+
export declare function mountRefBindings<TNode>(ref: NormalizedRef<TNode>, node: TNode): Cleanup;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const descriptors = new WeakSet();
|
|
2
|
+
/** Create a descriptor by stable component and part names. */
|
|
3
|
+
export function createPartDescriptor(component, part, state) {
|
|
4
|
+
const descriptor = Object.freeze({
|
|
5
|
+
component,
|
|
6
|
+
part,
|
|
7
|
+
...(state === undefined ? {} : { state }),
|
|
8
|
+
});
|
|
9
|
+
descriptors.add(descriptor);
|
|
10
|
+
return descriptor;
|
|
11
|
+
}
|
|
12
|
+
export function isPartDescriptor(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && descriptors.has(value);
|
|
14
|
+
}
|
|
15
|
+
const normalizedRef = Symbol("meowjsx.normalized-ref");
|
|
16
|
+
/** Flatten composed refs and resolve the single host part identity before hooks. */
|
|
17
|
+
export function normalizeRefBinding(value) {
|
|
18
|
+
const callbacks = [];
|
|
19
|
+
let part;
|
|
20
|
+
const visit = (entry) => {
|
|
21
|
+
if (entry === undefined || entry === null || entry === false)
|
|
22
|
+
return;
|
|
23
|
+
if (Array.isArray(entry)) {
|
|
24
|
+
for (const nested of entry)
|
|
25
|
+
visit(nested);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (isPartDescriptor(entry)) {
|
|
29
|
+
if (part && (part.component !== entry.component || part.part !== entry.part))
|
|
30
|
+
throw new Error("meowjsx host refs cannot bind multiple distinct parts");
|
|
31
|
+
if (!part || (part.state === undefined && entry.state !== undefined))
|
|
32
|
+
part = entry;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (typeof entry === "function") {
|
|
36
|
+
callbacks.push(entry);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
throw new Error("meowjsx ref bindings must be callbacks, part descriptors, or nested arrays");
|
|
40
|
+
};
|
|
41
|
+
visit(value);
|
|
42
|
+
return {
|
|
43
|
+
...(callbacks.length > 0
|
|
44
|
+
? {
|
|
45
|
+
ref: Object.freeze({
|
|
46
|
+
[normalizedRef]: true,
|
|
47
|
+
callbacks: Object.freeze(callbacks),
|
|
48
|
+
}),
|
|
49
|
+
}
|
|
50
|
+
: {}),
|
|
51
|
+
...(part ? { part } : {}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function isNormalizedRef(value) {
|
|
55
|
+
return (typeof value === "object" &&
|
|
56
|
+
value !== null &&
|
|
57
|
+
Object.hasOwn(value, normalizedRef) &&
|
|
58
|
+
value[normalizedRef] === true);
|
|
59
|
+
}
|
|
60
|
+
/** Invoke callbacks in order and release their successful cleanups in reverse order. */
|
|
61
|
+
export function mountRefBindings(ref, node) {
|
|
62
|
+
const cleanups = [];
|
|
63
|
+
let active = true;
|
|
64
|
+
const release = () => {
|
|
65
|
+
if (!active)
|
|
66
|
+
return;
|
|
67
|
+
active = false;
|
|
68
|
+
let firstError;
|
|
69
|
+
let failed = false;
|
|
70
|
+
for (let index = cleanups.length - 1; index >= 0; index -= 1) {
|
|
71
|
+
try {
|
|
72
|
+
cleanups[index]();
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (!failed) {
|
|
76
|
+
failed = true;
|
|
77
|
+
firstError = error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
cleanups.length = 0;
|
|
82
|
+
if (failed)
|
|
83
|
+
throw firstError;
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
for (const callback of ref.callbacks) {
|
|
87
|
+
const cleanup = callback(node);
|
|
88
|
+
if (typeof cleanup === "function")
|
|
89
|
+
cleanups.push(cleanup);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
try {
|
|
94
|
+
release();
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Keep the callback failure primary while attempting every rollback cleanup.
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
return release;
|
|
102
|
+
}
|
package/dist/src/registry.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { AnyShadowDefinition, Child, Cleanup, Component, ComponentMeta, ComponentRegistry, ComponentRenderContext, CreateContext, CreateHook, LifecycleScope, Props, RegisteredComponent, RenderHook, ResolveComponentHook, Runtime, RuntimePluginEnforce, ShadowRegistry, VNode } from "./types.js";
|
|
1
2
|
import { type ContextScope } from "./context.js";
|
|
2
|
-
import type { AnyShadowDefinition, Child, Cleanup, Component, ComponentContract, ComponentRegistry, ComponentRenderContext, CreateContext, CreateHook, LifecycleScope, Props, RenderHook, ResolveComponentHook, Runtime, RuntimePluginEnforce, ShadowRegistry, VNode } from "./types.js";
|
|
3
3
|
export interface HookEntry<Hook> {
|
|
4
4
|
readonly handler: Hook;
|
|
5
5
|
readonly enforce?: RuntimePluginEnforce;
|
|
@@ -13,16 +13,17 @@ export interface RuntimeState {
|
|
|
13
13
|
}
|
|
14
14
|
export declare function addHook<Hook>(hooks: HookEntry<Hook>[], handler: Hook, enforce: RuntimePluginEnforce | undefined): HookEntry<Hook>;
|
|
15
15
|
export declare function removeHook<Hook>(hooks: HookEntry<Hook>[], entry: HookEntry<Hook>): void;
|
|
16
|
-
export declare function createComponentRegistry(initial: readonly
|
|
16
|
+
export declare function createComponentRegistry(initial: readonly RegisteredComponent[]): ComponentRegistry;
|
|
17
17
|
export declare function createShadowRegistry(initial?: readonly AnyShadowDefinition[]): ShadowRegistry;
|
|
18
|
-
export declare function createComponentContext<
|
|
18
|
+
export declare function createComponentContext<Meta extends ComponentMeta, PropsType extends object>(runtime: Runtime, component: Component<PropsType, Meta>, props: PropsType, scope: LifecycleScope): ComponentRenderContext<Meta, PropsType>;
|
|
19
19
|
interface ComponentInvocation {
|
|
20
20
|
child: Child;
|
|
21
21
|
dispose: Cleanup;
|
|
22
22
|
contextScope?: ContextScope;
|
|
23
23
|
}
|
|
24
|
-
export declare function invokeComponent(component: Component<any, any
|
|
24
|
+
export declare function invokeComponent(component: Component<any, any>, props: Props, runtime: Runtime, contextScope?: ContextScope): ComponentInvocation;
|
|
25
25
|
export declare function createBaseVNode(context: CreateContext): VNode;
|
|
26
26
|
export declare function runCreateHooks(state: RuntimeState, context: CreateContext): VNode;
|
|
27
27
|
export declare function runRenderHooks(state: RuntimeState, runtime: Runtime, vnode: VNode): VNode;
|
|
28
|
-
export
|
|
28
|
+
export declare function normalizeHostVNode(vnode: VNode): VNode;
|
|
29
|
+
export { isNormalizedRef, isPartDescriptor, mountRefBindings } from "./parts.js";
|
package/dist/src/registry.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
+
import { createPartDescriptor, normalizeRefBinding, isNormalizedRef } from "./parts.js";
|
|
1
2
|
import { contextScopeForProvider, withContextScope } from "./context.js";
|
|
2
3
|
import { createLifecycleScope } from "./lifecycle.js";
|
|
3
4
|
export function addHook(hooks, handler, enforce) {
|
|
4
5
|
const entry = { handler, enforce };
|
|
5
6
|
if (enforce === "pre") {
|
|
6
|
-
const
|
|
7
|
-
if (
|
|
8
|
-
hooks.splice(
|
|
7
|
+
const index = hooks.findIndex((hook) => hook.enforce !== "pre");
|
|
8
|
+
if (index >= 0)
|
|
9
|
+
hooks.splice(index, 0, entry);
|
|
9
10
|
else
|
|
10
11
|
hooks.push(entry);
|
|
11
12
|
return entry;
|
|
12
13
|
}
|
|
13
|
-
if (enforce === "post")
|
|
14
|
+
if (enforce === "post")
|
|
14
15
|
hooks.push(entry);
|
|
15
|
-
|
|
16
|
+
else {
|
|
17
|
+
const index = hooks.findIndex((hook) => hook.enforce === "post");
|
|
18
|
+
if (index >= 0)
|
|
19
|
+
hooks.splice(index, 0, entry);
|
|
20
|
+
else
|
|
21
|
+
hooks.push(entry);
|
|
16
22
|
}
|
|
17
|
-
const firstPost = hooks.findIndex((hook) => hook.enforce === "post");
|
|
18
|
-
if (firstPost >= 0)
|
|
19
|
-
hooks.splice(firstPost, 0, entry);
|
|
20
|
-
else
|
|
21
|
-
hooks.push(entry);
|
|
22
23
|
return entry;
|
|
23
24
|
}
|
|
24
25
|
export function removeHook(hooks, entry) {
|
|
@@ -27,32 +28,37 @@ export function removeHook(hooks, entry) {
|
|
|
27
28
|
hooks.splice(index, 1);
|
|
28
29
|
}
|
|
29
30
|
export function createComponentRegistry(initial) {
|
|
30
|
-
const
|
|
31
|
-
const register = (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
const components = new Map();
|
|
32
|
+
const register = (component) => {
|
|
33
|
+
const meta = component.meta;
|
|
34
|
+
if (!meta?.name.trim())
|
|
35
|
+
throw new Error("meowjsx components need a nonempty meta.name");
|
|
36
|
+
if (!Array.isArray(meta.parts) || new Set(meta.parts).size !== meta.parts.length)
|
|
37
|
+
throw new Error(`meowjsx component ${meta.name} needs unique meta.parts`);
|
|
38
|
+
if (meta.parts.some((part) => typeof part !== "string" || !part.trim()))
|
|
39
|
+
throw new Error(`meowjsx component ${meta.name} has an invalid meta.parts entry`);
|
|
40
|
+
const previous = components.get(meta.name);
|
|
41
|
+
components.set(meta.name, component);
|
|
36
42
|
let active = true;
|
|
37
43
|
return () => {
|
|
38
44
|
if (!active)
|
|
39
45
|
return;
|
|
40
46
|
active = false;
|
|
41
|
-
if (
|
|
47
|
+
if (components.get(meta.name) !== component)
|
|
42
48
|
return;
|
|
43
49
|
if (previous)
|
|
44
|
-
|
|
50
|
+
components.set(meta.name, previous);
|
|
45
51
|
else
|
|
46
|
-
|
|
52
|
+
components.delete(meta.name);
|
|
47
53
|
};
|
|
48
54
|
};
|
|
49
|
-
for (const
|
|
50
|
-
register(
|
|
55
|
+
for (const component of initial)
|
|
56
|
+
register(component);
|
|
51
57
|
return {
|
|
52
58
|
register,
|
|
53
|
-
get: (name) =>
|
|
54
|
-
has: (name) =>
|
|
55
|
-
list: () => [...
|
|
59
|
+
get: (name) => components.get(name),
|
|
60
|
+
has: (name) => components.has(name),
|
|
61
|
+
list: () => [...components.values()],
|
|
56
62
|
};
|
|
57
63
|
}
|
|
58
64
|
export function createShadowRegistry(initial = []) {
|
|
@@ -71,29 +77,23 @@ export function createShadowRegistry(initial = []) {
|
|
|
71
77
|
};
|
|
72
78
|
for (const shadow of initial)
|
|
73
79
|
register(shadow);
|
|
74
|
-
return {
|
|
75
|
-
register,
|
|
76
|
-
list: () => [...shadows],
|
|
77
|
-
};
|
|
80
|
+
return { register, list: () => [...shadows] };
|
|
78
81
|
}
|
|
79
|
-
export function createComponentContext(runtime,
|
|
82
|
+
export function createComponentContext(runtime, component, props, scope) {
|
|
83
|
+
const meta = component.meta ?? { name: "anonymous", parts: [] };
|
|
84
|
+
const bindings = Object.create(null);
|
|
85
|
+
for (const part of meta.parts)
|
|
86
|
+
bindings[part] = createPartDescriptor(meta.name, part, props);
|
|
80
87
|
return {
|
|
81
88
|
...scope,
|
|
82
|
-
component:
|
|
89
|
+
component: meta,
|
|
83
90
|
props,
|
|
84
|
-
|
|
85
|
-
return runtime.part({
|
|
86
|
-
component: contract.name,
|
|
87
|
-
part,
|
|
88
|
-
state: props,
|
|
89
|
-
}, type, partProps, ...children);
|
|
90
|
-
},
|
|
91
|
+
parts: Object.freeze(bindings),
|
|
91
92
|
};
|
|
92
93
|
}
|
|
93
94
|
export function invokeComponent(component, props, runtime, contextScope) {
|
|
94
95
|
const scope = createLifecycleScope(runtime);
|
|
95
|
-
const
|
|
96
|
-
const componentContext = createComponentContext(runtime, contract ?? { name: "anonymous", parts: [] }, props, scope);
|
|
96
|
+
const componentContext = createComponentContext(runtime, component, props, scope);
|
|
97
97
|
try {
|
|
98
98
|
return {
|
|
99
99
|
child: withContextScope(contextScope, () => component(props, componentContext)),
|
|
@@ -108,9 +108,8 @@ export function invokeComponent(component, props, runtime, contextScope) {
|
|
|
108
108
|
}
|
|
109
109
|
export function createBaseVNode(context) {
|
|
110
110
|
const props = { ...context.props };
|
|
111
|
-
if (context.children.length > 0)
|
|
111
|
+
if (context.children.length > 0)
|
|
112
112
|
props.children = context.children.length === 1 ? context.children[0] : context.children;
|
|
113
|
-
}
|
|
114
113
|
const key = props.key;
|
|
115
114
|
return {
|
|
116
115
|
type: context.type,
|
|
@@ -122,22 +121,51 @@ export function createBaseVNode(context) {
|
|
|
122
121
|
export function runCreateHooks(state, context) {
|
|
123
122
|
if (context.raw || state.createHooks.length === 0)
|
|
124
123
|
return createBaseVNode(context);
|
|
125
|
-
const dispatch = (index,
|
|
124
|
+
const dispatch = (index, next) => {
|
|
126
125
|
const entry = state.createHooks[index];
|
|
127
126
|
if (!entry)
|
|
128
|
-
return createBaseVNode(
|
|
129
|
-
return entry.handler(
|
|
127
|
+
return createBaseVNode(next);
|
|
128
|
+
return entry.handler(next, (updated) => dispatch(index + 1, updated));
|
|
130
129
|
};
|
|
131
130
|
return dispatch(0, context);
|
|
132
131
|
}
|
|
133
132
|
export function runRenderHooks(state, runtime, vnode) {
|
|
134
133
|
if (state.renderHooks.length === 0)
|
|
135
134
|
return vnode;
|
|
136
|
-
const dispatch = (index,
|
|
135
|
+
const dispatch = (index, node) => {
|
|
137
136
|
const entry = state.renderHooks[index];
|
|
138
137
|
if (!entry)
|
|
139
|
-
return
|
|
140
|
-
return entry.handler({ vnode:
|
|
138
|
+
return node;
|
|
139
|
+
return entry.handler({ vnode: node, runtime }, (updated) => dispatch(index + 1, updated));
|
|
140
|
+
};
|
|
141
|
+
return normalizeHostVNode(dispatch(0, vnode));
|
|
142
|
+
}
|
|
143
|
+
export function normalizeHostVNode(vnode) {
|
|
144
|
+
if (typeof vnode.type !== "string" || !Object.hasOwn(vnode.props, "ref"))
|
|
145
|
+
return vnode;
|
|
146
|
+
if (isNormalizedRef(vnode.props.ref))
|
|
147
|
+
return vnode;
|
|
148
|
+
const normalized = normalizeRefBinding(vnode.props.ref);
|
|
149
|
+
const props = { ...vnode.props };
|
|
150
|
+
if (normalized.ref)
|
|
151
|
+
props.ref = normalized.ref;
|
|
152
|
+
else
|
|
153
|
+
delete props.ref;
|
|
154
|
+
if (!normalized.part)
|
|
155
|
+
return { ...vnode, props };
|
|
156
|
+
const current = vnode.componentPart;
|
|
157
|
+
if (current &&
|
|
158
|
+
(current.component !== normalized.part.component || current.part !== normalized.part.part))
|
|
159
|
+
throw new Error("meowjsx host refs cannot bind multiple distinct parts");
|
|
160
|
+
return {
|
|
161
|
+
...vnode,
|
|
162
|
+
raw: true,
|
|
163
|
+
props,
|
|
164
|
+
componentPart: {
|
|
165
|
+
component: normalized.part.component,
|
|
166
|
+
part: normalized.part.part,
|
|
167
|
+
state: normalized.part.state,
|
|
168
|
+
},
|
|
141
169
|
};
|
|
142
|
-
return dispatch(0, vnode);
|
|
143
170
|
}
|
|
171
|
+
export { isNormalizedRef, isPartDescriptor, mountRefBindings } from "./parts.js";
|
package/dist/src/renderer.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ContextScope } from "./context.js";
|
|
2
2
|
import { type KeyedChild } from "./keyed.js";
|
|
3
3
|
export { createComponentObservation, type ComponentObservation, type ComponentObservationOptions, } from "./observation.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { ComponentMeta, ComponentMetadata } from "./types.js";
|
|
5
5
|
/** A target-owned cleanup operation. */
|
|
6
6
|
export type RendererCleanup = () => void;
|
|
7
7
|
/**
|
|
@@ -24,7 +24,7 @@ export type RenderableProps = Record<string, unknown> & {
|
|
|
24
24
|
*/
|
|
25
25
|
export type RenderableComponent = {
|
|
26
26
|
(...args: never[]): RenderableChild;
|
|
27
|
-
readonly
|
|
27
|
+
readonly meta?: ComponentMeta;
|
|
28
28
|
metadata?: ComponentMetadata;
|
|
29
29
|
};
|
|
30
30
|
export interface RenderableComponentPart {
|
package/dist/src/renderer.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { isKeyedChild, readKeyedEntries } from "./keyed.js";
|
|
2
2
|
import { signal } from "./reactive.js";
|
|
3
|
+
import { normalizeClassName } from "./style.js";
|
|
4
|
+
import { isNormalizedRef, mountRefBindings } from "./parts.js";
|
|
3
5
|
export { createComponentObservation, } from "./observation.js";
|
|
4
6
|
const noop = () => { };
|
|
5
7
|
/** Run every cleanup, retaining the first error raised by any operation. */
|
|
@@ -512,9 +514,14 @@ export function createRenderer(strategy, options = {}) {
|
|
|
512
514
|
let childrenMount;
|
|
513
515
|
let observed = false;
|
|
514
516
|
try {
|
|
515
|
-
for (const [name,
|
|
517
|
+
for (const [name, prop] of Object.entries(vnode.props)) {
|
|
516
518
|
if (name === "children" || name === "key")
|
|
517
519
|
continue;
|
|
520
|
+
const value = name === "className" || name === "class" ? normalizeClassName(prop) : prop;
|
|
521
|
+
if (name === "ref" && isNormalizedRef(value)) {
|
|
522
|
+
propertyCleanups.push(mountRefBindings(value, element));
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
518
525
|
if (isSignal(value)) {
|
|
519
526
|
let propertyCleanup = cleanupResult(strategy.setProperty(element, name, value.value));
|
|
520
527
|
const unsubscribe = value.subscribe((next) => {
|
package/dist/src/runtime.d.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
export { createContext, useContext, type
|
|
1
|
+
export { createContext, useContext, type ContextScope, type ContextValue } from "./context.js";
|
|
2
2
|
export { keyed, type KeyedChild } from "./keyed.js";
|
|
3
|
-
|
|
4
|
-
export type {
|
|
3
|
+
export { createPartDescriptor, isPartDescriptor } from "./parts.js";
|
|
4
|
+
export type { PartDescriptor, RefBinding, RefCallback, RefCleanup } from "./parts.js";
|
|
5
|
+
import type { Child, Cleanup, ComponentMeta, Component, HFactory, ShadowDefinition, ShadowSetup, FragmentComponent, Props, Runtime, RuntimeOptions, RuntimePluginOption, VNode } from "./types.js";
|
|
6
|
+
export type { Child, ClassValue, Cleanup, Component, ComponentMeta, ComponentMetadata, ComponentPartRef, ComponentRegistry, ComponentRegistryMap, ComponentRenderContext, Context, CreateContext, CreateHook, Effect, ElementType, FragmentComponent, Key, LifecycleScope, MaybeSignal, MountResult, Props, ReadableSignal, RegisteredComponent, RegisteredComponentName, RegisteredComponentPart, RenderContext, RenderHook, ResolveComponentContext, ResolveComponentHook, Runtime, RuntimeOptions, RuntimePlugin, RuntimePluginContext, RuntimePluginEnforce, RuntimePluginOption, RuntimePluginSetup, RuntimeRendererOptions, RuntimeTargetHooks, ShadowContext, ShadowDefinition, ShadowRegistry, ShadowSetup, Signal, StyleObject, StyleScalar, StyleValue, Theme, ThemeContext, ThemeRule, ThemeRules, ThemeStyle, VNode, WritableSignal, } from "./types.js";
|
|
5
7
|
export { batch, isAsyncIterable, isPromiseLike, derived, isSignal, mapSignal, signal, unwrap, } from "./reactive.js";
|
|
6
8
|
export { createLifecycleScope } from "./lifecycle.js";
|
|
7
9
|
export { createThemePlugin, styleText } from "./style.js";
|
|
8
|
-
export declare function
|
|
9
|
-
|
|
10
|
+
export declare function defineShadow<C extends Component<any, any> & {
|
|
11
|
+
readonly meta: ComponentMeta;
|
|
12
|
+
}, Part extends C["meta"]["parts"][number], TNode = unknown>(component: C, part: Part, setup: ShadowSetup<Parameters<C>[0], C["meta"]["name"], Part, TNode>): ShadowDefinition<Parameters<C>[0], C["meta"]["name"], C["meta"]["parts"][number], Part, TNode>;
|
|
10
13
|
export declare const shadow: typeof defineShadow;
|
|
11
14
|
export declare function createRuntime(options?: RuntimeOptions): Runtime;
|
|
12
15
|
export declare const defaultRuntime: Runtime;
|
|
13
16
|
export declare const registry: import("./types.js").ComponentRegistry;
|
|
14
|
-
export declare const h:
|
|
17
|
+
export declare const h: HFactory;
|
|
15
18
|
export declare const raw: (type: string, props: Props | null, ...children: Child[]) => VNode;
|
|
16
|
-
export declare const part: (identity: ComponentPartRef, type: string, props: Props | null, ...children: Child[]) => VNode;
|
|
17
19
|
export declare const Fragment: FragmentComponent;
|
|
18
|
-
export declare const registerComponent: (
|
|
20
|
+
export declare const registerComponent: (component: import("./types.js").RegisteredComponent) => () => void;
|
|
19
21
|
export declare function useRuntime(plugin: RuntimePluginOption): Cleanup;
|