ziex 0.0.1-dev.0 → 0.0.1-dev.2
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/dom.d.ts +134 -0
- package/index.d.ts +2 -8
- package/index.js +1 -1
- package/package.json +1 -1
- package/types.d.ts +187 -0
package/dom.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { ComponentMetadata } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Result of preparing a component for hydration.
|
|
4
|
+
*
|
|
5
|
+
* Contains all the necessary data to render a React component into its server-rendered container.
|
|
6
|
+
*/
|
|
7
|
+
export type PreparedComponent = {
|
|
8
|
+
/**
|
|
9
|
+
* The HTML element where the component should be rendered.
|
|
10
|
+
*
|
|
11
|
+
* This is the DOM node that was server-rendered by ZX with the component's unique ID.
|
|
12
|
+
* The element already exists in the DOM and contains the server-rendered fallback content.
|
|
13
|
+
* React will hydrate this element, replacing its contents with the interactive component.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* // The DOM node corresponds to HTML like:
|
|
18
|
+
* // <div id="zx-dcde04c415da9d1b15ca2690d8b497ae" data-props="...">...</div>
|
|
19
|
+
*
|
|
20
|
+
* const { domNode } = await prepareComponent(component);
|
|
21
|
+
* createRoot(domNode).render(<Component {...props} />);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
domNode: HTMLElement;
|
|
25
|
+
/**
|
|
26
|
+
* Component props parsed from the server-rendered HTML.
|
|
27
|
+
*
|
|
28
|
+
* Props are extracted from the `data-props` attribute (JSON-encoded) on the component's
|
|
29
|
+
* container element. If the component has children from server side then they are automatically converted to
|
|
30
|
+
* `dangerouslySetInnerHTML` for React compatibility.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* // Server-rendered HTML:
|
|
35
|
+
* // <div data-props='{"max_count":10,"label":"Counter"}' data-children="<span>0</span>">...</div>
|
|
36
|
+
*
|
|
37
|
+
* const { props } = await prepareComponent(component);
|
|
38
|
+
* // props = {
|
|
39
|
+
* // max_count: 10,
|
|
40
|
+
* // label: "Counter",
|
|
41
|
+
* // dangerouslySetInnerHTML: { __html: "<span>0</span>" }
|
|
42
|
+
* // }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
props: Record<string, any> & {
|
|
46
|
+
/**
|
|
47
|
+
* React's special prop for setting inner HTML directly.
|
|
48
|
+
*
|
|
49
|
+
* Automatically added when the component has children in the ZX file. The HTML string
|
|
50
|
+
* is extracted from the `data-children` attribute on the server-rendered element.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* // In ZX file:
|
|
55
|
+
* <MyComponent @rendering={.csr}>
|
|
56
|
+
* <p>Child content</p>
|
|
57
|
+
* </MyComponent>
|
|
58
|
+
*
|
|
59
|
+
* // Results in:
|
|
60
|
+
* // props.dangerouslySetInnerHTML = { __html: "<p>Child content</p>" }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
dangerouslySetInnerHTML?: {
|
|
64
|
+
__html: string;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* The loaded React component function ready to render.
|
|
69
|
+
*
|
|
70
|
+
* This is the default export from the component module, lazy-loaded via the component's
|
|
71
|
+
* import function. The component is ready to be rendered with React's `createRoot().render()`.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* const { Component, props, domNode } = await prepareComponent(component);
|
|
76
|
+
*
|
|
77
|
+
* // Component is the default export from the component file:
|
|
78
|
+
* // export default function CounterComponent({ max_count }: { max_count: number }) {
|
|
79
|
+
* // return <div>Count: {max_count}</div>;
|
|
80
|
+
* // }
|
|
81
|
+
*
|
|
82
|
+
* createRoot(domNode).render(<Component {...props} />);
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
Component: (props: any) => React.ReactElement;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Prepares a client-side component for hydration by locating its DOM container, extracting
|
|
89
|
+
* props and children from server-rendered HTML attributes, and lazy-loading the component module.
|
|
90
|
+
*
|
|
91
|
+
* This function bridges server-rendered HTML (from ZX's Zig transpiler) and client-side React
|
|
92
|
+
* components. It reads data attributes (`data-props`, `data-children`) from the DOM element
|
|
93
|
+
* with the component's unique ID, then lazy-loads the component module for rendering.
|
|
94
|
+
*
|
|
95
|
+
* @param component - The component metadata containing ID, import function, and other metadata
|
|
96
|
+
* needed to locate and load the component
|
|
97
|
+
*
|
|
98
|
+
* @returns A Promise that resolves to a `PreparedComponent` object containing the DOM node,
|
|
99
|
+
* parsed props, and the loaded React component function
|
|
100
|
+
*
|
|
101
|
+
* @throws {Error} If the component's container element cannot be found in the DOM. This typically
|
|
102
|
+
* happens if the component ID doesn't match any element, the script runs before
|
|
103
|
+
* the HTML is loaded, or there's a mismatch between server and client metadata
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```tsx
|
|
107
|
+
* // Basic usage with React:
|
|
108
|
+
* import { createRoot } from "react-dom/client";
|
|
109
|
+
* import { prepareComponent } from "ziex";
|
|
110
|
+
* import { components } from "@ziex/components";
|
|
111
|
+
*
|
|
112
|
+
* for (const component of components) {
|
|
113
|
+
* prepareComponent(component).then(({ domNode, Component, props }) => {
|
|
114
|
+
* createRoot(domNode).render(<Component {...props} />);
|
|
115
|
+
* }).catch(console.error);
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```tsx
|
|
121
|
+
* // With async/await:
|
|
122
|
+
* async function hydrateComponent(component: ComponentMetadata) {
|
|
123
|
+
* try {
|
|
124
|
+
* const { domNode, Component, props } = await prepareComponent(component);
|
|
125
|
+
* createRoot(domNode).render(<Component {...props} />);
|
|
126
|
+
* } catch (error) {
|
|
127
|
+
* console.error(`Failed to hydrate ${component.name}:`, error);
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
*
|
|
131
|
+
* Promise.all(components.map(hydrateComponent));
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare function prepareComponent(component: ComponentMetadata): Promise<PreparedComponent>;
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
type
|
|
3
|
-
name: string;
|
|
4
|
-
path: string;
|
|
5
|
-
id: string;
|
|
6
|
-
import: () => Promise<(props: unknown) => React.ReactElement>;
|
|
7
|
-
};
|
|
8
|
-
export {};
|
|
1
|
+
export type { ComponentMetadata } from "./types";
|
|
2
|
+
export { prepareComponent, type PreparedComponent } from "./dom";
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
async function S(G){let k=document.getElementById(G.id);if(!k)throw Error(`Root element ${G.id} not found`);let J=JSON.parse(k.getAttribute("data-props")||"{}"),K=k.getAttribute("data-children")??void 0;if(K)J.dangerouslySetInnerHTML={__html:K};let Q=await G.import();return{domNode:k,props:J,Component:Q}}export{S as prepareComponent};
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ziex",
|
|
3
|
+
"version": "0.0.1-dev.2",
|
|
3
4
|
"description": "ZX is a framework for building web applications with Zig.",
|
|
4
5
|
"main": "index.js",
|
|
5
6
|
"type": "module",
|
|
6
|
-
"version": "0.0.1-dev.0",
|
|
7
7
|
"homepage": "https://github.com/nurulhudaapon/zx",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata for a client-side component used within a ZX file.
|
|
3
|
+
*
|
|
4
|
+
* This type represents the metadata for components that are marked with the `@rendering` attribute
|
|
5
|
+
* in ZX files. When a component is declared with `@rendering={.csr}` or `@rendering={.csz}` in a
|
|
6
|
+
* `.zx` file, the ZX transpiler generates a `ComponentMetadata` entry that is included in the
|
|
7
|
+
* generated `components` array.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* // In a ZX file (page.zx):
|
|
12
|
+
* <CounterComponent @rendering={.csr} max_count={10} />
|
|
13
|
+
*
|
|
14
|
+
* // Generated components array (components.ts):
|
|
15
|
+
* export const components: ComponentMetadata[] = [
|
|
16
|
+
* {
|
|
17
|
+
* name: "CounterComponent",
|
|
18
|
+
* path: "./components/CounterComponent.tsx",
|
|
19
|
+
* id: "zx-dcde04c415da9d1b15ca2690d8b497ae",
|
|
20
|
+
* type: "csr",
|
|
21
|
+
* import: () => import('./components/CounterComponent.tsx')
|
|
22
|
+
* }
|
|
23
|
+
* ];
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* // Using ComponentMetadata with prepareComponent:
|
|
29
|
+
* import { prepareComponent, type ComponentMetadata } from "ziex";
|
|
30
|
+
*
|
|
31
|
+
* for (const component of components) {
|
|
32
|
+
* const { domNode, props, Component } = await prepareComponent(component);
|
|
33
|
+
* // Render component to DOM node
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export type ComponentMetadata = {
|
|
38
|
+
/**
|
|
39
|
+
* The name of the component as declared in the ZX file.
|
|
40
|
+
*
|
|
41
|
+
* This is the tag name used in JSX syntax within `.zx` files. It corresponds to the component
|
|
42
|
+
* identifier used in the component declaration (e.g., `<CounterComponent />`).
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```tsx
|
|
46
|
+
* // In ZX file:
|
|
47
|
+
* <CounterComponent @rendering={.csr} />
|
|
48
|
+
*
|
|
49
|
+
* // name will be: "CounterComponent"
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
name: string;
|
|
53
|
+
/**
|
|
54
|
+
* The file path to the component module.
|
|
55
|
+
*
|
|
56
|
+
* This is the relative or absolute path to the component file that will be dynamically imported
|
|
57
|
+
* at runtime. For CSR components, this typically points to a `.tsx` or `.jsx` file. For CSZ
|
|
58
|
+
* components, this points to a Zig component file.
|
|
59
|
+
*
|
|
60
|
+
* The path is determined from the `@jsImport` directive in the ZX file, or defaults to
|
|
61
|
+
* `./{componentName}.tsx` if not explicitly specified.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```tsx
|
|
65
|
+
* // In ZX file:
|
|
66
|
+
* const CounterComponent = @jsImport("components/Counter.tsx");
|
|
67
|
+
* <CounterComponent @rendering={.csr} />
|
|
68
|
+
*
|
|
69
|
+
* // path will be: "components/Counter.tsx"
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* // Without explicit @jsImport:
|
|
75
|
+
* <MyComponent @rendering={.csr} />
|
|
76
|
+
*
|
|
77
|
+
* // path will default to: "./MyComponent.tsx"
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
path: string;
|
|
81
|
+
/**
|
|
82
|
+
* A unique HTML element identifier for the component's root DOM node.
|
|
83
|
+
*
|
|
84
|
+
* This ID is generated by hashing the component's path and name using MD5, then formatting it
|
|
85
|
+
* as a hex string with the "zx-" prefix. The ID is used to locate the component's container
|
|
86
|
+
* element in the DOM during client-side hydration.
|
|
87
|
+
*
|
|
88
|
+
* The ID format is: `zx-{32 hex characters}` (e.g., `zx-dcde04c415da9d1b15ca2690d8b497ae`)
|
|
89
|
+
*
|
|
90
|
+
* When the same component is used multiple times on a page, each instance gets the same base ID
|
|
91
|
+
* since they share the same path and name. The ZX runtime uses this ID along with `data-props`
|
|
92
|
+
* and `data-children` attributes to hydrate the component with the correct props and children.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```tsx
|
|
96
|
+
* // Component metadata:
|
|
97
|
+
* {
|
|
98
|
+
* name: "CounterComponent",
|
|
99
|
+
* path: "./components/Counter.tsx",
|
|
100
|
+
* id: "zx-dcde04c415da9d1b15ca2690d8b497ae"
|
|
101
|
+
* }
|
|
102
|
+
*
|
|
103
|
+
* // Generated HTML:
|
|
104
|
+
* <div id="zx-dcde04c415da9d1b15ca2690d8b497ae"
|
|
105
|
+
* data-props='{"max_count":10}'
|
|
106
|
+
* data-children="...">
|
|
107
|
+
* <!-- Server-rendered content -->
|
|
108
|
+
* </div>
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
id: string;
|
|
112
|
+
/**
|
|
113
|
+
* The rendering type of the component, determining how it will be rendered on the client.
|
|
114
|
+
*
|
|
115
|
+
* - **"csr"** (Client Side React): The component is a React component that will be rendered
|
|
116
|
+
* using React's client-side rendering. The component file should export a default React
|
|
117
|
+
* component function. This is the most common type for interactive UI components.
|
|
118
|
+
*
|
|
119
|
+
* - **"csz"** (Client Side Zig): The component is a Zig component that will be compiled to
|
|
120
|
+
* WebAssembly and rendered on the client side. This allows you to use Zig's performance
|
|
121
|
+
* and type safety for client-side components.
|
|
122
|
+
*
|
|
123
|
+
* The type is determined by the `@rendering` attribute value in the ZX file (`.csr` or `.csz`).
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```tsx
|
|
127
|
+
* // CSR component (React):
|
|
128
|
+
* <CounterComponent @rendering={.csr} max_count={10} />
|
|
129
|
+
* // type: "csr"
|
|
130
|
+
*
|
|
131
|
+
* // Component file (CounterComponent.tsx):
|
|
132
|
+
* export default function CounterComponent({ max_count }: { max_count: number }) {
|
|
133
|
+
* return <div>Count: {max_count}</div>;
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```tsx
|
|
139
|
+
* // CSZ component (Zig/WASM):
|
|
140
|
+
* <CounterComponent @rendering={.csz} />
|
|
141
|
+
* // type: "csz"
|
|
142
|
+
*
|
|
143
|
+
* // Component file (CounterComponent.zig):
|
|
144
|
+
* pub fn CounterComponent(allocator: zx.Allocator) zx.Component {
|
|
145
|
+
* return (<div @allocator={allocator}>Counter</div>);
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
type: "csr" | "csz";
|
|
150
|
+
/**
|
|
151
|
+
* A lazy-loading function that dynamically imports the component module.
|
|
152
|
+
*
|
|
153
|
+
* This function returns a Promise that resolves to the component function. It enables
|
|
154
|
+
* code-splitting and lazy loading of components, improving initial page load performance
|
|
155
|
+
* by only loading components when they are needed.
|
|
156
|
+
*
|
|
157
|
+
* For CSR components, the imported module should export a default React component.
|
|
158
|
+
* For CSZ components, the import mechanism depends on the WASM module structure.
|
|
159
|
+
*
|
|
160
|
+
* The function is called during client-side hydration to load and render the component
|
|
161
|
+
* into its corresponding DOM container element.
|
|
162
|
+
*
|
|
163
|
+
* @returns A Promise that resolves to a component function that accepts props and returns a React element.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```tsx
|
|
167
|
+
* // Component metadata:
|
|
168
|
+
* {
|
|
169
|
+
* import: () => import('./components/CounterComponent.tsx')
|
|
170
|
+
* }
|
|
171
|
+
*
|
|
172
|
+
* // Usage:
|
|
173
|
+
* const Component = await component.import();
|
|
174
|
+
* // Component is now the default export from CounterComponent.tsx
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```tsx
|
|
179
|
+
* // With prepareComponent helper:
|
|
180
|
+
* import { prepareComponent } from "ziex";
|
|
181
|
+
*
|
|
182
|
+
* const { Component, props, domNode } = await prepareComponent(component);
|
|
183
|
+
* // Component is loaded and ready to render with props
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
import: () => Promise<(props: any) => React.ReactElement>;
|
|
187
|
+
};
|