@sanity/preview-kit
Version:
General purpose utils for live content and visual editing
65 lines (64 loc) • 2.62 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
mod
));
Object.defineProperty(exports, "__esModule", { value: !0 });
var jsxRuntime = require("react/jsx-runtime"), react = require("react");
const ClientComponent = react.lazy(() => import("@sanity/preview-kit/live-query/client-component")), DEFAULT_PARAMS = {};
function LiveQuery(props) {
if (!props.enabled)
return props.children;
const { query, params = DEFAULT_PARAMS, initialData, as: LiveComponent } = props;
if (LiveComponent) {
if (react.Children.count(props.children) > 1)
throw new Error("LiveQuery: `as` prop can only be used with a single child");
if (!react.isValidElement(props.children))
throw new Error("LiveQuery: `as` prop requires a valid `children` prop");
return /* @__PURE__ */ jsxRuntime.jsx(
ClientComponent,
{
initialData,
query,
params,
throwOnMissingProvider: props.throwOnMissingProvider,
children: /* @__PURE__ */ jsxRuntime.jsx(
LiveComponent,
{
// eslint-disable-next-line no-warning-comments
// @ts-expect-error -- @todo fix the typings
...props.children.props
}
)
}
);
}
return /* @__PURE__ */ jsxRuntime.jsx(
ClientComponent,
{
initialData,
query,
params,
children: props.children
}
);
}
LiveQuery.displayName = "LiveQuery";
exports.LiveQuery = LiveQuery;
exports.default = LiveQuery;
//# sourceMappingURL=live-query.cjs.map