UNPKG

@uploadcare/react-uploader

Version:

React component for file uploads using Uploadcare

124 lines (123 loc) 3.43 kB
import P, { useState as w, useEffect as y } from "react"; import * as f from "@uploadcare/file-uploader"; const b = /* @__PURE__ */ new Set([ "children", "ref", "style", "className" ]), j = (e = {}) => Object == null ? void 0 : Object.keys(e).reduce((r, o) => { var s, t; const n = e == null ? void 0 : e[o], a = `on${(t = (s = n == null ? void 0 : n.split("-")) == null ? void 0 : s.map((c) => c.charAt(0).toUpperCase() + c.slice(1))) == null ? void 0 : t.join("")}`; return r[a] = n, r; }, {}), m = /* @__PURE__ */ new WeakMap(), A = (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: o, prevValueProp: s, event: t }) => { if (t !== void 0) { if (o !== s) { const n = A(e), a = n.has(t); let c = n.get(t); o !== void 0 ? a ? c.handleEvent = o : (c = { handleEvent: o }, n.set(t, c), e.addEventListener( t, (l) => c.handleEvent(l.detail) )) : a && (n.delete(t), e.removeEventListener(t, c)); } return; } e[r] = o, o == null && r in HTMLElement.prototype && e.removeAttribute(r); }, M = (e, r, o) => { const s = {}, t = {}; return Object.entries(e).forEach(([n, a]) => { b.has(n) ? s[n === "className" ? "class" : n] = a : r.has(n) || n in o.prototype ? t[n] = a : s[n] = a; }), { reactProps: s, customElProps: t }; }, g = ({ react: e, // https://react.dev/warnings/invalid-hook-call-warning tag: r, elClass: o, schemaEvents: s }) => { const t = j(s), n = new Set(Object.keys(t ?? {})), a = e.forwardRef( (c, l) => { const i = e.useRef(/* @__PURE__ */ new Map()), p = e.useRef(null), { reactProps: h, customElProps: v } = M(c, n, o); return e.useLayoutEffect(() => { if (p.current === null) return; const d = /* @__PURE__ */ new Map(); for (const u in v) E({ node: p.current, nameProp: u, valueProp: v[u], prevValueProp: i.current.get(u), event: t[u] }), i.current.delete(u), d.set(u, c[u]); for (const [u, C] of i.current) E({ node: p.current, nameProp: u, valueProp: void 0, prevValueProp: C, event: t[u] }); i.current = d; }), e.createElement(r ?? o.__tag, { ...h, ref: e.useCallback( (d) => { p.current = d, typeof l == "function" ? l(d) : l !== null && (l.current = d); }, [l] ) }); } ); return a.displayName = o.name, a; }, x = g({ react: P, tag: "uc-config", elClass: f.Config }), R = g({ react: P, tag: "uc-upload-ctx-provider", elClass: f.UploadCtxProvider, schemaEvents: f.UploadCtxProvider.EventType }), k = (e) => { const r = {}, o = {}, s = {}; for (const [t, n] of Object.entries(e)) { if (t.startsWith("on")) { r[t] = n; continue; } if (t === "headless") { s[t] = n; continue; } o[t] = n; } return { eventHandlers: r, uploader: s, config: o }; }, O = "1.10.0", U = "React-Uploader", I = () => `${U}/${O}`, L = ({ children: e }) => e, S = () => { const [e, r] = w(!1); return y(() => { typeof window < "u" && r(!0); }, []), e; }; export { x as A, L as C, I as a, R as b, k as g, g as j, S as u };