next-sanity
Version:
Sanity.io toolkit for Next.js
96 lines (95 loc) • 3.04 kB
JavaScript
import { jsx, Fragment, jsxs } from "react/jsx-runtime";
import { useState, useSyncExternalStore, useMemo } from "react";
import { Studio } from "sanity";
import { NextStudioNoScript, NextStudioLayout } from "./NextStudioNoScript.js";
import { createHashHistory } from "history";
import { useServerInsertedHTML } from "next/navigation.js";
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
function createHashHistoryForStudio() {
const history = createHashHistory();
return {
get action() {
return history.action;
},
get location() {
return history.location;
},
get createHref() {
return history.createHref;
},
get push() {
return history.push;
},
get replace() {
return history.replace;
},
get go() {
return history.go;
},
get back() {
return history.back;
},
get forward() {
return history.forward;
},
get block() {
return history.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.listen(({ location }) => {
listener(location);
});
}
};
}
function StyledComponentsRegistry({
children,
isMounted
}) {
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());
return useServerInsertedHTML(() => {
const styles = styledComponentsStyleSheet.getStyleElement();
return styledComponentsStyleSheet.instance.clearTag(), /* @__PURE__ */ jsx(Fragment, { children: styles });
}), isMounted ? /* @__PURE__ */ jsx(Fragment, { children }) : /* @__PURE__ */ jsx(StyleSheetManager, { sheet: styledComponentsStyleSheet.instance, children });
}
function useIsMounted() {
return useSyncExternalStore(
emptySubscribe,
() => !0,
() => !1
);
}
const emptySubscribe = () => () => {
};
function NextStudioComponent({
children,
config,
unstable__noScript = !0,
scheme,
history,
...props
}) {
const isMounted = useIsMounted(), unstableHistory = useMemo(() => {
if (props.unstable_history && history)
throw new Error("Cannot use both `unstable_history` and `history` props at the same time");
return isMounted && history === "hash" ? createHashHistoryForStudio() : props.unstable_history;
}, [history, isMounted, props.unstable_history]);
return /* @__PURE__ */ jsxs(Fragment, { children: [
unstable__noScript && /* @__PURE__ */ jsx(NextStudioNoScript, {}),
/* @__PURE__ */ jsx(StyledComponentsRegistry, { isMounted, children: /* @__PURE__ */ jsx(NextStudioLayout, { children: history === "hash" && !isMounted ? null : children || /* @__PURE__ */ jsx(
Studio,
{
config,
scheme,
unstable_globalStyles: !0,
...props,
unstable_history: unstableHistory
}
) }) })
] });
}
export {
NextStudioComponent as default
};
//# sourceMappingURL=NextStudio.js.map