UNPKG

croact

Version:

Simple React for compatible use

11 lines (10 loc) 17.1 kB
/* Copyright (c) Daybrush name: croact license: MIT author: Daybrush repository: git+https://github.com/daybrush/croact.git version: 1.0.4 */ !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).Croact={})}(this,(function(t){"use strict";var n="function",e="object",r="string",o="number",i="undefined",u=typeof document!==i&&document;function c(t){return typeof t===r}function f(t){return typeof t===o}function s(t){return typeof t===n}function a(t,n){return void 0===n&&(n="-"),t.replace(/([a-z])([A-Z])/g,(function(t,e,r){return""+e+n+r.toLowerCase()}))}function p(t){return Object.keys(t)}function d(t){return p(t).map((function(n){return[n,t[n]]}))}function l(t,n,e,r){t.removeEventListener(n,e,r)}function v(t){return(null==t?void 0:t.ownerDocument)||u}function h(t){return(n=t)&&typeof n===e&&t.nodeName&&t.nodeType&&"ownerDocument"in t;var n}var _=function(t,n){return _=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e])},_(t,n)};function y(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function e(){this.constructor=t}_(t,n),t.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}var m=function(){return m=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t},m.apply(this,arguments)};function b(t,n){var e={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&n.indexOf(r)<0&&(e[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)n.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(e[r[o]]=t[r[o]])}return e}function g(t,n,e){if(e||2===arguments.length)for(var r,o=0,i=n.length;o<i;o++)!r&&o in n||(r||(r=Array.prototype.slice.call(n,0,o)),r[o]=n[o]);return t.concat(r||Array.prototype.slice.call(n))}function O(t){var n=0;return t.map((function(t){return null==t?"$compat".concat(++n):"".concat(t)}))}function $(t){var n=[];return t.forEach((function(t){var e;n=n.concat((e=t,Array.isArray(e)?$(t):t))})),n}function E(t,n){if(!n)return t;for(var e in n)typeof t[e]===i&&(t[e]=n[e]);return t}function x(t,n){if(t===n)return!1;for(var e in t)if(!(e in n))return!0;for(var e in n)if(t[e]!==n[e])return!0;return!1}function C(t){var n=t.className,e=b(t,["className"]);return null!=n&&(e.class=n),delete e.style,delete e.children,e}function w(t){var n={},e={};for(var r in t)0===r.indexOf("on")?e[r]=t[r]:n[r]=t[r];return[n,e]}function T(t){if(!t)return null;var n=t.b;return h(n)?n:T(t.c)}function j(t){var n=t.parentNode;n&&n.removeChild(t)}function P(t){t.forEach((function(t){t()}))}function A(){return this.constructor(this.props,this.context)}var S=0,N=function(){function t(t,n,e,r,o,i,u){void 0===u&&(u={}),this.t=t,this.d=n,this.k=e,this.i=r,this.c=o,this.ref=i,this.ps=u,this.typ="prov",this._ps=[],this._cs={},this._hyd=null,this._sel=!1}return t.prototype.s=function(){return!0},t.prototype.u=function(t,n,e,r,o){var i,u,f,s,a=this,d=a.d,l=$((i=n,p(i).map((function(t){return i[t]}))).filter((function(t){return t.$_req})).map((function(t){return t.$_subs}))),v=(s=function(t,n,e){void 0===e&&(e=-1);for(var r=t.length,o=0;o<r;++o)if(n(t[o],o,t))return o;return e}(u=l,(function(t){return t.d===d})),s>-1?u[s]:f);if(a.b&&!c(e)&&!o&&!a.s(e.props,r)&&!v)return l.reduce((function(t,n){var e=n.d;return t[0]?t[0].d===e&&t.push(n):e>d&&t.push(n),t}),[]).forEach((function(e){nt(e,e._ps,[e.o],t,n,!0)})),!1;a.o=e,a.ss(r);var h=a.ps;return c(e)||(a.ps=e.props,a.ref=e.ref),k(this),a.r(t,n,a.b?h:{},r),!0},t.prototype.md=function(){this.rr()},t.prototype.ss=function(){},t.prototype.ud=function(){this.rr()},t.prototype.rr=function(){var t=this,n=t.ref,e=t.fr;n&&n(e?e.current:t.b)},t}();function U(){return Object.__CROACT_CURRENT_INSTNACE__}function R(){return S}function k(t){return Object.__CROACT_CURRENT_INSTNACE__=t,S=0,t}var D=function(){function t(t,n){void 0===t&&(t={}),this.props=t,this.context=n,this.state={},this.$_timer=0,this.$_state={},this.$_subs=[],this.$_cs={}}return t.prototype.render=function(){return null},t.prototype.shouldComponentUpdate=function(t,n){return this.props!==t||this.state!==n},t.prototype.setState=function(t,n,e){var r=this;r.$_timer||(r.$_state={}),clearTimeout(r.$_timer),r.$_timer=0,r.$_state=m(m({},r.$_state),t),e?r.$_setState(n,e):r.$_timer=window.setTimeout((function(){r.$_timer=0,r.$_setState(n,e)}))},t.prototype.forceUpdate=function(t){this.setState({},t,!0)},t.prototype.componentDidMount=function(){},t.prototype.componentDidUpdate=function(t,n){},t.prototype.componentWillUnmount=function(){},t.prototype.$_setState=function(t,n){var e=[],r=this.$_p;nt(r.c,[r],[r.o],e,r._cs,m(m({},this.state),this.$_state),n)&&(t&&e.push(t),P(e),k(null))},t}(),M=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return y(n,t),n.prototype.shouldComponentUpdate=function(t,n){return x(this.props,t)||x(this.state,n)},n}(D);function I(t){var n=function(t){n.current=t};return n.current=t,n}function L(t,n,e,r){var o,i;return(null===(o=null==t?void 0:t.prototype)||void 0===o?void 0:o.render)?i=new t(n,e):((i=new D(n,e)).constructor=t,t._fr?(r.fr=I(),i.render=function(){return this.constructor(this.props,r.fr)}):i.render=A),i.$_p=r,i}var B=function(t){function n(n,e,r,o,i,u,c){void 0===c&&(c={});var f=t.call(this,n,e,r,o,i,u,E(c,n.defaultProps))||this;return f.typ="comp",f._usefs=[],f._uefs=[],f._defs=[],f}return y(n,t),n.prototype.s=function(t,n){var e=this.b;return!1!==e.shouldComponentUpdate(E(t,this.t.defaultProps),n||e.state)},n.prototype.r=function(t,n,e){var r,o,i=this,u=i.t;i.ps=E(i.ps,i.t.defaultProps);var c=i.ps,f=!i.b,s=u.contextType,a=i.b,p=null==s?void 0:s.get(i);i._cs=n,f?(a=L(u,c,p,i),i.b=a):(a.props=c,a.context=p);var d=a.state;i._usefs=[],i._uefs=[];var l=a.render();0===(null===(o=null===(r=null==l?void 0:l.props)||void 0===r?void 0:r.children)||void 0===o?void 0:o.length)&&(l.props.children=i.ps.children);var v=m(m({},n),a.$_cs);nt(i,i._ps,l?[l]:[],t,v),f?i._uefs.push((function(){null==s||s.register(i),a.componentDidMount()})):i._uefs.push((function(){a.componentDidUpdate(e,d)})),t.push((function(){i._usefs.forEach((function(t){t()})),f?i.md():i.ud(),i._defs=i._uefs.map((function(t){return t()}))}))},n.prototype.ss=function(t){var n=this.b;n&&t&&(n.state=t)},n.prototype.un=function(){var t,n=this;n._ps.forEach((function(t){t.un()})),null===(t=n.t.contextType)||void 0===t||t.unregister(n),clearTimeout(n.b.$_timer),n._defs.forEach((function(t){t&&t()})),n.b.componentWillUnmount()},n}(N),W=function(){function t(){this.keys=[],this.values=[]}var n=t.prototype;return n.get=function(t){return this.values[this.keys.indexOf(t)]},n.set=function(t,n){var e=this.keys,r=this.values,o=e.indexOf(t),i=-1===o?e.length:o;e[i]=t,r[i]=n},t}(),q=function(){function t(){this.object={}}var n=t.prototype;return n.get=function(t){return this.object[t]},n.set=function(t,n){this.object[t]=n},t}(),z="function"==typeof Map,H=function(){function t(){}var n=t.prototype;return n.connect=function(t,n){this.prev=t,this.next=n,t&&(t.next=this),n&&(n.prev=this)},n.disconnect=function(){var t=this.prev,n=this.next;t&&(t.next=n),n&&(n.prev=t)},n.getIndex=function(){for(var t=this,n=-1;t;)t=t.prev,++n;return n},t}();var G=function(){function t(t,n,e,r,o,i,u,c){this.prevList=t,this.list=n,this.added=e,this.removed=r,this.changed=o,this.maintained=i,this.changedBeforeAdded=u,this.fixed=c}var n=t.prototype;return Object.defineProperty(n,"ordered",{get:function(){return this.cacheOrdered||this.caculateOrdered(),this.cacheOrdered},enumerable:!0,configurable:!0}),Object.defineProperty(n,"pureChanged",{get:function(){return this.cachePureChanged||this.caculateOrdered(),this.cachePureChanged},enumerable:!0,configurable:!0}),n.caculateOrdered=function(){var t=function(t,n){var e=[],r=[];return t.forEach((function(t){var n=t[0],o=t[1],i=new H;e[n]=i,r[o]=i})),e.forEach((function(t,n){t.connect(e[n-1])})),t.filter((function(t,e){return!n[e]})).map((function(t,n){var o=t[0],i=t[1];if(o===i)return[0,0];var u=e[o],c=r[i-1],f=u.getIndex();return u.disconnect(),c?u.connect(c,c.next):u.connect(void 0,e[0]),[f,u.getIndex()]}))}(this.changedBeforeAdded,this.fixed),n=this.changed,e=[];this.cacheOrdered=t.filter((function(t,r){var o=t[0],i=t[1],u=n[r],c=u[0],f=u[1];if(o!==i)return e.push([c,f]),!0})),this.cachePureChanged=e},t}();function V(t,n,e){var r=z?Map:e?q:W,o=e||function(t){return t},i=[],u=[],c=[],f=t.map(o),s=n.map(o),a=new r,p=new r,d=[],l=[],v={},h=[],_=0,y=0;return f.forEach((function(t,n){a.set(t,n)})),s.forEach((function(t,n){p.set(t,n)})),f.forEach((function(t,n){var e=p.get(t);void 0===e?(++y,u.push(n)):v[e]=y})),s.forEach((function(t,n){var e=a.get(t);void 0===e?(i.push(n),++_):(c.push([e,n]),y=v[n]||0,d.push([e-y,n-_]),l.push(n===e),e!==n&&h.push([e,n]))})),u.reverse(),new G(t,n,i,u,h,c,d,l)}function Z(t,n){var e=p(t),r=p(n),o=V(e,r,(function(t){return t})),i={},u={},c={};return o.added.forEach((function(t){var e=r[t];i[e]=n[e]})),o.removed.forEach((function(n){var r=e[n];u[r]=t[r]})),o.maintained.forEach((function(r){var o=r[0],i=e[o],u=[t[i],n[i]];t[i]!==n[i]&&(c[i]=u)})),{added:i,removed:u,changed:c}}var F=function(t){function n(){var n=null!==t&&t.apply(this,arguments)||this;return n.typ="elem",n._es={},n._svg=!1,n}return y(n,t),n.prototype.e=function(t,n){var e,r,o,i,u=this,c=u._es,f=u.b,s=function(t){return t.replace(/^on/g,"").toLowerCase()}(t);n?(l(f,s,c[t]),delete c[t]):(c[t]=function(n){var e,r;null===(r=(e=u.ps)[t])||void 0===r||r.call(e,n)},e=f,r=s,o=c[t],e.addEventListener(r,o,i))},n.prototype.s=function(t){return x(this.ps,t)},n.prototype.r=function(t,n,e){var r,o=this,i=!o.b,u=o.ps;if(i){var c=T(o.c),f=!1;f=!(!o._svg&&"svg"!==o.t)||c&&c.ownerSVGElement,o._svg=f;var s=null===(r=o._hyd)||void 0===r?void 0:r.splice(0,1)[0],p=o.t;if(s)o._hyd=[].slice.call(s.children||[]);else{var d=v(c);s=f?d.createElementNS("http://www.w3.org/2000/svg",p):d.createElement(p)}o.b=s}nt(o,o._ps,u.children,t,n);var l=o.b,h=w(e),_=h[0],y=h[1],m=w(u),b=m[0],g=m[1];return function(t,n,e){var r=Z(C(t),C(n)),o=r.added,i=r.removed,u=r.changed;for(var c in o)e.setAttribute(c,o[c]);for(var f in u)e.setAttribute(f,u[f][1]);for(var s in i)e.removeAttribute(s)}(_,b,l),function(t,n,e){var r=Z(t,n),o=r.added,i=r.removed;for(var u in i)e.e(u,!0);for(var c in o)e.e(c)}(y,g,o),function(t,n,e){var r=e.style,o=Z(t,n),i=o.added,u=o.removed,c=o.changed;for(var f in i){var s=a(f,"-");r.setProperty(s,i[f])}for(var f in c){var p=a(f,"-");r.setProperty(p,c[f][1])}for(var f in u){var d=a(f,"-");r.removeProperty(d)}}(e.style||{},u.style||{},l),t.push((function(){i?o.md():o.ud()})),!0},n.prototype.un=function(){var t=this,n=t._es,e=t.b;for(var r in n)l(e,r,n[r]);t._ps.forEach((function(t){t.un()})),t._es={},t._sel||j(e)},n}(N);function J(t){if(!t||h(t))return t;var n=t.$_p._ps;return n.length?J(n[0].b):null}function K(t){if(t){if(t.b&&h(t.b))return t;var n=t._ps;return n.length?K(n[0]):null}}function Q(t,n){for(var e=[],r=2;r<arguments.length;r++)e[r-2]=arguments[r];var o,i=n||{},u=i.key,c=i.ref,f=b(i,["key","ref"]);return{type:t,key:u,ref:c,props:m(m({},f),{children:(o=e,o.reduce((function(t,n){return t.concat(n)}),[])).filter((function(t){return null!=t&&!1!==t}))})}}var X=function(t){function n(n,e){void 0===e&&(e=0);var r=t.call(this,"container",e,"container",0,null)||this;return r.typ="container",r.b=n,r}return y(n,t),n.prototype.r=function(){return!0},n.prototype.un=function(){},n}(N),Y=function(t){function n(){var n=null!==t&&t.apply(this,arguments)||this;return n.typ="text",n}return y(n,t),n.prototype.r=function(t){var n,e=this,r=!e.b;if(r){var o=T(e.c),i=null===(n=e._hyd)||void 0===n?void 0:n.splice(0,1)[0];e.b=i||v(o).createTextNode(e.t.replace("text_",""))}return t.push((function(){r?e.md():e.ud()})),!0},n.prototype.un=function(){j(this.b)},n}(N);function tt(t,n,e,r){var o=r.d+1;if(c(t)||f(t))return new Y("text_".concat(t),o,n,e,r,null,{});var i=t.type;return new("string"==typeof i?F:B)(i,o,n,e,r,t.ref,t.props)}function nt(t,n,e,r,o,i,u){var f=function(t,n,e){var r=e.map((function(t){return c(t)?null:t.key})),o=V(O(n.map((function(t){return t.k}))),O(r),(function(t){return t}));o.removed.forEach((function(t){n.splice(t,1)[0].un()})),o.ordered.forEach((function(t){var e=t[0],r=t[1],o=n.splice(e,1)[0];n.splice(r,0,o);var i=J(o.b),u=J(n[r+1]&&n[r+1].b);i&&i.parentNode.insertBefore(i,u)})),o.added.forEach((function(o){n.splice(o,0,tt(e[o],r[o],o,t))}));var i=o.maintained.filter((function(o){o[0];var i=o[1],u=e[i],f=n[i];return(c(u)?"text_".concat(u):u.type)!==f.t?(f.un(),n.splice(i,1,tt(u,r[i],i,t)),!0):(f.i=i,!1)}));return g(g([],o.added,!0),i.map((function(t){return t[0],t[1]})),!0)}(t,n,e),s=t._hyd,a=n.filter((function(t,n){return t._hyd=s,t.u(r,o,e[n],i,u)}));"container"===t.typ&&t._sel&&n.forEach((function(t){var n=K(t);n&&(n._sel=!0)})),t._hyd=null;var p=T(t);return p&&f.reverse().forEach((function(e){var r=n[e],o=J(r.b);if(o&&p!==o&&!o.parentNode){var i=function(t,n){for(var e=t._ps,r=e.length,o=n.i+1;o<r;++o){var i=J(e[o].b);if(i)return i}return null}(t,r);p.insertBefore(o,i)}})),a.length>0}function et(t,n,e,r){void 0===e&&(e=n.__CROACT__),void 0===r&&(r={});var o=!!e;e||(e=new X(n));var i=[];return nt(e,e._ps,t?[t]:[],i,r,void 0,void 0),P(i),k(null),o||(n.__CROACT__=e),e}var rt=0;var ot=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return y(n,t),n.prototype.componentDidMount=function(){var t=this,n=t.props,e=n.element,r=n.container,o=t.$_p;t._pp=new X(r,o.d+1),et(e,r,t._pp,o._cs)},n.prototype.componentDidUpdate=function(){var t=this,n=t.props;et(n.element,n.container,t._pp,t.$_p._cs)},n.prototype.componentWillUnmount=function(){var t=this;et(null,t.props.container,t._pp,t.$_p._cs),this._pp=null},n}(M);function it(t,n,e){if(null==t)return"";if(c(t)||f(t)||s(t))return t;var r=t.type,o=t.props;if(c(r)){var i={};d(o).forEach((function(t){var n=t[0],e=t[1];if("style"===n)i[n]=d(e).map((function(t){var n=t[0],e=t[1];return"".concat(a(n),": ").concat(e,";")})).join("");else{if("children"===n)return;n.match(/^on[^a-z]/g)||(i[n]="".concat(e))}}));var u=o.children.map((function(t){return it(t,n)})).join("");if(e)return{tagName:r,props:i,children:u};var p=g([r],d(i).map((function(t){var n=t[0],e=t[1];return"".concat(n,'="').concat(e.replace(/"/g,'\\"'),'"')})),!0);return"<".concat(p.join(" "),">").concat(u,"</").concat(r,">")}if(s(r)){var l=r.contextType,v={b:null,_hs:[],_usefs:[],_uefs:[],_defs:[],_cs:n};k(v);var h=L(r,o,null==l?void 0:l.get(v),{}),_=m(m({},n),h.$_cs);return v.b=h,it(h.render(),_,e)}return""}function ut(t){var n=U(),e=n._hs||(n._hs=[]),r=R(),o=e[r];if(S=r+1,o){if(!x(o.deps,t.deps))return o.updated=!1,o;e[r]=t}else e.push(t);return t.value=t.func(),t.updated=!0,t}function ct(t,n){return ut({func:t,deps:n}).value}function ft(t,n,e){var r=U(),o=ut({func:function(){return t},deps:n}),i=e?r._usefs:r._uefs;o.updated?i.push((function(){return o.effect&&o.effect(),o.effect=t(),o.effect})):i.push((function(){return o.effect}))}t.Component=D,t.ContainerProvider=X,t.PureComponent=M,t.createContext=function(t){var n="c".concat(++rt);function e(t,n){return t.children(n)}function r(t){return t._cs[n]}var o={$_id:n,$_dv:t,Consumer:e,Provider:function(t){var e=this;if(!e.$_cs[n]){e.$_cs[n]=e;e.shouldComponentUpdate=function(t){t.value!==e.props.value&&(e.$_req=!0)},e.render=function(){return e.props.children[0]},e.$_subs=[]}return t.children[0]},get:function(n){var e,o;return null!==(o=null===(e=r(n))||void 0===e?void 0:e.props.value)&&void 0!==o?o:t},register:function(t){var n,e,o=r(t);o&&(n=o.$_subs,e=t,-1===n.indexOf(e)&&n.push(e))},unregister:function(t){var n=r(t);if(n){var e=n.$_subs,o=e.indexOf(t);o>-1&&e.splice(o,1)}}};return e.contextType=o,o},t.createElement=Q,t.createPortal=function(t,n){return Q(ot,{element:t,container:n})},t.createRef=I,t.findDOMNode=J,t.findNodeProvider=K,t.forwardRef=function(t){return t._fr=!0,t},t.render=function(t,n,e){var r=n.__CROACT__;t&&!r&&(n.innerHTML=""),et(t,n,r),e&&e()},t.renderSelf=function(t,n,e){return!e&&t&&((e=new X(n.parentElement))._hyd=[n],e._sel=!0),et(t,n,e),e},t.renderSelfToString=function(t){return it(t,{},!0)},t.renderToString=function(t){return it(t,{})},t.useContext=function(t){var n=U(),e=t.$_id,r=n._cs;return e in r?r[e].props.value:t.$_dv,ft((function(){return t.register(n),function(){t.unregister(n)}}),[]),t.get(n)},t.useEffect=ft,t.useImperativeHandle=function(t,n,e){ft((function(){null==t||t(n())}),e,!0)},t.useMemo=ct,t.useRef=function(t){return ct((function(){return I(t)}),[])},t.useState=function(t){var n=U(),e=R(),r=n.b;return ut({func:function(){r.state[e]=s(t)?t():t},deps:[]}),[r.state[e],function(t){var n;return r.setState(((n={})[e]=t,n))}]},t.version="simple-1.0.0"})); //# sourceMappingURL=croact.min.js.map