@sanity/sdk-react 0.0.0-alpha.3 → 0.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +78 -28
  2. package/dist/_chunks-es/context.js +8 -0
  3. package/dist/_chunks-es/context.js.map +1 -0
  4. package/dist/_chunks-es/useLogOut.js +11 -11
  5. package/dist/_chunks-es/useLogOut.js.map +1 -1
  6. package/dist/components.d.ts +11 -186
  7. package/dist/components.js +52 -198
  8. package/dist/components.js.map +1 -1
  9. package/dist/context.d.ts +39 -0
  10. package/dist/context.js +5 -0
  11. package/dist/context.js.map +1 -0
  12. package/dist/hooks.d.ts +201 -15
  13. package/dist/hooks.js +80 -11
  14. package/dist/hooks.js.map +1 -1
  15. package/package.json +17 -15
  16. package/src/_exports/components.ts +2 -13
  17. package/src/_exports/context.ts +2 -0
  18. package/src/_exports/hooks.ts +18 -2
  19. package/src/components/SanityApp.test.tsx +54 -0
  20. package/src/components/SanityApp.tsx +26 -0
  21. package/src/components/auth/AuthBoundary.test.tsx +5 -18
  22. package/src/components/auth/AuthBoundary.tsx +2 -2
  23. package/src/components/auth/Login.test.tsx +3 -17
  24. package/src/components/auth/Login.tsx +25 -16
  25. package/src/components/auth/LoginCallback.test.tsx +2 -17
  26. package/src/components/auth/LoginCallback.tsx +6 -4
  27. package/src/components/auth/LoginError.test.tsx +2 -17
  28. package/src/components/auth/LoginError.tsx +8 -12
  29. package/src/components/auth/LoginFooter.test.tsx +2 -16
  30. package/src/components/auth/LoginFooter.tsx +11 -18
  31. package/src/components/auth/LoginLayout.test.tsx +2 -16
  32. package/src/components/auth/LoginLayout.tsx +8 -19
  33. package/src/components/auth/authTestHelpers.tsx +18 -0
  34. package/src/{components/context → context}/SanityProvider.test.tsx +1 -1
  35. package/src/hooks/client/useClient.test.tsx +1 -1
  36. package/src/hooks/comlink/useFrameConnection.test.tsx +122 -0
  37. package/src/hooks/comlink/useFrameConnection.ts +111 -0
  38. package/src/hooks/comlink/useWindowConnection.test.ts +94 -0
  39. package/src/hooks/comlink/useWindowConnection.ts +82 -0
  40. package/src/hooks/context/useSanityInstance.test.tsx +1 -1
  41. package/src/hooks/context/useSanityInstance.ts +2 -2
  42. package/src/hooks/documentCollection/useDocuments.ts +53 -6
  43. package/src/hooks/helpers/createCallbackHook.tsx +1 -1
  44. package/src/hooks/helpers/createStateSourceHook.tsx +1 -1
  45. package/src/hooks/preview/usePreview.test.tsx +17 -8
  46. package/src/hooks/preview/usePreview.tsx +52 -7
  47. package/src/vite-env.d.ts +10 -0
  48. package/dist/assets/bundle-CcAyERuZ.css +0 -11
  49. package/src/components/DocumentGridLayout/DocumentGridLayout.stories.tsx +0 -113
  50. package/src/components/DocumentGridLayout/DocumentGridLayout.test.tsx +0 -42
  51. package/src/components/DocumentGridLayout/DocumentGridLayout.tsx +0 -21
  52. package/src/components/DocumentListLayout/DocumentListLayout.stories.tsx +0 -105
  53. package/src/components/DocumentListLayout/DocumentListLayout.test.tsx +0 -42
  54. package/src/components/DocumentListLayout/DocumentListLayout.tsx +0 -12
  55. package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.md +0 -49
  56. package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.stories.tsx +0 -39
  57. package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.test.tsx +0 -30
  58. package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.tsx +0 -171
  59. package/src/components/Login/LoginLinks.test.tsx +0 -100
  60. package/src/components/Login/LoginLinks.tsx +0 -73
  61. package/src/css/css.config.js +0 -220
  62. package/src/css/paramour.css +0 -2347
  63. package/src/css/styles.css +0 -11
  64. /package/src/{components/context → context}/SanityProvider.tsx +0 -0
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  <h1 align="center">Sanity SDK - React</h1>
6
6
  </p>
7
7
 
8
- React components and hooks for creating Sanity applications.
8
+ React hooks for creating Sanity applications.
9
9
 
10
10
  ## Installation
11
11
 
@@ -26,63 +26,113 @@ Here's how to implement your Sanity application:
26
26
  npm create vite@latest my-content-os-app -- --template react-ts -y
27
27
  cd my-content-os-app
