@relicloops/cathode 2.0.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/LICENSE +201 -0
- package/README.md +390 -0
- package/index.js +9 -0
- package/jsx-dev-runtime.js +1 -0
- package/jsx-runtime.js +20 -0
- package/lib/css.js +40 -0
- package/lib/eject.js +3 -0
- package/lib/error-boundary.js +10 -0
- package/lib/inject.js +27 -0
- package/lib/lazy-helpers.js +98 -0
- package/lib/lazy.js +96 -0
- package/lib/mount.js +17 -0
- package/lib/runtime.js +188 -0
- package/lib/suspense.js +40 -0
- package/package.json +35 -0
- package/types/index.d.ts +21 -0
- package/types/jsx-dev-runtime.d.ts +1 -0
- package/types/jsx-runtime.d.ts +17 -0
- package/types/lib/css.d.ts +4 -0
- package/types/lib/eject.d.ts +1 -0
- package/types/lib/error-boundary.d.ts +6 -0
- package/types/lib/inject.d.ts +5 -0
- package/types/lib/lazy-helpers.d.ts +61 -0
- package/types/lib/lazy.d.ts +20 -0
- package/types/lib/mount.d.ts +7 -0
- package/types/lib/runtime.d.ts +10 -0
- package/types/lib/suspense.d.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
# Cathode
|
|
2
|
+
|
|
3
|
+
Formerly **NeonJSX** -- renamed to align with the Cathode -> Phosphor ecosystem.
|
|
4
|
+
|
|
5
|
+
Cathode is a tiny JSX runtime with a straightforward render pipeline. It turns
|
|
6
|
+
JSX into lightweight virtual nodes, then renders them to real DOM nodes with a
|
|
7
|
+
single pass. No diffing, no reconciliation, no hooks, just the essentials.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
- Small, dependency-free runtime
|
|
11
|
+
- Classic JSX pragma support (`h` / `Fragment`)
|
|
12
|
+
- Simple DOM renderer (`render`)
|
|
13
|
+
- CSS loading with browser cache optimization (`css`)
|
|
14
|
+
- Dynamic component loading (`lazy` / `Suspense`)
|
|
15
|
+
- TypeScript-friendly, ESM-first
|
|
16
|
+
|
|
17
|
+
## Compile TSX/JSX (your app)
|
|
18
|
+
Cathode is a runtime only. You bring the compiler, and it should use the classic
|
|
19
|
+
JSX transform with `h` and `Fragment`.
|
|
20
|
+
|
|
21
|
+
## Complete example (esbuild)
|
|
22
|
+
A full, minimal app you can copy into a new directory.
|
|
23
|
+
|
|
24
|
+
Project layout:
|
|
25
|
+
```
|
|
26
|
+
cathode-example/
|
|
27
|
+
package.json
|
|
28
|
+
tsconfig.json
|
|
29
|
+
src/
|
|
30
|
+
index.tsx
|
|
31
|
+
public/
|
|
32
|
+
index.html
|
|
33
|
+
app.css
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`package.json`:
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"name": "cathode-example",
|
|
40
|
+
"private": true,
|
|
41
|
+
"type": "module",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "esbuild src/index.tsx --bundle --format=esm --platform=browser --jsx=transform --jsx-factory=h --jsx-fragment=Fragment --outfile=public/app.js"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@relicloops/cathode": "^2.0.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"esbuild": "^0.27.2"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`tsconfig.json` (editor types only):
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"compilerOptions": {
|
|
58
|
+
"target": "ES2020",
|
|
59
|
+
"module": "ESNext",
|
|
60
|
+
"moduleResolution": "bundler",
|
|
61
|
+
"jsx": "react-jsx",
|
|
62
|
+
"jsxImportSource": "@relicloops/cathode",
|
|
63
|
+
"strict": true
|
|
64
|
+
},
|
|
65
|
+
"include": ["src"]
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`src/index.tsx`:
|
|
70
|
+
```ts
|
|
71
|
+
import { render, css } from '@relicloops/cathode';
|
|
72
|
+
|
|
73
|
+
const App = () => {
|
|
74
|
+
css('./app.css'); // URL-based, browser cached
|
|
75
|
+
css('.highlight { color: blue; }', { inline: true }); // Inline CSS
|
|
76
|
+
return (
|
|
77
|
+
<main>
|
|
78
|
+
<h1>Cathode</h1>
|
|
79
|
+
<p className="highlight">Runtime-only JSX, compiled by your toolchain.</p>
|
|
80
|
+
</main>
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
render(<App />, document.getElementById('root')!);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`public/index.html`:
|
|
88
|
+
```html
|
|
89
|
+
<!doctype html>
|
|
90
|
+
<html lang="en">
|
|
91
|
+
<head>
|
|
92
|
+
<meta charset="utf-8" />
|
|
93
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
94
|
+
<title>Cathode Example</title>
|
|
95
|
+
</head>
|
|
96
|
+
<body>
|
|
97
|
+
<div id="root"></div>
|
|
98
|
+
<script type="module" src="./app.js"></script>
|
|
99
|
+
</body>
|
|
100
|
+
</html>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`public/app.css`:
|
|
104
|
+
```css
|
|
105
|
+
* {
|
|
106
|
+
margin: 0;
|
|
107
|
+
padding: 0;
|
|
108
|
+
box-sizing: border-box;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
body {
|
|
112
|
+
font-family: system-ui, sans-serif;
|
|
113
|
+
background: #1a1a2e;
|
|
114
|
+
color: #eee;
|
|
115
|
+
min-height: 100vh;
|
|
116
|
+
display: flex;
|
|
117
|
+
align-items: center;
|
|
118
|
+
justify-content: center;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
main {
|
|
122
|
+
text-align: center;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
h1 {
|
|
126
|
+
font-size: 2.5rem;
|
|
127
|
+
margin-bottom: 0.5rem;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Run it:
|
|
132
|
+
```bash
|
|
133
|
+
npm install
|
|
134
|
+
npm run build
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
For a complete example with lazy loading, Suspense, trigger-based loading patterns, and animated spinners, see [docs/README.md](docs/README.md).
|
|
138
|
+
|
|
139
|
+
To download only the example:
|
|
140
|
+
```bash
|
|
141
|
+
git clone --depth 1 --filter=blob:none --sparse https://github.com/relicloops/cathode.git
|
|
142
|
+
cd cathode
|
|
143
|
+
git sparse-checkout set docs
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## API
|
|
147
|
+
|
|
148
|
+
### `h(type, props, ...children)`
|
|
149
|
+
Creates a virtual node. This is the JSX factory function.
|
|
150
|
+
|
|
151
|
+
### `Fragment`
|
|
152
|
+
Collects children without adding an extra DOM element.
|
|
153
|
+
|
|
154
|
+
### `render(node, parent)`
|
|
155
|
+
Initial mount only. Clears the parent and appends the rendered DOM tree.
|
|
156
|
+
Use this when you bootstrap the app at first page load and you want the
|
|
157
|
+
root container to represent the whole application.
|
|
158
|
+
|
|
159
|
+
Why keep `render()`?
|
|
160
|
+
- It is synchronous and simple, which is ideal for the first mount.
|
|
161
|
+
- It communicates that this is the primary app root, not a dynamic insert.
|
|
162
|
+
- It keeps existing examples and mental models for app startup intact.
|
|
163
|
+
|
|
164
|
+
For dynamic updates inside existing DOM, use `inject()` or `mount()`.
|
|
165
|
+
|
|
166
|
+
### `mount(node, parent)`
|
|
167
|
+
Async root mount with an explicit lifecycle handle. `mount()` is the
|
|
168
|
+
modern, explicit alternative to `render()` when you want control over
|
|
169
|
+
cleanup and future lifecycle capabilities.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
const root = await mount( <App />, document.getElementById( 'root' )! );
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
What it does today:
|
|
176
|
+
- Performs a `render()` into the container (replace mode).
|
|
177
|
+
- Returns a handle with `unmount()` and `cleanup()` methods.
|
|
178
|
+
- Tracks that the container is a mounted root.
|
|
179
|
+
|
|
180
|
+
Why use it:
|
|
181
|
+
- Clear intent: "this is a mounted root I may later tear down".
|
|
182
|
+
- Safer teardown: you can unmount predictably, even across routes or hot reloads.
|
|
183
|
+
- Forward compatible: the handle gives us space to add updates or teardown hooks.
|
|
184
|
+
|
|
185
|
+
### `unmount(parent)`
|
|
186
|
+
Async teardown of a mounted root container.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
await unmount( document.getElementById( 'root' )! );
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Current behavior:
|
|
193
|
+
- Clears the container.
|
|
194
|
+
- Removes the container from the internal mounted registry.
|
|
195
|
+
|
|
196
|
+
### `cleanup(parent)`
|
|
197
|
+
Async cleanup alias for `unmount()`. This is the semantic "end of lifecycle"
|
|
198
|
+
operation you can call when you want to ensure the container is empty and
|
|
199
|
+
the mount is discarded.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
await cleanup( document.getElementById( 'root' )! );
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Why both `unmount` and `cleanup`?
|
|
206
|
+
- `unmount()` is lifecycle terminology that matches other UI runtimes.
|
|
207
|
+
- `cleanup()` reads more like a finalization step, especially in async flows.
|
|
208
|
+
|
|
209
|
+
### `inject(node, parent, options?)`
|
|
210
|
+
Async injection for dynamic content inside an existing DOM container.
|
|
211
|
+
This is ideal for spinners, toasts, dialogs, or any UI that lives inside
|
|
212
|
+
an already-mounted part of the page.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
await inject( <Spinner />, contentEl, { mode: 'append' } );
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Modes:
|
|
219
|
+
- `replace` (default): same DOM behavior as `render()` but without implying
|
|
220
|
+
this is the root app mount.
|
|
221
|
+
- `append`: inserts the rendered nodes after the current children.
|
|
222
|
+
- `prepend`: inserts the rendered nodes before the current children.
|
|
223
|
+
|
|
224
|
+
Notes:
|
|
225
|
+
- `inject()` is async so the API can evolve (cleanup hooks, async rendering,
|
|
226
|
+
or future diffing) without breaking your call sites.
|
|
227
|
+
- Use `eject()` to remove injected content when you are done.
|
|
228
|
+
|
|
229
|
+
### `eject(parent)`
|
|
230
|
+
Async cleanup for injected content. Clears the parent container.
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
await eject( contentEl );
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### How the APIs fit together
|
|
237
|
+
Think of the DOM in two layers: root app mounts and local injections.
|
|
238
|
+
|
|
239
|
+
Root mount (app lifecycle):
|
|
240
|
+
- Use `render()` for the simplest initial boot.
|
|
241
|
+
- Use `mount()` if you want explicit teardown and a returned handle.
|
|
242
|
+
- Use `unmount()` or `cleanup()` to remove the root app.
|
|
243
|
+
|
|
244
|
+
Local injection (dynamic UI inside the app):
|
|
245
|
+
- Use `inject()` to place content inside an existing DOM container.
|
|
246
|
+
- Use `eject()` to clear that container.
|
|
247
|
+
|
|
248
|
+
This split keeps the intent clear:
|
|
249
|
+
- `render()`/`mount()` communicate "this is the app root".
|
|
250
|
+
- `inject()` communicates "this is a dynamic insertion".
|
|
251
|
+
|
|
252
|
+
### `css(urlOrContent, options?)`
|
|
253
|
+
Loads CSS on first component render with deduplication.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
css('/styles/button.css'); // URL-based (browser cached)
|
|
257
|
+
css('.btn { color: red; }', { inline: true }); // Inline CSS
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
- **URL mode**: Injects `<link rel="stylesheet">` into `<head>`
|
|
261
|
+
- **Inline mode**: Injects `<style>` into `<head>` (pass `{ inline: true }`)
|
|
262
|
+
- **Deduplication**: Same CSS is only loaded once across all components
|
|
263
|
+
- **Status attribute**: `data-cathode-css-status` = `loading` | `loaded` | `error`
|
|
264
|
+
- **SSR safe**: No-op when `document` is undefined
|
|
265
|
+
|
|
266
|
+
### `lazy(loader)`
|
|
267
|
+
Wraps a dynamic import for on-demand component loading. The component loads automatically when rendered.
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
import { lazy, Suspense, render } from '@relicloops/cathode';
|
|
271
|
+
|
|
272
|
+
const Dashboard = lazy(() => import('./Dashboard.js'));
|
|
273
|
+
|
|
274
|
+
const App = () => (
|
|
275
|
+
<Suspense fallback={<p>Loading...</p>}>
|
|
276
|
+
<Dashboard userId={123} />
|
|
277
|
+
</Suspense>
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
render(<App />, document.getElementById('root')!);
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
- **Caching**: Same loader returns the same component instance
|
|
284
|
+
- **Exports**: Accepts a default export, a direct function module, or a single named export
|
|
285
|
+
- **SSR safe**: No-op when `document` is undefined
|
|
286
|
+
- **Preloading**: Call `Dashboard.awake()` to preload before render
|
|
287
|
+
|
|
288
|
+
### `lazyOnDemand(loader)`
|
|
289
|
+
Like `lazy()`, but requires explicit triggering - the component won't load until you call `awake()`.
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
import { lazyOnDemand, Suspense } from '@relicloops/cathode';
|
|
293
|
+
|
|
294
|
+
const Settings = lazyOnDemand(() => import('./Settings.js'));
|
|
295
|
+
|
|
296
|
+
const App = () => (
|
|
297
|
+
<>
|
|
298
|
+
<button onClick={() => Settings.awake()}>Load Settings</button>
|
|
299
|
+
<Suspense fallback={<p>Loading...</p>}>
|
|
300
|
+
<Settings />
|
|
301
|
+
</Suspense>
|
|
302
|
+
</>
|
|
303
|
+
);
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Note: `__load()` is deprecated but still available for backward compatibility. Prefer `awake()`.
|
|
307
|
+
|
|
308
|
+
### Lazy Loading Helpers
|
|
309
|
+
|
|
310
|
+
**`lazyOnHover(component)`** - Load on mouse enter:
|
|
311
|
+
```ts
|
|
312
|
+
import { lazyOnDemand, lazyOnHover } from '@relicloops/cathode';
|
|
313
|
+
|
|
314
|
+
const Dashboard = lazyOnDemand(() => import('./Dashboard.js'));
|
|
315
|
+
const hoverProps = lazyOnHover(Dashboard);
|
|
316
|
+
|
|
317
|
+
<a href="/dashboard" {...hoverProps}>Dashboard</a>
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
**`lazyAfterDelay(component, ms)`** - Load after timeout:
|
|
321
|
+
```ts
|
|
322
|
+
import { lazyOnDemand, lazyAfterDelay } from '@relicloops/cathode';
|
|
323
|
+
|
|
324
|
+
const Analytics = lazyOnDemand(() => import('./Analytics.js'));
|
|
325
|
+
lazyAfterDelay(Analytics, 3000); // Load after 3 seconds
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
**`lazyWhenIdle(component, timeout?)`** - Load when browser is idle:
|
|
329
|
+
```ts
|
|
330
|
+
import { lazyOnDemand, lazyWhenIdle } from '@relicloops/cathode';
|
|
331
|
+
|
|
332
|
+
const ChatWidget = lazyOnDemand(() => import('./ChatWidget.js'));
|
|
333
|
+
lazyWhenIdle(ChatWidget); // Load during idle time
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
**`LazyOnVisible`** - Load when scrolled into view:
|
|
337
|
+
```ts
|
|
338
|
+
import { lazyOnDemand, LazyOnVisible } from '@relicloops/cathode';
|
|
339
|
+
|
|
340
|
+
const Footer = lazyOnDemand(() => import('./Footer.js'));
|
|
341
|
+
|
|
342
|
+
<LazyOnVisible
|
|
343
|
+
component={Footer}
|
|
344
|
+
fallback={<div style="height: 200px">Scroll to load footer</div>}
|
|
345
|
+
rootMargin="200px"
|
|
346
|
+
/>
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### `Suspense`
|
|
350
|
+
Shows a fallback while lazy components inside are loading. Fallback can be a VNode, string, or number.
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
354
|
+
<LazyComponent />
|
|
355
|
+
</Suspense>
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Multiple lazy components share the same fallback:
|
|
359
|
+
```ts
|
|
360
|
+
const Chart = lazy(() => import('./Chart.js'));
|
|
361
|
+
const Table = lazy(() => import('./Table.js'));
|
|
362
|
+
|
|
363
|
+
<Suspense fallback={<div class="skeleton" />}>
|
|
364
|
+
<Chart />
|
|
365
|
+
<Table />
|
|
366
|
+
</Suspense>
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### `ErrorBoundary`
|
|
370
|
+
Catches errors in children and displays a fallback.
|
|
371
|
+
|
|
372
|
+
```ts
|
|
373
|
+
import { ErrorBoundary } from '@relicloops/cathode';
|
|
374
|
+
|
|
375
|
+
<ErrorBoundary fallback={(error) => <p>Error: {error.message}</p>}>
|
|
376
|
+
<Suspense fallback={<p>Loading...</p>}>
|
|
377
|
+
<Dashboard />
|
|
378
|
+
</Suspense>
|
|
379
|
+
</ErrorBoundary>
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Types
|
|
383
|
+
- `ComponentType<P>`: Component function signature for typing props and returns.
|
|
384
|
+
|
|
385
|
+
## Notes
|
|
386
|
+
- This renderer clears the target container on each `render`.
|
|
387
|
+
- Event handlers use `onClick`, `onInput`, etc. (lowercased when assigned).
|
|
388
|
+
|
|
389
|
+
## License
|
|
390
|
+
Apache-2.0
|
package/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './lib/runtime.js';
|
|
2
|
+
export { inject } from './lib/inject.js';
|
|
3
|
+
export { eject } from './lib/eject.js';
|
|
4
|
+
export { mount, unmount, cleanup } from './lib/mount.js';
|
|
5
|
+
export { css } from './lib/css.js';
|
|
6
|
+
export { lazy, lazyOnDemand } from './lib/lazy.js';
|
|
7
|
+
export { lazyOnHover, lazyAfterDelay, lazyWhenIdle, LazyOnVisible } from './lib/lazy-helpers.js';
|
|
8
|
+
export { Suspense } from './lib/suspense.js';
|
|
9
|
+
export { ErrorBoundary } from './lib/error-boundary.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './jsx-runtime.js';
|
package/jsx-runtime.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { h, Fragment } from './lib/runtime.js';
|
|
2
|
+
export { Fragment };
|
|
3
|
+
function toChildArray(children) {
|
|
4
|
+
if (children === undefined || children === null) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
if (Array.isArray(children)) {
|
|
8
|
+
return children;
|
|
9
|
+
}
|
|
10
|
+
return [children];
|
|
11
|
+
}
|
|
12
|
+
export function jsx(type, props, key) {
|
|
13
|
+
const { children, ...rest } = props ?? {};
|
|
14
|
+
if (key !== undefined) {
|
|
15
|
+
rest.key = key;
|
|
16
|
+
}
|
|
17
|
+
return h(type, rest, ...toChildArray(children));
|
|
18
|
+
}
|
|
19
|
+
export const jsxs = jsx;
|
|
20
|
+
export const jsxDEV = jsx;
|
package/lib/css.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const loadedCSS = new Set();
|
|
2
|
+
function hashString(str) {
|
|
3
|
+
let hash = 0;
|
|
4
|
+
for (let i = 0; i < str.length; i++) {
|
|
5
|
+
hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0;
|
|
6
|
+
}
|
|
7
|
+
return hash.toString(36);
|
|
8
|
+
}
|
|
9
|
+
export function css(urlOrContent, options) {
|
|
10
|
+
const isInline = options?.inline ?? false;
|
|
11
|
+
const key = isInline ? `inline:${hashString(urlOrContent)}` : urlOrContent;
|
|
12
|
+
if (loadedCSS.has(key)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
loadedCSS.add(key);
|
|
16
|
+
if (typeof document === 'undefined') {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (isInline) {
|
|
20
|
+
const style = document.createElement('style');
|
|
21
|
+
style.setAttribute('data-cathode-css', key);
|
|
22
|
+
style.setAttribute('data-cathode-css-status', 'loaded');
|
|
23
|
+
style.textContent = urlOrContent;
|
|
24
|
+
document.head.appendChild(style);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const link = document.createElement('link');
|
|
28
|
+
link.rel = 'stylesheet';
|
|
29
|
+
link.href = urlOrContent;
|
|
30
|
+
link.setAttribute('data-cathode-css', key);
|
|
31
|
+
link.setAttribute('data-cathode-css-status', 'loading');
|
|
32
|
+
link.addEventListener('load', () => {
|
|
33
|
+
link.setAttribute('data-cathode-css-status', 'loaded');
|
|
34
|
+
}, { once: true });
|
|
35
|
+
link.addEventListener('error', () => {
|
|
36
|
+
link.setAttribute('data-cathode-css-status', 'error');
|
|
37
|
+
}, { once: true });
|
|
38
|
+
document.head.appendChild(link);
|
|
39
|
+
}
|
|
40
|
+
}
|
package/lib/eject.js
ADDED