@tltdsh/mx 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +27 -1
- package/CLAUDE.md +26 -0
- package/README.md +4 -4
- package/mx.d.ts +11 -0
- package/mx.min.js +1 -1
- package/mx.mjs +2 -2
- package/package.json +1 -1
package/AGENTS.md
CHANGED
|
@@ -13,7 +13,7 @@ When transferring from React/Vue/Svelte, read [mxjs.dev/what-mx-is-not](https://
|
|
|
13
13
|
| Rule | Do | Don't |
|
|
14
14
|
|------|----|-------|
|
|
15
15
|
| Component definition | `$(props) {}` (method shorthand) | `$: function(props) {}` - legacy |
|
|
16
|
-
| Component name | `define('my-widget', {...})` kebab-case
|
|
16
|
+
| Component name | `define('my-widget', {...})` kebab-case (camelCase also works — auto-converts to kebab) | Mismatched names between define and call site |
|
|
17
17
|
| Call site | `mx.myWidget()` camelCase | `mx.MyWidget()` - PascalCase gives `-my-widget` (invalid) |
|
|
18
18
|
| State updates | `this.$({ key: value })` | `this.$state.key = value` - bypasses the merge, forbidden |
|
|
19
19
|
| Empty re-render | Pass actual changed data: `this.$({ key: newValue })` | `this.$({})` with empty object - code smell |
|
|
@@ -91,6 +91,23 @@ let picker = dom.datePicker({});
|
|
|
91
91
|
mx.MyWidget()
|
|
92
92
|
// ✓ camelCase or string
|
|
93
93
|
mx.myWidget() // or mx('my-widget')
|
|
94
|
+
|
|
95
|
+
// ✗ Static array re-allocated on every render
|
|
96
|
+
$({ value }) {
|
|
97
|
+
let statuses = ['open', 'pending', 'closed']; // fresh array each call
|
|
98
|
+
return mx.select({ '.value': value }, ...statuses.map(s => mx.option({ value: s }, s)));
|
|
99
|
+
}
|
|
100
|
+
// ✓ Hoist constants to module scope
|
|
101
|
+
let STATUSES = ['open', 'pending', 'closed'];
|
|
102
|
+
|
|
103
|
+
// ✗ Positional rows lose typed input / checkbox state after a sort
|
|
104
|
+
container.render(...rows.map(r => mx.div(mx.span(r.name), mx.input())));
|
|
105
|
+
// ✓ Keyed dom() rows move by identity, state intact (see Keyed Lists below)
|
|
106
|
+
|
|
107
|
+
// ✗ Logic driven by rendered text
|
|
108
|
+
if (Number(this.querySelector('.count').textContent) > 0) this.$({ hasItems: true });
|
|
109
|
+
// ✓ Read from state / data attributes
|
|
110
|
+
if (this.$state.items.length) this.$({ hasItems: true });
|
|
94
111
|
```
|
|
95
112
|
|
|
96
113
|
---
|
|
@@ -107,6 +124,8 @@ __defineSetter__, __lookupGetter__, __lookupSetter__, __proto__
|
|
|
107
124
|
|
|
108
125
|
`mx.toString()` produces a broken description that throws `InvalidCharacterError` at `createElement` time. Also applies to `define('toString', ...)`.
|
|
109
126
|
|
|
127
|
+
Tag/component names only - **attribute** names are safe: the reconciler tracks attrs by own-key checks (`Object.hasOwn`), so `{ toString: 'x' }` sets and removes cleanly, and attrs objects with an own `constructor` key are still recognized as attrs.
|
|
128
|
+
|
|
110
129
|
---
|
|
111
130
|
|
|
112
131
|
## Checklist for New Components
|
|
@@ -127,6 +146,9 @@ __defineSetter__, __lookupGetter__, __lookupSetter__, __proto__
|
|
|
127
146
|
14. CSS transitions for animations, not JS
|
|
128
147
|
15. Formatters in render, not in data prep
|
|
129
148
|
16. Never write `this.$state.x = ...` directly - always `this.$({...})`
|
|
149
|
+
17. Hoist constant arrays/objects out of `$()` - don't re-allocate per render
|
|
150
|
+
18. Stateful list rows use a keyed `dom()` Map, not positional `mx()` rows
|
|
151
|
+
19. Drive logic from `$state` / data attributes, never from rendered text
|
|
130
152
|
|
|
131
153
|
---
|
|
132
154
|
|
|
@@ -231,6 +253,10 @@ Caveats vs `render()`: `$attrs` cannot clear event handlers (it calls `removeAtt
|
|
|
231
253
|
|
|
232
254
|
**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.
|
|
233
255
|
|
|
256
|
+
**Custom events**: `on*` attrs only register listeners for spec-known handler properties (`onclick`, `oninput`, ...). A custom event type (`onping: fn`) is a silent expando - use `addEventListener` once (guarded) for custom events.
|
|
257
|
+
|
|
258
|
+
**Children-first ordering**: `render()` reconciles an element's children before applying its own attributes (load-bearing for `select.value`). Consequences: a child that dispatches an event synchronously during mount is missed by the parent's not-yet-attached handler, and a child reading a parent attribute during `$()` sees the previous render's value. Pass data via `$state`, dispatch after mount.
|
|
259
|
+
|
|
234
260
|
---
|
|
235
261
|
|
|
236
262
|
## Conditional Rendering
|
package/CLAUDE.md
CHANGED
|
@@ -91,6 +91,23 @@ let picker = dom.datePicker({});
|
|
|
91
91
|
mx.MyWidget()
|
|
92
92
|
// ✓ camelCase or string
|
|
93
93
|
mx.myWidget() // or mx('my-widget')
|
|
94
|
+
|
|
95
|
+
// ✗ Static array re-allocated on every render
|
|
96
|
+
$({ value }) {
|
|
97
|
+
let statuses = ['open', 'pending', 'closed']; // fresh array each call
|
|
98
|
+
return mx.select({ '.value': value }, ...statuses.map(s => mx.option({ value: s }, s)));
|
|
99
|
+
}
|
|
100
|
+
// ✓ Hoist constants to module scope
|
|
101
|
+
let STATUSES = ['open', 'pending', 'closed'];
|
|
102
|
+
|
|
103
|
+
// ✗ Positional rows lose typed input / checkbox state after a sort
|
|
104
|
+
container.render(...rows.map(r => mx.div(mx.span(r.name), mx.input())));
|
|
105
|
+
// ✓ Keyed dom() rows move by identity, state intact (see Keyed Lists below)
|
|
106
|
+
|
|
107
|
+
// ✗ Logic driven by rendered text
|
|
108
|
+
if (Number(this.querySelector('.count').textContent) > 0) this.$({ hasItems: true });
|
|
109
|
+
// ✓ Read from state / data attributes
|
|
110
|
+
if (this.$state.items.length) this.$({ hasItems: true });
|
|
94
111
|
```
|
|
95
112
|
|
|
96
113
|
---
|
|
@@ -107,6 +124,8 @@ __defineSetter__, __lookupGetter__, __lookupSetter__, __proto__
|
|
|
107
124
|
|
|
108
125
|
`mx.toString()` produces a broken description that throws `InvalidCharacterError` at `createElement` time. Also applies to `define('toString', ...)`.
|
|
109
126
|
|
|
127
|
+
Tag/component names only - **attribute** names are safe: the reconciler tracks attrs by own-key checks (`Object.hasOwn`), so `{ toString: 'x' }` sets and removes cleanly, and attrs objects with an own `constructor` key are still recognized as attrs.
|
|
128
|
+
|
|
110
129
|
---
|
|
111
130
|
|
|
112
131
|
## Checklist for New Components
|
|
@@ -127,6 +146,9 @@ __defineSetter__, __lookupGetter__, __lookupSetter__, __proto__
|
|
|
127
146
|
14. CSS transitions for animations, not JS
|
|
128
147
|
15. Formatters in render, not in data prep
|
|
129
148
|
16. Never write `this.$state.x = ...` directly - always `this.$({...})`
|
|
149
|
+
17. Hoist constant arrays/objects out of `$()` - don't re-allocate per render
|
|
150
|
+
18. Stateful list rows use a keyed `dom()` Map, not positional `mx()` rows
|
|
151
|
+
19. Drive logic from `$state` / data attributes, never from rendered text
|
|
130
152
|
|
|
131
153
|
---
|
|
132
154
|
|
|
@@ -231,6 +253,10 @@ Caveats vs `render()`: `$attrs` cannot clear event handlers (it calls `removeAtt
|
|
|
231
253
|
|
|
232
254
|
**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.
|
|
233
255
|
|
|
256
|
+
**Custom events**: `on*` attrs only register listeners for spec-known handler properties (`onclick`, `oninput`, ...). A custom event type (`onping: fn`) is a silent expando - use `addEventListener` once (guarded) for custom events.
|
|
257
|
+
|
|
258
|
+
**Children-first ordering**: `render()` reconciles an element's children before applying its own attributes (load-bearing for `select.value`). Consequences: a child that dispatches an event synchronously during mount is missed by the parent's not-yet-attached handler, and a child reading a parent attribute during `$()` sees the previous render's value. Pass data via `$state`, dispatch after mount.
|
|
259
|
+
|
|
234
260
|
---
|
|
235
261
|
|
|
236
262
|
## Conditional Rendering
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# mx.js
|
|
2
2
|
|
|
3
|
-
Sub-1KB reactive DOM reconciliation engine for building massive platforms and user interfaces at scale. **
|
|
3
|
+
Sub-1KB reactive DOM reconciliation engine for building massive platforms and user interfaces at scale. **Under 1KB** 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
6
|
|
|
@@ -21,7 +21,7 @@ Sub-1KB reactive DOM reconciliation engine for building massive platforms and us
|
|
|
21
21
|
### Pinned version
|
|
22
22
|
|
|
23
23
|
```html
|
|
24
|
-
<script src="https://cdn.jsdelivr.net/npm/@tltdsh/mx@1.3.
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/@tltdsh/mx@1.3.2/mx.min.js"></script>
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### npm / yarn / bun
|
|
@@ -198,8 +198,8 @@ container.render(...data.map(d => rowMap.get(d.id)))
|
|
|
198
198
|
|
|
199
199
|
- `mx.*` = descriptions (arrays) for inside `render()`
|
|
200
200
|
- `dom.*` = real HTMLElements for persistent references
|
|
201
|
-
- `$()`
|
|
202
|
-
-
|
|
201
|
+
- `$()` returns a single `mx.*` description **or** an array of children
|
|
202
|
+
- The parent always wins the `$state` merge - a re-passed prop overwrites local state (seed `this._x` if it must survive)
|
|
203
203
|
- Never `addEventListener` inside `$()` - use direct property assignment
|
|
204
204
|
- Numbers auto-cast - no `String()` needed
|
|
205
205
|
- Use `_ =>` for unused arrow params
|
package/mx.d.ts
CHANGED
|
@@ -121,6 +121,17 @@ interface MxHTMLAttrs {
|
|
|
121
121
|
'aria-checked'?: boolean | 'true' | 'false' | 'mixed';
|
|
122
122
|
'aria-pressed'?: boolean | 'true' | 'false' | 'mixed';
|
|
123
123
|
|
|
124
|
+
// ARIA relations cluster (accessibility)
|
|
125
|
+
'aria-controls'?: string;
|
|
126
|
+
'aria-owns'?: string;
|
|
127
|
+
'aria-haspopup'?: boolean | 'true' | 'false' | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog';
|
|
128
|
+
|
|
129
|
+
// ARIA live region cluster (accessibility)
|
|
130
|
+
'aria-live'?: 'off' | 'polite' | 'assertive';
|
|
131
|
+
'aria-atomic'?: boolean | 'true' | 'false';
|
|
132
|
+
'aria-busy'?: boolean | 'true' | 'false';
|
|
133
|
+
'aria-current'?: boolean | 'true' | 'false' | 'page' | 'step' | 'location' | 'date' | 'time';
|
|
134
|
+
|
|
124
135
|
// Loading
|
|
125
136
|
fetchpriority?: 'high' | 'low' | 'auto';
|
|
126
137
|
|
package/mx.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(
|
|
1
|
+
!function(m,i,_,u){let $='http://www.w3.org/2000/svg',t={selected:1,value:1,checked:1},x=Symbol(),n=(i,t)=>!t&'svg'!=i?l(m.createElement(i),_[i]):m.createElementNS($,i),R=m=>h[m]||=m.replace(/[A-Z]/g,'-$&').toLowerCase(),h={},l=Object.assign,w=(m,n={})=>new Proxy(m,{get:(i,_)=>n[_]||=m.bind(0,_)});define=(i,m={})=>{if(m.$)m.$$||=m.$,m.$=function(i){let _=m.$$.call(this,l(this.$state||={},i));return this.render(..._?._?[_]:_||[])};_[R(i)]=m};i.render=function(...l){let i=this,_=i.namespaceURI==$&'foreignObject'!=i.localName,m=i.firstChild;for(let $ of l){let w,R,g;if($==null|!1===$)continue;if($._)[w,R,g]=$;else if($.nodeType){$!=m&&(m?m.replaceWith($):i.append($)),m=$.nextSibling;continue}if(m)!w?m.splitText?m.data!=$+''&&(m.data=$):m.replaceWith(m=new Text($)):w!=m.localName&&m.replaceWith(m=n(w,_));else if(w)i.append(m=n(w,_));else{i.append($);continue}if(w)if(m.$)m[x]=0,m.$(R);else{m.render(...g);let[_,n]=m[x]||=[{},{}],l=u=-~u;for(let i in _){R&&Object.hasOwn(R,i)||(('.'==i[0]?t[i.slice(1)]&&delete n[i.slice(1)]:(t[i]&&(delete n[i],m[i]=''),m.R(i))),delete _[i])}for(let i in R){let $=R[i];_[i]===$?'.'==i[0]?(m[i.slice(1)]=$,n[i.slice(1)]=l):t[i]?(m[i]=$,n[i]=l):$?.call&&(n[i]=l):'.'==i[0]?(m[i.slice(1)]=$,n[i.slice(1)]=l,_[i]=$):$!=null&!1!==$?($.call?(m[i]=$,n[i]=l):(t[i]?(m[i]=$,n[i]=l):delete n[i],m.setAttribute(i,$)),_[i]=$):(t[i]&&(delete n[i],m[i]=''),m.R(i),delete _[i])}for(let i in n)l-n[i]&&(delete n[i],m[i]=null)}m=m.nextSibling}for(let i;m;m=i)i=m.nextSibling,m.remove();return this},i.R=i.removeAttribute,_=components={},i.$attrs=function(m,i,$){for(i in m)$=m[i],'.'==i[0]?this[i.slice(1)]=$:$!=null&!1!==$?($.call?this[i]=$:this.setAttribute(i,$)):this.R(i);return this},mx=w((m,i,..._)=>(m=R(m),_=i?.__proto__==Object.prototype&&!i?.nodeType?[m,i,_]:[m,null,[i,..._]],_._=1,_)),dom=w((m,i,..._)=>(m=n(R(m)),i?.__proto__==Object.prototype&&!i?.nodeType?m.$?m.$(i):m.$attrs(i).render(..._):m.render(i,..._),m))}(document,Element.prototype)
|
package/mx.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
!function(
|
|
1
|
+
let mx, dom, define, components;
|
|
2
|
+
!function(m,i,_,u){let $='http://www.w3.org/2000/svg',t={selected:1,value:1,checked:1},x=Symbol(),n=(i,t)=>!t&'svg'!=i?l(m.createElement(i),_[i]):m.createElementNS($,i),R=m=>h[m]||=m.replace(/[A-Z]/g,'-$&').toLowerCase(),h={},l=Object.assign,w=(m,n={})=>new Proxy(m,{get:(i,_)=>n[_]||=m.bind(0,_)});define=(i,m={})=>{if(m.$)m.$$||=m.$,m.$=function(i){let _=m.$$.call(this,l(this.$state||={},i));return this.render(..._?._?[_]:_||[])};_[R(i)]=m};i.render=function(...l){let i=this,_=i.namespaceURI==$&'foreignObject'!=i.localName,m=i.firstChild;for(let $ of l){let w,R,g;if($==null|!1===$)continue;if($._)[w,R,g]=$;else if($.nodeType){$!=m&&(m?m.replaceWith($):i.append($)),m=$.nextSibling;continue}if(m)!w?m.splitText?m.data!=$+''&&(m.data=$):m.replaceWith(m=new Text($)):w!=m.localName&&m.replaceWith(m=n(w,_));else if(w)i.append(m=n(w,_));else{i.append($);continue}if(w)if(m.$)m[x]=0,m.$(R);else{m.render(...g);let[_,n]=m[x]||=[{},{}],l=u=-~u;for(let i in _){R&&Object.hasOwn(R,i)||(('.'==i[0]?t[i.slice(1)]&&delete n[i.slice(1)]:(t[i]&&(delete n[i],m[i]=''),m.R(i))),delete _[i])}for(let i in R){let $=R[i];_[i]===$?'.'==i[0]?(m[i.slice(1)]=$,n[i.slice(1)]=l):t[i]?(m[i]=$,n[i]=l):$?.call&&(n[i]=l):'.'==i[0]?(m[i.slice(1)]=$,n[i.slice(1)]=l,_[i]=$):$!=null&!1!==$?($.call?(m[i]=$,n[i]=l):(t[i]?(m[i]=$,n[i]=l):delete n[i],m.setAttribute(i,$)),_[i]=$):(t[i]&&(delete n[i],m[i]=''),m.R(i),delete _[i])}for(let i in n)l-n[i]&&(delete n[i],m[i]=null)}m=m.nextSibling}for(let i;m;m=i)i=m.nextSibling,m.remove();return this},i.R=i.removeAttribute,_=components={},i.$attrs=function(m,i,$){for(i in m)$=m[i],'.'==i[0]?this[i.slice(1)]=$:$!=null&!1!==$?($.call?this[i]=$:this.setAttribute(i,$)):this.R(i);return this},mx=w((m,i,..._)=>(m=R(m),_=i?.__proto__==Object.prototype&&!i?.nodeType?[m,i,_]:[m,null,[i,..._]],_._=1,_)),dom=w((m,i,..._)=>(m=n(R(m)),i?.__proto__==Object.prototype&&!i?.nodeType?m.$?m.$(i):m.$attrs(i).render(..._):m.render(i,..._),m))}(document,Element.prototype)
|
|
3
3
|
export { mx, dom, define, components };
|