28
28
  # Install Sanity dependencies
29
- npm i @sanity/sdk-react @sanity/sdk @sanity/ui
29
+ npm i @sanity/sdk-react @sanity/sdk
30
30
  # Run the app
31
31
  npm run dev
32
+ # In another terminal, run the Sanity CoreUI
33
+ npx @sanity/os-cli run --url=http://localhost:5173/
32
34
  ```
33
35
 
34
36
  ```tsx
35
37
  // src/App.tsx
36
- import {createSanityInstance} from '@sanity/sdk'
37
- import {AuthBoundary, SanityProvider} from '@sanity/sdk-react/components'
38
+ import {SanityConfig} from '@sanity/sdk'
39
+ import {SanityApp} from '@sanity/sdk-react/components'
38
40
  import {useCurrentUser, useLogOut} from '@sanity/sdk-react/hooks'
39
- import {Button, Flex, Spinner, Text, ThemeProvider} from '@sanity/ui'
40
- import {buildTheme} from '@sanity/ui/theme'
41
- import {Suspense} from 'react'
42
41
 
43
- const theme = buildTheme({})
44
- const sanityInstance = createSanityInstance({
42
+ import './App.css'
43
+
44
+ const sanityConfig: SanityConfig = {
45
45
  projectId: '<your-project-id>',
46
46
  dataset: '<your-dataset>',
47
47
  // optional auth config set projectId and dataset to '' and authScope to 'org' for a global token
48
48
  // auth: {
49
- // authScope: 'org',
50
- // ...
49
+ // authScope: "org",
51
50
  // },
52
- })
51
+ }
53
52
 
54
53
  export function App(): JSX.Element {
55
54
  return (
56
- <ThemeProvider theme={theme}>
57
- <Suspense fallback={<Spinner />}>
58
- <SanityProvider sanityInstance={sanityInstance}>
59
- <AuthBoundary header={<Text>My Sanity App</Text>}>
60
- <Authenticated />
61
- </AuthBoundary>
62
- </SanityProvider>
63
- </Suspense>
64
- </ThemeProvider>
55
+ <SanityApp sanityConfig={sanityConfig}>
56
+ <MyApp />
57
+ </SanityApp>
65
58
  )
66
59
  }
67
60
 
68
- function Authenticated() {
61
+ function MyApp() {
69
62
  const currentUser = useCurrentUser()
70
63
  const logout = useLogOut()
71
64
 
72
65
  return (
73
- <Flex direction="column" gap={2}>
74
- <Text>Hello, {currentUser?.name}!</Text>
75
- <Button text="Logout" onClick={logout} mode="ghost" />
76
- </Flex>
66
+ <div>
67
+ <h1>Hello, {currentUser?.name}!</h1>
68
+ <button onClick={logout}>Logout</button>
69
+ </div>
77
70
  )
78
71
  }
79
72
 
80
73
  export default App
81
74
  ```
82
75
 
83
- ## Customizing your application
76
+ ```css
77
+ /* src/App.css */
78
+ #root {
79
+ margin: auto;
80
+ }
81
+
82
+ .sc-login-layout {
83
+ min-height: 100vh;
84
+ display: flex;
85
+ background: #f3f3f3;
86
+ }
87
+
88
+ .sc-login-layout__container {
89
+ margin: auto;
90
+ padding: 2rem;
91
+ }
92
+
93
+ .sc-login-layout__card {
94
+ background: white;
95
+ padding: 2rem;
96
+ }
97
+
98
+ .sc-login__title,
99
+ .sc-login-callback {
100
+ text-align: center;
101
+ margin-bottom: 2rem;
102
+ color: #333;
103
+ }
104
+
105
+ .sc-login-providers {
106
+ display: flex;
107
+ flex-direction: column;
108
+ gap: 1rem;
109
+ }
84
110
 
