cajoa 0.5.0 → 0.7.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 CHANGED
@@ -1,215 +1,223 @@
1
- # Cajoa
2
-
3
- A small, beginner-friendly frontend framework built on **signals** and **JSX**.
4
- No virtual DOM, no hooks rules, no re-rendering components.
5
-
6
- > ⚠️ **v0.5 (early stage).** The API may still change. Not production-ready yet.
7
-
8
- ## Why Cajoa?
9
-
10
- - **Components run once.** A component is just a function that builds DOM. Only the parts that depend on a signal update.
11
- - **Plain JSX.** Write `<button onClick={...}>`, no special syntax to learn.
12
- - **Tiny and readable.** The whole core is small enough to read in one sitting.
13
- - **TypeScript-first**, ESM + CommonJS, zero runtime dependencies.
14
-
15
- ## Install
16
-
17
- ```bash
18
- npm create cajoa@latest my-app
19
- ```
20
-
21
- ## Setup (JSX)
22
-
23
- In your `tsconfig.json`:
24
-
25
- ```json
26
- {
27
- "compilerOptions": {
28
- "jsx": "react-jsx",
29
- "jsxImportSource": "cajoa"
30
- }
31
- }
32
- ```
33
-
34
- If you use Vite and your JSX is not transformed, also set it in `vite.config.ts`
35
- (`oxc: { jsx: { runtime: "automatic", importSource: "cajoa" } }` on newer Vite,
36
- or `esbuild: { jsx: "automatic", jsxImportSource: "cajoa" }` on older ones).
37
-
38
- ## Quick start
39
-
40
- ```tsx
41
- import { createSignal, render } from "cajoa";
42
-
43
- function Counter() {
44
- const [count, setCount] = createSignal(0);
45
-
46
- return (
47
- <button onClick={() => setCount((c) => c + 1)}>
48
- Clicked {count} times
49
- </button>
50
- );
51
- }
52
-
53
- render(Counter, document.getElementById("app")!);
54
- ```
55
-
56
- ## Two rules to remember
57
-
58
- Cajoa has no compiler, so JSX expressions run exactly once. That gives two rules:
59
-
60
- 1. **Pass the signal, don't call it.**
61
- `{count}` is reactive. `{count()}` is read once and never updates.
62
- For computed values, pass a function: `{() => count() * 2}`.
63
- 2. **`Show` children are a function.**
64
- `<Show when={on}>{() => <p>Hello</p>}</Show>`
65
- Otherwise the element is created even when it is hidden.
66
-
67
- ## Lists and conditions
68
-
69
- ```tsx
70
- import { createSignal, Show, For } from "cajoa";
71
-
72
- const [todos, setTodos] = createSignal([{ id: 1, text: "Learn Cajoa" }]);
73
-
74
- <Show when={() => todos().length > 0} fallback={() => <p>Nothing to do.</p>}>
75
- {() => (
76
- <ul>
77
- <For each={todos}>{(todo) => <li>{todo.text}</li>}</For>
78
- </ul>
79
- )}
80
- </Show>;
81
- ```
82
-
83
- ## Routing
84
-
85
- ```tsx
86
- import { render, Router, Route, Link, useParams } from "cajoa";
87
-
88
- const User = () => <h1>User {useParams().id}</h1>;
89
-
90
- render(
91
- () => (
92
- <div>
93
- <nav>
94
- <Link href="/">Home</Link> <Link href="/users/7">User 7</Link>
95
- </nav>
96
- <Router>
97
- <Route path="/" component={() => <h1>Home</h1>} />
98
- <Route path="/users/:id" component={User} />
99
- <Route path="*" component={() => <h1>Not found</h1>} />
100
- </Router>
101
- </div>
102
- ),
103
- document.getElementById("app")!,
104
- );
105
- ```
106
-
107
- Routes are matched in order, so put `path="*"` last.
108
-
109
- ## API
110
-
111
- ### Reactivity
112
-
113
- | Function | What it does |
114
- | --------------------- | ---------------------------------------------------------------------------------------------------------------------- |
115
- | `createSignal(value)` | Returns `[get, set]`. Reading `get()` inside an effect subscribes to it. `set` accepts a value or an updater function. |
116
- | `createEffect(fn)` | Runs `fn` now and again whenever a signal it read changes. |
117
- | `createMemo(fn)` | Cached computed value. Only notifies when the result changes. |
118
- | `batch(fn)` | Groups several `set` calls into one update. |
119
-
120
- ### Components
121
-
122
- | Component | What it does |
123
- | ---------------------- | ---------------------------------------------------------------------------------------------------- |
124
- | `<Show when fallback>` | Conditional rendering. `children` and `fallback` are functions. |
125
- | `<For each key?>` | List rendering. Reuses DOM nodes for unchanged items. The child is a function that returns a `Node`. |
126
-
127
- ### Rendering
128
-
129
- | Function | What it does |
130
- | ------------------------------ | ------------------------------------------------- |
131
- | `render(component, container)` | Mounts a component. Returns a `dispose` function. |
132
- | `h(tag, props, ...children)` | Low-level element creator. JSX compiles to this. |
133
-
134
- ### Lifecycle
135
-
136
- | Function | What it does |
137
- | ---------------- | ----------------------------------------------------------------- |
138
- | `onMount(fn)` | Runs `fn` after the component is rendered. |
139
- | `onCleanup(fn)` | Runs `fn` before an effect re-runs or when its owner is disposed. |
140
- | `createRoot(fn)` | Creates a top-level owner you can dispose manually. |
141
-
142
- ### Context
143
-
144
- | Function | What it does |
145
- | ------------------------- | ------------------------------------------------------ |
146
- | `createContext(default)` | Creates a context with a default value. |
147
- | `provide(ctx, value, fn)` | Runs `fn`; everything created inside can read `value`. |
148
- | `useContext(ctx)` | Reads the nearest provided value (or the default). |
149
-
150
- ### Router
151
-
152
- | Function | What it does |
153
- | ---------------------------- | ---------------------------------------------------------- |
154
- | `<Router>` | Renders the first `<Route>` that matches the current path. |
155
- | `<Route path component>` | Maps a path (`/users/:id`, `*`) to a component. |
156
- | `<Link href>` | An `<a>` that navigates without reloading the page. |
157
- | `navigate(to, { replace? })` | Navigates programmatically. |
158
- | `useParams()` | Reads the route params (`{ id: "7" }`). |
159
- | `usePath()` | Returns a signal getter for the current pathname. |
160
- | `untrack(fn)` | Runs `fn` without subscribing to signals. |
161
-
162
- ### Renderer
163
-
164
- | Function | What it does |
165
- | ----------------------- | ------------------------------------------------------------------ |
166
- | `setRenderer(renderer)` | Swaps the renderer (default: DOM). Used to target other platforms. |
167
- | `getRenderer()` | Returns the current renderer. |
168
- | `domRenderer` | The built-in DOM renderer. |
169
-
170
- ## Good to know
171
-
172
- - `For` uses the item itself as the key by default. If you pass `key`, items with the same key are **not** re-rendered when their data changes, so keys must be unique and the item should hold its own signals if it needs to update.
173
- - `useContext` only works while a component is being created (or inside `onMount` / effects). Read it at the top of your component and use the value later in handlers.
174
- - Updating an object in place will not trigger effects. Set a new object instead.
175
- - Fragments (`<>...</>`) work anywhere, but inside `<For>` the child must be a single element, because `For` reuses that node.
176
-
177
- ## Known limitations
178
-
179
- - No SSR.
180
- - Router: pathname only (no query/hash handling), no nested routes, no `base` path.
181
- - `<Show>` and `<Router>` return a function, so they must sit inside an element, not be the root returned by `render`.
182
- - Single-page apps need a server fallback to `index.html` for unknown paths.
183
- - `createMemo` can show a brief inconsistent state when memos depend on each other.
184
- - `set` calls are not batched automatically inside event handlers. Wrap them in `batch()`.
185
- - The Router and event names (`onClick`) are still web-specific for now.
186
-
187
- ## Roadmap
188
-
189
- - [x] Signals and effects
190
- - [x] DOM bindings and `render`
191
- - [x] `Show`, `For` (keyed)
192
- - [x] Lifecycle and disposal
193
- - [x] `createMemo`, `batch`
194
- - [x] Context
195
- - [x] JSX support
196
- - [x] Fragments
197
- - [x] Router
198
- - [x] Renderer abstraction (core no longer depends on DOM)
199
- - [ ] Split into packages ('@cajoa/core', '@cajoa/dom')
200
- - [ ] Vite plugin (removes the two rules above)
201
- - [ ] Native renderer (mobile, desktop)
202
- - [ ] SSR
203
-
204
- ## Development
205
-
206
- ```bash
207
- npm install
208
- npm test # run tests
209
- npm run dev # open the playground
210
- npm run build # build to dist/
211
- ```
212
-
213
- ## License
214
-
215
- [Apache-2.0](./LICENSE)
1
+ # Cajoa
2
+
3
+ A small, beginner-friendly frontend framework built on **signals** and **JSX**.
4
+ No virtual DOM, no hooks rules, no re-rendering components.
5
+
6
+ > ⚠️ **v0.6 (early stage).** The API may still change. Not production-ready yet.
7
+
8
+ ## Why Cajoa?
9
+
10
+ - **Components run once.** A component is just a function that builds DOM. Only the parts that depend on a signal update.
11
+ - **Plain JSX.** Write `<button onClick={...}>`, no special syntax to learn.
12
+ - **Tiny and readable.** The whole core is small enough to read in one sitting.
13
+ - **TypeScript-first**, ESM + CommonJS, zero runtime dependencies.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm create cajoa@latest my-app
19
+ ```
20
+
21
+ ## Setup (JSX)
22
+
23
+ In your `tsconfig.json`:
24
+
25
+ ```json
26
+ {
27
+ "compilerOptions": {
28
+ "jsx": "react-jsx",
29
+ "jsxImportSource": "cajoa"
30
+ }
31
+ }
32
+ ```
33
+
34
+ If you use Vite and your JSX is not transformed, also set it in `vite.config.ts`
35
+ (`oxc: { jsx: { runtime: "automatic", importSource: "cajoa" } }` on newer Vite,
36
+ or `esbuild: { jsx: "automatic", jsxImportSource: "cajoa" }` on older ones).
37
+
38
+ ## Quick start
39
+
40
+ ```tsx
41
+ import { createSignal, render } from "cajoa";
42
+
43
+ function Counter() {
44
+ const [count, setCount] = createSignal(0);
45
+
46
+ return (
47
+ <button onClick={() => setCount((c) => c + 1)}>
48
+ Clicked {count} times
49
+ </button>
50
+ );
51
+ }
52
+
53
+ render(Counter, document.getElementById("app")!);
54
+ ```
55
+
56
+ ## Two rules to remember
57
+
58
+ Cajoa has no compiler, so JSX expressions run exactly once. That gives two rules:
59
+
60
+ 1. **Pass the signal, don't call it.**
61
+ `{count}` is reactive. `{count()}` is read once and never updates.
62
+ For computed values, pass a function: `{() => count() * 2}`.
63
+ 2. **`Show` children are a function.**
64
+ `<Show when={on}>{() => <p>Hello</p>}</Show>`
65
+ Otherwise the element is created even when it is hidden.
66
+
67
+ ## Lists and conditions
68
+
69
+ ```tsx
70
+ import { createSignal, Show, For } from "cajoa";
71
+
72
+ const [todos, setTodos] = createSignal([{ id: 1, text: "Learn Cajoa" }]);
73
+
74
+ <Show when={() => todos().length > 0} fallback={() => <p>Nothing to do.</p>}>
75
+ {() => (
76
+ <ul>
77
+ <For each={todos}>{(todo) => <li>{todo.text}</li>}</For>
78
+ </ul>
79
+ )}
80
+ </Show>;
81
+ ```
82
+
83
+ ## Routing
84
+
85
+ ```tsx
86
+ import { render, Router, Route, Link, useParams } from "cajoa";
87
+
88
+ const User = () => <h1>User {useParams().id}</h1>;
89
+
90
+ render(
91
+ () => (
92
+ <div>
93
+ <nav>
94
+ <Link href="/">Home</Link> <Link href="/users/7">User 7</Link>
95
+ </nav>
96
+ <Router>
97
+ <Route path="/" component={() => <h1>Home</h1>} />
98
+ <Route path="/users/:id" component={User} />
99
+ <Route path="*" component={() => <h1>Not found</h1>} />
100
+ </Router>
101
+ </div>
102
+ ),
103
+ document.getElementById("app")!,
104
+ );
105
+ ```
106
+
107
+ Routes are matched in order, so put `path="*"` last.
108
+
109
+ ## API
110
+
111
+ ### Reactivity
112
+
113
+ | Function | What it does |
114
+ | --------------------- | ---------------------------------------------------------------------------------------------------------------------- |
115
+ | `createSignal(value)` | Returns `[get, set]`. Reading `get()` inside an effect subscribes to it. `set` accepts a value or an updater function. |
116
+ | `createEffect(fn)` | Runs `fn` now and again whenever a signal it read changes. |
117
+ | `createMemo(fn)` | Cached computed value. Only notifies when the result changes. |
118
+ | `batch(fn)` | Groups several `set` calls into one update. |
119
+
120
+ ### Components
121
+
122
+ | Component | What it does |
123
+ | ---------------------- | ---------------------------------------------------------------------------------------------------- |
124
+ | `<Show when fallback>` | Conditional rendering. `children` and `fallback` are functions. |
125
+ | `<For each key?>` | List rendering. Reuses DOM nodes for unchanged items. The child is a function that returns a `Node`. |
126
+
127
+ ### Rendering
128
+
129
+ | Function | What it does |
130
+ | ------------------------------ | ------------------------------------------------- |
131
+ | `render(component, container)` | Mounts a component. Returns a `dispose` function. |
132
+ | `h(tag, props, ...children)` | Low-level element creator. JSX compiles to this. |
133
+
134
+ ### Lifecycle
135
+
136
+ | Function | What it does |
137
+ | ---------------- | ----------------------------------------------------------------- |
138
+ | `onMount(fn)` | Runs `fn` after the component is rendered. |
139
+ | `onCleanup(fn)` | Runs `fn` before an effect re-runs or when its owner is disposed. |
140
+ | `createRoot(fn)` | Creates a top-level owner you can dispose manually. |
141
+
142
+ ### Context
143
+
144
+ | Function | What it does |
145
+ | ------------------------- | ------------------------------------------------------ |
146
+ | `createContext(default)` | Creates a context with a default value. |
147
+ | `provide(ctx, value, fn)` | Runs `fn`; everything created inside can read `value`. |
148
+ | `useContext(ctx)` | Reads the nearest provided value (or the default). |
149
+
150
+ ### Router
151
+
152
+ | Function | What it does |
153
+ | ---------------------------- | ---------------------------------------------------------- |
154
+ | `<Router>` | Renders the first `<Route>` that matches the current path. |
155
+ | `<Route path component>` | Maps a path (`/users/:id`, `*`) to a component. |
156
+ | `<Link href>` | An `<a>` that navigates without reloading the page. |
157
+ | `navigate(to, { replace? })` | Navigates programmatically. |
158
+ | `useParams()` | Reads the route params (`{ id: "7" }`). |
159
+ | `usePath()` | Returns a signal getter for the current pathname. |
160
+ | `untrack(fn)` | Runs `fn` without subscribing to signals. |
161
+
162
+ ### Renderer
163
+
164
+ | Function | What it does |
165
+ | ----------------------- | ------------------------------------------------------------------ |
166
+ | `setRenderer(renderer)` | Swaps the renderer (default: DOM). Used to target other platforms. |
167
+ | `getRenderer()` | Returns the current renderer. |
168
+ | `domRenderer` | The built-in DOM renderer. |
169
+
170
+ ## Packages
171
+
172
+ | Package | What it is |
173
+ | ------------- | -------------------------------------------------------------------- |
174
+ | `cajoa` | The package most apps install (core + DOM) |
175
+ | `@cajoa/core` | Renderer-agnostic core: signals, JSX runtime, `Show`, `For`, context |
176
+ | `@cajoa/dom` | DOM renderer and router |
177
+
178
+ ## Good to know
179
+
180
+ - `For` uses the item itself as the key by default. If you pass `key`, items with the same key are **not** re-rendered when their data changes, so keys must be unique and the item should hold its own signals if it needs to update.
181
+ - `useContext` only works while a component is being created (or inside `onMount` / effects). Read it at the top of your component and use the value later in handlers.
182
+ - Updating an object in place will not trigger effects. Set a new object instead.
183
+ - Fragments (`<>...</>`) work anywhere, but inside `<For>` the child must be a single element, because `For` reuses that node.
184
+
185
+ ## Known limitations
186
+
187
+ - No SSR.
188
+ - Router: pathname only (no query/hash handling), no nested routes, no `base` path.
189
+ - `<Show>` and `<Router>` return a function, so they must sit inside an element, not be the root returned by `render`.
190
+ - Single-page apps need a server fallback to `index.html` for unknown paths.
191
+ - `createMemo` can show a brief inconsistent state when memos depend on each other.
192
+ - `set` calls are not batched automatically inside event handlers. Wrap them in `batch()`.
193
+ - The Router and event names (`onClick`) are still web-specific for now.
194
+
195
+ ## Roadmap
196
+
197
+ - [x] Signals and effects
198
+ - [x] DOM bindings and `render`
199
+ - [x] `Show`, `For` (keyed)
200
+ - [x] Lifecycle and disposal
201
+ - [x] `createMemo`, `batch`
202
+ - [x] Context
203
+ - [x] JSX support
204
+ - [x] Fragments
205
+ - [x] Router
206
+ - [x] Renderer abstraction (core no longer depends on DOM)
207
+ - [ ] Split into packages ('@cajoa/core', '@cajoa/dom')
208
+ - [ ] Vite plugin (removes the two rules above)
209
+ - [ ] Native renderer (mobile, desktop)
210
+ - [ ] SSR
211
+
212
+ ## Development
213
+
214
+ ```bash
215
+ npm install
216
+ npm test # run tests
217
+ npm run dev # open the playground
218
+ npm run build # build to dist/
219
+ ```
220
+
221
+ ## License
222
+
223
+ [Apache-2.0](./LICENSE)