UNPKG

nexora

Version:

A lightweight, production-ready JavaScript library for building user interfaces, supporting JSX.

2 lines (1 loc) 7.92 kB
(function(i,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(i=typeof globalThis<"u"?globalThis:i||self,p(i.Nexora={}))})(this,function(i){"use strict";var B=Object.defineProperty;var q=(i,p,l)=>p in i?B(i,p,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[p]=l;var _=(i,p,l)=>q(i,typeof p!="symbol"?p+"":p,l);function p(e,t){if(e===t)return!0;if(typeof e!="object"||e===null||typeof t!="object"||t===null)return!1;const n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(const s of n)if(!t.hasOwnProperty(s)||e[s]!==t[s])return!1;return!0}function l(e){return e?e._dom?e._dom:e._rendered?l(e._rendered):null:null}function O(e){let t=null;return n=>!n&&!t?e(n):!t||!p(t,n)?(t={...n},e(n)):null}const w=new Map,R=new WeakMap,P=new WeakMap;function D(e){var o;const t=y.currentComponentFn;if(!t)throw new Error("onInit must be called within a component");w.has(t)||(w.set(t,[]),R.set(t,new Map)),(o=w.get(t))==null||o.push(e);const n=R.get(t);if(!n.has(e)){const[c,a]=y.createState(null);n.set(e,[c,a]),e().then(h=>{a(h),y.triggerUpdate(t)})}const[r,s]=n.get(e);return[r(),s]}function A(e){const t=w.get(e);return t&&(y.currentComponentFn=e,t.forEach(n=>{const r=Promise.resolve(n()).then(s=>(s!==void 0&&R.set(e,s),s));P.set(e,r)})),R.get(e)}function L(e,t,n=null){var a;const r=e.type,s=e.props;A(r);const o=(a=s.children)==null?void 0:a[0],c=d(o,t,n);return e._rendered=o,e._dom=c,c}function M(e,t,n){Object.keys(t).forEach(r=>{if(r!=="children"&&!(r in n))if(r.startsWith("on")){const s=r.toLowerCase().substring(2);e.removeEventListener(s,t[r])}else e[r]=""}),Object.keys(n).forEach(r=>{if(r!=="children"&&t[r]!==n[r])if(r.startsWith("on")){const s=r.toLowerCase().substring(2);t[r]&&e.removeEventListener(s,t[r]),e.addEventListener(s,n[r])}else e[r]=n[r]})}function d(e,t,n=null){var s;if(Array.isArray(e)){const o=document.createDocumentFragment();return e.forEach(c=>d(c,o,null)),t.appendChild(o),o.firstChild}if(e.type==="reactive-wrapper"){const o=(s=e.props.children)==null?void 0:s[0];if(!o)throw new Error("Reactive wrapper must have a child");e.props._componentFn&&(o.props=o.props||{},o.props._componentFn=e.props._componentFn);const c=d(o,t,n);return e._dom=c,c}if(typeof e.type=="function")return L(e,t,n);const r=e.type==="TEXT_ELEMENT"?document.createTextNode(e.props.nodeValue):document.createElement(e.type);return r._vnode=e,e._dom=r,r instanceof HTMLElement&&M(r,{},e.props),r instanceof HTMLElement&&e.props.children&&e.props.children.forEach(o=>{o&&d(o,r)}),n?t.insertBefore(r,n):t.appendChild(r),r}function g(e,t,n){var s,o,c,a,h;if(Array.isArray(t)&&Array.isArray(n)){const f=Math.max(t.length,n.length);for(let u=0;u<f;u++)if(u<t.length&&u<n.length)g(e,t[u],n[u]);else if(u>=t.length)d(n[u],e);else{const m=l(t[u]);(s=m==null?void 0:m.parentNode)==null||s.removeChild(m)}return}if(Array.isArray(t)||Array.isArray(n)){const f=Array.isArray(t)?l(t[0]):l(t);if(f){const u=f.parentNode;u&&(Array.isArray(t)?t.forEach(m=>{const C=l(m);C&&u.removeChild(C)}):u.removeChild(f),d(n,e))}return}if(t.type==="reactive-wrapper"&&n.type==="reactive-wrapper"){const f=(o=t.props.children)==null?void 0:o[0],u=(c=n.props.children)==null?void 0:c[0];if(!f||!u)return;if(t.props._renderKey!==n.props._renderKey){const m=l(t),C=d(u,e);m&&((a=m.parentNode)==null||a.replaceChild(C,m)),n._dom=C;return}g(e,f,u),n._dom=t._dom;return}if(t.type!==n.type){const f=l(t),u=d(n,e);f&&((h=f.parentNode)==null||h.replaceChild(u,f));return}if(n.type==="TEXT_ELEMENT"){const f=l(t);f&&t.props.nodeValue!==n.props.nodeValue&&(f.nodeValue=n.props.nodeValue),n._dom=f;return}const r=l(t);r&&(M(r,t.props,n.props),n._dom=r,S(r,t.props.children||[],n.props.children||[]))}function S(e,t,n){const r=Math.max(t.length,n.length);for(let s=0;s<r;s++){const o=t[s],c=n[s];if(!c&&o){const a=l(o);a&&e.removeChild(a)}else!o&&c?d(c,e):o&&c&&g(e,o,c)}}function v(e,t){const n=t._vnode;n?(g(t,n,e),n.props={},n.ref=null):d(e,t),t._vnode=e}class F{constructor(){_(this,"stateIndexes",new WeakMap);_(this,"currentComponentFn",null);_(this,"rootElement",null);_(this,"states",new WeakMap)}createState(t){if(this.rootElement??(this.rootElement=document.getElementById("app")),!this.currentComponentFn)throw new Error("createState must be called within a component");const n=this.currentComponentFn;this.stateIndexes.has(n)||this.stateIndexes.set(n,0);const r=this.stateIndexes.get(n);this.stateIndexes.set(n,r+1),this.states.has(n)||this.states.set(n,new Map);const s=this.states.get(n);return s.has(r)||s.set(r,t),[()=>{const a=this.states.get(n);return a==null?void 0:a.get(r)},a=>{const h=this.states.get(n);if(!h)return;const f=h.get(r),u=typeof a=="function"?a(f):a;Object.is(f,u)||(h.set(r,u),queueMicrotask(()=>{const m=this.render(n);if(this.rootElement){const C=this.findElementByComponent(this.rootElement,n);C&&C._vnode&&v(m,C)}}))}]}hasState(t){return this.states.has(t)}triggerUpdate(t){queueMicrotask(()=>{const n=this.render(t);if(this.rootElement){const r=this.findElementByComponent(this.rootElement,t);r&&r._vnode&&v(n,r)}})}findElementByComponent(t,n){var s,o;if(((o=(s=t._vnode)==null?void 0:s.props)==null?void 0:o._componentFn)===n)return t;const r=c=>{var a,h;for(const f of Array.from(c.children)){if(((h=(a=f._vnode)==null?void 0:a.props)==null?void 0:h._componentFn)===n)return f;const u=r(f);if(u)return u}return null};return r(t)}render(t,n){const r=this.currentComponentFn;this.currentComponentFn=t;try{this.stateIndexes.set(t,0);const s=t(n||{}),o=Date.now();return s&&typeof s=="object"&&Object.keys(s).forEach(c=>{c.startsWith("_temp")&&delete s[c]}),{type:"reactive-wrapper",props:{children:[s],_componentFn:t,_renderKey:o},key:null,ref:null}}finally{this.currentComponentFn=r}}}const y=new F;function b(e){return y.createState(e)}function E(e,t,...n){if(typeof e=="function"){const o=y.currentComponentFn;y.currentComponentFn=e;try{y.stateIndexes.set(e,0);const c=e({...t,children:n});return t&&Object.keys(t).forEach(a=>{a.startsWith("_temp")&&delete t[a]}),y.hasState(e)?{type:"reactive-wrapper",props:{children:[c],_componentFn:e,_renderKey:Date.now()},key:null,ref:null}:{type:e,props:{...t,children:[c],_componentFn:e},key:null,ref:null}}finally{y.currentComponentFn=o}}const r=t?{...t}:{};r.style&&typeof r.style=="object"&&(r.style=Object.entries(r.style).reduce((o,[c,a])=>{const h=c.replace(/([A-Z])/g,"-$1").toLowerCase();return o?`${o};${h}:${a}`:`${h}:${a}`},""));const s=n.map(o=>typeof o=="object"?o:{type:"TEXT_ELEMENT",props:{nodeValue:o}});return{type:e,props:{...r,children:s}}}let I={getRouterContext:()=>({currentPath:window.location.pathname,params:{}}),setRouterContext:()=>{}};function k({value:e,children:t}){return I=e,t}function T(){if(!I)throw new Error("useRouter must be used within a Router");return I}function x(e){window.history.pushState({},"",e),T().setRouterContext(n=>({...n,currentPath:e}))}function W({to:e,children:t,className:n}){return E("a",{href:e,onClick:s=>{s.preventDefault(),x(e)},className:n},t)}function $({path:e,component:t}){const{getRouterContext:n}=T(),{currentPath:r}=n();return j(e,r)?E(t,null):null}function j(e,t){const n=e.replace(/:[^\s/]+/g,"([^/]+)").replace(/\*/g,".*");return new RegExp(`^${n}$`).test(t)}function K({children:e}){console.log("Router");const[t,n]=b({currentPath:window.location.pathname,params:{}}),r=()=>{n(s=>({...s,currentPath:window.location.pathname}))};return typeof window<"u"&&window.addEventListener("popstate",r),E(k,{value:{getRouterContext:t,setRouterContext:n}},E("div",null,e))}i.Link=W,i.Nexora=E,i.Route=$,i.Router=K,i.RouterProvider=k,i.createState=b,i.diffChildren=S,i.executeInitCallbacks=A,i.findDom=l,i.freeze=O,i.mount=d,i.mountComponent=L,i.navigate=x,i.onInit=D,i.patch=g,i.reactive=y,i.render=v,i.shallowEqual=p,i.updateProps=M,i.useRouter=T,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});