UNPKG

next-sanity

Version:
1 lines 7.41 kB
{"version":3,"file":"NextStudio.cjs","sources":["../../src/studio/client-component/createHashHistoryForStudio.ts","../../src/studio/client-component/registry.tsx","../../src/studio/client-component/useIsMounted.ts","../../src/studio/client-component/NextStudio.tsx"],"sourcesContent":["import {createHashHistory, type History, type Listener} from 'history'\n\n/** @internal */\nexport function createHashHistoryForStudio(): History {\n const history = createHashHistory()\n return {\n get action() {\n return history.action\n },\n get location() {\n return history.location\n },\n get createHref() {\n return history.createHref\n },\n get push() {\n return history.push\n },\n get replace() {\n return history.replace\n },\n get go() {\n return history.go\n },\n get back() {\n return history.back\n },\n get forward() {\n return history.forward\n },\n get block() {\n return history.block\n },\n // Overriding listen to workaround a problem where native history provides history.listen(location => void), but the npm package is history.listen(({action, location}) => void)\n listen(listener: Listener) {\n // return history.listen(({ action, location }) => {\n return history.listen(({location}) => {\n // console.debug('history.listen', action, location)\n // @ts-expect-error -- working around a bug? in studio\n listener(location)\n })\n },\n }\n}\n","// https://nextjs.org/docs/app/building-your-application/styling/css-in-js#styled-components\nimport {useServerInsertedHTML} from 'next/navigation.js'\nimport {useState} from 'react'\nimport {ServerStyleSheet, StyleSheetManager} from 'styled-components'\n\nexport function StyledComponentsRegistry({\n children,\n isMounted,\n}: {\n children: React.ReactNode\n isMounted: boolean\n}): React.JSX.Element {\n // Only create stylesheet once with lazy initial state\n // x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state\n const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet())\n\n useServerInsertedHTML(() => {\n const styles = styledComponentsStyleSheet.getStyleElement()\n styledComponentsStyleSheet.instance.clearTag()\n return <>{styles}</>\n })\n\n if (isMounted) return <>{children}</>\n\n return (\n <StyleSheetManager sheet={styledComponentsStyleSheet.instance}>{children}</StyleSheetManager>\n )\n}\n","import {useSyncExternalStore} from 'react'\n\n/** @internal */\nexport function useIsMounted(): boolean {\n return useSyncExternalStore(\n emptySubscribe,\n () => true,\n () => false,\n )\n}\n// eslint-disable-next-line no-empty-function\nconst emptySubscribe = () => () => {}\n","import {useMemo} from 'react'\nimport {Studio, type StudioProps} from 'sanity'\n\nimport {NextStudioLayout} from '../NextStudioLayout'\nimport {NextStudioNoScript} from '../NextStudioNoScript'\nimport {createHashHistoryForStudio} from './createHashHistoryForStudio'\nimport {StyledComponentsRegistry} from './registry'\nimport {useIsMounted} from './useIsMounted'\n\n/** @public */\nexport interface NextStudioProps extends StudioProps {\n children?: React.ReactNode\n /**\n * Render the <noscript> tag\n * @defaultValue true\n * @alpha\n */\n unstable__noScript?: boolean\n /**\n * The 'hash' option is new feature that is not yet stable for production, but is available for testing and its API won't change in a breaking way.\n * If 'hash' doesn't work for you, or if you want to use a memory based history, you can use the `unstable_history` prop instead.\n * @alpha\n * @defaultValue 'browser'\n */\n history?: 'browser' | 'hash'\n}\n/**\n * Override how the Studio renders by passing children.\n * This is useful for advanced use cases where you're using StudioProvider and StudioLayout instead of Studio:\n * ```\n * import {StudioProvider, StudioLayout} from 'sanity'\n * import {NextStudio} from 'next-sanity/studio'\n * <NextStudio config={config}>\n * <StudioProvider config={config}>\n * <CustomComponentThatUsesContextFromStudioProvider />\n * <StudioLayout />\n * </StudioProvider>\n * </NextStudio>\n * ```\n * @public\n */\nexport default function NextStudioComponent({\n children,\n config,\n unstable__noScript = true,\n scheme,\n history,\n ...props\n}: NextStudioProps): React.JSX.Element {\n const isMounted = useIsMounted()\n const unstableHistory = useMemo<typeof props.unstable_history>(() => {\n if (props.unstable_history && history) {\n throw new Error('Cannot use both `unstable_history` and `history` props at the same time')\n }\n\n if (isMounted && history === 'hash') {\n return createHashHistoryForStudio()\n }\n return props.unstable_history\n }, [history, isMounted, props.unstable_history])\n\n return (\n <>\n {unstable__noScript && <NextStudioNoScript />}\n <StyledComponentsRegistry isMounted={isMounted}>\n <NextStudioLayout>\n {history === 'hash' && !isMounted\n ? null\n : children || (\n <Studio\n config={config}\n scheme={scheme}\n unstable_globalStyles\n {...props}\n unstable_history={unstableHistory}\n />\n )}\n </NextStudioLayout>\n </StyledComponentsRegistry>\n </>\n )\n}\n"],"names":["history","createHashHistory","useState","ServerStyleSheet","useServerInsertedHTML","jsx","Fragment","StyleSheetManager","useSyncExternalStore","useMemo","jsxs","NextStudioNoScript","NextStudioLayout","Studio"],"mappings":";;AAGO,SAAS,6BAAsC;AACpD,QAAMA,YAAUC,QAAAA,kBAAkB;AAC3B,SAAA;AAAA,IACL,IAAI,SAAS;AACX,aAAOD,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,WAAW;AACb,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,aAAa;AACf,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,OAAO;AACT,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,UAAU;AACZ,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,KAAK;AACP,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,OAAO;AACT,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,UAAU;AACZ,aAAOA,UAAQ;AAAA,IACjB;AAAA,IACA,IAAI,QAAQ;AACV,aAAOA,UAAQ;AAAA,IACjB;AAAA;AAAA,IAEA,OAAO,UAAoB;AAEzB,aAAOA,UAAQ,OAAO,CAAC,EAAC,eAAc;AAGpC,iBAAS,QAAQ;AAAA,MAAA,CAClB;AAAA,IAAA;AAAA,EAEL;AACF;ACtCO,SAAS,yBAAyB;AAAA,EACvC;AAAA,EACA;AACF,GAGsB;AAGpB,QAAM,CAAC,0BAA0B,IAAIE,eAAS,MAAM,IAAIC,mCAAkB;AAQ1E,SANAC,oCAAsB,MAAM;AACpB,UAAA,SAAS,2BAA2B,gBAAgB;AAC1D,WAAA,2BAA2B,SAAS,SAC7B,GAAAC,2BAAA,IAAAC,qBAAA,EAAG,UAAO,QAAA;AAAA,EAClB,CAAA,GAEG,YAAqBD,2BAAAA,IAAAC,WAAAA,UAAA,EAAA,S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