@prismicio/react 2.0.0-beta.3 → 2.0.0-beta.7

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.
package/src/SliceZone.tsx CHANGED
@@ -136,6 +136,44 @@ export const TODOSliceComponent = __PRODUCTION__
136
136
  );
137
137
  };
138
138
 
139
+ /**
140
+ * Arguments for a `<SliceZone>` `resolver` function.
141
+ */
142
+ type SliceZoneResolverArgs<TSlice extends SliceLike = SliceLike> = {
143
+ /**
144
+ * The Slice to resolve to a React component.
145
+ */
146
+ slice: TSlice;
147
+
148
+ /**
149
+ * The name of the Slice.
150
+ */
151
+ sliceName: TSlice["slice_type"];
152
+
153
+ /**
154
+ * The index of the Slice in the Slice Zone.
155
+ */
156
+ i: number;
157
+ };
158
+
159
+ /**
160
+ * A function that determines the rendered React component for each Slice in the
161
+ * Slice Zone. If a nullish value is returned, the component will fallback to
162
+ * the `components` or `defaultComponent` props to determine the rendered component.
163
+ *
164
+ * @deprecated Use the `components` prop instead.
165
+ *
166
+ * @param args - Arguments for the resolver function.
167
+ *
168
+ * @returns The React component to render for a Slice.
169
+ */
170
+ export type SliceZoneResolver<
171
+ TSlice extends SliceLike = SliceLike,
172
+ TContext = unknown,
173
+ > = (
174
+ args: SliceZoneResolverArgs<TSlice>,
175
+ ) => SliceComponentType<TSlice, TContext> | undefined | null;
176
+
139
177
  /**
140
178
  * React props for the `<SliceZone>` component.
141
179
  *
@@ -156,6 +194,18 @@ export type SliceZoneProps<
156
194
  */
157
195
  components?: SliceZoneComponents<TSlice, TContext>;
158
196
 
197
+ /**
198
+ * A function that determines the rendered React component for each Slice in
199
+ * the Slice Zone.
200
+ *
201
+ * @deprecated Use the `components` prop instead.
202
+ *
203
+ * @param args - Arguments for the resolver function.
204
+ *
205
+ * @returns The React component to render for a Slice.
206
+ */
207
+ resolver?: SliceZoneResolver<TSlice, TContext>;
208
+
159
209
  /**
160
210
  * The React component rendered if a component mapping from the `components`
161
211
  * prop cannot be found.
@@ -178,20 +228,37 @@ export type SliceZoneProps<
178
228
  *
179
229
  * @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
180
230
  * @typeParam TContext - Arbitrary data made available to all Slice components.
231
+ *
181
232
  * @returns The Slice Zone's content as React components.
233
+ *
182
234
  * @see Learn about Prismic Slices and Slice Zones {@link https://prismic.io/docs/core-concepts/slices}
183
235
  */
