UNPKG

next-sanity

Version:
89 lines (88 loc) 3.2 kB
"use strict"; var jsxRuntime = require("react/jsx-runtime"), react = require("react"), sanity = require("sanity"), NextStudioNoScript = require("./NextStudioNoScript.cjs"), history = require("history"), navigation_js = require("next/navigation.js"), styledComponents = require("styled-components"); function createHashHistoryForStudio() { const history$1 = history.createHashHistory(); return { get action() { return history$1.action; }, get location() { return history$1.location; }, get createHref() { return history$1.createHref; }, get push() { return history$1.push; }, get replace() { return history$1.replace; }, get go() { return history$1.go; }, get back() { return history$1.back; }, get forward() { return history$1.forward; }, get block() { return history$1.block; }, // Overriding listen to workaround a problem where native history provides history.listen(location => void), but the npm package is history.listen(({action, location}) => void) listen(listener) { return history$1.listen(({ location }) => { listener(location); }); } }; } function StyledComponentsRegistry({ children, isMounted }) { const [styledComponentsStyleSheet] = react.useState(() => new styledComponents.ServerStyleSheet()); return navigation_js.useServerInsertedHTML(() => { const styles = styledComponentsStyleSheet.getStyleElement(); return styledComponentsStyleSheet.instance.clearTag(), /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: styles }); }), isMounted ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children }) : /* @__PURE__ */ jsxRuntime.jsx(styledComponents.StyleSheetManager, { sheet: styledComponentsStyleSheet.instance, children }); } function useIsMounted() { return react.useSyncExternalStore( emptySubscribe, () => !0, () => !1 ); } const emptySubscribe = () => () => { }; function NextStudioComponent({ children, config, unstable__noScript = !0, scheme, history: history2, ...props }) { const isMounted = useIsMounted(), unstableHistory = react.useMemo(() => { if (props.unstable_history && history2) throw new Error("Cannot use both `unstable_history` and `history` props at the same time"); return isMounted && history2 === "hash" ? createHashHistoryForStudio() : props.unstable_history; }, [history2, isMounted, props.unstable_history]); return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [ unstable__noScript && /* @__PURE__ */ jsxRuntime.jsx(NextStudioNoScript.NextStudioNoScript, {}), /* @__PURE__ */ jsxRuntime.jsx(StyledComponentsRegistry, { isMounted, children: /* @__PURE__ */ jsxRuntime.jsx(NextStudioNoScript.NextStudioLayout, { children: history2 === "hash" && !isMounted ? null : children || /* @__PURE__ */ jsxRuntime.jsx( sanity.Studio, { config, scheme, unstable_globalStyles: !0, ...props, unstable_history: unstableHistory } ) }) }) ] }); } exports.default = NextStudioComponent; //# sourceMappingURL=NextStudio.cjs.map