85
- If you would like to implement a custom look and feel, you can use the hooks in your own components.
111
+ .sc-login-providers a {
112
+ padding: 0.8rem;
113
+ border: 1px solid #ddd;
114
+ text-decoration: none;
115
+ color: #333;
116
+ text-align: center;
117
+ }
118
+
119
+ .sc-login-footer {
120
+ margin-top: 2rem;
121
+ text-align: center;
122
+ }
123
+
124
+ .sc-login-footer__links {
125
+ list-style: none;
126
+ padding: 0;
127
+ display: flex;
128
+ justify-content: center;
129
+ gap: 1.5rem;
130
+ }
131
+
132
+ .sc-login-footer__link a {
133
+ font-size: 0.9rem;
134
+ }
135
+ ```
86
136
 
87
137
  ## Available Hooks
88
138
 
@@ -0,0 +1,8 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext } from "react";
3
+ const SanityInstanceContext = createContext(null), SanityProvider = ({ children, sanityInstance }) => /* @__PURE__ */ jsx(SanityInstanceContext.Provider, { value: sanityInstance, children });
4
+ export {
5
+ SanityInstanceContext,
6
+ SanityProvider
7
+ };
8
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sources":["../../src/context/SanityProvider.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {createContext, type ReactElement} from 'react'\n\n/**\n * @public\n */\nexport interface SanityProviderProps {\n children: React.ReactNode\n sanityInstance: SanityInstance\n}\n\nexport const SanityInstanceContext = createContext<SanityInstance | null>(null)\n\n/**\n * Top-level context provider that provides a Sanity configuration instance.\n * This must wrap any Sanity SDK React component.\n * @public\n * @param props - Sanity project and dataset configuration\n * @returns Rendered component\n * @example\n * ```tsx\n * import {createSanityInstance} from '@sanity/sdk'\n * import {ExampleComponent, SanityProvider} from '@sanity/sdk-react'\n *\n * const sanityInstance = createSanityInstance({projectId: 'your-project-id', dataset: 'production'})\n *\n * export default function MyApp() {\n * return (\n * <SanityProvider sanityInstance={sanityInstance}>\n * <ExampleComponent />\n * </SanityProvider>\n * )\n * }\n * ```\n */\nexport const SanityProvider = ({children, sanityInstance}: SanityProviderProps): ReactElement => {\n return (\n <SanityInstanceContext.Provider value={sanityInstance}>\n {children}\n </SanityInstanceContext.Provider>\n )\n}\n"],"names":[],"mappings":";;AAWO,MAAM,wBAAwB,cAAqC,IAAI,GAwBjE,iBAAiB,CAAC,EAAC,UAAU,eAAc,0BAEnD,sBAAsB,UAAtB,EAA+B,OAAO,gBACpC,SACH,CAAA;"}
@@ -1,7 +1,7 @@
1
- import { getAuthState, getLoginUrlsState, fetchLoginUrls, handleCallback, logout } from "@sanity/sdk";
2
- import { createContext, useContext, useMemo, useSyncExternalStore, useCallback } from "react";
3
- import { jsx } from "react/jsx-runtime";
4
- const SanityInstanceContext = createContext(null), SanityProvider = ({ children, sanityInstance }) => /* @__PURE__ */ jsx(SanityInstanceContext.Provider, { value: sanityInstance, children }), useSanityInstance = () => {
1
+ import { getAuthState, handleCallback, getLoginUrlsState, fetchLoginUrls, logout } from "@sanity/sdk";
2
+ import { useContext, useMemo, useSyncExternalStore, useCallback } from "react";
3
+ import { SanityInstanceContext } from "./context.js";
4
+ const useSanityInstance = () => {
5
5
  const sanityInstance = useContext(SanityInstanceContext);
6
6
  if (!sanityInstance)
7
7
  throw new Error("useSanityInstance must be called from within the SanityProvider");
@@ -19,11 +19,6 @@ function createStateSourceHook(stateSourceFactory) {
19
19
  return useHook;
20
20
  }
21
21
  const useAuthState = createStateSourceHook(getAuthState);
22
- function useLoginUrls() {
23
- const instance = useSanityInstance(), { subscribe, getCurrent } = useMemo(() => getLoginUrlsState(instance), [instance]);
24
- if (!getCurrent()) throw fetchLoginUrls(instance);
25
- return useSyncExternalStore(subscribe, getCurrent);
26
- }
27
22
  function createCallbackHook(callback) {
28
23
  function useHook() {
29
24
  const instance = useSanityInstance();
@@ -31,9 +26,14 @@ function createCallbackHook(callback) {
31
26
  }
32
27
  return useHook;
33
28
  }
34
- const useHandleCallback = createCallbackHook(handleCallback), useLogOut = createCallbackHook(logout);
29
+ const useHandleCallback = createCallbackHook(handleCallback);
30
+ function useLoginUrls() {
31
+ const instance = useSanityInstance(), { subscribe, getCurrent } = useMemo(() => getLoginUrlsState(instance), [instance]);
32
+ if (!getCurrent()) throw fetchLoginUrls(instance);
33
+ return useSyncExternalStore(subscribe, getCurrent);
34
+ }
35
+ const useLogOut = createCallbackHook(logout);
35
36
  export {
36
- SanityProvider,
37
37
  createStateSourceHook,
38
38
  useAuthState,
39
39
  useHandleCallback,
@@ -1 +1 @@
1
- {"version":3,"file":"useLogOut.js","sources":["../../src/components/context/SanityProvider.tsx","../../src/hooks/context/useSanityInstance.ts","../../src/hooks/helpers/createStateSourceHook.tsx","../../src/hooks/auth/useAuthState.tsx","../../src/hooks/auth/useLoginUrls.tsx","../../src/hooks/helpers/createCallbackHook.tsx","../../src/hooks/auth/useHandleCallback.tsx","../../src/hooks/auth/useLogOut.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {createContext, type ReactElement} from 'react'\n\n/**\n * @public\n */\nexport interface SanityProviderProps {\n children: React.ReactNode\n sanityInstance: SanityInstance\n}\n\nexport const SanityInstanceContext = createContext<SanityInstance | null>(null)\n\n/**\n * Top-level context provider that provides a Sanity configuration instance.\n * This must wrap any Sanity SDK React component.\n * @public\n * @param props - Sanity project and dataset configuration\n * @returns Rendered component\n * @example\n * ```tsx\n * import {createSanityInstance} from '@sanity/sdk'\n * import {ExampleComponent, SanityProvider} from '@sanity/sdk-react'\n *\n * const sanityInstance = createSanityInstance({projectId: 'your-project-id', dataset: 'production'})\n *\n * export default function MyApp() {\n * return (\n * <SanityProvider sanityInstance={sanityInstance}>\n * <ExampleComponent />\n * </SanityProvider>\n * )\n * }\n * ```\n */\nexport const SanityProvider = ({children, sanityInstance}: SanityProviderProps): ReactElement => {\n return (\n <SanityInstanceContext.Provider value={sanityInstance}>\n {children}\n </SanityInstanceContext.Provider>\n )\n}\n","import type {SanityInstance} from '@sanity/sdk'\nimport {useContext} from 'react'\n\nimport {SanityInstanceContext} from '../../components/context/SanityProvider'\n\n/**\n * Hook that provides the current Sanity instance from the context.\n * This must be called from within a `SanityProvider` component.\n * @public\n * @returns the current Sanity instance\n * @example\n * ```tsx\n * const instance = useSanityInstance()\n * ```\n */\nexport const useSanityInstance = (): SanityInstance => {\n const sanityInstance = useContext(SanityInstanceContext)\n if (!sanityInstance) {\n throw new Error('useSanityInstance must be called from within the SanityProvider')\n }\n\n return sanityInstance\n}\n","import type {SanityInstance, StateSource} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\nexport function createStateSourceHook<TParams extends unknown[], TState>(\n stateSourceFactory: (instance: SanityInstance, ...params: TParams) => StateSource<TState>,\n): (...params: TParams) => TState {\n function useHook(...params: TParams) {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(\n () => stateSourceFactory(instance, ...params),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [instance, ...params],\n )\n\n return useSyncExternalStore(subscribe, getCurrent)\n }\n\n return useHook\n}\n","import {getAuthState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * A React hook that subscribes to authentication state changes.\n *\n * This hook provides access to the current authentication state type from the Sanity auth store.\n * It automatically re-renders the component when the authentication state changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to auth state changes\n * and ensure consistency between server and client rendering.\n *\n * @returns The current authentication state type\n *\n * @example\n * ```tsx\n * function AuthStatus() {\n * const authState = useAuthState()\n * return <div>Current auth state: {authState}</div>\n * }\n * ```\n *\n * @public\n */\nexport const useAuthState = createStateSourceHook(getAuthState)\n","import {type AuthProvider, fetchLoginUrls, getLoginUrlsState} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * A React hook that retrieves the available authentication provider URLs for login.\n *\n * @remarks\n * This hook fetches the login URLs from the Sanity auth store when the component mounts.\n * Each provider object contains information about an authentication method, including its URL.\n * The hook will suspend if the login URLs have not yet loaded.\n *\n * @example\n * ```tsx\n * // LoginProviders component that uses the hook\n * function LoginProviders() {\n * const providers = useLoginUrls()\n *\n * return (\n * <div>\n * {providers.map((provider) => (\n * <a key={provider.name} href={provider.url}>\n * Login with {provider.title}\n * </a>\n * ))}\n * </div>\n * )\n * }\n *\n * // Parent component with Suspense boundary\n * function LoginPage() {\n * return (\n * <Suspense fallback={<div>Loading authentication providers...</div>}>\n * <LoginProviders />\n * </Suspense>\n * )\n * }\n * ```\n *\n * @returns An array of {@link AuthProvider} objects containing login URLs and provider information\n * @public\n */\nexport function useLoginUrls(): AuthProvider[] {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(() => getLoginUrlsState(instance), [instance])\n\n if (!getCurrent()) throw fetchLoginUrls(instance)\n\n return useSyncExternalStore(subscribe, getCurrent as () => AuthProvider[])\n}\n","import type {SanityInstance} from '@sanity/sdk'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\nexport function createCallbackHook<TParams extends unknown[], TReturn>(\n callback: (instance: SanityInstance, ...params: TParams) => TReturn,\n): () => (...params: TParams) => TReturn {\n function useHook() {\n const instance = useSanityInstance()\n return useCallback((...params: TParams) => callback(instance, ...params), [instance])\n }\n\n return useHook\n}\n","import {handleCallback} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * A React hook that returns a function for handling authentication callbacks.\n *\n * @remarks\n * This hook provides access to the authentication store's callback handler,\n * which processes auth redirects by extracting the session ID and fetching the\n * authentication token. If fetching the long-lived token is successful,\n * `handleCallback` will return a Promise that resolves a new location that\n * removes the short-lived token from the URL. Use this in combination with\n * `history.replaceState` or your own router's `replace` function to update the\n * current location without triggering a reload.\n *\n * @example\n * ```tsx\n * function AuthCallback() {\n * const handleCallback = useHandleCallback()\n * const router = useRouter() // Example router\n *\n * useEffect(() => {\n * async function processCallback() {\n * // Handle the callback and get the cleaned URL\n * const newUrl = await handleCallback(window.location.href)\n *\n * if (newUrl) {\n * // Replace URL without triggering navigation\n * router.replace(newUrl, {shallow: true})\n * }\n * }\n *\n * processCallback().catch(console.error)\n * }, [handleCallback, router])\n *\n * return <div>Completing login...</div>\n * }\n * ```\n *\n * @returns A callback handler function that processes OAuth redirects\n * @public\n */\nexport const useHandleCallback = createCallbackHook(handleCallback)\n","import {logout} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * Hook to log out of the current session\n * @public\n * @returns A function to log out of the current session\n */\nexport const useLogOut = createCallbackHook(logout)\n"],"names":[],"mappings":";;;AAWO,MAAM,wBAAwB,cAAqC,IAAI,GAwBjE,iBAAiB,CAAC,EAAC,UAAU,eAAc,0BAEnD,sBAAsB,UAAtB,EAA+B,OAAO,gBACpC,SACH,CAAA,GCxBS,oBAAoB,MAAsB;AAC/C,QAAA,iBAAiB,WAAW,qBAAqB;AACvD,MAAI,CAAC;AACG,UAAA,IAAI,MAAM,iEAAiE;AAG5E,SAAA;AACT;ACjBO,SAAS,sBACd,oBACgC;AAChC,WAAS,WAAW,QAAiB;AACnC,UAAM,WAAW,kBAAkB,GAC7B,EAAC,WAAW,WAAc,IAAA;AAAA,MAC9B,MAAM,mBAAmB,UAAU,GAAG,MAAM;AAAA;AAAA,MAE5C,CAAC,UAAU,GAAG,MAAM;AAAA,IACtB;AAEO,WAAA,qBAAqB,WAAW,UAAU;AAAA,EAAA;AAG5C,SAAA;AACT;ACMa,MAAA,eAAe,sBAAsB,YAAY;ACiBvD,SAAS,eAA+B;AAC7C,QAAM,WAAW,qBACX,EAAC,WAAW,WAAU,IAAI,QAAQ,MAAM,kBAAkB,QAAQ,GAAG,CAAC,QAAQ,CAAC;AAErF,MAAI,CAAC,WAAA,EAAc,OAAM,eAAe,QAAQ;AAEzC,SAAA,qBAAqB,WAAW,UAAkC;AAC3E;AC7CO,SAAS,mBACd,UACuC;AACvC,WAAS,UAAU;AACjB,UAAM,WAAW,kBAAkB;AAC5B,WAAA,YAAY,IAAI,WAAoB,SAAS,UAAU,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC;AAAA,EAAA;AAG/E,SAAA;AACT;AC6Ba,MAAA,oBAAoB,mBAAmB,cAAc,GClCrD,YAAY,mBAAmB,MAAM;"}
1
+ {"version":3,"file":"useLogOut.js","sources":["../../src/hooks/context/useSanityInstance.ts","../../src/hooks/helpers/createStateSourceHook.tsx","../../src/hooks/auth/useAuthState.tsx","../../src/hooks/helpers/createCallbackHook.tsx","../../src/hooks/auth/useHandleCallback.tsx","../../src/hooks/auth/useLoginUrls.tsx","../../src/hooks/auth/useLogOut.tsx"],"sourcesContent":["import {type SanityInstance} from '@sanity/sdk'\nimport {useContext} from 'react'\n\nimport {SanityInstanceContext} from '../../context/SanityProvider'\n\n/**\n * Hook that provides the current Sanity instance from the context.\n * This must be called from within a `SanityProvider` component.\n * @public\n * @returns the current Sanity instance\n * @example\n * ```tsx\n * const instance = useSanityInstance()\n * ```\n */\nexport const useSanityInstance = (): SanityInstance => {\n const sanityInstance = useContext(SanityInstanceContext)\n if (!sanityInstance) {\n throw new Error('useSanityInstance must be called from within the SanityProvider')\n }\n\n return sanityInstance\n}\n","import {type SanityInstance, type StateSource} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\nexport function createStateSourceHook<TParams extends unknown[], TState>(\n stateSourceFactory: (instance: SanityInstance, ...params: TParams) => StateSource<TState>,\n): (...params: TParams) => TState {\n function useHook(...params: TParams) {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(\n () => stateSourceFactory(instance, ...params),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [instance, ...params],\n )\n\n return useSyncExternalStore(subscribe, getCurrent)\n }\n\n return useHook\n}\n","import {getAuthState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * A React hook that subscribes to authentication state changes.\n *\n * This hook provides access to the current authentication state type from the Sanity auth store.\n * It automatically re-renders the component when the authentication state changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to auth state changes\n * and ensure consistency between server and client rendering.\n *\n * @returns The current authentication state type\n *\n * @example\n * ```tsx\n * function AuthStatus() {\n * const authState = useAuthState()\n * return <div>Current auth state: {authState}</div>\n * }\n * ```\n *\n * @public\n */\nexport const useAuthState = createStateSourceHook(getAuthState)\n","import {type SanityInstance} from '@sanity/sdk'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\nexport function createCallbackHook<TParams extends unknown[], TReturn>(\n callback: (instance: SanityInstance, ...params: TParams) => TReturn,\n): () => (...params: TParams) => TReturn {\n function useHook() {\n const instance = useSanityInstance()\n return useCallback((...params: TParams) => callback(instance, ...params), [instance])\n }\n\n return useHook\n}\n","import {handleCallback} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * A React hook that returns a function for handling authentication callbacks.\n *\n * @remarks\n * This hook provides access to the authentication store's callback handler,\n * which processes auth redirects by extracting the session ID and fetching the\n * authentication token. If fetching the long-lived token is successful,\n * `handleCallback` will return a Promise that resolves a new location that\n * removes the short-lived token from the URL. Use this in combination with\n * `history.replaceState` or your own router's `replace` function to update the\n * current location without triggering a reload.\n *\n * @example\n * ```tsx\n * function AuthCallback() {\n * const handleCallback = useHandleCallback()\n * const router = useRouter() // Example router\n *\n * useEffect(() => {\n * async function processCallback() {\n * // Handle the callback and get the cleaned URL\n * const newUrl = await handleCallback(window.location.href)\n *\n * if (newUrl) {\n * // Replace URL without triggering navigation\n * router.replace(newUrl, {shallow: true})\n * }\n * }\n *\n * processCallback().catch(console.error)\n * }, [handleCallback, router])\n *\n * return <div>Completing login...</div>\n * }\n * ```\n *\n * @returns A callback handler function that processes OAuth redirects\n * @public\n */\nexport const useHandleCallback = createCallbackHook(handleCallback)\n","import {type AuthProvider, fetchLoginUrls, getLoginUrlsState} from '@sanity/sdk'\nimport {useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * A React hook that retrieves the available authentication provider URLs for login.\n *\n * @remarks\n * This hook fetches the login URLs from the Sanity auth store when the component mounts.\n * Each provider object contains information about an authentication method, including its URL.\n * The hook will suspend if the login URLs have not yet loaded.\n *\n * @example\n * ```tsx\n * // LoginProviders component that uses the hook\n * function LoginProviders() {\n * const providers = useLoginUrls()\n *\n * return (\n * <div>\n * {providers.map((provider) => (\n * <a key={provider.name} href={provider.url}>\n * Login with {provider.title}\n * </a>\n * ))}\n * </div>\n * )\n * }\n *\n * // Parent component with Suspense boundary\n * function LoginPage() {\n * return (\n * <Suspense fallback={<div>Loading authentication providers...</div>}>\n * <LoginProviders />\n * </Suspense>\n * )\n * }\n * ```\n *\n * @returns An array of {@link AuthProvider} objects containing login URLs and provider information\n * @public\n */\nexport function useLoginUrls(): AuthProvider[] {\n const instance = useSanityInstance()\n const {subscribe, getCurrent} = useMemo(() => getLoginUrlsState(instance), [instance])\n\n if (!getCurrent()) throw fetchLoginUrls(instance)\n\n return useSyncExternalStore(subscribe, getCurrent as () => AuthProvider[])\n}\n","import {logout} from '@sanity/sdk'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n * Hook to log out of the current session\n * @public\n * @returns A function to log out of the current session\n */\nexport const useLogOut = createCallbackHook(logout)\n"],"names":[],"mappings":";;;AAeO,MAAM,oBAAoB,MAAsB;AAC/C,QAAA,iBAAiB,WAAW,qBAAqB;AACvD,MAAI,CAAC;AACG,UAAA,IAAI,MAAM,iEAAiE;AAG5E,SAAA;AACT;ACjBO,SAAS,sBACd,oBACgC;AAChC,WAAS,WAAW,QAAiB;AACnC,UAAM,WAAW,kBAAkB,GAC7B,EAAC,WAAW,WAAc,IAAA;AAAA,MAC9B,MAAM,mBAAmB,UAAU,GAAG,MAAM;AAAA;AAAA,MAE5C,CAAC,UAAU,GAAG,MAAM;AAAA,IACtB;AAEO,WAAA,qBAAqB,WAAW,UAAU;AAAA,EAAA;AAG5C,SAAA;AACT;ACMa,MAAA,eAAe,sBAAsB,YAAY;ACrBvD,SAAS,mBACd,UACuC;AACvC,WAAS,UAAU;AACjB,UAAM,WAAW,kBAAkB;AAC5B,WAAA,YAAY,IAAI,WAAoB,SAAS,UAAU,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC;AAAA,EAAA;AAG/E,SAAA;AACT;AC6Ba,MAAA,oBAAoB,mBAAmB,cAAc;ACA3D,SAAS,eAA+B;AAC7C,QAAM,WAAW,qBACX,EAAC,WAAW,WAAU,IAAI,QAAQ,MAAM,kBAAkB,QAAQ,GAAG,CAAC,QAAQ,CAAC;AAErF,MAAI,CAAC,WAAA,EAAc,OAAM,eAAe,QAAQ;AAEzC,SAAA,qBAAqB,WAAW,UAAkC;AAC3E;ACzCa,MAAA,YAAY,mBAAmB,MAAM;"}
@@ -1,10 +1,6 @@
1
1
  import {FallbackProps} from 'react-error-boundary'
