UNPKG

@uploadcare/react-adapter

Version:

84 lines (83 loc) 2.58 kB
const w = /* @__PURE__ */ new Set([ "children", "ref", "style", "className" ]), g = (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 = /* @__PURE__ */ new WeakMap(), M = (e) => { let r = m.get(e); return r === void 0 && (r = /* @__PURE__ */ 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 = M(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); }, b = (e, r, s) => { const o = {}, n = {}; return Object.entries(e).forEach(([t, c]) => { w.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, // https://react.dev/warnings/invalid-hook-call-warning tag: r, elClass: s, schemaEvents: o }) => { const n = g(o), t = new Set(Object.keys(n ?? {})), c = e.forwardRef( (u, l) => { const f = e.useRef(/* @__PURE__ */ new Map()), d = e.useRef(null), { reactProps: h, customElProps: p } = b(u, t, s); return e.useLayoutEffect(() => { if (d.current === null) return; const a = /* @__PURE__ */ 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; }; export { j as customElementToReactComponent };