184
236
  export const SliceZone = <TSlice extends SliceLike, TContext>({
185
237
  slices = [],
186
238
  components = {} as SliceZoneComponents<TSlice, TContext>,
239
+ resolver,
187
240
  defaultComponent = TODOSliceComponent,
188
241
  context = {} as TContext,
189
242
  }: SliceZoneProps<TSlice, TContext>): JSX.Element => {
190
243
  const renderedSlices = React.useMemo(() => {
191
244
  return slices.map((slice, index) => {
192
- const Comp = (components[slice.slice_type as keyof typeof components] ||
245
+ let Comp = (components[slice.slice_type as keyof typeof components] ||
193
246
  defaultComponent) as SliceComponentType<TSlice, TContext>;
194
- const key = JSON.stringify(slice);
247
+
248
+ // TODO: Remove `resolver` in v3 in favor of `components`.
249
+ if (resolver) {
250
+ const resolvedComp = resolver({
251
+ slice,
252
+ sliceName: slice.slice_type,
253
+ i: index,
254
+ });
255
+
256
+ if (resolvedComp) {
257
+ Comp = resolvedComp;
258
+ }
259
+ }
260
+
261
+ const key = `${index}-${JSON.stringify(slice)}`;
195
262
 
196
263
  return (
197
264
  <Comp
@@ -12,10 +12,13 @@ import {
12
12
  *
13
13
  * @remarks
14
14
  * An additional `@prismicio/client` instance can be provided at `params.client`.
15
+ *
15
16
  * @typeParam TDocument - Type of Prismic documents returned
17
+ *
16
18
  * @param params - Parameters to filter, sort, and paginate results
17
19
  *
18
20
  * @returns The composable payload {@link ClientHookReturnType}
21
+ *
19
22
  * @see Underlying `@prismicio/client` method {@link proto.get}
20
23
  */
21
24
  export const usePrismicDocuments = <TDocument extends prismicT.PrismicDocument>(
@@ -29,10 +32,13 @@ export const usePrismicDocuments = <TDocument extends prismicT.PrismicDocument>(
29
32
  *
30
33
  * @remarks
31
34
  * An additional `@prismicio/client` instance can be provided at `params.client`.
35
+ *
32
36
  * @typeParam TDocument - Type of the Prismic document returned
37
+ *
33
38
  * @param params - Parameters to filter, sort, and paginate results
34
39
  *
35
40
  * @returns The composable payload {@link ClientHookReturnType}
41
+ *
36
42
  * @see Underlying `@prismicio/client` method {@link proto.getFirst}
37
43
  */
38
44
  export const useFirstPrismicDocument = <
@@ -48,10 +54,13 @@ export const useFirstPrismicDocument = <
48
54
  *
49
55
  * @remarks
50
56
  * An additional `@prismicio/client` instance can be provided at `params.client`.
57
+ *
51
58
  * @typeParam TDocument - Type of Prismic documents returned
59
+ *
52
60
  * @param params - Parameters to filter and sort results
53
61
  *
54
62
  * @returns The composable payload {@link ClientHookReturnType}
63
+ *
55
64
  * @see Underlying `@prismicio/client` method {@link proto.getAll}
56
65
  */
57
66
  export const useAllPrismicDocumentsDangerously = <
@@ -69,11 +78,14 @@ export const useAllPrismicDocumentsDangerously = <
69
78
  *
70
79
  * @remarks
71
80
  * An additional `@prismicio/client` instance can be provided at `params.client`.
81
+ *
72
82
  * @typeParam TDocument - Type of the Prismic document returned
83
+ *
73
84
  * @param id - ID of the document
74
85
  * @param params - Parameters to filter, sort, and paginate results
75
86
  *
76
87
  * @returns The composable payload {@link ClientHookReturnType}
88
+ *
77
89
  * @see Underlying `@prismicio/client` method {@link proto.getByID}
78
90
  */
79
91
  export const usePrismicDocumentByID = <
@@ -91,11 +103,14 @@ export const usePrismicDocumentByID = <
91
103
  *
92
104
  * @remarks
93
105
  * An additional `@prismicio/client` instance can be provided at `params.client`.
106
+ *
94
107
  * @typeParam TDocument - Type of Prismic documents returned
108
+ *
95
109
  * @param ids - A list of document IDs
96
110
  * @param params - Parameters to filter, sort, and paginate results
97
111
  *
98
112
  * @returns The composable payload {@link ClientHookReturnType}
113
+ *
99
114
  * @see Underlying `@prismicio/client` method {@link proto.getByIDs}
100
115
  */
101
116
  export const usePrismicDocumentsByIDs = <
@@ -113,11 +128,14 @@ export const usePrismicDocumentsByIDs = <
113
128
  *
114
129
  * @remarks
115
130
  * An additional `@prismicio/client` instance can be provided at `params.client`.
131
+ *
116
132
  * @typeParam TDocument - Type of Prismic documents returned
133
+ *
117
134
  * @param ids - A list of document IDs
118
135
  * @param params - Parameters to filter and sort results
119
136
  *
120
137
  * @returns The composable payload {@link ClientHookReturnType}
138
+ *
121
139
  * @see Underlying `@prismicio/client` method {@link proto.getAllByIDs}
122
140
  */
123
141
  export const useAllPrismicDocumentsByIDs = <
@@ -136,12 +154,15 @@ export const useAllPrismicDocumentsByIDs = <
136
154
  *
137
155
  * @remarks
138
156
  * An additional `@prismicio/client` instance can be provided at `params.client`.
157
+ *
139
158
  * @typeParam TDocument - Type of the Prismic document returned
159
+ *
140
160
  * @param documentType - The API ID of the document's Custom Type
141
161
  * @param uid - UID of the document
142
162
  * @param params - Parameters to filter, sort, and paginate results
143
163
  *
144
164
  * @returns The composable payload {@link ClientHookReturnType}
165
+ *
145
166
  * @see Underlying `@prismicio/client` method {@link proto.getByUID}
146
167
  */
147
168
  export const usePrismicDocumentByUID = <
@@ -161,12 +182,15 @@ export const usePrismicDocumentByUID = <
161
182
  *
162
183
  * @remarks
163
184
  * An additional `@prismicio/client` instance can be provided at `params.client`.
185
+ *
164
186
  * @typeParam TDocument - Type of the Prismic document returned
187
+ *
165
188
  * @param documentType - The API ID of the document's Custom Type
166
189
  * @param uids - A list of document UIDs.
167
190
  * @param params - Parameters to filter, sort, and paginate results
168
191
  *
169
192
  * @returns The composable payload {@link ClientHookReturnType}
193
+ *
170
194
  * @see Underlying `@prismicio/client` method {@link proto.getByUID}
171
195
  */
172
196
  export const usePrismicDocumentsByUIDs = <
@@ -186,12 +210,15 @@ export const usePrismicDocumentsByUIDs = <
186
210
  *
187
211
  * @remarks
188
212
  * An additional `@prismicio/client` instance can be provided at `params.client`.
213
+ *
189
214
  * @typeParam TDocument - Type of the Prismic document returned
215
+ *
190
216
  * @param documentType - The API ID of the document's Custom Type
191
217
  * @param uids - A list of document UIDs.
192
218
  * @param params - Parameters to filter, sort, and paginate results
193
219
  *
194
220
  * @returns The composable payload {@link ClientHookReturnType}
221
+ *
195
222
  * @see Underlying `@prismicio/client` method {@link proto.getByUID}
196
223
  */
197
224
  export const useAllPrismicDocumentsByUIDs = <
@@ -211,11 +238,14 @@ export const useAllPrismicDocumentsByUIDs = <
211
238
  *
212
239
  * @remarks
213
240
  * An additional `@prismicio/client` instance can be provided at `params.client`.
241
+ *
214
242
  * @typeParam TDocument - Type of the Prismic document returned
243
+ *
215
244
  * @param documentType - The API ID of the singleton Custom Type
216
245
  * @param params - Parameters to filter, sort, and paginate results
217
246
  *
218
247
  * @returns The composable payload {@link ClientHookReturnType}
248
+ *
219
249
  * @see Underlying `@prismicio/client` method {@link proto.getSingle}
220
250
  */
221
251
  export const useSinglePrismicDocument = <
@@ -233,11 +263,14 @@ export const useSinglePrismicDocument = <
233
263
  *
234
264
  * @remarks
235
265
  * An additional `@prismicio/client` instance can be provided at `params.client`.
266
+ *
236
267
  * @typeParam TDocument - Type of Prismic documents returned
268
+ *
237
269
  * @param documentType - The API ID of the Custom Type
238
270
  * @param params - Parameters to filter, sort, and paginate results
239
271
  *
240
272
  * @returns The composable payload {@link ClientHookReturnType}
273
+ *
241
274
  * @see Underlying `@prismicio/client` method {@link proto.getByType}
242
275
  */
243
276
  export const usePrismicDocumentsByType = <
@@ -256,11 +289,14 @@ export const usePrismicDocumentsByType = <
256
289
  *
257
290
  * @remarks
258
291
  * An additional `@prismicio/client` instance can be provided at `params.client`.
292
+ *
259
293
  * @typeParam TDocument - Type of Prismic documents returned
294
+ *
260
295
  * @param documentType - The API ID of the Custom Type
261
296
  * @param params - Parameters to filter and sort results
262
297
  *
263
298
  * @returns The composable payload {@link ClientHookReturnType}
299
+ *
264
300
  * @see Underlying `@prismicio/client` method {@link proto.getAllByType}
265
301
  */
266
302
  export const useAllPrismicDocumentsByType = <
@@ -278,11 +314,14 @@ export const useAllPrismicDocumentsByType = <
278
314
  *
279
315
  * @remarks
280
316
  * An additional `@prismicio/client` instance can be provided at `params.client`.
317
+ *
281
318
  * @typeParam TDocument - Type of Prismic documents returned
319
+ *
282
320
  * @param tag - The tag that must be included on a document
283
321
  * @param params - Parameters to filter, sort, and paginate results
284
322
  *
285
323
  * @returns The composable payload {@link ClientHookReturnType}
324
+ *
286
325
  * @see Underlying `@prismicio/client` method {@link proto.getByTag}
287
326
  */
288
327
  export const usePrismicDocumentsByTag = <
@@ -300,11 +339,14 @@ export const usePrismicDocumentsByTag = <
300
339
  *
301
340
  * @remarks
302
341
  * An additional `@prismicio/client` instance can be provided at `params.client`.
342
+ *
303
343
  * @typeParam TDocument - Type of Prismic documents returned
344
+ *
304
345
  * @param tag - The tag that must be included on a document
305
346
  * @param params - Parameters to filter and sort results
306
347
  *
307
348
  * @returns The composable payload {@link ClientHookReturnType}
349
+ *
308
350
  * @see Underlying `@prismicio/client` method {@link proto.getAllByTag}
309
351
  */
310
352
  export const useAllPrismicDocumentsByTag = <
@@ -323,11 +365,14 @@ export const useAllPrismicDocumentsByTag = <
323
365
  *
324
366
  * @remarks
325
367
  * An additional `@prismicio/client` instance can be provided at `params.client`.
368
+ *
326
369
  * @typeParam TDocument - Type of Prismic documents returned
370
+ *
327
371
  * @param tags - A list of tags that must be included on a document
328
372
  * @param params - Parameters to filter, sort, and paginate results
329
373
  *
330
374
  * @returns The composable payload {@link ClientHookReturnType}
375
+ *
331
376
  * @see Underlying `@prismicio/client` method {@link proto.getByTags}
332
377
  */
333
378
  export const usePrismicDocumentsBySomeTags = <
@@ -346,11 +391,14 @@ export const usePrismicDocumentsBySomeTags = <
346
391
  *
347
392
  * @remarks
348
393
  * An additional `@prismicio/client` instance can be provided at `params.client`.
394
+ *
349
395
  * @typeParam TDocument - Type of Prismic documents returned
396
+ *
350
397
  * @param tags - A list of tags that must be included on a document
351
398
  * @param params - Parameters to filter and sort results
352
399
  *
353
400
  * @returns The composable payload {@link ClientHookReturnType}
401
+ *
354
402
  * @see Underlying `@prismicio/client` method {@link proto.getAllByTags}
355
403
  */
356
404
  export const useAllPrismicDocumentsBySomeTags = <
@@ -369,11 +417,14 @@ export const useAllPrismicDocumentsBySomeTags = <
369
417
  *
370
418
  * @remarks
371
419
  * An additional `@prismicio/client` instance can be provided at `params.client`.
420
+ *
372
421
  * @typeParam TDocument - Type of Prismic documents returned
422
+ *
373
423
  * @param tags - A list of tags that must be included on a document
374
424
  * @param params - Parameters to filter, sort, and paginate results
375
425
  *
376
426
  * @returns The composable payload {@link ClientHookReturnType}
427
+ *
377
428
  * @see Underlying `@prismicio/client` method {@link proto.getByTags}
378
429
  */
379
430
  export const usePrismicDocumentsByEveryTag = <
@@ -392,11 +443,14 @@ export const usePrismicDocumentsByEveryTag = <
392
443
  *
393
444
  * @remarks
394
445
  * An additional `@prismicio/client` instance can be provided at `params.client`.
446
+ *
395
447
  * @typeParam TDocument - Type of Prismic documents returned
448
+ *
396
449
  * @param tags - A list of tags that must be included on a document
397
450
  * @param params - Parameters to filter and sort results
398
451
  *
399
452
  * @returns The composable payload {@link ClientHookReturnType}
453
+ *
400
454
  * @see Underlying `@prismicio/client` method {@link proto.getAllByTags}
401
455
  */
402
456
  export const useAllPrismicDocumentsByEveryTag = <
package/src/index.ts CHANGED
@@ -25,11 +25,15 @@ export type {
25
25
  SliceZoneComponents,
26
26
  SliceZoneLike,
27
27
  SliceZoneProps,
28
+ SliceZoneResolver,
28
29
  } from "./SliceZone";
29
30
 
30
31
  export { PrismicToolbar } from "./PrismicToolbar";
31
32
  export type { PrismicToolbarProps } from "./PrismicToolbar";
32
33
 
34
+ export { usePrismicPreviewResolver } from "./usePrismicPreviewResolver";
35
+ export type { UsePrismicPreviewResolverArgs } from "./usePrismicPreviewResolver";
36
+
33
37
  export {
34
38
  useAllPrismicDocumentsDangerously,
35
39
  useAllPrismicDocumentsByEveryTag,
@@ -49,7 +53,7 @@ export {
49
53
  usePrismicDocumentsByType,
50
54
  usePrismicDocumentsByUIDs,
51
55
  useSinglePrismicDocument,
52
- } from "./hooks";
56
+ } from "./clientHooks";
53
57
 
54
58
  export type {
55
59
  JSXMapSerializer,
@@ -0,0 +1,88 @@
1
+ import type * as prismic from "@prismicio/client";
2
+
3
+ import * as React from "react";
4
+
5
+ import {
6
+ ClientHookReturnType,
7
+ useStatefulPrismicClientMethod,
8
+ } from "./useStatefulPrismicClientMethod";
9
+
10
+ export type UsePrismicPreviewResolverArgs = {
11
+ /**
12
+ * An optional `@prismicio/client` instance to override the Client provided to
13
+ * `<PrismicProvider>`
14
+ */
15
+ client?: prismic.Client;
16
+
17
+ /**
18
+ * A function that maps a Prismic document to a URL within your app.
19
+ */
20
+ linkResolver?: Parameters<
21
+ prismic.Client["resolvePreviewURL"]
22
+ >[0]["linkResolver"];
23
+
24
+ /**
25
+ * A fallback URL if the Link Resolver does not return a value.
26
+ */
27
+ defaultURL?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["defaultURL"];
28
+
29
+ /**
30
+ * The preview token (also known as a ref) that will be used to query preview
31
+ * content from the Prismic repository.
32
+ */
33
+ previewToken?: Parameters<
34
+ prismic.Client["resolvePreviewURL"]
35
+ >[0]["previewToken"];
36
+
37
+ /**
38
+ * The previewed document that will be used to determine the destination URL.
39
+ */
40
+ documentID?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["documentID"];
41
+
42
+ /**
43
+ * A function to automatically navigate to the resolved URL. If a function is
44
+ * not provded, `usePreviewResolver` will not navigate to the URL.
45
+ *
46
+ * @param url - The resolved preview URL.
47
+ */
48
+ navigate?: (url: string) => unknown;
49
+ };
50
+
51
+ /**
52
+ * Resolve a preview session's URL. The resolved URL can be used to redirect to
53
+ * the previewed document.
54
+ *
55
+ * If a `navigate` function is provided, the hook will automatically navigate to
56
+ * the previewed document's URL.
57
+ *
58
+ * @param args - Arguments to configure how a URL is resolved.
59
+ *
60
+ * @returns A tuple containing the resolved URL and the hook's state.
61
+ */
62
+ export const usePrismicPreviewResolver = (
63
+ args: UsePrismicPreviewResolverArgs = {},
64
+ ): ClientHookReturnType<string> => {
65
+ const result = useStatefulPrismicClientMethod(
66
+ "resolvePreviewURL",
67
+ [
68
+ {
69
+ linkResolver: args.linkResolver,
70
+ defaultURL: args.defaultURL || "/",
71
+ previewToken: args.previewToken,
72
+ documentID: args.documentID,
73
+ },
74
+ ],
75
+ args.client,
76
+ );
77
+
78
+ const [resolvedURL] = result;
79
+ const { navigate } = args;
80
+
81
+ React.useEffect(() => {
82
+ if (resolvedURL && navigate) {
83
+ navigate(resolvedURL);
84
+ }
85
+ }, [resolvedURL, navigate]);
86
+
87
+ return result;
88
+ };
@@ -129,6 +129,7 @@ export type ClientHookReturnType<TData = unknown> = [
129
129
  * will be forwarded.
130
130
  *
131
131
  * @returns A new React hook configured for the provided method.
132
+ *
132
133
  * @internal
133
134
  */
134
135
  export const useStatefulPrismicClientMethod = <
@@ -138,16 +139,17 @@ export const useStatefulPrismicClientMethod = <
138
139
  >(
139
140
  methodName: TMethodName,
140
141
  args: TArgs,
142
+ explicitClient?: prismic.Client,
141
143
  ): ClientHookReturnType<TData> => {
142
144
  const lastArg = args[args.length - 1];
143
145
  const {
144
- client: explicitClient,
146
+ client: lastArgExplicitClient,
145
147
  skip,
146
148
  ...params
147
149
  } = isParams(lastArg) ? lastArg : ({} as HookOnlyParameters);
148
150
  const argsWithoutParams = isParams(lastArg) ? args.slice(0, -1) : args;
149
151
 
150
- const client = usePrismicClient(explicitClient);
152
+ const client = usePrismicClient(explicitClient || lastArgExplicitClient);
151
153
 
152
154
  const [state, dispatch] = React.useReducer<
153
155
  React.Reducer<StateMachineState<TData>, StateMachineAction<TData>>