2
- import {ForwardRefExoticComponent} from 'react'
3
2
  import {JSX} from 'react'
4
- import type {PropsWithChildren} from 'react'
5
- import {ReactElement} from 'react'
6
- import {RefAttributes} from 'react'
7
- import {SanityInstance} from '@sanity/sdk'
3
+ import {SanityConfig} from '@sanity/sdk'
8
4
 
9
5
  /**
10
6
  * A component that handles authentication flow and error boundaries for a
@@ -35,7 +31,7 @@ export declare function AuthBoundary({
35
31
  /**
36
32
  * @alpha
37
33
  */
38
- export declare interface AuthBoundaryProps extends LoginLayoutProps {
34
+ declare interface AuthBoundaryProps extends LoginLayoutProps {
39
35
  /**
40
36
  * Custom component to render the login screen.
41
37
  * Receives all login layout props. Defaults to {@link Login}.
@@ -54,144 +50,16 @@ export declare interface AuthBoundaryProps extends LoginLayoutProps {
54
50
  LoginErrorComponent?: React.ComponentType<LoginErrorProps>
55
51
  }
56
52
 
57
- /**
58
- * Error class for authentication-related errors. Wraps errors thrown during the
59
- * authentication flow.
60
- *
61
- * @remarks
62
- * This class provides a consistent error type for authentication failures while
63
- * preserving the original error as the cause. If the original error has a
64
- * message property, it will be used as the error message.
65
- *
66
- * @alpha
67
- */
68
- export declare class AuthError extends Error {
69
- constructor(error: unknown)
70
- }
71
-
72
- /**
73
- * @public
74
- */
75
- export declare const DocumentGridLayout: {
76
- (props: PropsWithChildren): ReactElement
77
- displayName: string
78
- }
79
-
80
- /**
81
- * @public
82
- */
83
- export declare const DocumentListLayout: {
84
- (props: PropsWithChildren): ReactElement
85
- displayName: string
86
- }
87
-
88
- /**
89
- * This is a component that renders a document preview.
90
- *
91
- * @public
92
- *
93
- * @param props - The props for the DocumentPreviewLayout component.
94
- * @returns - The DocumentPreviewLayout component.
95
- */
96
- export declare const DocumentPreviewLayout: ForwardRefExoticComponent<
97
- DocumentPreviewLayoutProps & RefAttributes<HTMLElement>
98
- >
99
-
100
- /**
101
- * @public
102
- */
103
- export declare interface DocumentPreviewLayoutProps {
104
- docType?: string
105
- media?:
106
- | {
107
- type: string
108
- url: string
109
- }
110
- | null
111
- | undefined
112
- onClick?: () => void
113
- selected?: boolean
114
- status?: string
115
- subtitle?: string
116
- title: string
117
- }
118
-
119
- /**
120
- * Login component that displays available authentication providers.
121
- * Renders a list of login options with a loading fallback while providers load.
122
- *
123
- * @alpha
124
- */
125
- export declare function Login({header, footer}: LoginLayoutProps): JSX.Element
126
-
127
- /**
128
- /**
129
- * Component shown during auth callback processing that handles login completion.
130
- * Automatically processes the auth callback when mounted and updates the URL
131
- * to remove callback parameters without triggering a page reload.
132
- *
133
- * @alpha
134
- */
135
- export declare function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode
136
-
137
- /**
138
- * Displays authentication error details and provides retry functionality.
139
- * Only handles {@link AuthError} instances - rethrows other error types.
140
- *
141
- * @alpha
142
- */
143
- export declare function LoginError({
144
- error,
145
- resetErrorBoundary,
146
- header,
147
- footer,
148
- }: LoginErrorProps): React.ReactNode
149
-
150
53
  /**
151
54
  * @alpha
152
55
  */
153
- export declare type LoginErrorProps = FallbackProps & LoginLayoutProps
154
-
155
- /**
156
- * Layout component for login-related screens providing consistent styling and structure.
157
- * Renders content in a centered card with optional header and footer sections.
158
- *
159
- * Can be used to build custom login screens for the AuthBoundary component, including:
160
- * - Login provider selection (LoginComponent)
161
- * - OAuth callback handling (CallbackComponent)
162
- * - Error states (LoginErrorComponent)
163
- *
164
- * @example
165
- * ```tsx
166
- * // Custom login screen using the layout
167
- * function CustomLogin({header, footer}: LoginLayoutProps) {
168
- * return (
169
- * <LoginLayout
170
- * header={header}
171
- * footer={footer}
172
- * >
173
- * <CustomLoginContent />
174
- * </LoginLayout>
175
- * )
176
- * }
177
- *
178
- * // Use with AuthBoundary
179
- * <AuthBoundary
180
- * LoginComponent={CustomLogin}
181
- * header={<Logo />}
182
- * >
183
- * <ProtectedContent />
184
- * </AuthBoundary>
185
- * ```
186
- *
187
- * @alpha
188
- */
189
- export declare function LoginLayout({children, footer, header}: LoginLayoutProps): React.ReactNode
56
+ declare type LoginErrorProps = FallbackProps & LoginLayoutProps
190
57
 
191
58
  /**
192
59
  * @alpha
60
+ * @internal
193
61
  */
194
- export declare interface LoginLayoutProps {
62
+ declare interface LoginLayoutProps {
195
63
  /** Optional header content rendered at top of card */
196
64
  header?: React.ReactNode
197
65
  /** Optional footer content rendered below card. Defaults to an internal login footer */
@@ -201,59 +69,16 @@ export declare interface LoginLayoutProps {
201
69
  }
202
70
 
203
71
  /**
204
- * Component that handles Sanity authentication flow and renders login provider options
205
- *
206
72
  * @public
207
73
  *
208
- * @returns Rendered component
209
- *
210
- * @remarks
211
- * The component handles three states:
212
- * 1. Loading state during token exchange
213
- * 2. Success state after successful authentication
214
- * 3. Provider selection UI when not authenticated
215
- *
216
- * @example
217
- * ```tsx
218
- * const config = { projectId: 'your-project-id', dataset: 'production' }
219
- * return <LoginLinks sanityInstance={config} />
220
- * ```
221
- */
222
- export declare const LoginLinks: () => ReactElement
223
-
224
- /**
225
- * Top-level context provider that provides a Sanity configuration instance.
226
- * This must wrap any Sanity SDK React component.
227
- * @public
228
- * @param props - Sanity project and dataset configuration
229
- * @returns Rendered component
230
- * @example
231
- * ```tsx
232
- * import {createSanityInstance} from '@sanity/sdk'
233
- * import {ExampleComponent, SanityProvider} from '@sanity/sdk-react'
234
- *
235
- * const sanityInstance = createSanityInstance({projectId: 'your-project-id', dataset: 'production'})
236
- *
237
- * export default function MyApp() {
238
- * return (
239
- * <SanityProvider sanityInstance={sanityInstance}>
240
- * <ExampleComponent />
241
- * </SanityProvider>
242
- * )
243
- * }
244
- * ```
74
+ * @returns Rendered child component wrapped in a SanityProvider and AuthBoundary
245
75
  */
246
- export declare const SanityProvider: ({
76
+ export declare function SanityApp({
77
+ sanityConfig,
247
78
  children,
248
- sanityInstance,
249
- }: SanityProviderProps) => ReactElement
250
-
251
- /**
252
- * @public
253
- */
254
- export declare interface SanityProviderProps {
79
+ }: {
255
80
  children: React.ReactNode
256
- sanityInstance: SanityInstance
257
- }
81
+ sanityConfig: SanityConfig
82
+ }): JSX.Element
258
83
 
259
84
  export {}