@tltdsh/mx 1.4.1 → 1.6.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;
@@ -151,6 +155,10 @@ interface MxHTMLAttrs {
151
155
  'aria-invalid'?: boolean | 'true' | 'false' | 'grammar' | 'spelling';
152
156
  'aria-readonly'?: boolean | 'true' | 'false';
153
157
 
158
+ // ARIA misc cluster (accessibility)
159
+ 'aria-modal'?: boolean | 'true' | 'false';
160
+ 'aria-roledescription'?: string;
161
+
154
162
  // Loading
155
163
  fetchpriority?: 'high' | 'low' | 'auto';
156
164
 
@@ -303,6 +311,12 @@ interface MxSVGAttrs {
303
311
  'stop-opacity'?: string | number;
304
312
  }
305
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
+
306
320
  /**
307
321
  * Full attribute type. Supports:
308
322
  * - Standard HTML/SVG attributes (autocomplete)
@@ -312,7 +326,7 @@ interface MxSVGAttrs {
312
326
  * - `name: true` - sets attribute to the string "true" (`setAttribute(name, 'true')`)
313
327
  * - `[key: string]: any` - custom attributes, data-*, aria-*
314
328
  */
315
- type MxAttrs = MxHTMLAttrs & MxSVGAttrs & {
329
+ type MxAttrs = MxRemovable<MxHTMLAttrs> & MxRemovable<MxSVGAttrs> & {
316
330
  [key: string]: any;
317
331
  };
318
332
 
@@ -325,9 +339,10 @@ interface MxComponentDefinition {
325
339
  /**
326
340
  * Render function. Called with merged $state + incoming props.
327
341
  * `this` is the component element.
328
- * 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.
329
344
  */
330
- $?(this: MxElement, props: Record<string, any>): MxChild | MxChild[];
345
+ $?(this: MxElement, props: Record<string, any>): MxDescription | readonly MxChild[] | null | undefined | false | void;
331
346
 
332
347
  /** Any other properties are mixed into the created element */
333
348
  [key: string]: any;
@@ -346,6 +361,9 @@ interface MxElement extends HTMLElement {
346
361
  * reconciles DOM. Returns this element.
347
362
  */
348
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;
349
367
  }
350
368
 
351
369
  // ---------------------------------------------------------------------------
@@ -524,7 +542,7 @@ interface MxHTMLTags {
524
542
  *
525
543
  * @example
526
544
  * mx.div({ class: 'card' }, mx.h2('Title'), mx.p('Body'))
527
- * 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) })
528
546
  * mx.myComponent({ count: 5 })
529
547
  */
530
548
  interface MxProxy extends MxHTMLTags {
@@ -566,16 +584,30 @@ interface DomHTMLTags {
566
584
  em: DomTagFn<HTMLElement>;
567
585
  b: DomTagFn<HTMLElement>;
568
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>;
569
593
  code: DomTagFn<HTMLElement>;
570
594
  pre: DomTagFn<HTMLPreElement>;
595
+ kbd: DomTagFn<HTMLElement>;
596
+ abbr: DomTagFn<HTMLElement>;
597
+ cite: DomTagFn<HTMLElement>;
598
+ q: DomTagFn<HTMLQuoteElement>;
599
+ time: DomTagFn<HTMLTimeElement>;
571
600
  br: DomTagFn<HTMLBRElement>;
572
601
  hr: DomTagFn<HTMLHRElement>;
602
+ wbr: DomTagFn<HTMLElement>;
573
603
 
574
604
  // Lists
575
605
  ul: DomTagFn<HTMLUListElement>;
576
606
  ol: DomTagFn<HTMLOListElement>;
577
607
  li: DomTagFn<HTMLLIElement>;
578
608
  dl: DomTagFn<HTMLDListElement>;
609
+ dt: DomTagFn<HTMLElement>;
610
+ dd: DomTagFn<HTMLElement>;
579
611
 
580
612
  // Table
581
613
  table: DomTagFn<HTMLTableElement>;
@@ -586,6 +618,8 @@ interface DomHTMLTags {
586
618
  th: DomTagFn<HTMLTableCellElement>;
587
619
  td: DomTagFn<HTMLTableCellElement>;
588
620
  caption: DomTagFn<HTMLTableCaptionElement>;
621
+ colgroup: DomTagFn<HTMLTableColElement>;
622
+ col: DomTagFn<HTMLTableColElement>;
589
623
 
590
624
  // Forms
591
625
  form: DomTagFn<HTMLFormElement>;
@@ -598,6 +632,7 @@ interface DomHTMLTags {
598
632
  label: DomTagFn<HTMLLabelElement>;
599
633
  fieldset: DomTagFn<HTMLFieldSetElement>;
600
634
  legend: DomTagFn<HTMLLegendElement>;
635
+ datalist: DomTagFn<HTMLDataListElement>;
601
636
  output: DomTagFn<HTMLOutputElement>;
602
637
  progress: DomTagFn<HTMLProgressElement>;
603
638
  meter: DomTagFn<HTMLMeterElement>;
@@ -608,8 +643,11 @@ interface DomHTMLTags {
608
643
  source: DomTagFn<HTMLSourceElement>;
609
644
  video: DomTagFn<HTMLVideoElement>;
610
645
  audio: DomTagFn<HTMLAudioElement>;
646
+ track: DomTagFn<HTMLTrackElement>;
611
647
  canvas: DomTagFn<HTMLCanvasElement>;
612
648
  iframe: DomTagFn<HTMLIFrameElement>;
649
+ object: DomTagFn<HTMLObjectElement>;
650
+ embed: DomTagFn<HTMLEmbedElement>;
613
651
 
614
652
  // SVG
615
653
  //
@@ -626,6 +664,7 @@ interface DomHTMLTags {
626
664
 
627
665
  // Interactive
628
666
  details: DomTagFn<HTMLDetailsElement>;
667
+ summary: DomTagFn<HTMLElement>;
629
668
  dialog: DomTagFn<HTMLDialogElement>;
630
669
  menu: DomTagFn<HTMLMenuElement>;
631
670
 
@@ -635,7 +674,15 @@ interface DomHTMLTags {
635
674
  style: DomTagFn<HTMLStyleElement>;
636
675
  link: DomTagFn<HTMLLinkElement>;
637
676
  slot: DomTagFn<HTMLSlotElement>;
677
+ meta: DomTagFn<HTMLMetaElement>;
678
+ noscript: DomTagFn<HTMLElement>;
638
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>;
639
686
  }
640
687
 
641
688
  /**
@@ -653,10 +700,13 @@ interface DomHTMLTags {
653
700
  * let chart = dom.priceChart({ asset }) // component
654
701
  * chart.$({ asset: newAsset }) // update later
655
702
  */
656
- interface DomProxy extends DomHTMLTags {
657
- /** Any tag name - CamelCase converts to kebab-case. Returns Element (or MxElement for components). */
658
- [tag: string]: DomTagFn<Element>;
659
- }
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
+ };
660
710
 
661
711
  // ---------------------------------------------------------------------------
662
712
  // define() - register a component
@@ -676,7 +726,7 @@ interface DomProxy extends DomHTMLTags {
676
726
  * }
677
727
  * })
678
728
  */
679
- type MxDefine = (name: string, definition: MxComponentDefinition) => void;
729
+ type MxDefine = (name: string, definition?: MxComponentDefinition) => void;
680
730
 
681
731
  // ---------------------------------------------------------------------------
682
732
  // Element.prototype augmentations
@@ -686,7 +736,8 @@ declare global {
686
736
  interface Element {
687
737
  /**
688
738
  * Reconcile children left-to-right against existing DOM.
689
- * 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).
690
741
  * Excess old nodes are removed. Returns `this`.
691
742
  */
692
743
  render(...children: MxChild[]): this;
@@ -697,9 +748,6 @@ declare global {
697
748
  */
698
749
  $attrs(attrs: MxAttrs): this;
699
750
 
700
- /** Alias for removeAttribute (used internally) */
701
- R(name: string): void;
702
-
703
751
  /** Component state - merged via Object.assign on each $() call */
704
752
  $state: Record<string, any>;
705
753
 
@@ -710,17 +758,7 @@ declare global {
710
758
  $(props?: Record<string, any>): this;
711
759
  }
712
760
 
713
- /** mx - virtual description proxy */
714
- var mx: MxProxy;
715
-
716
- /** dom - real element proxy */
717
- var dom: DomProxy;
718
-
719
- /** Register a component */
720
- var define: MxDefine;
721
761
 
722
- /** Component registry - maps lowercase names to definitions */
723
- var components: Record<string, MxComponentDefinition>;
724
762
  }
725
763
 
726
764
  // ---------------------------------------------------------------------------
package/mx.min.js CHANGED
@@ -1 +1 @@
1
- !function(l,i){let _='http://www.w3.org/2000/svg',w=l=>s[l]||=l.replace(/[A-Z]/g,'-$&').toLowerCase(),A=Symbol(),f=Object.assign,R=components={},$=(l,n={})=>new Proxy(l,{get:(_,i)=>n[i]||=l.bind(0,i)}),g=Symbol(),s={},n=(i,d)=>'svg'!=i&!d?f(l.createElement(i),R[i]):l.createElementNS(_,i),d={__proto__:null,value:1,selected:1,checked:1};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){if(!e){for(let _ of f)_?.nodeType&&(_[g]=f);e=1}for(;l&&l!=_&&l[g]!=f;l=$)$=l.nextSibling,i.removeChild(l);l!=_&&(l?.nextSibling==_?i.removeChild(l):i.insertBefore(_,l)),l=_.nextSibling;continue}if(l)!$?l.splitText?l.data!=''+_&&(l.data=''+_):l.replaceWith(l=new Text(_)):$!=l.localName&&l.replaceWith(l=n($,R));else i.append(l=$?n($,R):new Text(_));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)
1
+ !function(l,i){let f=Object.assign,d={__proto__:null,value:1,selected:1,checked:1},R=components={__proto__:null},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={__proto__:null},$=(l,n={__proto__:null})=>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.removeAttribute(i))):(d[i]?l[i]='':R[i]?.call&&(l[i]=null),l.removeAttribute(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.removeAttribute(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.removeAttribute(i);return this},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(l,i){let _='http://www.w3.org/2000/svg',w=l=>s[l]||=l.replace(/[A-Z]/g,'-$&').toLowerCase(),A=Symbol(),f=Object.assign,R=components={},$=(l,n={})=>new Proxy(l,{get:(_,i)=>n[i]||=l.bind(0,i)}),g=Symbol(),s={},n=(i,d)=>'svg'!=i&!d?f(l.createElement(i),R[i]):l.createElementNS(_,i),d={__proto__:null,value:1,selected:1,checked:1};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){if(!e){for(let _ of f)_?.nodeType&&(_[g]=f);e=1}for(;l&&l!=_&&l[g]!=f;l=$)$=l.nextSibling,i.removeChild(l);l!=_&&(l?.nextSibling==_?i.removeChild(l):i.insertBefore(_,l)),l=_.nextSibling;continue}if(l)!$?l.splitText?l.data!=''+_&&(l.data=''+_):l.replaceWith(l=new Text(_)):$!=l.localName&&l.replaceWith(l=n($,R));else i.append(l=$?n($,R):new Text(_));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)
2
+ !function(l,i){let f=Object.assign,d={__proto__:null,value:1,selected:1,checked:1},R=components={__proto__:null},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={__proto__:null},$=(l,n={__proto__:null})=>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.removeAttribute(i))):(d[i]?l[i]='':R[i]?.call&&(l[i]=null),l.removeAttribute(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.removeAttribute(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.removeAttribute(i);return this},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.1",
4
- "description": "<1KB UI engine. Build big. Ship small. Iterate fast.",
3
+ "version": "1.6.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
+ ```