@tltdsh/mx 1.0.10 → 1.1.1
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/AGENTS.md +121 -11
- package/CLAUDE.md +121 -11
- package/README.md +4 -2
- package/mx.d.ts +14 -2
- package/package.json +2 -2
package/AGENTS.md
CHANGED
|
@@ -2,17 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
Instructions for AI coding agents working with mx.js codebases.
|
|
4
4
|
|
|
5
|
+
**Full documentation: [mxjs.dev](https://mxjs.dev)** - API reference, patterns, gotchas, playground.
|
|
6
|
+
|
|
7
|
+
When transferring from React/Vue/Svelte, read [mxjs.dev/what-mx-is-not](https://mxjs.dev/what-mx-is-not) first. mx deliberately has no hooks, no JSX, no fragments, no portals, no memo, no synthetic events, no signals. That page lists every omission and what replaces it.
|
|
8
|
+
|
|
5
9
|
---
|
|
6
10
|
|
|
7
11
|
## Hard Rules
|
|
8
12
|
|
|
9
13
|
| Rule | Do | Don't |
|
|
10
14
|
|------|----|-------|
|
|
15
|
+
| Component definition | `$(props) {}` (method shorthand) | `$: function(props) {}` - legacy |
|
|
16
|
+
| Component name | `define('my-widget', {...})` kebab-case | `define('myWidget', {...})` - lookup silently fails |
|
|
17
|
+
| Call site | `mx.myWidget()` camelCase | `mx.MyWidget()` - PascalCase gives `-my-widget` (invalid) |
|
|
18
|
+
| State updates | `this.$({ key: value })` | `this.$state.key = value` - bypasses the merge, forbidden |
|
|
19
|
+
| Empty re-render | Pass actual changed data: `this.$({ key: newValue })` | `this.$({})` with empty object - code smell |
|
|
11
20
|
| Numbers in render | `mx.span(count)` | `mx.span(String(count))` - auto-casts |
|
|
12
21
|
| Event names | `onclick` (lowercase) | `onClick` - not React |
|
|
13
22
|
| Class attribute | `class: 'active'` | `className: 'active'` - not React |
|
|
14
23
|
| Label for | `for: 'input-id'` | `htmlFor: 'input-id'` - not React |
|
|
15
24
|
| Inline styles | `style: 'width:' + pct + '%'` | `style: { width: pct + '%' }` - not supported |
|
|
25
|
+
| Array children | `mx.ul(...items.map(i => mx.li(i)))` | `mx.ul(items.map(...))` - stringifies to `"1,2,3"` text |
|
|
16
26
|
| No attrs, just children | `mx.div(child1, child2)` | `mx.div({}, child)` or `mx.div(null, child)` |
|
|
17
27
|
| Boolean attributes | `disabled: true` sets `disabled=""` | Expecting it to set a JS property |
|
|
18
28
|
| First arg type | `mx.div({class: 'x'}, child)` | `mx.div(myMap, child)` - non-plain-Object treated as child |
|
|
@@ -22,6 +32,31 @@ Instructions for AI coding agents working with mx.js codebases.
|
|
|
22
32
|
## Common Mistakes
|
|
23
33
|
|
|
24
34
|
```js
|
|
35
|
+
// ✗ Legacy method syntax
|
|
36
|
+
define('foo', { $: function(props) { ... } });
|
|
37
|
+
// ✓ Method shorthand
|
|
38
|
+
define('foo', { $(props) { ... } });
|
|
39
|
+
|
|
40
|
+
// ✗ Mutating $state directly bypasses the declarative flow
|
|
41
|
+
this.$state.count = 5;
|
|
42
|
+
// ✓ Always go through $()
|
|
43
|
+
this.$({ count: 5 });
|
|
44
|
+
|
|
45
|
+
// ✗ Empty re-render with side-effect mutations - hidden data flow
|
|
46
|
+
$() {
|
|
47
|
+
this._items.push(newItem);
|
|
48
|
+
this.$({});
|
|
49
|
+
}
|
|
50
|
+
// ✓ Pass the actual change
|
|
51
|
+
$({ items = [] }) {
|
|
52
|
+
mx.button({ onclick: () => this.$({ items: [...items, newItem] }) }, 'Add');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ✗ Arrays of children stringified to text
|
|
56
|
+
mx.ul(items.map(i => mx.li(i.name)))
|
|
57
|
+
// ✓ Spread so each li is a child
|
|
58
|
+
mx.ul(...items.map(i => mx.li(i.name)))
|
|
59
|
+
|
|
25
60
|
// ✗ Mutates parent's array
|
|
26
61
|
assets.sort((a, b) => a.price - b.price);
|
|
27
62
|
// ✓ Copy first
|
|
@@ -46,14 +81,38 @@ mx.button({ onclick: handler })
|
|
|
46
81
|
items.length && mx.div('Has items')
|
|
47
82
|
// ✓ Coerce to boolean
|
|
48
83
|
!!items.length && mx.div('Has items')
|
|
84
|
+
|
|
85
|
+
// ✗ dom() with no attrs skips component init
|
|
86
|
+
let picker = dom.datePicker();
|
|
87
|
+
// ✓ Pass {} to trigger $()
|
|
88
|
+
let picker = dom.datePicker({});
|
|
89
|
+
|
|
90
|
+
// ✗ PascalCase produces leading hyphen (invalid tag)
|
|
91
|
+
mx.MyWidget()
|
|
92
|
+
// ✓ camelCase or string
|
|
93
|
+
mx.myWidget() // or mx('my-widget')
|
|
49
94
|
```
|
|
50
95
|
|
|
51
96
|
---
|
|
52
97
|
|
|
98
|
+
## Reserved Tag Names
|
|
99
|
+
|
|
100
|
+
The internal tag cache is a plain object; it inherits `Object.prototype` methods. Don't use any of these as component names:
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
toString, valueOf, constructor, hasOwnProperty, isPrototypeOf,
|
|
104
|
+
propertyIsEnumerable, toLocaleString, __defineGetter__,
|
|
105
|
+
__defineSetter__, __lookupGetter__, __lookupSetter__, __proto__
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`mx.toString()` produces a broken description that throws `InvalidCharacterError` at `createElement` time. Also applies to `define('toString', ...)`.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
53
112
|
## Checklist for New Components
|
|
54
113
|
|
|
55
|
-
1. `define('kebab-name', { $(
|
|
56
|
-
2. Destructure all props with defaults in `$()` signature
|
|
114
|
+
1. `define('kebab-name', { $(props) {} })` - kebab-case only in define()
|
|
115
|
+
2. Destructure all props with defaults in `$()` signature - no `??=` needed for primitives
|
|
57
116
|
3. Early return on empty state: `if (!data) return [];`
|
|
58
117
|
4. Use `this._name` for private caches (Maps, flags, intervals)
|
|
59
118
|
5. Use `this._name ||= new Map` for persistent references
|
|
@@ -67,6 +126,7 @@ items.length && mx.div('Has items')
|
|
|
67
126
|
13. `clearInterval(this._interval)` at top of `$()`
|
|
68
127
|
14. CSS transitions for animations, not JS
|
|
69
128
|
15. Formatters in render, not in data prep
|
|
129
|
+
16. Never write `this.$state.x = ...` directly - always `this.$({...})`
|
|
70
130
|
|
|
71
131
|
---
|
|
72
132
|
|
|
@@ -78,7 +138,21 @@ Real element to hold a reference? → dom.tag()
|
|
|
78
138
|
Need to call .$() on it later? → dom.tag()
|
|
79
139
|
```
|
|
80
140
|
|
|
81
|
-
|
|
141
|
+
### `$state` vs `this._property`
|
|
142
|
+
|
|
143
|
+
This is the single most common source of confusion for teams adopting mx. Pick the narrowest tier that holds the data's actual lifetime.
|
|
144
|
+
|
|
145
|
+
| | `this.$state.x` | `this._x` |
|
|
146
|
+
|---|---|---|
|
|
147
|
+
| **What it holds** | Accumulated props from the parent | Internal bookkeeping - tracking values, DOM refs, caches, flags, timers |
|
|
148
|
+
| **Who writes it** | The parent (via `el.$({...})`) and the component itself (via `this.$({...})`) | Only the component itself |
|
|
149
|
+
| **Merge behavior** | `Object.assign(this.$state, props)` runs before every `$()` call | Plain own property - set directly, no merge |
|
|
150
|
+
| **Read from outside** | OK, for aggregation (e.g. form submit reading `nameField.$state.value`) | Forbidden - it's private |
|
|
151
|
+
| **Write from outside** | Forbidden - always go through `comp.$({ key: newValue })` | Forbidden - it's private |
|
|
152
|
+
|
|
153
|
+
**Rule:** if a parent needs to see or set it, it's `$state`. Everything else is `this._`.
|
|
154
|
+
|
|
155
|
+
**Never** put internal bookkeeping (`_interval`, Maps, DOM refs) in `$state` - it pollutes the props namespace and risks collisions.
|
|
82
156
|
|
|
83
157
|
---
|
|
84
158
|
|
|
@@ -112,7 +186,11 @@ define('greeting', {
|
|
|
112
186
|
});
|
|
113
187
|
```
|
|
114
188
|
|
|
115
|
-
When parent calls `el.$({ count: 5 })`, count updates. When the component calls `this.$({ count: count + 1 })`, it self-updates. The destructuring default `= 0`
|
|
189
|
+
When parent calls `el.$({ count: 5 })`, count updates. When the component calls `this.$({ count: count + 1 })`, it self-updates. The destructuring default (`= 0`) applies whenever that key's value is `undefined` in `$state`.
|
|
190
|
+
|
|
191
|
+
### The parent always wins the merge
|
|
192
|
+
|
|
193
|
+
Because `Object.assign` runs **before** your `$` function, a parent re-passing the same prop will overwrite locally updated state. Usually that's what you want (controlled components). If you need state that survives parent re-passes, take the prop as a seed and store local state under a different key on `this._`.
|
|
116
194
|
|
|
117
195
|
---
|
|
118
196
|
|
|
@@ -149,7 +227,9 @@ After processing all children, excess old DOM nodes are removed.
|
|
|
149
227
|
this.$attrs({ disabled, class: 'my-component' });
|
|
150
228
|
```
|
|
151
229
|
|
|
152
|
-
|
|
230
|
+
Caveats vs `render()`: `$attrs` cannot clear event handlers (it calls `removeAttribute`, which is a no-op for handlers) and does not reset `value`/`checked`/`selected` properties on null. For full cleanup semantics, use `render()` with fresh descriptions. See [mxjs.dev/reference](https://mxjs.dev/reference).
|
|
231
|
+
|
|
232
|
+
**Stamp system**: Event handlers set via `render()` attrs in render N but absent in render N+1 are auto-nullified. You never manually remove handlers.
|
|
153
233
|
|
|
154
234
|
---
|
|
155
235
|
|
|
@@ -168,7 +248,7 @@ return [
|
|
|
168
248
|
];
|
|
169
249
|
```
|
|
170
250
|
|
|
171
|
-
`false`, `null`, and `undefined` are skipped. `0` is not - it's a valid number that renders as a text node.
|
|
251
|
+
`false`, `null`, and `undefined` are skipped. `0` is not - it's a valid number that renders as a text node. Prefix with `!!` any time the left side of `&&` might be a number (`.length`, counters, etc.).
|
|
172
252
|
|
|
173
253
|
---
|
|
174
254
|
|
|
@@ -224,6 +304,31 @@ let events = (() => {
|
|
|
224
304
|
|
|
225
305
|
---
|
|
226
306
|
|
|
307
|
+
## Memoization (cache the function, not the component)
|
|
308
|
+
|
|
309
|
+
Before adding any cache, measure - the reconciler is cheap enough that component-level memoization is usually pointless. When a function is genuinely expensive, put the cache on the function itself:
|
|
310
|
+
|
|
311
|
+
```js
|
|
312
|
+
function expensiveFormat(arg) {
|
|
313
|
+
let c = expensiveFormat.lookup ||= {};
|
|
314
|
+
return c[arg] ??= /* actual expensive work */;
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
For object-identity inputs, use a Map (plain objects stringify keys):
|
|
319
|
+
|
|
320
|
+
```js
|
|
321
|
+
function expensiveFormat(item) {
|
|
322
|
+
let c = expensiveFormat.lookup ||= new Map;
|
|
323
|
+
if (!c.has(item)) c.set(item, /* expensive work */);
|
|
324
|
+
return c.get(item);
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Function-level caches work across all callers and all component instances. No per-component bookkeeping, no invalidation logic scattered through `$()`.
|
|
329
|
+
|
|
330
|
+
---
|
|
331
|
+
|
|
227
332
|
## Pattern Library
|
|
228
333
|
|
|
229
334
|
### Counter (minimal stateful component)
|
|
@@ -342,7 +447,7 @@ toast.success = msg => toast(msg, 'success');
|
|
|
342
447
|
toast.error = msg => toast(msg, 'error');
|
|
343
448
|
```
|
|
344
449
|
|
|
345
|
-
### Timer component
|
|
450
|
+
### Timer component (reading latest state)
|
|
346
451
|
|
|
347
452
|
```js
|
|
348
453
|
define('player-bar', {
|
|
@@ -351,6 +456,7 @@ define('player-bar', {
|
|
|
351
456
|
|
|
352
457
|
if (isPlaying && track) {
|
|
353
458
|
this._interval = setInterval(() => {
|
|
459
|
+
// Read from $state, NOT the closed-over `progress` (that's stale from render time)
|
|
354
460
|
let p = this.$state.progress + 1;
|
|
355
461
|
if (p >= this.$state.track.duration) return;
|
|
356
462
|
this.$({ progress: p });
|
|
@@ -408,14 +514,14 @@ define('code-editor', {
|
|
|
408
514
|
let me = this;
|
|
409
515
|
me.$attrs({ disabled });
|
|
410
516
|
|
|
411
|
-
if (!me
|
|
412
|
-
me
|
|
517
|
+
if (!me._initialized) {
|
|
518
|
+
me._initialized = true;
|
|
413
519
|
me._container = dom.div();
|
|
414
520
|
|
|
415
521
|
loadLibrary('codemirror').then(() => {
|
|
416
522
|
me.$editor = CodeMirror(me._container, { lineNumbers: true });
|
|
417
523
|
me.$editor.on('change', () => onChange?.(me.$editor.getValue()));
|
|
418
|
-
me.$editor.setValue(
|
|
524
|
+
me.$editor.setValue(value);
|
|
419
525
|
});
|
|
420
526
|
}
|
|
421
527
|
|
|
@@ -468,4 +574,8 @@ function icon(name, size = 24) {
|
|
|
468
574
|
|
|
469
575
|
```js
|
|
470
576
|
mx.div({ style: 'width:' + pct + '%;background:' + color })
|
|
471
|
-
```
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
---
|
|
580
|
+
|
|
581
|
+
More at [mxjs.dev](https://mxjs.dev).
|
package/CLAUDE.md
CHANGED
|
@@ -2,17 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
Instructions for AI coding agents working with mx.js codebases.
|
|
4
4
|
|
|
5
|
+
**Full documentation: [mxjs.dev](https://mxjs.dev)** - API reference, patterns, gotchas, playground.
|
|
6
|
+
|
|
7
|
+
When transferring from React/Vue/Svelte, read [mxjs.dev/what-mx-is-not](https://mxjs.dev/what-mx-is-not) first. mx deliberately has no hooks, no JSX, no fragments, no portals, no memo, no synthetic events, no signals. That page lists every omission and what replaces it.
|
|
8
|
+
|
|
5
9
|
---
|
|
6
10
|
|
|
7
11
|
## Hard Rules
|
|
8
12
|
|
|
9
13
|
| Rule | Do | Don't |
|
|
10
14
|
|------|----|-------|
|
|
15
|
+
| Component definition | `$(props) {}` (method shorthand) | `$: function(props) {}` - legacy |
|
|
16
|
+
| Component name | `define('my-widget', {...})` kebab-case | `define('myWidget', {...})` - lookup silently fails |
|
|
17
|
+
| Call site | `mx.myWidget()` camelCase | `mx.MyWidget()` - PascalCase gives `-my-widget` (invalid) |
|
|
18
|
+
| State updates | `this.$({ key: value })` | `this.$state.key = value` - bypasses the merge, forbidden |
|
|
19
|
+
| Empty re-render | Pass actual changed data: `this.$({ key: newValue })` | `this.$({})` with empty object - code smell |
|
|
11
20
|
| Numbers in render | `mx.span(count)` | `mx.span(String(count))` - auto-casts |
|
|
12
21
|
| Event names | `onclick` (lowercase) | `onClick` - not React |
|
|
13
22
|
| Class attribute | `class: 'active'` | `className: 'active'` - not React |
|
|
14
23
|
| Label for | `for: 'input-id'` | `htmlFor: 'input-id'` - not React |
|
|
15
24
|
| Inline styles | `style: 'width:' + pct + '%'` | `style: { width: pct + '%' }` - not supported |
|
|
25
|
+
| Array children | `mx.ul(...items.map(i => mx.li(i)))` | `mx.ul(items.map(...))` - stringifies to `"1,2,3"` text |
|
|
16
26
|
| No attrs, just children | `mx.div(child1, child2)` | `mx.div({}, child)` or `mx.div(null, child)` |
|
|
17
27
|
| Boolean attributes | `disabled: true` sets `disabled=""` | Expecting it to set a JS property |
|
|
18
28
|
| First arg type | `mx.div({class: 'x'}, child)` | `mx.div(myMap, child)` - non-plain-Object treated as child |
|
|
@@ -22,6 +32,31 @@ Instructions for AI coding agents working with mx.js codebases.
|
|
|
22
32
|
## Common Mistakes
|
|
23
33
|
|
|
24
34
|
```js
|
|
35
|
+
// ✗ Legacy method syntax
|
|
36
|
+
define('foo', { $: function(props) { ... } });
|
|
37
|
+
// ✓ Method shorthand
|
|
38
|
+
define('foo', { $(props) { ... } });
|
|
39
|
+
|
|
40
|
+
// ✗ Mutating $state directly bypasses the declarative flow
|
|
41
|
+
this.$state.count = 5;
|
|
42
|
+
// ✓ Always go through $()
|
|
43
|
+
this.$({ count: 5 });
|
|
44
|
+
|
|
45
|
+
// ✗ Empty re-render with side-effect mutations - hidden data flow
|
|
46
|
+
$() {
|
|
47
|
+
this._items.push(newItem);
|
|
48
|
+
this.$({});
|
|
49
|
+
}
|
|
50
|
+
// ✓ Pass the actual change
|
|
51
|
+
$({ items = [] }) {
|
|
52
|
+
mx.button({ onclick: () => this.$({ items: [...items, newItem] }) }, 'Add');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ✗ Arrays of children stringified to text
|
|
56
|
+
mx.ul(items.map(i => mx.li(i.name)))
|
|
57
|
+
// ✓ Spread so each li is a child
|
|
58
|
+
mx.ul(...items.map(i => mx.li(i.name)))
|
|
59
|
+
|
|
25
60
|
// ✗ Mutates parent's array
|
|
26
61
|
assets.sort((a, b) => a.price - b.price);
|
|
27
62
|
// ✓ Copy first
|
|
@@ -46,14 +81,38 @@ mx.button({ onclick: handler })
|
|
|
46
81
|
items.length && mx.div('Has items')
|
|
47
82
|
// ✓ Coerce to boolean
|
|
48
83
|
!!items.length && mx.div('Has items')
|
|
84
|
+
|
|
85
|
+
// ✗ dom() with no attrs skips component init
|
|
86
|
+
let picker = dom.datePicker();
|
|
87
|
+
// ✓ Pass {} to trigger $()
|
|
88
|
+
let picker = dom.datePicker({});
|
|
89
|
+
|
|
90
|
+
// ✗ PascalCase produces leading hyphen (invalid tag)
|
|
91
|
+
mx.MyWidget()
|
|
92
|
+
// ✓ camelCase or string
|
|
93
|
+
mx.myWidget() // or mx('my-widget')
|
|
49
94
|
```
|
|
50
95
|
|
|
51
96
|
---
|
|
52
97
|
|
|
98
|
+
## Reserved Tag Names
|
|
99
|
+
|
|
100
|
+
The internal tag cache is a plain object; it inherits `Object.prototype` methods. Don't use any of these as component names:
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
toString, valueOf, constructor, hasOwnProperty, isPrototypeOf,
|
|
104
|
+
propertyIsEnumerable, toLocaleString, __defineGetter__,
|
|
105
|
+
__defineSetter__, __lookupGetter__, __lookupSetter__, __proto__
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`mx.toString()` produces a broken description that throws `InvalidCharacterError` at `createElement` time. Also applies to `define('toString', ...)`.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
53
112
|
## Checklist for New Components
|
|
54
113
|
|
|
55
|
-
1. `define('kebab-name', { $(
|
|
56
|
-
2. Destructure all props with defaults in `$()` signature
|
|
114
|
+
1. `define('kebab-name', { $(props) {} })` - kebab-case only in define()
|
|
115
|
+
2. Destructure all props with defaults in `$()` signature - no `??=` needed for primitives
|
|
57
116
|
3. Early return on empty state: `if (!data) return [];`
|
|
58
117
|
4. Use `this._name` for private caches (Maps, flags, intervals)
|
|
59
118
|
5. Use `this._name ||= new Map` for persistent references
|
|
@@ -67,6 +126,7 @@ items.length && mx.div('Has items')
|
|
|
67
126
|
13. `clearInterval(this._interval)` at top of `$()`
|
|
68
127
|
14. CSS transitions for animations, not JS
|
|
69
128
|
15. Formatters in render, not in data prep
|
|
129
|
+
16. Never write `this.$state.x = ...` directly - always `this.$({...})`
|
|
70
130
|
|
|
71
131
|
---
|
|
72
132
|
|
|
@@ -78,7 +138,21 @@ Real element to hold a reference? → dom.tag()
|
|
|
78
138
|
Need to call .$() on it later? → dom.tag()
|
|
79
139
|
```
|
|
80
140
|
|
|
81
|
-
|
|
141
|
+
### `$state` vs `this._property`
|
|
142
|
+
|
|
143
|
+
This is the single most common source of confusion for teams adopting mx. Pick the narrowest tier that holds the data's actual lifetime.
|
|
144
|
+
|
|
145
|
+
| | `this.$state.x` | `this._x` |
|
|
146
|
+
|---|---|---|
|
|
147
|
+
| **What it holds** | Accumulated props from the parent | Internal bookkeeping - tracking values, DOM refs, caches, flags, timers |
|
|
148
|
+
| **Who writes it** | The parent (via `el.$({...})`) and the component itself (via `this.$({...})`) | Only the component itself |
|
|
149
|
+
| **Merge behavior** | `Object.assign(this.$state, props)` runs before every `$()` call | Plain own property - set directly, no merge |
|
|
150
|
+
| **Read from outside** | OK, for aggregation (e.g. form submit reading `nameField.$state.value`) | Forbidden - it's private |
|
|
151
|
+
| **Write from outside** | Forbidden - always go through `comp.$({ key: newValue })` | Forbidden - it's private |
|
|
152
|
+
|
|
153
|
+
**Rule:** if a parent needs to see or set it, it's `$state`. Everything else is `this._`.
|
|
154
|
+
|
|
155
|
+
**Never** put internal bookkeeping (`_interval`, Maps, DOM refs) in `$state` - it pollutes the props namespace and risks collisions.
|
|
82
156
|
|
|
83
157
|
---
|
|
84
158
|
|
|
@@ -112,7 +186,11 @@ define('greeting', {
|
|
|
112
186
|
});
|
|
113
187
|
```
|
|
114
188
|
|
|
115
|
-
When parent calls `el.$({ count: 5 })`, count updates. When the component calls `this.$({ count: count + 1 })`, it self-updates. The destructuring default `= 0`
|
|
189
|
+
When parent calls `el.$({ count: 5 })`, count updates. When the component calls `this.$({ count: count + 1 })`, it self-updates. The destructuring default (`= 0`) applies whenever that key's value is `undefined` in `$state`.
|
|
190
|
+
|
|
191
|
+
### The parent always wins the merge
|
|
192
|
+
|
|
193
|
+
Because `Object.assign` runs **before** your `$` function, a parent re-passing the same prop will overwrite locally updated state. Usually that's what you want (controlled components). If you need state that survives parent re-passes, take the prop as a seed and store local state under a different key on `this._`.
|
|
116
194
|
|
|
117
195
|
---
|
|
118
196
|
|
|
@@ -149,7 +227,9 @@ After processing all children, excess old DOM nodes are removed.
|
|
|
149
227
|
this.$attrs({ disabled, class: 'my-component' });
|
|
150
228
|
```
|
|
151
229
|
|
|
152
|
-
|
|
230
|
+
Caveats vs `render()`: `$attrs` cannot clear event handlers (it calls `removeAttribute`, which is a no-op for handlers) and does not reset `value`/`checked`/`selected` properties on null. For full cleanup semantics, use `render()` with fresh descriptions. See [mxjs.dev/reference](https://mxjs.dev/reference).
|
|
231
|
+
|
|
232
|
+
**Stamp system**: Event handlers set via `render()` attrs in render N but absent in render N+1 are auto-nullified. You never manually remove handlers.
|
|
153
233
|
|
|
154
234
|
---
|
|
155
235
|
|
|
@@ -168,7 +248,7 @@ return [
|
|
|
168
248
|
];
|
|
169
249
|
```
|
|
170
250
|
|
|
171
|
-
`false`, `null`, and `undefined` are skipped. `0` is not - it's a valid number that renders as a text node.
|
|
251
|
+
`false`, `null`, and `undefined` are skipped. `0` is not - it's a valid number that renders as a text node. Prefix with `!!` any time the left side of `&&` might be a number (`.length`, counters, etc.).
|
|
172
252
|
|
|
173
253
|
---
|
|
174
254
|
|
|
@@ -224,6 +304,31 @@ let events = (() => {
|
|
|
224
304
|
|
|
225
305
|
---
|
|
226
306
|
|
|
307
|
+
## Memoization (cache the function, not the component)
|
|
308
|
+
|
|
309
|
+
Before adding any cache, measure - the reconciler is cheap enough that component-level memoization is usually pointless. When a function is genuinely expensive, put the cache on the function itself:
|
|
310
|
+
|
|
311
|
+
```js
|
|
312
|
+
function expensiveFormat(arg) {
|
|
313
|
+
let c = expensiveFormat.lookup ||= {};
|
|
314
|
+
return c[arg] ??= /* actual expensive work */;
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
For object-identity inputs, use a Map (plain objects stringify keys):
|
|
319
|
+
|
|
320
|
+
```js
|
|
321
|
+
function expensiveFormat(item) {
|
|
322
|
+
let c = expensiveFormat.lookup ||= new Map;
|
|
323
|
+
if (!c.has(item)) c.set(item, /* expensive work */);
|
|
324
|
+
return c.get(item);
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Function-level caches work across all callers and all component instances. No per-component bookkeeping, no invalidation logic scattered through `$()`.
|
|
329
|
+
|
|
330
|
+
---
|
|
331
|
+
|
|
227
332
|
## Pattern Library
|
|
228
333
|
|
|
229
334
|
### Counter (minimal stateful component)
|
|
@@ -342,7 +447,7 @@ toast.success = msg => toast(msg, 'success');
|
|
|
342
447
|
toast.error = msg => toast(msg, 'error');
|
|
343
448
|
```
|
|
344
449
|
|
|
345
|
-
### Timer component
|
|
450
|
+
### Timer component (reading latest state)
|
|
346
451
|
|
|
347
452
|
```js
|
|
348
453
|
define('player-bar', {
|
|
@@ -351,6 +456,7 @@ define('player-bar', {
|
|
|
351
456
|
|
|
352
457
|
if (isPlaying && track) {
|
|
353
458
|
this._interval = setInterval(() => {
|
|
459
|
+
// Read from $state, NOT the closed-over `progress` (that's stale from render time)
|
|
354
460
|
let p = this.$state.progress + 1;
|
|
355
461
|
if (p >= this.$state.track.duration) return;
|
|
356
462
|
this.$({ progress: p });
|
|
@@ -408,14 +514,14 @@ define('code-editor', {
|
|
|
408
514
|
let me = this;
|
|
409
515
|
me.$attrs({ disabled });
|
|
410
516
|
|
|
411
|
-
if (!me
|
|
412
|
-
me
|
|
517
|
+
if (!me._initialized) {
|
|
518
|
+
me._initialized = true;
|
|
413
519
|
me._container = dom.div();
|
|
414
520
|
|
|
415
521
|
loadLibrary('codemirror').then(() => {
|
|
416
522
|
me.$editor = CodeMirror(me._container, { lineNumbers: true });
|
|
417
523
|
me.$editor.on('change', () => onChange?.(me.$editor.getValue()));
|
|
418
|
-
me.$editor.setValue(
|
|
524
|
+
me.$editor.setValue(value);
|
|
419
525
|
});
|
|
420
526
|
}
|
|
421
527
|
|
|
@@ -468,4 +574,8 @@ function icon(name, size = 24) {
|
|
|
468
574
|
|
|
469
575
|
```js
|
|
470
576
|
mx.div({ style: 'width:' + pct + '%;background:' + color })
|
|
471
|
-
```
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
---
|
|
580
|
+
|
|
581
|
+
More at [mxjs.dev](https://mxjs.dev).
|
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Sub-1KB reactive DOM reconciliation engine for building massive platforms and user interfaces at scale. **849 bytes** brotli. 4 globals, zero dependencies, no virtual DOM, no build step. Small enough to fit in an AI context window.
|
|
4
4
|
|
|
5
|
-
[
|
|
5
|
+
**Full documentation & live playground: [mxjs.dev](https://mxjs.dev)**
|
|
6
|
+
|
|
7
|
+
- [Getting started](https://mxjs.dev/getting-started) · [API reference](https://mxjs.dev/reference) · [Patterns](https://mxjs.dev/patterns) · [Gotchas](https://mxjs.dev/gotchas) · [What mx is not](https://mxjs.dev/what-mx-is-not)
|
|
6
8
|
|
|
7
9
|
## Install
|
|
8
10
|
|
|
@@ -19,7 +21,7 @@ Sub-1KB reactive DOM reconciliation engine for building massive platforms and us
|
|
|
19
21
|
### Pinned version
|
|
20
22
|
|
|
21
23
|
```html
|
|
22
|
-
<script src="https://cdn.jsdelivr.net/npm/@tltdsh/mx@1.
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/@tltdsh/mx@1.1.1/mx.min.js"></script>
|
|
23
25
|
```
|
|
24
26
|
|
|
25
27
|
### npm / yarn / bun
|
package/mx.d.ts
CHANGED
|
@@ -100,6 +100,13 @@ interface MxHTMLAttrs {
|
|
|
100
100
|
popovertarget?: string;
|
|
101
101
|
popovertargetaction?: 'toggle' | 'show' | 'hide';
|
|
102
102
|
|
|
103
|
+
// ARIA labels cluster (accessibility)
|
|
104
|
+
'aria-label'?: string;
|
|
105
|
+
'aria-labelledby'?: string;
|
|
106
|
+
'aria-describedby'?: string;
|
|
107
|
+
'aria-placeholder'?: string;
|
|
108
|
+
'aria-errormessage'?: string;
|
|
109
|
+
|
|
103
110
|
// Loading
|
|
104
111
|
fetchpriority?: 'high' | 'low' | 'auto';
|
|
105
112
|
|
|
@@ -183,8 +190,13 @@ interface MxHTMLAttrs {
|
|
|
183
190
|
onload?: ((e: Event) => void) | null;
|
|
184
191
|
onabort?: ((e: Event) => void) | null;
|
|
185
192
|
|
|
186
|
-
// Toggle events
|
|
187
|
-
ontoggle?: ((e:
|
|
193
|
+
// Toggle events (dialog, details, popover)
|
|
194
|
+
ontoggle?: ((e: ToggleEvent) => void) | null;
|
|
195
|
+
onbeforetoggle?: ((e: ToggleEvent) => void) | null;
|
|
196
|
+
|
|
197
|
+
// Dialog events
|
|
198
|
+
onclose?: ((e: Event) => void) | null;
|
|
199
|
+
oncancel?: ((e: Event) => void) | null;
|
|
188
200
|
|
|
189
201
|
// Animation / transition
|
|
190
202
|
onanimationstart?: ((e: AnimationEvent) => void) | null;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tltdsh/mx",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "<1KB UI engine. Build big. Ship small
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "<1KB UI engine. Build big. Ship small. Iterate fast.",
|
|
5
5
|
"license": "0BSD",
|
|
6
6
|
"author": "tltdsh",
|
|
7
7
|
"homepage": "https://mxjs.dev",
|