@prismicio/react 2.1.1 → 2.2.0

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/dist/index.d.ts CHANGED
@@ -5,9 +5,6 @@ import * as prismicT from '@prismicio/types';
5
5
  import * as prismicR from '@prismicio/richtext';
6
6
  export { Element } from '@prismicio/richtext';
7
7
 
8
- declare module "react" {
9
- function forwardRef<T, P = Record<string, never>>(render: (props: P, ref: React.Ref<T>) => JSX.Element | null): (props: P & React.RefAttributes<T>) => JSX.Element | null;
10
- }
11
8
  /**
12
9
  * Props provided to a component when rendered with `<PrismicLink>`.
13
10
  */
@@ -34,7 +31,7 @@ interface LinkProps {
34
31
  /**
35
32
  * Props for `<PrismicLink>`.
36
33
  */
37
- declare type PrismicLinkProps<InternalComponent extends React.ElementType<LinkProps> = React.ElementType<LinkProps>, ExternalComponent extends React.ElementType<LinkProps> = React.ElementType<LinkProps>, LinkResolverFunction extends prismicH.LinkResolverFunction = prismicH.LinkResolverFunction> = Omit<React.ComponentProps<InternalComponent> & React.ComponentProps<ExternalComponent>, keyof LinkProps> & {
34
+ declare type PrismicLinkProps<InternalComponent extends React.ElementType<LinkProps> = React.ElementType<LinkProps>, ExternalComponent extends React.ElementType<LinkProps> = React.ElementType<LinkProps>, LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction> = Omit<React.ComponentPropsWithoutRef<InternalComponent> & React.ComponentPropsWithoutRef<ExternalComponent>, keyof LinkProps> & {
38
35
  /**
39
36
  * The Link Resolver used to resolve links.
40
37
  *
@@ -87,7 +84,9 @@ declare type PrismicLinkProps<InternalComponent extends React.ElementType<LinkPr
87
84
  */
88
85
  href: string | null | undefined;
89
86
  });
90
- declare const PrismicLink: <InternalComponent extends React.ElementType<LinkProps> = "a", ExternalComponent extends React.ElementType<LinkProps> = "a", LinkResolverFunction extends prismicH.LinkResolverFunction<string> = prismicH.LinkResolverFunction<string>, Ref extends (InternalComponent extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[InternalComponent] : any) | (ExternalComponent extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[ExternalComponent] : any) = (InternalComponent extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[InternalComponent] : any) | (ExternalComponent extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[ExternalComponent] : any)>(props: PrismicLinkProps<InternalComponent, ExternalComponent, LinkResolverFunction> & React.RefAttributes<Ref>) => JSX.Element | null;
87
+ declare const PrismicLink: <InternalComponent extends React.ElementType<LinkProps>, ExternalComponent extends React.ElementType<LinkProps>, LinkResolverFunction extends prismicH.LinkResolverFunction<any>>(props: PrismicLinkProps<InternalComponent, ExternalComponent, LinkResolverFunction> & {
88
+ ref?: React.Ref<Element> | undefined;
89
+ }) => JSX.Element | null;
91
90
 
92
91
  /**
93
92
  * A function mapping Rich Text block types to React Components. It is used to
@@ -112,7 +111,7 @@ declare type PrismicClientHookState = "idle" | "loading" | "loaded" | "failed";
112
111
  * React context value containing shared configuration for `@prismicio/react`
113
112
  * components and hooks.
114
113
  */
115
- declare type PrismicContextValue = {
114
+ declare type PrismicContextValue<LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction> = {
116
115
  /**
117
116
  * A `@prismicio/client` instance used to fetch content from a Prismic
118
117
  * repository. This is used by `@prismicio/react` hooks, such as
@@ -127,7 +126,7 @@ declare type PrismicContextValue = {
127
126
  * repository's content, a Link Resolver does not need to be provided.
128
127
  * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
129
128
  */
130
- linkResolver?: prismicH.LinkResolverFunction;
129
+ linkResolver?: LinkResolverFunction;
131
130
  /**
132
131
  * A map or function that maps a Rich Text block to a React component.
133
132
  *
@@ -171,14 +170,14 @@ declare type PrismicContextValue = {
171
170
  /**
172
171
  * Props for `<PrismicProvider>`.
173
172
  */
174
- declare type PrismicProviderProps = PrismicContextValue;
173
+ declare type PrismicProviderProps<LinkResolverFunction extends prismicH.LinkResolverFunction> = PrismicContextValue<LinkResolverFunction>;
175
174
  /**
176
175
  * React context provider to share configuration for `@prismicio/react`
177
176
  * components and hooks.
178
177
  *
179
178
  * @returns A React context provider with shared configuration.
180
179
  */
181
- declare const PrismicProvider: ({ client, linkResolver, richTextComponents, internalLinkComponent, externalLinkComponent, children, }: PrismicProviderProps) => JSX.Element;
180
+ declare const PrismicProvider: <LinkResolverFunction extends prismicH.LinkResolverFunction<any>>({ client, linkResolver, richTextComponents, internalLinkComponent, externalLinkComponent, children, }: PrismicProviderProps<LinkResolverFunction>) => JSX.Element;
182
181
 
183
182
  /**
184
183
  * React hook used to read shared configuration for `@prismicio/react`
@@ -240,7 +239,7 @@ declare const PrismicText: (props: PrismicTextProps) => JSX.Element | null;
240
239
  /**
241
240
  * Props for `<PrismicRichText>`.
242
241
  */
243
- declare type PrismicRichTextProps = {
242
+ declare type PrismicRichTextProps<LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction> = {
244
243
  /**
245
244
  * The Prismic Rich Text field to render.
246
245
  */
@@ -253,7 +252,7 @@ declare type PrismicRichTextProps = {
253
252
  * repository's content, a Link Resolver does not need to be provided.
254
253
  * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
255
254
  */
256
- linkResolver?: PrismicLinkProps["linkResolver"];
255
+ linkResolver?: LinkResolverFunction;
257
256
  /**
258
257
  * A function that maps a Rich Text block to a React component.
259
258
  *
@@ -282,7 +281,7 @@ declare type PrismicRichTextProps = {
282
281
  * (type, node, content, children) => {
283
282
  * switch (type) {
284
283
  * case "heading1": {
285
- * return <Heading>{chidlren}</Heading>;
284
+ * return <Heading>{children}</Heading>;
286
285
  * }
287
286
  * }
288
287
  * };
@@ -301,6 +300,11 @@ declare type PrismicRichTextProps = {
301
300
  * @defaultValue `<a>`
302
301
  */
303
302
  externalLinkComponent?: PrismicLinkProps["externalComponent"];
303
+ /**
304
+ * The value to be rendered when the field is empty. If a fallback is not
305
+ * given, `null` will be rendered.
306
+ */
307
+ fallback?: React.ReactNode;
304
308
  };
305
309
  /**
306
310
  * React component that renders content from a Prismic Rich Text field. By
@@ -342,7 +346,7 @@ declare type PrismicRichTextProps = {
342
346
  * @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}
343
347
  * @see Learn about Rich Text serializers {@link https://prismic.io/docs/core-concepts/html-serializer}
344
348
  */
345
- declare const PrismicRichText: (props: PrismicRichTextProps) => JSX.Element | null;
349
+ declare const PrismicRichText: <LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction<string>>(props: PrismicRichTextProps<LinkResolverFunction>) => JSX.Element | null;
346
350
 
347
351
  declare type WordSeparators = "-" | "_" | " ";
348
352
  declare type Split<S extends string, Delimiter extends string> = S extends `${infer Head}${Delimiter}${infer Tail}` ? [Head, ...Split<Tail, Delimiter>] : S extends Delimiter ? [] : [S];
@@ -362,20 +366,47 @@ declare type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K exte
362
366
  */
363
367
  declare type PascalCase<Value> = CamelCase<Value> extends string ? Capitalize<CamelCase<Value>> : CamelCase<Value>;
364
368
 
369
+ /**
370
+ * Returns the type of a `SliceLike` type.
371
+ *
372
+ * @typeParam Slice - The Slice from which the type will be extracted.
373
+ */
374
+ declare type ExtractSliceType<Slice extends SliceLike> = Slice extends SliceLikeRestV2 ? Slice["slice_type"] : Slice extends SliceLikeGraphQL ? Slice["type"] : never;
375
+ /**
376
+ * The minimum required properties to represent a Prismic Slice from the Prismic
377
+ * Rest API V2 for the `<SliceZone>` component.
378
+ *
379
+ * If using Prismic's Rest API V2, use the `Slice` export from
380
+ * `@prismicio/types` for a full interface.
381
+ *
382
+ * @typeParam SliceType - Type name of the Slice.
383
+ */
384
+ declare type SliceLikeRestV2<SliceType extends string = string> = {
385
+ slice_type: prismicT.Slice<SliceType>["slice_type"];
386
+ };
387
+ /**
388
+ * The minimum required properties to represent a Prismic Slice from the Prismic
389
+ * GraphQL API for the `<SliceZone>` component.
390
+ *
391
+ * @typeParam SliceType - Type name of the Slice.
392
+ */
393
+ declare type SliceLikeGraphQL<SliceType extends string = string> = {
394
+ type: prismicT.Slice<SliceType>["slice_type"];
395
+ };
365
396
  /**
366
397
  * The minimum required properties to represent a Prismic Slice for the
367
398
  * `<SliceZone>` component.
368
399
  *
369
- * If using Prismic's REST API, use the `Slice` export from `@prismicio/types`
370
- * for a full interface.
400
+ * If using Prismic's Rest API V2, use the `Slice` export from
401
+ * `@prismicio/types` for a full interface.
371
402
  *
372
403
  * @typeParam SliceType - Type name of the Slice.
373
404
  */
374
- declare type SliceLike<SliceType extends string = string> = Pick<prismicT.Slice<SliceType>, "slice_type">;
405
+ declare type SliceLike<SliceType extends string = string> = SliceLikeRestV2<SliceType> | SliceLikeGraphQL<SliceType>;
375
406
  /**
376
407
  * A looser version of the `SliceZone` type from `@prismicio/types` using `SliceLike`.
377
408
  *
378
- * If using Prismic's REST API, use the `SliceZone` export from
409
+ * If using Prismic's Rest API V2, use the `SliceZone` export from
379
410
  * `@prismicio/types` for the full type.
380
411
  *
381
412
  * @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
@@ -422,7 +453,7 @@ declare type SliceComponentType<TSlice extends SliceLike = SliceLike, TContext =
422
453
  * @typeParam TContext - Arbitrary data made available to all Slice components.
423
454
  */
424
455
  declare type SliceZoneComponents<TSlice extends SliceLike = SliceLike, TContext = unknown> = {
425
- [SliceType in keyof Record<TSlice["slice_type"], never>]: SliceComponentType<Extract<TSlice, SliceLike<SliceType>> extends never ? SliceLike : Extract<TSlice, SliceLike<SliceType>>, TContext>;
456
+ [SliceType in ExtractSliceType<TSlice>]: SliceComponentType<Extract<TSlice, SliceLike<SliceType>> extends never ? SliceLike : Extract<TSlice, SliceLike<SliceType>>, TContext>;
426
457
  };
427
458
  /**
428
459
  * This Slice component can be used as a reminder to provide a proper implementation.
@@ -442,7 +473,7 @@ declare type SliceZoneResolverArgs<TSlice extends SliceLike = SliceLike> = {
442
473
  /**
443
474
  * The name of the Slice.
444
475
  */
445
- sliceName: PascalCase<TSlice["slice_type"]>;
476
+ sliceName: PascalCase<ExtractSliceType<TSlice>>;
446
477
  /**
447
478
  * The index of the Slice in the Slice Zone.
448
479
  */
package/dist/index.js CHANGED
@@ -237,7 +237,7 @@ const PrismicRichText = (props) => {
237
237
  });
238
238
  return /* @__PURE__ */ React.createElement(React.Fragment, null, serialized);
239
239
  } else {
240
- return null;
240
+ return props.fallback != null ? /* @__PURE__ */ React.createElement(React.Fragment, null, props.fallback) : null;
241
241
  }
242
242
  }, [
243
243
  props.field,
@@ -245,6 +245,7 @@ const PrismicRichText = (props) => {
245
245
  props.externalLinkComponent,
246
246
  props.components,
247
247
  props.linkResolver,
248
+ props.fallback,
248
249
  context.linkResolver,
249
250
  context.richTextComponents
250
251
  ]);
@@ -260,13 +261,14 @@ const pascalCase = (input) => {
260
261
  const TODOSliceComponent = __PRODUCTION__ ? () => null : ({
261
262
  slice
262
263
  }) => {
264
+ const type = "slice_type" in slice ? slice.slice_type : slice.type;
263
265
  React.useEffect(() => {
264
- console.warn(`[SliceZone] Could not find a component for Slice type "${slice.slice_type}"`, slice);
265
- }, [slice]);
266
+ console.warn(`[SliceZone] Could not find a component for Slice type "${type}"`, slice);
267
+ }, [slice, type]);
266
268
  return /* @__PURE__ */ React.createElement("section", {
267
269
  "data-slice-zone-todo-component": "",
268
- "data-slice-type": slice.slice_type
269
- }, "Could not find a component for Slice type \u201C", slice.slice_type, "\u201D");
270
+ "data-slice-type": type
271
+ }, "Could not find a component for Slice type \u201C", type, "\u201D");
270
272
  };
271
273
  const SliceZone = ({
272
274
  slices = [],
@@ -277,11 +279,12 @@ const SliceZone = ({
277
279
  }) => {
278
280
  const renderedSlices = React.useMemo(() => {
279
281
  return slices.map((slice, index) => {
280
- let Comp = components[slice.slice_type] || defaultComponent;
282
+ const type = "slice_type" in slice ? slice.slice_type : slice.type;
283
+ let Comp = components[type] || defaultComponent;
281
284
  if (resolver) {
282
285
  const resolvedComp = resolver({
283
286
  slice,
284
- sliceName: pascalCase(slice.slice_type),
287
+ sliceName: pascalCase(type),
285
288
  i: index
286
289
  });
287
290
  if (resolvedComp) {