UNPKG

omi-reactify

Version:

Bridge between omi and react

2 lines (1 loc) 3.14 kB
(function(i,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react"),require("react-dom"),require("react-dom/client")):typeof define=="function"&&define.amd?define(["exports","react","react-dom","react-dom/client"],c):(i=typeof globalThis<"u"?globalThis:i||self,c(i.index={},i.React,i.ReactDOM,i.client))})(this,function(i,c,m,l){"use strict";const w=()=>typeof l.createRoot<"u",h=new WeakMap,p=n=>{if(w()){let r=h.get(n);return r||(r=l.createRoot(n),h.set(n,r)),{render:s=>{r.render(s)},unmount:()=>{r.unmount(),h.delete(n)}}}else return{render:r=>{m.render(r,n)},unmount:()=>{m.unmountComponentAtNode(n)}}},R=n=>{if(typeof n!="function")return!1;const r=n.toString();return["useState","useEffect","useRef","useContext","useMemo","useCallback","useReducer"].some(e=>r.includes(e))},H=n=>{var s;return!!(typeof n=="function"&&((s=n.prototype)!=null&&s.render))},y=/\B([A-Z])/g;function C(n){return n.replace(y,"-$1").toLowerCase()}const S=n=>{const r=new Set(["animationIterationCount","boxFlex","boxFlexGroup","boxOrdinalGroup","columnCount","fillOpacity","flex","flexGrow","flexShrink","fontWeight","lineClamp","lineHeight","opacity","order","orphans","tabSize","widows","zIndex","zoom"]);return Object.entries(n).filter(([s,e])=>e!=null&&e!=="").map(([s,e])=>{const t=s.replace(/[A-Z]/g,d=>`-${d.toLowerCase()}`);let o=e;return typeof e=="number"&&e!==0&&!r.has(s)&&(o=`${e}px`),`${t}:${o};`}).join(" ")},A=n=>{class r extends c.Component{constructor(e){super(e),this.eventHandlers=[],this.renderUnmountHandlers=new Map;const{innerRef:t}=e;this.ref=t||c.createRef()}setEvent(e,t){var o;this.eventHandlers.push([e,t]),(o=this.ref.current)==null||o.addEventListener(e,t)}update(){this.clearEventHandlers(),this.ref.current&&Object.entries(this.props).forEach(([e,t])=>{var o,d,E,x;if(!["innerRef","children"].includes(e)){if(typeof t=="function"){if(e.match(/^on[A-Za-z]/)){const u=e.slice(2),f=u[0].toLowerCase()+u.slice(1);this.setEvent(f,t)}else if(e.match(/^render[A-Za-z]/)){const u=t,f=(a,b)=>{let g=R(t)||H(t)?c.createElement(u,{...a}):u(a);return p(b||document.createElement("div")).render(g),b};this.ref.current[e]=f}return}if(typeof t=="object"){if((o=t==null?void 0:t.$$typeof)!=null&&o.toString().match(/react/)){const u=f=>(p(f||document.createElement("div")).render(t),f);this.ref.current[e]=u;return}if(e==="style"){(d=this.ref.current)==null||d.setAttribute("style",S(t));return}this.ref.current[e]=t;return}if(e.match(y)){(E=this.ref.current)==null||E.setAttribute(C(e),t),(x=this.ref.current)==null||x.removeAttribute(e);return}}})}componentDidUpdate(){this.update()}componentDidMount(){this.update()}componentWillUnmount(){this.clearEventHandlers()}clearEventHandlers(){this.eventHandlers.forEach(([e,t])=>{var o;(o=this.ref.current)==null||o.removeEventListener(e,t)}),this.eventHandlers=[]}render(){const{children:e,className:t,innerRef:o,...d}=this.props;return c.createElement(n,{class:t,...d,ref:this.ref},e)}}return c.forwardRef((s,e)=>c.createElement(r,{...s,innerRef:e}))};i.default=A,i.hyphenate=C,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});