@croct/plug-react
Version:
React components and hooks to plug your React applications into Croct.
82 lines (81 loc) • 2.85 kB
JavaScript
;
"use client";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var CroctProvider_exports = {};
__export(CroctProvider_exports, {
CroctContext: () => CroctContext,
CroctProvider: () => CroctProvider
});
module.exports = __toCommonJS(CroctProvider_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ssr_polyfills = require('./ssr-polyfills.cjs');
const CroctContext = (0, import_react.createContext)(null);
CroctContext.displayName = "CroctContext";
function useLiveRef(value) {
const ref = (0, import_react.useRef)(value);
ref.current = value;
return ref;
}
const CroctProvider = (props) => {
const { children, ...configuration } = props;
const parent = (0, import_react.useContext)(CroctContext);
const baseConfiguration = useLiveRef(configuration);
if (parent !== null) {
throw new Error(
"You cannot render <CroctProvider> inside another <CroctProvider>. Croct should only be initialized once in the application."
);
}
const context = (0, import_react.useMemo)(
() => ({
get plug() {
if (!import_ssr_polyfills.croct.initialized) {
import_ssr_polyfills.croct.plug(baseConfiguration.current);
}
return new Proxy(import_ssr_polyfills.croct, {
get: function getProperty(target, property) {
if (property === "plug") {
return (options) => {
target.plug({ ...baseConfiguration.current, ...options });
};
}
return target[property];
}
});
}
}),
[baseConfiguration]
);
(0, import_react.useEffect)(
() => {
import_ssr_polyfills.croct.plug(baseConfiguration.current);
return () => {
import_ssr_polyfills.croct.unplug().catch(() => {
});
};
},
[baseConfiguration]
);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CroctContext.Provider, { value: context, children });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CroctContext,
CroctProvider
});