ajo 0.1.31 → 0.1.32

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/LLMs.md CHANGED
@@ -1,329 +1,349 @@
1
- # Ajo LLM Instructions
2
-
3
- Ajo is a micro UI library using JSX and generators. **No React imports**: JSX compiles to Ajo via build config.
4
-
5
- ## Stateless Component
6
-
7
- ```tsx
8
- import type { Stateless, WithChildren } from 'ajo'
9
- import clsx from 'clsx' // optional, for conditional classes
10
-
11
- type Args = WithChildren<{ title: string; active?: boolean }>
12
-
13
- const Card: Stateless<Args> = ({ title, active, children }) => ( // can destructure args here
14
- <div class={clsx('card', { active })}>
15
- <h3>{title}</h3>
16
- {children}
17
- </div>
18
- )
19
-
20
- // Example usage - everything goes to args:
21
- <Card title="Hello" active>
22
- <p>This is a card.</p>
23
- </Card>
24
- ```
25
-
26
- ## Stateful Component
27
-
28
- ```tsx
29
- import type { Stateful } from 'ajo'
30
-
31
- type Args = { initial: number; step?: number }
32
-
33
- const Counter: Stateful<Args, 'section'> = function* (args) { // do NOT destructure args here
34
-
35
- // before loop: persistent state & handlers
36
-
37
- let count = args.initial
38
- let inputRef: HTMLInputElement | null = null
39
-
40
- const inc = () => this.next(({ step = 1 }) => count += step)
41
- const dec = () => this.next(() => count--)
42
-
43
- const handleKeydown = (e: KeyboardEvent) => {
44
- if (e.key === 'ArrowUp') inc()
45
- else if (e.key === 'ArrowDown' && count > 0) dec()
46
- }
47
-
48
- this.addEventListener('keydown', handleKeydown, { signal: this.signal }) // auto-cleanup on unmount
49
-
50
- while (true) { // main render loop
51
-
52
- try { // optional: error boundary
53
-
54
- // fresh destructure each render
55
- const { step = 1 } = args
56
-
57
- // derived values
58
- const isEven = count % 2 === 0
59
-
60
- yield (
61
- <>
62
- <input
63
- ref={el => inputRef = el}
64
- value={count}
65
- set:oninput={e => this.next(() => count = +(e.target as HTMLInputElement).value)}
66
- />
67
- <button set:onclick={inc}>+{step}</button>
68
- <button set:onclick={dec} disabled={count <= 0}>-</button>
69
- <p memo={isEven}>Even: {isEven ? 'yes' : 'no'}</p>
70
- <footer memo>Static content - rendered once</footer>
71
- <div skip>{/* third-party managed DOM here */}</div>
72
- </>
73
- )
74
- } catch (err: unknown) {
75
- yield <p class="error">{err instanceof Error ? err.message : String(err)}</p>
76
- }
77
- }
78
- }
79
-
80
- Counter.is = 'section' // wrapper element (default: div)
81
- Counter.attrs = { class: 'counter-wrap' } // default wrapper attributes
82
- Counter.args = { step: 1 } // default args
83
-
84
- // Example usage - special attrs apply to wrapper, rest goes to args:
85
-
86
- let ref: ThisParameterType<typeof Counter> | null = null
87
-
88
- <Counter
89
- initial={0} step={5} // → args
90
- attr:id="main" attr:class="my-counter" // → wrapper attributes (HTML attrs)
91
- set:onclick={fn} // → wrapper properties (DOM props)
92
- key={id} // → wrapper key
93
- memo={[id]} // → wrapper memo (array)
94
- memo={id} // → wrapper memo (single value)
95
- memo // → wrapper memo (render once)
96
- ref={el => ref = el} // → wrapper ref (el is <section> + .next()/.throw()/.return())
97
- />
98
-
99
- ref?.next() // trigger re-render from outside
100
- ```
101
-
102
- ## Rules
103
-
104
- | Topic | Rule |
105
- |-------|------|
106
- | **Elements** | Everything becomes HTML attributes. `set:prop` assigns DOM properties instead (`node[prop] = value`) |
107
- | **Stateless** | Everything goes to `args`. Special attrs like `memo` must be applied to elements inside |
108
- | **Stateful** | `key`, `memo`, `skip`, `ref`, `set:*` apply to implicit wrapper element. `attr:*` sets wrapper attributes. Rest goes to `args` |
109
- | **Events** | `set:onclick`, `set:oninput`, etc. Never `onClick` |
110
- | **Classes** | `class`, never `className`. Must be string, no object/array syntax. Use `clsx()` or template literals |
111
- | **Styles** | `style` must be string (`style="color: red"`), not object. No special handling |
112
- | **Args** | Never destructure in generator signature. Use `args` param |
113
- | **Root JSX** | Use `<>...</>` in stateful to avoid double wrapper |
114
- | **Re-render** | `this.next(fn?)` returns `fn`'s result. Use `this.throw(e)` for explicit error routing |
115
- | **Context** | `context<T>(fallback)` creates context. Stateless: read only. Stateful: read/write inside `while` loop |
116
- | **Lists** | Always provide unique `key` on elements |
117
- | **Refs** | `ref={el => ...}` on elements. Receives `null` on unmount. Stateful ref type: `ThisParameterType<typeof Component>` |
118
- | **Memo** | `memo={[deps]}` array, `memo={value}` single, or just `memo` (never re-render). Skips subtree if unchanged |
119
- | **Skip** | `skip` excludes children from reconciliation. Use for `set:textContent`/`set:innerHTML` or third-party maneged DOM |
120
- | **Custom wrapper** | Set `.is = 'tagname'` AND TypeScript generic `Stateful<Args, 'tagname'>` for stateful components. Default is `div` (no need to set) |
121
- | **Default attrs** | `.attrs = { class: '...' }` on stateful component generator function |
122
- | **Default args** | `.args = { prop: value }` on stateful component generator function |
123
- | **Cleanup** | `this.signal` for APIs that accept AbortSignal (fetch, addEventListener). `try/finally` for the rest |
124
- | **Error recovery** | `try { ... } catch { yield error UI }` inside loop |
125
- | **this** | Stateful wrapper element with `.signal`, `.next(fn?)` (returns `fn`'s result), `.throw()`, `.return()`. Type: `ThisParameterType<typeof Component>` |
126
-
127
- ## Common Patterns
128
-
129
- ```tsx
130
- import type { Stateful, Stateless, WithChildren } from 'ajo'
131
- import { context } from 'ajo/context'
132
-
133
- // Async data loading
134
- type LoaderArgs = { url: string }
135
-
136
- const DataLoader: Stateful<LoaderArgs> = function* (args) {
137
-
138
- let data: unknown = null
139
- let error: Error | null = null
140
-
141
- fetch(args.url, { signal: this.signal })
142
- .then(r => r.json())
143
- .then(d => this.next(() => data = d))
144
- .catch(e => this.next(() => error = e))
145
-
146
- while (true) yield (
147
- <>
148
- {error ? <p>Error: {error.message}</p>
149
- : data ? <div>{JSON.stringify(data)}</div>
150
- : <p>Loading...</p>}
151
- </>
152
- )
153
- }
154
-
155
- // List with keys
156
- type Item = { id: string; text: string }
157
-
158
- const List: Stateless<{ items: Item[] }> = ({ items }) => (
159
- <ul>
160
- {items.map(item => <li key={item.id}>{item.text}</li>)}
161
- </ul>
162
- )
163
-
164
- // Conditional rendering
165
- type ShowArgs = WithChildren<{ when: boolean }>
166
-
167
- const Show: Stateless<ShowArgs> = ({ when, children }) => when ? children : null
168
-
169
- // Context - create with fallback value
170
- const ThemeContext = context<'light' | 'dark'>('light')
171
- const UserContext = context<{ name: string } | null>(null)
172
-
173
- // Stateless - read only (call without args)
174
- const ThemedCard: Stateless<{ title: string }> = ({ title }) => {
175
- const theme = ThemeContext() // reads current value
176
- return <div class={`card theme-${theme}`}>{title}</div>
177
- }
178
-
179
- // Stateful - read/write inside while loop
180
- const ThemeProvider: Stateful<WithChildren> = function* (args) {
181
-
182
- let theme: 'light' | 'dark' = 'light'
183
-
184
- const toggle = () => this.next(() => theme = theme === 'light' ? 'dark' : 'light')
185
-
186
- while (true) {
187
-
188
- ThemeContext(theme) // write: sets value for descendants
189
- const user = UserContext() // read: gets value from ancestor
190
-
191
- yield (
192
- <>
193
- <button set:onclick={toggle}>Theme: {theme}</button>
194
- {user && <span>User: {user.name}</span>}
195
- {args.children}
196
- </>
197
- )
198
- }
199
- }
200
-
201
- // Ref typing for stateful components
202
- let counterRef: ThisParameterType<typeof Counter> | null = null
203
- <Counter ref={el => counterRef = el} initial={0} />
204
- // counterRef is <section> element + .next(), .throw(), .return() methods
205
- counterRef?.next() // trigger re-render from outside
206
-
207
- // memo variations
208
- <div memo={[a, b]}>{/* re-render when a or b changes */}</div>
209
- <div memo={count}>{/* re-render when count changes (single value) */}</div>
210
- <div memo>{/* render once, never update - good for static content */}</div>
211
-
212
- // Boolean attributes
213
- <input type="checkbox" checked disabled /> // checked="" disabled=""
214
- <button disabled={false} /> // removes disabled attr
215
-
216
- // Attributes vs Properties (HTML-first)
217
- <input value="text" /> // HTML attribute: initial value only
218
- <input set:value={text} /> // DOM property: syncs with state
219
- <input type="checkbox" checked /> // HTML attribute: initial state
220
- <input type="checkbox" set:checked={bool} /> // DOM property: syncs with state
221
- <video set:currentTime={0} set:muted /> // DOM properties
222
- <div set:textContent={str} skip /> // DOM property + skip (required!)
223
- <div set:innerHTML={html} skip /> // DOM property + skip (required!)
224
-
225
- // Post-render work (DOM is updated by the time the microtask runs)
226
- const ScrollList: Stateful<{ items: Item[] }> = function* (args) {
227
- let container: HTMLUListElement | null = null
228
- while (true) {
229
- queueMicrotask(() => container!.scrollTop = container!.scrollHeight)
230
- yield (
231
- <ul ref={el => container = el}>
232
- {args.items.map(item => <li key={item.id}>{item.text}</li>)}
233
- </ul>
234
- )
235
- }
236
- }
237
-
238
- // Third-party managed DOM
239
- let map: MapLibrary | null = null
240
- <div skip ref={el => el ? (map ??= new MapLibrary(el)) : map?.destroy()} /> // skip lets library control children
241
- ```
242
-
243
- ## Anti-patterns
244
-
245
- ```tsx
246
- // React patterns: NEVER use
247
- import React from 'react'
248
- className="..."
249
- onClick={...}
250
- useState, useEffect, useCallback
251
-
252
- // class/style as object or array: no special handling in Ajo
253
- <div class={{ active: isActive }} /> // won't work
254
- <div class={['btn', 'primary']} /> // won't work
255
- <div style={{ color: 'red' }} /> // won't work
256
-
257
- // ✅ class/style must be strings
258
- <div class={`btn ${isActive ? 'active' : ''}`} />
259
- <div class={clsx('btn', { active: isActive })} /> // use clsx library
260
- <div style="color: red; font-size: 14px" />
261
- <div style={`color: ${color}`} />
262
-
263
- // ❌ memo in args but not applied to element - does nothing
264
- const Bad: Stateless<{ memo: unknown }> = ({ memo }) => (
265
- <div>content</div> // memo arg ignored, no memoization
266
- )
267
-
268
- // ✅ Pass deps in args, apply to root element
269
- const Good: Stateless<{ deps?: unknown }> = ({ deps }) => (
270
- <div memo={deps}>content</div> // memoized when deps provided
271
- )
272
- // <Good deps={[id]} /> or <Good deps={data} />
273
-
274
- // ❌ Destructure in signature - locks to initial values
275
- function* Bad({ count }) { ... }
276
-
277
- // Context read/write outside loop in stateful - stale values
278
- function* Bad(args) {
279
- const theme = ThemeContext() // frozen at mount
280
- ThemeContext('dark') // only set once, not updated
281
- while (true) yield ...
282
- }
283
-
284
- // Context read/write inside loop in stateful
285
- function* Good(args) {
286
- let theme = 'light'
287
- while (true) {
288
- ThemeContext(theme) // write: updated each render
289
- const user = UserContext() // read: fresh value each render
290
- yield ...
291
- }
292
- }
293
-
294
- // Missing key in lists
295
- {items.map(item => <li>{item}</li>)}
296
-
297
- // ❌ Direct state mutation without next()
298
- const inc = () => count++ // won't re-render
299
-
300
- // set:textContent/innerHTML without skip: content gets cleared
301
- <div set:innerHTML={html} />
302
-
303
- // Correct
304
- const inc = () => this.next(() => count++)
305
- <div set:innerHTML={html} skip />
306
- ```
307
-
308
- ## Setup
309
-
310
- ```bash
311
- npm install ajo
312
- pnpm add ajo
313
- yarn add ajo
314
- ```
315
-
316
- Configure JSX factory (Vite example):
317
-
318
- ```ts
319
- // vite.config.ts
320
- export default defineConfig({
321
- esbuild: {
322
- jsxFactory: 'h',
323
- jsxFragment: 'Fragment',
324
- jsxInject: `import { h, Fragment } from 'ajo'`,
325
- },
326
- })
327
- ```
328
-
329
- For other build systems: `jsxFactory: 'h'`, `jsxFragment: 'Fragment'`, auto-import `{ h, Fragment }` from `'ajo'`.
1
+ # Ajo LLM Instructions
2
+
3
+ Ajo is a micro UI library using JSX and generators. **No React imports**: JSX compiles to Ajo via build config.
4
+
5
+ ## Stateless Component
6
+
7
+ ```tsx
8
+ import type { Stateless, WithChildren } from 'ajo'
9
+ import clsx from 'clsx' // optional, for conditional classes
10
+
11
+ type Args = WithChildren<{ title: string; active?: boolean }>
12
+
13
+ const Card: Stateless<Args> = ({ title, active, children }) => (
14
+ <div class={clsx('card', { active })}>
15
+ <h3>{title}</h3>
16
+ {children}
17
+ </div>
18
+ )
19
+
20
+ // Example usage - everything goes to args:
21
+ <Card title="Hello" active>
22
+ <p>This is a card.</p>
23
+ </Card>
24
+ ```
25
+
26
+ ## Stateful Component
27
+
28
+ ```tsx
29
+ import type { Stateful } from 'ajo'
30
+
31
+ type Args = { initial: number; step?: number }
32
+
33
+ const Counter: Stateful<Args, 'section'> = function* ({ initial }) {
34
+
35
+ // before loop: init state & handlers (use parameter for initial args)
36
+
37
+ let count = initial
38
+ let inputRef: HTMLInputElement | null = null
39
+
40
+ const inc = () => this.next(({ step = 1 }) => count += step)
41
+ const dec = () => this.next(() => count--)
42
+
43
+ const handleKeydown = (e: KeyboardEvent) => {
44
+ if (e.key === 'ArrowUp') inc()
45
+ else if (e.key === 'ArrowDown' && count > 0) dec()
46
+ }
47
+
48
+ this.addEventListener('keydown', handleKeydown, { signal: this.signal }) // auto-cleanup on lifecycle end
49
+
50
+ for (const { step = 1 } of this) { // main render loop — `for...of this` yields fresh args each cycle
51
+
52
+ try { // optional: error boundary
53
+
54
+ // derived values
55
+ const isEven = count % 2 === 0
56
+
57
+ yield (
58
+ <>
59
+ <input
60
+ ref={el => inputRef = el}
61
+ value={count}
62
+ set:oninput={e => this.next(() => count = +(e.target as HTMLInputElement).value)}
63
+ />
64
+ <button set:onclick={inc}>+{step}</button>
65
+ <button set:onclick={dec} disabled={count <= 0}>-</button>
66
+ <p memo={isEven}>Even: {isEven ? 'yes' : 'no'}</p>
67
+ <footer memo>Static content - rendered once</footer>
68
+ <div skip>{/* third-party managed DOM here */}</div>
69
+ </>
70
+ )
71
+ } catch (err: unknown) {
72
+ yield <p class="error">{err instanceof Error ? err.message : String(err)}</p>
73
+ }
74
+ }
75
+ }
76
+
77
+ Counter.is = 'section' // wrapper element (default: div)
78
+ Counter.attrs = { class: 'counter-wrap' } // default wrapper attributes
79
+ Counter.args = { step: 1 } // default args
80
+
81
+ // Or use stateful() to avoid duplicating 'section':
82
+ // const Counter = stateful(function* ({ initial }: Args) { ... }, 'section')
83
+ // Counter.attrs = { class: 'counter-wrap' }
84
+ // Counter.args = { step: 1 }
85
+
86
+ // Example usage - special attrs apply to wrapper, rest goes to args:
87
+
88
+ let ref: ThisParameterType<typeof Counter> | null = null
89
+
90
+ <Counter
91
+ initial={0} step={5} // → args
92
+ attr:id="main" attr:class="my-counter" // → wrapper attributes (HTML attrs)
93
+ set:onclick={fn} // → wrapper properties (DOM props)
94
+ key={id} // → wrapper key
95
+ memo={[id]} // → wrapper memo (array)
96
+ memo={id} // → wrapper memo (single value)
97
+ memo // → wrapper memo (render once)
98
+ ref={el => ref = el} // → wrapper ref (el is <section> + .next()/.throw()/.return())
99
+ />
100
+
101
+ ref?.next() // trigger re-render from outside
102
+ ```
103
+
104
+ ## Rules
105
+
106
+ | Topic | Rule |
107
+ |-------|------|
108
+ | **Elements** | Everything becomes HTML attributes. `set:prop` assigns DOM properties instead (`node[prop] = value`) |
109
+ | **Stateless** | Everything goes to `args`. Special attrs like `memo` must be applied to elements inside |
110
+ | **Stateful** | `key`, `memo`, `skip`, `ref`, `set:*` apply to implicit wrapper element. `attr:*` sets wrapper attributes. Rest goes to `args` |
111
+ | **Events** | `set:onclick`, `set:oninput`, etc. Never `onClick` |
112
+ | **Classes** | `class`, never `className`. Must be string, no object/array syntax. Use `clsx()` or template literals |
113
+ | **Styles** | `style` must be string (`style="color: red"`), not object. No special handling |
114
+ | **Args** | Destructure in parameter for init code. Render loops: `for (const { prop } of this)` for fresh args each cycle (new bindings), or `for ({ prop } of this)` without `const` to reassign variables from the outer scope. `while (true)` when you don't need fresh args |
115
+ | **Generators** | Stateful components are JS generators with a main render loop (`while`/`for...of`). Since they're generators, yields can also appear outside the loop — sequential phases, conditional blocks, etc. are all valid |
116
+ | **Root JSX** | Use `<>...</>` in stateful to avoid double wrapper |
117
+ | **Re-render** | `this.next(fn?)` returns `fn`'s result. Safe after unmount (no-op). Use `this.throw(e)` for explicit error routing |
118
+ | **Context** | `context<T>(fallback)` creates context. Stateless: read only. Stateful: read/write. Reads go inside loop (fresh each render). Constant writes can go before loop; dynamic writes go inside loop |
119
+ | **Lists** | Always provide unique `key` on elements |
120
+ | **Refs** | `ref={el => ...}` on elements. Receives `null` on unmount. Stateful ref type: `ThisParameterType<typeof Component>` |
121
+ | **Memo** | `memo={[deps]}` array, `memo={value}` single, or just `memo` (never re-render). Skips subtree if unchanged |
122
+ | **Skip** | `skip` excludes children from reconciliation. Use for `set:textContent`/`set:innerHTML` or third-party managed DOM |
123
+ | **Custom wrapper** | `stateful(fn, 'tagname')` sets `.is` and infers `this` type. Or manually: `Stateful<Args, 'tagname'>` + `.is = 'tagname'`. Default is `div` (no need to set) |
124
+ | **Default attrs** | `.attrs = { class: '...' }` on stateful component generator function |
125
+ | **Default args** | `.args = { prop: value }` on stateful component generator function |
126
+ | **Cleanup** | `this.signal` aborts on lifecycle end (unmount, `this.return()`, generator done). Use for fetch, addEventListener, etc. `try/finally` for the rest |
127
+ | **Error recovery** | `try { ... } catch { yield error UI }` inside loop |
128
+ | **this** | Stateful wrapper element with `.signal`, `.next(fn?)` (returns `fn`'s result), `.throw()`, `.return(deep?)`. Iterable: `for...of this` yields args. Type: `ThisParameterType<typeof Component>` |
129
+
130
+ ## Common Patterns
131
+
132
+ ```tsx
133
+ import type { Stateful, Stateless, WithChildren } from 'ajo'
134
+ import { stateful } from 'ajo'
135
+ import { context } from 'ajo/context'
136
+
137
+ // Async data loading
138
+ type LoaderArgs = { url: string }
139
+
140
+ const DataLoader: Stateful<LoaderArgs> = function* ({ url }) {
141
+
142
+ let data: unknown = null
143
+ let error: Error | null = null
144
+
145
+ fetch(url, { signal: this.signal }) // signal auto-aborts on lifecycle end
146
+ .then(r => r.json())
147
+ .then(d => this.next(() => data = d)) // no-op if unmounted
148
+ .catch(e => this.next(() => error = e)) // no-op if unmounted
149
+
150
+ while (true) yield (
151
+ <>
152
+ {error ? <p>Error: {error.message}</p>
153
+ : data ? <div>{JSON.stringify(data)}</div>
154
+ : <p>Loading...</p>}
155
+ </>
156
+ )
157
+ }
158
+
159
+ // List with keys
160
+ type Item = { id: string; text: string }
161
+
162
+ const List: Stateless<{ items: Item[] }> = ({ items }) => (
163
+ <ul>
164
+ {items.map(item => <li key={item.id}>{item.text}</li>)}
165
+ </ul>
166
+ )
167
+
168
+ // Conditional rendering
169
+ type ShowArgs = WithChildren<{ when: boolean }>
170
+
171
+ const Show: Stateless<ShowArgs> = ({ when, children }) => when ? children : null
172
+
173
+ // Context - create with fallback value
174
+ const ThemeContext = context<'light' | 'dark'>('light')
175
+ const UserContext = context<{ name: string } | null>(null)
176
+
177
+ // Stateless - read only (call without args)
178
+ const ThemedCard: Stateless<{ title: string }> = ({ title }) => {
179
+ const theme = ThemeContext() // reads current value
180
+ return <div class={`card theme-${theme}`}>{title}</div>
181
+ }
182
+
183
+ // Stateful - read/write inside render loop
184
+ const ThemeProvider: Stateful<WithChildren> = function* () {
185
+
186
+ let theme: 'light' | 'dark' = 'light'
187
+
188
+ const toggle = () => this.next(() => theme = theme === 'light' ? 'dark' : 'light')
189
+
190
+ for (const { children } of this) {
191
+
192
+ ThemeContext(theme) // write: sets value for descendants
193
+ const user = UserContext() // read: gets value from ancestor
194
+
195
+ yield (
196
+ <>
197
+ <button set:onclick={toggle}>Theme: {theme}</button>
198
+ {user && <span>User: {user.name}</span>}
199
+ {children}
200
+ </>
201
+ )
202
+ }
203
+ }
204
+
205
+ // Ref typing for stateful components
206
+ let counterRef: ThisParameterType<typeof Counter> | null = null
207
+ <Counter ref={el => counterRef = el} initial={0} />
208
+ // counterRef is <section> element + .next(), .throw(), .return() methods
209
+ counterRef?.next() // trigger re-render from outside
210
+
211
+ // memo variations
212
+ <div memo={[a, b]}>{/* re-render when a or b changes */}</div>
213
+ <div memo={count}>{/* re-render when count changes (single value) */}</div>
214
+ <div memo>{/* render once, never update - good for static content */}</div>
215
+
216
+ // Boolean attributes
217
+ <input type="checkbox" checked disabled /> // checked="" disabled=""
218
+ <button disabled={false} /> // removes disabled attr
219
+
220
+ // Attributes vs Properties (HTML-first)
221
+ <input value="text" /> // HTML attribute: initial value only
222
+ <input set:value={text} /> // DOM property: syncs with state
223
+ <input type="checkbox" checked /> // HTML attribute: initial state
224
+ <input type="checkbox" set:checked={bool} /> // DOM property: syncs with state
225
+ <video set:currentTime={0} set:muted /> // DOM properties
226
+ <div set:textContent={str} skip /> // DOM property + skip (required!)
227
+ <div set:innerHTML={html} skip /> // DOM property + skip (required!)
228
+
229
+ // Post-render work (DOM is updated by the time the microtask runs)
230
+ const ScrollList: Stateful<{ items: Item[] }> = function* () {
231
+ let container: HTMLUListElement | null = null
232
+ for (const { items } of this) {
233
+ queueMicrotask(() => container!.scrollTop = container!.scrollHeight)
234
+ yield (
235
+ <ul ref={el => container = el}>
236
+ {items.map(item => <li key={item.id}>{item.text}</li>)}
237
+ </ul>
238
+ )
239
+ }
240
+ }
241
+
242
+ // Reassign parameter vars in render loop (no const/let — updates outer scope)
243
+ const Greeter: Stateful<{ name: string }> = function* ({ name }) {
244
+ // name is available for init code here
245
+ for ({ name } of this) { // reassigns `name` from parameter each cycle
246
+ yield <p>Hello, {name}!</p>
247
+ }
248
+ }
249
+
250
+ // Third-party managed DOM
251
+ let map: MapLibrary | null = null
252
+ <div skip ref={el => el ? (map ??= new MapLibrary(el)) : map?.destroy()} /> // skip lets library control children
253
+ ```
254
+
255
+ ## Anti-patterns
256
+
257
+ ```tsx
258
+ // React patterns: NEVER use
259
+ import React from 'react'
260
+ className="..."
261
+ onClick={...}
262
+ useState, useEffect, useCallback
263
+
264
+ // class/style as object or array: no special handling in Ajo
265
+ <div class={{ active: isActive }} /> // won't work
266
+ <div class={['btn', 'primary']} /> // won't work
267
+ <div style={{ color: 'red' }} /> // won't work
268
+
269
+ // class/style must be strings
270
+ <div class={`btn ${isActive ? 'active' : ''}`} />
271
+ <div class={clsx('btn', { active: isActive })} /> // use clsx library
272
+ <div style="color: red; font-size: 14px" />
273
+ <div style={`color: ${color}`} />
274
+
275
+ // memo in args but not applied to element - does nothing
276
+ const Bad: Stateless<{ memo: unknown }> = ({ memo }) => (
277
+ <div>content</div> // memo arg ignored, no memoization
278
+ )
279
+
280
+ // Pass deps in args, apply to root element
281
+ const Good: Stateless<{ deps?: unknown }> = ({ deps }) => (
282
+ <div memo={deps}>content</div> // memoized when deps provided
283
+ )
284
+ // <Good deps={[id]} /> or <Good deps={data} />
285
+
286
+ // Context read outside loop - stale values
287
+ function* Bad() {
288
+ const theme = ThemeContext() // frozen at mount, never updated
289
+ while (true) yield ...
290
+ }
291
+
292
+ // ✅ Context read inside loop - fresh each render
293
+ function* Good() {
294
+ while (true) {
295
+ const user = UserContext() // read: fresh value each render
296
+ yield ...
297
+ }
298
+ }
299
+
300
+ // Constant context write outside loop - value never changes, OK
301
+ function* Good() {
302
+ ThemeContext('dark') // write once: constant value
303
+ while (true) yield ...
304
+ }
305
+
306
+ // ✅ Dynamic context write inside loop - value depends on state
307
+ function* Good() {
308
+ let theme = 'light'
309
+ while (true) {
310
+ ThemeContext(theme) // write: updated each render
311
+ yield ...
312
+ }
313
+ }
314
+
315
+ // ❌ Missing key in lists
316
+ {items.map(item => <li>{item}</li>)}
317
+
318
+ // ❌ Direct state mutation without next()
319
+ const inc = () => count++ // won't re-render
320
+
321
+ // ❌ set:textContent/innerHTML without skip: content gets cleared
322
+ <div set:innerHTML={html} />
323
+
324
+ // Correct
325
+ const inc = () => this.next(() => count++)
326
+ <div set:innerHTML={html} skip />
327
+ ```
328
+
329
+ ## Setup
330
+
331
+ ```bash
332
+ npm install ajo
333
+ pnpm add ajo
334
+ yarn add ajo
335
+ ```
336
+
337
+ Configure JSX automatic runtime (Vite example):
338
+
339
+ ```ts
340
+ // vite.config.ts
341
+ export default defineConfig({
342
+ esbuild: {
343
+ jsx: 'automatic',
344
+ jsxImportSource: 'ajo',
345
+ },
346
+ })
347
+ ```
348
+
349
+ For other build systems: `jsx: 'react-jsx'` (or `'automatic'`), `jsxImportSource: 'ajo'`. No manual imports needed — the build tool auto-imports from `ajo/jsx-runtime`.