springtype
Version:
1k TypeScript/TSX nano-framework for the web
3 lines (2 loc) • 2.67 kB
JavaScript
export*from"springtype-types";const e="$st";window.$st||(window.$st={state:{}});const t=window.$st;function n(){return(n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}const r=e=>e.filter(e=>{return!((t=e)&&"object"==typeof t&&!t.attributes&&!t.type&&!t.children);var t}),o=function(e){this.update=e},s=(e,t,...s)=>(s=r([].concat.apply([],s)),t=n({},t),"fragment"===e?r(s):"function"==typeof e?(t.ref&&(t.ref.onUpdate=o.bind(t.ref)),e(n({children:s},t))):{type:e,attributes:t,children:s});t.dom||(t.dom={hasElNamespace:e=>"http://www.w3.org/2000/svg"===e.namespaceURI,hasSvgNamespace:(e,n)=>t.dom.hasElNamespace(e)&&"STYLE"!==n&&"SCRIPT"!==n,createElementOrElements:(e,n)=>Array.isArray(e)?t.dom.createChildElements(e,n):void 0!==e?t.dom.createElement(e,n):t.dom.createTextNode("",n),createElement:(e,n)=>{let r;return r="SVG"===e.type.toUpperCase()||n&&t.dom.hasSvgNamespace(n,e.type.toUpperCase())?document.createElementNS("http://www.w3.org/2000/svg",e.type):document.createElement(e.type),r.$st=t,e.attributes&&t.dom.setAttributes(e.attributes,r),e.children&&t.dom.createChildElements(e.children,r),n&&(n.appendChild(r),"function"==typeof r.$onMount&&r.$onMount()),r},createTextNode:(e,t)=>{const n=document.createTextNode(e.toString());return t&&t.appendChild(n),n},createChildElements:(e,n)=>{const r=[];for(let o=0;o<e.length;o++){const s=e[o];r.push(null===s||"object"!=typeof s&&"function"!=typeof s?t.dom.createTextNode((null==s?"":s).toString(),n):t.dom.createElement(s,n))}return r},setAttribute:(e,n,r)=>{if(void 0!==n){if("ref"===e&&"function"!=typeof n?n.current=r:"ref"===e&&"function"==typeof n&&n(r),e.startsWith("on")&&"function"==typeof n){let t=e.substring(2).toLowerCase();const o=t.indexOf("capture"),s=o>-1;return"mount"===t&&(r.$onMount=n),s&&(t=t.substring(0,o)),void r.addEventListener(t,n,s)}if("className"===e&&(e="class"),"class"===e&&Array.isArray(n)&&(n=n.join(" ")),t.dom.hasElNamespace(r)&&e.startsWith("xlink"))r.setAttributeNS("http://www.w3.org/1999/xlink",`xlink:${e.replace("xlink","")}`.toLowerCase(),n);else if("style"===e&&"string"!=typeof n){const e=Object.keys(n);for(let t=0;t<e.length;t++)r.style[e[t]]=n[e[t]]}else"boolean"==typeof n?r[e]=n:r.setAttribute(e,n)}},setAttributes:(e,n,r)=>{const o=Object.keys(e);for(let s=0;s<o.length;s++)t.dom.setAttribute(o[s],e[o[s]],n,r)}});const a=t.render=(e,n=document.body)=>"string"==typeof e?t.dom.createTextNode(e,n):t.dom.createElementOrElements(e,n);export{e as ST_KEY,a as render,t as st,s as tsx};
//# sourceMappingURL=index.modern.js.map