@uploadcare/react-adapter
Version:
2 lines (1 loc) • 1.72 kB
JavaScript
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=new Set(["children","ref","style","className"]),b=(e={})=>Object==null?void 0:Object.keys(e).reduce((r,s)=>{var t,c;const o=e==null?void 0:e[s],n=`on${(c=(t=o==null?void 0:o.split("-"))==null?void 0:t.map(u=>u.charAt(0).toUpperCase()+u.slice(1)))==null?void 0:c.join("")}`;return r[n]=o,r},{}),m=new WeakMap,w=e=>{let r=m.get(e);return r===void 0&&(r=new Map,m.set(e,r)),r},E=({node:e,nameProp:r,valueProp:s,prevValueProp:o,event:n})=>{if(n!==void 0){if(s!==o){const t=w(e),c=t.has(n);let u=t.get(n);s!==void 0?c?u.handleEvent=s:(u={handleEvent:s},t.set(n,u),e.addEventListener(n,l=>u.handleEvent(l.detail))):c&&(t.delete(n),e.removeEventListener(n,u))}return}e[r]=s,s==null&&r in HTMLElement.prototype&&e.removeAttribute(r)},M=(e,r,s)=>{const o={},n={};return Object.entries(e).forEach(([t,c])=>{g.has(t)?o[t==="className"?"class":t]=c:r.has(t)||t in s.prototype?n[t]=c:o[t]=c}),{reactProps:o,customElProps:n}},j=({react:e,tag:r,elClass:s,schemaEvents:o})=>{const n=b(o),t=new Set(Object.keys(n??{})),c=e.forwardRef((u,l)=>{const f=e.useRef(new Map),d=e.useRef(null),{reactProps:h,customElProps:p}=M(u,t,s);return e.useLayoutEffect(()=>{if(d.current===null)return;const a=new Map;for(const i in p)E({node:d.current,nameProp:i,valueProp:p[i],prevValueProp:f.current.get(i),event:n[i]}),f.current.delete(i),a.set(i,u[i]);for(const[i,P]of f.current)E({node:d.current,nameProp:i,valueProp:void 0,prevValueProp:P,event:n[i]});f.current=a}),e.createElement(r??s.__tag,{...h,ref:e.useCallback(a=>{d.current=a,typeof l=="function"?l(a):l!==null&&(l.current=a)},[l])})});return c.displayName=s.name,c};exports.customElementToReactComponent=j;
;