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