@tltdsh/mx 1.4.0 → 1.5.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/mx.d.ts CHANGED
@@ -11,7 +11,10 @@ interface MxDescription extends Array<any> {
11
11
  _: 1 | true;
12
12
  }
13
13
 
14
- /** Anything that can appear as a child in render(), mx.*(), or dom.*() */
14
+ /**
15
+ * Anything that can appear as a child in render(), mx.*(), or dom.*().
16
+ * Plain arrays (an unspread `.map()` result) flatten, nested arrays too.
17
+ */
15
18
  type MxChild =
16
19
  | MxDescription
17
20
  | Node
@@ -19,7 +22,8 @@ type MxChild =
19
22
  | number
20
23
  | false
21
24
  | null
22
- | undefined;
25
+ | undefined
26
+ | readonly MxChild[];
23
27
 
24
28
  // ---------------------------------------------------------------------------
25
29
  // Attributes
@@ -117,13 +121,13 @@ interface MxHTMLAttrs {
117
121
  // NOTE: `false` REMOVES an attribute (`removeAttribute`), it does not write "false".
118
122
  // For the four states below, absent is NOT the same as "false" to a screen reader
119
123
  // (absent aria-expanded means "not expandable", absent aria-pressed means "not a
120
- // toggle button"), so they only accept the explicit strings. Pass String(flag).
124
+ // toggle button"), so they reject `false`. Pass String(flag); `null` still removes.
121
125
  'aria-hidden'?: boolean | 'true' | 'false';
122
- 'aria-expanded'?: 'true' | 'false';
126
+ 'aria-expanded'?: true | 'true' | 'false' | (string & {});
123
127
  'aria-disabled'?: boolean | 'true' | 'false';
124
- 'aria-selected'?: 'true' | 'false';
125
- 'aria-checked'?: 'true' | 'false' | 'mixed';
126
- 'aria-pressed'?: 'true' | 'false' | 'mixed';
128
+ 'aria-selected'?: true | 'true' | 'false' | (string & {});
129
+ 'aria-checked'?: true | 'true' | 'false' | 'mixed' | (string & {});
130
+ 'aria-pressed'?: true | 'true' | 'false' | 'mixed' | (string & {});
127
131
 
128
132
  // ARIA relations cluster (accessibility)
129
133
  'aria-controls'?: string;
@@ -144,6 +148,17 @@ interface MxHTMLAttrs {
144
148
  'aria-orientation'?: 'horizontal' | 'vertical';
145
149
  'aria-sort'?: 'none' | 'ascending' | 'descending' | 'other';
146
150
 
151
+ // ARIA form validation cluster (accessibility)
152
+ // Absent IS the default ("not required", "valid", "editable") for these three,
153
+ // so plain booleans are fine: `false` removes the attribute, which reads the same.
154
+ 'aria-required'?: boolean | 'true' | 'false';
155
+ 'aria-invalid'?: boolean | 'true' | 'false' | 'grammar' | 'spelling';
156
+ 'aria-readonly'?: boolean | 'true' | 'false';
157
+
158
+ // ARIA misc cluster (accessibility)
159
+ 'aria-modal'?: boolean | 'true' | 'false';
160
+ 'aria-roledescription'?: string;
161
+
147
162
  // Loading
148
163
  fetchpriority?: 'high' | 'low' | 'auto';
149
164
 
@@ -296,6 +311,12 @@ interface MxSVGAttrs {
296
311
  'stop-opacity'?: string | number;
297
312
  }
298
313
 
314
+ /** The four aria states where `false` would remove the attribute, which a screen reader reads differently from "false" */
315
+ type MxTriState = 'aria-expanded' | 'aria-selected' | 'aria-checked' | 'aria-pressed';
316
+
317
+ /** Every typed attribute also takes `null` and `false` (removal), except `false` on the four tri-state aria states */
318
+ type MxRemovable<T> = { [K in keyof T]?: T[K] | null | (K extends MxTriState ? never : false) };
319
+
299
320
  /**
300
321
  * Full attribute type. Supports:
301
322
  * - Standard HTML/SVG attributes (autocomplete)
@@ -305,7 +326,7 @@ interface MxSVGAttrs {
305
326
  * - `name: true` - sets attribute to the string "true" (`setAttribute(name, 'true')`)
306
327
  * - `[key: string]: any` - custom attributes, data-*, aria-*
307
328
  */
308
- type MxAttrs = MxHTMLAttrs & MxSVGAttrs & {
329
+ type MxAttrs = MxRemovable<MxHTMLAttrs> & MxRemovable<MxSVGAttrs> & {
309
330
  [key: string]: any;
310
331
  };
311
332
 
@@ -318,9 +339,10 @@ interface MxComponentDefinition {
318
339
  /**
319
340
  * Render function. Called with merged $state + incoming props.
320
341
  * `this` is the component element.
321
- * Return a single MxDescription or an array of MxChild.
342
+ * Return a single MxDescription or an array of MxChild (real dom() nodes go
343
+ * inside an array). null, false or nothing renders the component empty.
322
344
  */
323
- $?(this: MxElement, props: Record<string, any>): MxChild | MxChild[];
345
+ $?(this: MxElement, props: Record<string, any>): MxDescription | readonly MxChild[] | null | undefined | false | void;
324
346
 
325
347
  /** Any other properties are mixed into the created element */
326
348
  [key: string]: any;
@@ -339,6 +361,9 @@ interface MxElement extends HTMLElement {
339
361
  * reconciles DOM. Returns this element.
340
362
  */
341
363
  $(props?: Record<string, any>): this;
364
+
365
+ /** Methods mixed in from the definition, and private `this._x` fields (timers, caches, DOM refs) */
366
+ [key: string]: any;
342
367
  }
343
368
 
344
369
  // ---------------------------------------------------------------------------
@@ -517,7 +542,7 @@ interface MxHTMLTags {
517
542
  *
518
543
  * @example
519
544
  * mx.div({ class: 'card' }, mx.h2('Title'), mx.p('Body'))
520
- * mx.input({ type: 'text', oninput: e => console.log(e.target.value) })
545
+ * mx.input({ type: 'text', oninput: e => console.log((e.target as HTMLInputElement).value) })
521
546
  * mx.myComponent({ count: 5 })
522
547
  */
523
548
  interface MxProxy extends MxHTMLTags {
@@ -559,16 +584,30 @@ interface DomHTMLTags {
559
584
  em: DomTagFn<HTMLElement>;
560
585
  b: DomTagFn<HTMLElement>;
561
586
  i: DomTagFn<HTMLElement>;
587
+ u: DomTagFn<HTMLElement>;
588
+ s: DomTagFn<HTMLElement>;
589
+ small: DomTagFn<HTMLElement>;
590
+ sub: DomTagFn<HTMLElement>;
591
+ sup: DomTagFn<HTMLElement>;
592
+ mark: DomTagFn<HTMLElement>;
562
593
  code: DomTagFn<HTMLElement>;
563
594
  pre: DomTagFn<HTMLPreElement>;
595
+ kbd: DomTagFn<HTMLElement>;
596
+ abbr: DomTagFn<HTMLElement>;
597
+ cite: DomTagFn<HTMLElement>;
598
+ q: DomTagFn<HTMLQuoteElement>;
599
+ time: DomTagFn<HTMLTimeElement>;
564
600
  br: DomTagFn<HTMLBRElement>;
565
601
  hr: DomTagFn<HTMLHRElement>;
602
+ wbr: DomTagFn<HTMLElement>;
566
603
 
567
604
  // Lists
568
605
  ul: DomTagFn<HTMLUListElement>;
569
606
  ol: DomTagFn<HTMLOListElement>;
570
607
  li: DomTagFn<HTMLLIElement>;
571
608
  dl: DomTagFn<HTMLDListElement>;
609
+ dt: DomTagFn<HTMLElement>;
610
+ dd: DomTagFn<HTMLElement>;
572
611
 
573
612
  // Table
574
613
  table: DomTagFn<HTMLTableElement>;
@@ -579,6 +618,8 @@ interface DomHTMLTags {
579
618
  th: DomTagFn<HTMLTableCellElement>;
580
619
  td: DomTagFn<HTMLTableCellElement>;
581
620
  caption: DomTagFn<HTMLTableCaptionElement>;
621
+ colgroup: DomTagFn<HTMLTableColElement>;
622
+ col: DomTagFn<HTMLTableColElement>;
582
623
 
583
624
  // Forms
584
625
  form: DomTagFn<HTMLFormElement>;
@@ -591,6 +632,7 @@ interface DomHTMLTags {
591
632
  label: DomTagFn<HTMLLabelElement>;
592
633
  fieldset: DomTagFn<HTMLFieldSetElement>;
593
634
  legend: DomTagFn<HTMLLegendElement>;
635
+ datalist: DomTagFn<HTMLDataListElement>;
594
636
  output: DomTagFn<HTMLOutputElement>;
595
637
  progress: DomTagFn<HTMLProgressElement>;
596
638
  meter: DomTagFn<HTMLMeterElement>;
@@ -601,8 +643,11 @@ interface DomHTMLTags {
601
643
  source: DomTagFn<HTMLSourceElement>;
602
644
  video: DomTagFn<HTMLVideoElement>;
603
645
  audio: DomTagFn<HTMLAudioElement>;
646
+ track: DomTagFn<HTMLTrackElement>;
604
647
  canvas: DomTagFn<HTMLCanvasElement>;
605
648
  iframe: DomTagFn<HTMLIFrameElement>;
649
+ object: DomTagFn<HTMLObjectElement>;
650
+ embed: DomTagFn<HTMLEmbedElement>;
606
651
 
607
652
  // SVG
608
653
  //
@@ -619,6 +664,7 @@ interface DomHTMLTags {
619
664
 
620
665
  // Interactive
621
666
  details: DomTagFn<HTMLDetailsElement>;
667
+ summary: DomTagFn<HTMLElement>;
622
668
  dialog: DomTagFn<HTMLDialogElement>;
623
669
  menu: DomTagFn<HTMLMenuElement>;
624
670
 
@@ -628,7 +674,15 @@ interface DomHTMLTags {
628
674
  style: DomTagFn<HTMLStyleElement>;
629
675
  link: DomTagFn<HTMLLinkElement>;
630
676
  slot: DomTagFn<HTMLSlotElement>;
677
+ meta: DomTagFn<HTMLMetaElement>;
678
+ noscript: DomTagFn<HTMLElement>;
631
679
  blockquote: DomTagFn<HTMLQuoteElement>;
680
+ figure: DomTagFn<HTMLElement>;
681
+ figcaption: DomTagFn<HTMLElement>;
682
+ address: DomTagFn<HTMLElement>;
683
+ data: DomTagFn<HTMLDataElement>;
684
+ map: DomTagFn<HTMLMapElement>;
685
+ area: DomTagFn<HTMLAreaElement>;
632
686
  }
633
687
 
634
688
  /**
@@ -646,10 +700,13 @@ interface DomHTMLTags {
646
700
  * let chart = dom.priceChart({ asset }) // component
647
701
  * chart.$({ asset: newAsset }) // update later
648
702
  */
649
- interface DomProxy extends DomHTMLTags {
650
- /** Any tag name - CamelCase converts to kebab-case. Returns Element (or MxElement for components). */
651
- [tag: string]: DomTagFn<Element>;
652
- }
703
+ type DomProxy = DomHTMLTags & {
704
+ /**
705
+ * Any tag name - CamelCase converts to kebab-case. A custom tag is always an
706
+ * HTMLElement, and an MxElement (with `$`, `$state` and its mixins) for a component.
707
+ */
708
+ [tag: string]: DomTagFn<MxElement>;
709
+ };
653
710
 
654
711
  // ---------------------------------------------------------------------------
655
712
  // define() - register a component
@@ -669,7 +726,7 @@ interface DomProxy extends DomHTMLTags {
669
726
  * }
670
727
  * })
671
728
  */
672
- type MxDefine = (name: string, definition: MxComponentDefinition) => void;
729
+ type MxDefine = (name: string, definition?: MxComponentDefinition) => void;
673
730
 
674
731
  // ---------------------------------------------------------------------------
675
732
  // Element.prototype augmentations
@@ -679,7 +736,8 @@ declare global {
679
736
  interface Element {
680
737
  /**
681
738
  * Reconcile children left-to-right against existing DOM.
682
- * Accepts mx descriptions, real DOM nodes, strings, numbers, null, false.
739
+ * Accepts mx descriptions, real DOM nodes, strings, numbers, null, false,
740
+ * undefined, and plain arrays of these (flattened, nested arrays too).
683
741
  * Excess old nodes are removed. Returns `this`.
684
742
  */
685
743
  render(...children: MxChild[]): this;
@@ -703,17 +761,7 @@ declare global {
703
761
  $(props?: Record<string, any>): this;
704
762
  }
705
763
 
706
- /** mx - virtual description proxy */
707
- var mx: MxProxy;
708
-
709
- /** dom - real element proxy */
710
- var dom: DomProxy;
711
-
712
- /** Register a component */
713
- var define: MxDefine;
714
764
 
715
- /** Component registry - maps lowercase names to definitions */
716
- var components: Record<string, MxComponentDefinition>;
717
765
  }
718
766
 
719
767
  // ---------------------------------------------------------------------------
package/mx.min.js CHANGED
@@ -1 +1 @@
1
- !function(m,i,_,u){let $='http://www.w3.org/2000/svg',t={__proto__:null,value:1,checked:1,selected: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],k=i.slice(1);_[i]===$?'.'==i[0]?(m[k]===$||(m[k]=$),n[k]=l):t[i]?(m[i]===$||(m[i]=$),n[i]=l):$?.call&&(n[i]=l):'.'==i[0]?(m[k]===$||(m[k]=$),n[k]=l,_[i]=$):$!=null&!1!==$?($.call?(m[i]=$,n[i]=l):(t[i]?(m[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.R(i))}m=m.nextSibling}for(let $;m;m=$)$=m.nextSibling,i.removeChild(m);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.render(..._).$attrs(i):m.render(i,..._),m))}(document,Element.prototype)
1
+ !function(l,i){let f=Object.assign,d={__proto__:null,value:1,selected:1,checked:1},R=components={},F=_=>_.flatMap(_=>_?._||!_?.pop?[_]:[!1,...F(_)]),M=f=>{for(let _ of f)_?.nodeType?_[g]=f:_?._||_?.pop&&f.splice(0,1/0,...F(f))},A=Symbol(),g=Symbol(),w=l=>s[l]||=l.replace(/[A-Z]/g,'-$&').toLowerCase(),s={},$=(l,n={})=>new Proxy(l,{get:(_,i)=>n[i]||=l.bind(0,i)}),n=(i,d)=>'svg'!=i&!d?f(l.createElement(i),R[i]):l.createElementNS(_,i),_='http://www.w3.org/2000/svg';define=(i,l={})=>{R[w(i)]=l;if(l.$)l.$$||=l.$,l.$=function(i){let _=l.$$.call(this,f(this.$state||={},i));return this.render(..._?._?[_]:_||[])}},dom=$((l,i,..._)=>(l=n(w(l)),i?.__proto__==Object.prototype&&!i?.nodeType?l.$?l.$(i):l.render(..._).$attrs(i):l.render(i,..._),l)),i.render=function(...f){let i=this,R=i.namespaceURI==_&'foreignObject'!=i.localName,l=i.firstChild,e;for(let _ of f){let $,w,m;if(_==null|!1===_)continue;if(_._)[$,w,m]=_;else if(_.nodeType){e||M(e=f);for(;l&&l!=_&&l[g]!=f;l=$)$=l.nextSibling,i.removeChild(l);l!=_&&(l?.nextSibling==_?i.removeChild(l):i.insertBefore(_,l)),l=_.nextSibling;continue}else if(_.pop){f.splice(0,1/0,...F(f));continue}(l?.[g]?(e||M(e=f),l[g]!=f):l)?!$?l.splitText?l.data!=''+_&&(l.data=''+_):l.replaceWith(l=new Text(_)):$!=l.localName&&l.replaceWith(l=n($,R)):l=i.insertBefore($?n($,R):new Text(_),l);if($)if(l.$)l[A]=0,l.$(w);else{let R=l[A]||={__proto__:null};for(let i in R)w&&Object.hasOwn(w,i)||(i[0]=='.'?(delete R[i],d[i=i.slice(1)]||w&&Object.hasOwn(w,i)&&w[i]||(l[i]=null,l.R(i))):(d[i]?l[i]='':R[i]?.call&&(l[i]=null),l.R(i),delete R[i]));l.render(...m);for(let i in w)_=w[i],i[0]=='.'?(R[i]=_,l[i=i.slice(1)]===_||(l[i]=_)):i in R&&R[i]===_?d[i]&&(l[i]===_||(l[i]=_)):_!=null&!1!==_?(_.call?l[i]=_:(d[i]&&(l[i]===_||(l[i]=_)),l.setAttribute(i,_)),R[i]=_):(d[i]?l[i]==''||(l[i]=''):R[i]?.call&&(l[i]=null),l.R(i),delete R[i])}l=l.nextSibling}for(let $;l;l=$)$=l.nextSibling,i.removeChild(l);return this},i.$attrs=function(l,i,_){for(i in l)_=l[i],i[0]=='.'?this[i.slice(1)]=_:_!=null&!1!==_?(_.call?this[i]=_:this.setAttribute(i,_)):this.R(i);return this},i.R=i.removeAttribute,mx=$((l,i,..._)=>(l=w(l),_=i?.__proto__==Object.prototype&&!i?.nodeType?[l,i,_]:[l,null,[i,..._]],_._=1,_))}(document,Element.prototype)
package/mx.mjs CHANGED
@@ -1,3 +1,3 @@
1
1
  let mx, dom, define, components;
2
- !function(m,i,_,u){let $='http://www.w3.org/2000/svg',t={__proto__:null,value:1,checked:1,selected: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],k=i.slice(1);_[i]===$?'.'==i[0]?(m[k]===$||(m[k]=$),n[k]=l):t[i]?(m[i]===$||(m[i]=$),n[i]=l):$?.call&&(n[i]=l):'.'==i[0]?(m[k]===$||(m[k]=$),n[k]=l,_[i]=$):$!=null&!1!==$?($.call?(m[i]=$,n[i]=l):(t[i]?(m[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.R(i))}m=m.nextSibling}for(let $;m;m=$)$=m.nextSibling,i.removeChild(m);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.render(..._).$attrs(i):m.render(i,..._),m))}(document,Element.prototype)
2
+ !function(l,i){let f=Object.assign,d={__proto__:null,value:1,selected:1,checked:1},R=components={},F=_=>_.flatMap(_=>_?._||!_?.pop?[_]:[!1,...F(_)]),M=f=>{for(let _ of f)_?.nodeType?_[g]=f:_?._||_?.pop&&f.splice(0,1/0,...F(f))},A=Symbol(),g=Symbol(),w=l=>s[l]||=l.replace(/[A-Z]/g,'-$&').toLowerCase(),s={},$=(l,n={})=>new Proxy(l,{get:(_,i)=>n[i]||=l.bind(0,i)}),n=(i,d)=>'svg'!=i&!d?f(l.createElement(i),R[i]):l.createElementNS(_,i),_='http://www.w3.org/2000/svg';define=(i,l={})=>{R[w(i)]=l;if(l.$)l.$$||=l.$,l.$=function(i){let _=l.$$.call(this,f(this.$state||={},i));return this.render(..._?._?[_]:_||[])}},dom=$((l,i,..._)=>(l=n(w(l)),i?.__proto__==Object.prototype&&!i?.nodeType?l.$?l.$(i):l.render(..._).$attrs(i):l.render(i,..._),l)),i.render=function(...f){let i=this,R=i.namespaceURI==_&'foreignObject'!=i.localName,l=i.firstChild,e;for(let _ of f){let $,w,m;if(_==null|!1===_)continue;if(_._)[$,w,m]=_;else if(_.nodeType){e||M(e=f);for(;l&&l!=_&&l[g]!=f;l=$)$=l.nextSibling,i.removeChild(l);l!=_&&(l?.nextSibling==_?i.removeChild(l):i.insertBefore(_,l)),l=_.nextSibling;continue}else if(_.pop){f.splice(0,1/0,...F(f));continue}(l?.[g]?(e||M(e=f),l[g]!=f):l)?!$?l.splitText?l.data!=''+_&&(l.data=''+_):l.replaceWith(l=new Text(_)):$!=l.localName&&l.replaceWith(l=n($,R)):l=i.insertBefore($?n($,R):new Text(_),l);if($)if(l.$)l[A]=0,l.$(w);else{let R=l[A]||={__proto__:null};for(let i in R)w&&Object.hasOwn(w,i)||(i[0]=='.'?(delete R[i],d[i=i.slice(1)]||w&&Object.hasOwn(w,i)&&w[i]||(l[i]=null,l.R(i))):(d[i]?l[i]='':R[i]?.call&&(l[i]=null),l.R(i),delete R[i]));l.render(...m);for(let i in w)_=w[i],i[0]=='.'?(R[i]=_,l[i=i.slice(1)]===_||(l[i]=_)):i in R&&R[i]===_?d[i]&&(l[i]===_||(l[i]=_)):_!=null&!1!==_?(_.call?l[i]=_:(d[i]&&(l[i]===_||(l[i]=_)),l.setAttribute(i,_)),R[i]=_):(d[i]?l[i]==''||(l[i]=''):R[i]?.call&&(l[i]=null),l.R(i),delete R[i])}l=l.nextSibling}for(let $;l;l=$)$=l.nextSibling,i.removeChild(l);return this},i.$attrs=function(l,i,_){for(i in l)_=l[i],i[0]=='.'?this[i.slice(1)]=_:_!=null&!1!==_?(_.call?this[i]=_:this.setAttribute(i,_)):this.R(i);return this},i.R=i.removeAttribute,mx=$((l,i,..._)=>(l=w(l),_=i?.__proto__==Object.prototype&&!i?.nodeType?[l,i,_]:[l,null,[i,..._]],_._=1,_))}(document,Element.prototype)
3
3
  export { mx, dom, define, components };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tltdsh/mx",
3
- "version": "1.4.0",
4
- "description": "<1KB UI engine. Build big. Ship small. Iterate fast.",
3
+ "version": "1.5.0",
4
+ "description": "A sub-1KB (brotli) DOM reconciliation engine for building user interfaces in plain JavaScript",
5
5
  "license": "0BSD",
6
6
  "author": "tltdsh",
7
7
  "homepage": "https://mxjs.dev",
@@ -11,18 +11,21 @@
11
11
  "types": "mx.d.ts",
12
12
  "exports": {
13
13
  ".": {
14
+ "types": "./mx.d.ts",
14
15
  "import": "./mx.mjs",
15
16
  "require": "./mx.min.js",
16
- "types": "./mx.d.ts",
17
17
  "default": "./mx.min.js"
18
- }
18
+ },
19
+ "./classic": { "types": "./classic.d.ts", "default": "./mx.min.js" }
19
20
  },
20
21
  "files": [
21
22
  "mx.min.js",
22
23
  "mx.mjs",
23
24
  "mx.d.ts",
25
+ "classic.d.ts",
24
26
  "AGENTS.md",
25
27
  "CLAUDE.md",
28
+ "skill",
26
29
  "README.md",
27
30
  "LICENSE"
28
31
  ],
@@ -38,8 +41,6 @@
38
41
  "dom-patching",
39
42
  "dom-manipulation",
40
43
  "dom-first",
41
- "reactive",
42
- "reactivity",
43
44
  "render",
44
45
  "rendering",
45
46
  "reconciliation",
package/skill/SKILL.md ADDED
@@ -0,0 +1,135 @@
1
+ ---
2
+ name: mx
3
+ description: Write and debug UI code with mx.js, the sub-1KB DOM reconciler with no JSX, hooks or virtual DOM. Use when a project loads ui-mx.min.js, ui-mx.js or @tltdsh/mx, or calls mx.tag(), dom.tag(), define(), this.$() or el.render().
4
+ license: 0BSD
5
+ ---
6
+
7
+ # mx skill
8
+
9
+ `ui-mx.js` sits next to this file. It is the readable engine source and the authority on behaviour, so when the rules below leave a question open, read it rather than guess. `examples.md` holds four canonical patterns copied from the docs.
10
+
11
+ ---
12
+
13
+ # mx.js
14
+
15
+ > A sub-1KB (brotli) DOM reconciliation engine for building UIs in plain
16
+ > JavaScript. Four API names, zero dependencies, no virtual DOM, no build step.
17
+ > Small enough to paste into an AI context window. mx is a reconciler, NOT a
18
+ > framework: no JSX, no hooks, no signals, no synthetic events.
19
+
20
+ If you are an AI agent writing mx, do not pattern-match it onto React, Vue, or
21
+ Svelte. That is the source of almost every mistake. Use native DOM names.
22
+
23
+ ## The four API names
24
+
25
+ The classic script puts them on window. The ESM build exports them instead and
26
+ creates no globals: `import { mx, dom, define, components } from '@tltdsh/mx'`.
27
+
28
+ - `mx.tag(attrs?, ...children)` -> a description ARRAY (`._ === 1`); hand it to `render()`.
29
+ - `dom.tag(attrs?, ...children)` -> a real element you can keep a handle to (and call `.$()` on, if it is a component).
30
+ - `define(name, { $(props) {} })` -> register a component. camelCase and kebab-case names both convert to a kebab tag.
31
+ - `components` -> the component registry object.
32
+
33
+ Decision rule: need to hold a reference and update it later? use `dom`. Just
34
+ producing output for `render()`? use `mx`.
35
+
36
+ ## Components
37
+
38
+ ```
39
+ define('user-card', {
40
+ $({ name = '', active = false }) { // receives the merged $state
41
+ return [
42
+ mx.span({ class: 'name' }, name),
43
+ active && mx.span({ class: 'badge' }, 'online')
44
+ ];
45
+ }
46
+ });
47
+ container.render(mx.userCard({ name: 'Ada', active: true }));
48
+ ```
49
+
50
+ `$` runs on every `.$()` call and every time a parent render reaches the
51
+ component through an mx description (`mx.tag(...)`); `dom.tag(attrs)` runs it
52
+ once at creation, `dom.tag()` with no attrs does not run it until you call
53
+ `.$()`. A component passed to `render()` as a real node (a kept `dom()` handle)
54
+ is only placed, not re-run: call `el.$({...})` to update it. It is not an
55
+ attach/detach hook. Return one mx description OR an array of children (a bare
56
+ DOM node throws; use `[node]`). A component's attrs all go to `$state`, none
57
+ become HTML attributes, and children passed to `mx.tag(attrs, ...children)` are
58
+ ignored.
59
+
60
+ ## State ($state)
61
+
62
+ - `this.$({ key: val })` does `Object.assign($state, { key: val })` then re-runs `$`.
63
+ - A parent's `el.$({...})` merges the same way. The merge runs BEFORE your `$`, so THE PARENT ALWAYS WINS any key it re-passes. `??=` does not protect against this; seed a private `this._x` for state that must survive re-passes.
64
+ - Never assign `this.$state.x = v` directly (no merge, no re-render).
65
+ - `this.$({})` with an empty object is a code smell - pass the actual change.
66
+
67
+ ## Attributes (native DOM names, lowercase)
68
+
69
+ - `onclick: fn` - events lowercase, set as a property. NOT `onClick`.
70
+ - Custom event types can't use `on*` props (silent expando) - use `addEventListener` once, guarded.
71
+ - `class: 'x'` - NOT `className`.
72
+ - `for: 'id'` - NOT `htmlFor`.
73
+ - `'.value': v` - a leading dot sets a JS property instead of an attribute.
74
+ - `disabled: true` -> `disabled="true"`; `disabled: null` / `false` / `undefined` -> removed.
75
+ - `style: 'width:' + pct + '%'` - style is a STRING, never an object.
76
+ - In `render()`, `value`, `checked`, `selected` set both attribute and property, re-asserted every render. `dom.tag(attrs)` sets the attribute only.
77
+
78
+ ## render() rules
79
+
80
+ - mx description -> create/reuse by tag name, apply attrs, recurse children.
81
+ - real DOM node -> match by identity (===); moves with its internal state.
82
+ - string / number -> text node (numbers auto-cast; no `String()` needed).
83
+ - `false` / `null` / `undefined` -> skipped. `0` is NOT skipped; it renders as "0".
84
+ - plain array -> flattened into the child list, nested arrays too (a description carries `._` and stays whole).
85
+ - excess old children are removed.
86
+
87
+ ## The rules agents get wrong
88
+
89
+ - Guard numbers before `&&`: `!!list.length && mx.div(...)`, never `list.length && ...`.
90
+ - Arrays in children flatten, nested arrays too: `mx.ul(items.map(i => mx.li(i.name)))` and `mx.ul(...items.map(...))` render the same. mx 1.4.1 and earlier stringify an unspread array to "li,,a,li,,b", so spread when targeting those.
91
+ - No PascalCase tags: `mx.UserCard()` -> `<-user-card>`, which throws `InvalidCharacterError` at render. Use `mx.userCard()`.
92
+ - Copy prop arrays before sorting: `[...items].sort(...)`, never `items.sort(...)`.
93
+ - Never `addEventListener` inside `$()` (stacks each render); assign `this.onclick = fn`.
94
+ - `clearInterval(this._interval)` at the TOP of `$()` before re-arming a timer.
95
+ - Hoist static option/config arrays OUT of `$()`; don't re-allocate per render.
96
+ - Stateful list rows: use a keyed `dom()` Map matched by identity, not positional `mx()` rows (positional rows leave typed input at the old index after a sort).
97
+ - `key` is not special: `mx.li({ key: id }, ...)` writes a plain `key="..."` attribute, rows still match by position (tag name, left to right) and nothing warns. For identity, use the keyed `dom()` Map above.
98
+ - Drive logic from `$state` / data attributes, never from rendered text (`el.textContent`).
99
+ - Write `$(props) {}` (or `$: function(props) {}`). Never an arrow `$: props =>`: it loses `this`. A Svelte `$:` label is not reactive.
100
+
101
+ ## Docs
102
+
103
+ - [Getting Started](https://mxjs.dev/getting-started): install via CDN or npm, the four API names, a first component
104
+ - [Elements & Render](https://mxjs.dev/elements): how mx() and dom() build elements, apply attributes and events, and how render() reconciles
105
+ - [Components](https://mxjs.dev/components): define(), $state, slots, composition, updates from outside
106
+ - [State](https://mxjs.dev/state): persistent state, the event bus, cross-component coordination
107
+ - [Lifecycle](https://mxjs.dev/lifecycle): owning less, isConnected and MutationObserver cleanup, when custom element callbacks help
108
+ - [Forms](https://mxjs.dev/forms): controlled and uncontrolled inputs, validation, debounced input, multi-step forms
109
+ - [Patterns](https://mxjs.dev/patterns): mx() vs dom(), keyed dom() Maps, sortable tables, virtual scroll, drag and drop
110
+ - [API Reference](https://mxjs.dev/reference): mx(), dom(), define(), render(), $attrs(), components
111
+ - [Cheat Sheet](https://mxjs.dev/cheatsheet): one-page API summary, attributes, render rules
112
+ - [Gotchas](https://mxjs.dev/gotchas): common mistakes, each with the fix
113
+ - [For AI agents](https://mxjs.dev/ai): paste-ready system prompt, the React/Vue/Svelte habits that break mx, a self-check list
114
+ - [Testing](https://mxjs.dev/testing): direct DOM assertions and browser checks
115
+ - [What mx is not](https://mxjs.dev/what-mx-is-not): what mx deliberately leaves out, and what to use instead
116
+
117
+ ## Source
118
+
119
+ - [Engine](https://mxjs.dev/ui-mx.min.js): the whole reconciler, minified
120
+ - [Engine, readable source](https://cdn.jsdelivr.net/npm/@tltdsh/mx/skill/ui-mx.js): the whole reconciler, the authority on behaviour
121
+ - [Agent skill](https://mxjs.dev/mx-skill.zip): these rules plus the readable engine source, installable in a skills folder
122
+ - [AGENTS.md in the npm package](https://cdn.jsdelivr.net/npm/@tltdsh/mx/AGENTS.md): rules for coding agents
123
+ - [npm package](https://www.npmjs.com/package/@tltdsh/mx): @tltdsh/mx
124
+
125
+ ## Optional
126
+
127
+ - [Installation](https://mxjs.dev/installation): CDN, npm, ES modules, sizes
128
+ - [Tutorial](https://mxjs.dev/tutorial): a contact form with validation, step by step
129
+ - [Accessibility](https://mxjs.dev/accessibility): aria attributes, focus management, keyboard navigation, live regions
130
+ - [Web Components](https://mxjs.dev/web-components): mx inside custom elements, Shadow DOM, interop
131
+ - [Architecture](https://mxjs.dev/architecture): routing, lazy loading, real-time data, large apps
132
+ - [Best practices](https://mxjs.dev/best-practices): events, properties, security, component patterns
133
+ - [Examples](https://mxjs.dev/examples): small complete apps to read and run
134
+ - [Performance](https://mxjs.dev/performance): benchmarks against other frameworks
135
+ - [Changelog](https://mxjs.dev/changelog): what changed in each version
@@ -0,0 +1,107 @@
1
+ # mx examples
2
+
3
+ Copied from the mxjs.dev docs at build time, so each one matches the published sample.
4
+
5
+ ## A component with $state
6
+
7
+ Props arrive merged into `$state`, and `this.$({...})` merges and re-renders.
8
+
9
+ https://mxjs.dev/components#stateful-components
10
+
11
+ ```js
12
+ define('a-counter', {
13
+ $({ count = 0, step = 1 }) {
14
+ let { div, button, span } = mx;
15
+ return div(
16
+ button({ onclick: _ => this.$({ count: count - step }) }, '−'),
17
+ span(count),
18
+ button({ onclick: _ => this.$({ count: count + step }) }, '+')
19
+ );
20
+ }
21
+ });
22
+
23
+ document.body.render(
24
+ mx.aCounter({ count: 10, step: 5 })
25
+ );
26
+ ```
27
+
28
+ ## Keyed dom() rows
29
+
30
+ Stateful rows live in a Map of real nodes, so a reorder moves them along with their typed input.
31
+
32
+ https://mxjs.dev/gotchas#positional-reorder
33
+
34
+ ```js
35
+ // dom() + Map: identity-stable rows
36
+ let rows = new Map;
37
+ for (let i of items) {
38
+ let el = rows.get(i.id) ?? rows.set(i.id, dom.div()).get(i.id);
39
+ el.render(mx.span(i.name), mx.input({ placeholder: 'notes...' }));
40
+ }
41
+ container.render(...items.map(i => rows.get(i.id))); // nodes physically move
42
+ ```
43
+
44
+ ## A form
45
+
46
+ One `oninput` on the form hears every field, and `.value` sets the property.
47
+
48
+ https://mxjs.dev/forms#form-oninput
49
+
50
+ ```js
51
+ let { form, label, input, button, p } = mx;
52
+
53
+ let name = '', email = '';
54
+ let host = dom.div();
55
+
56
+ function update() {
57
+ host.render(
58
+ form({
59
+ // One listener on the form hears every field: input events bubble.
60
+ oninput(e) {
61
+ if (e.target.name === 'name') name = e.target.value;
62
+ if (e.target.name === 'email') email = e.target.value;
63
+ update();
64
+ },
65
+ onsubmit(e) {
66
+ e.preventDefault();
67
+ alert('Submitting: ' + JSON.stringify({ name, email }));
68
+ }
69
+ },
70
+ label('Name', input({ name: 'name', type: 'text', '.value': name })),
71
+ label('Email', input({ name: 'email', type: 'email', '.value': email })),
72
+ button({ type: 'submit' }, 'Submit'),
73
+ name ? p('Hello, ' + name + (email ? ' (' + email + ')' : '')) : null
74
+ )
75
+ );
76
+ }
77
+
78
+ update();
79
+ document.body.render(host);
80
+ ```
81
+
82
+ ## Parasitic hydration
83
+
84
+ `render()` reuses server-rendered nodes that match by tag, without hydration markers. Custom elements that mx did not create need their `define()` methods attached once.
85
+
86
+ https://mxjs.dev/what-mx-is-not#no-ssr
87
+
88
+ ```html
89
+ <!-- Static HTML shipped by server, minified -->
90
+ <div id="app"><h1>Welcome</h1><p>Server-rendered content</p></div>
91
+
92
+ <script>
93
+ // Client picks up where server left off - no hydration marker needed
94
+ document.getElementById('app').render(
95
+ mx.h1('Welcome'), // matches existing h1, reused
96
+ mx.p('Client-updated content') // matches existing p, text updated
97
+ );
98
+ </script>
99
+ ```
100
+
101
+ https://mxjs.dev/architecture#ssg-upgrade
102
+
103
+ ```js
104
+ // In your app entry point, after all define() calls have executed:
105
+ document.querySelectorAll(Object.keys(components)).forEach(el =>
106
+ !el.$ && Object.assign(el, components[el.localName]));
107
+ ```