@prismicio/react 2.0.0-beta.2 → 2.0.0-beta.6

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.
@@ -49,69 +49,70 @@ export type PrismicLinkProps<
49
49
  ExternalComponent extends string | React.ComponentType<LinkProps> =
50
50
  | string
51
51
  | React.ComponentType<LinkProps>,
52
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
54
- > = ComponentProps<InternalComponent> &
55
- ComponentProps<ExternalComponent> & {
56
- /**
57
- * The Link Resolver used to resolve links.
58
- *
59
- * @remarks
60
- * If your app uses Route Resolvers when querying for your Prismic
61
- * repository's content, a Link Resolver does not need to be provided.
62
- * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
63
- */
64
- linkResolver?: LinkResolverFunction;
65
-
66
- /**
67
- * The component rendered for internal URLs. Defaults to `<a>`.
68
- *
69
- * If your app uses a client-side router that requires a special Link
70
- * component, provide the Link component to this prop.
71
- */
72
- internalComponent?: InternalComponent;
73
-
74
- /**
75
- * The component rendered for external URLs. Defaults to `<a>`.
76
- */
77
- externalComponent?: ExternalComponent;
78
-
79
- /**
80
- * The `target` attribute for anchor elements. If the Prismic field is
81
- * configured to open in a new window, this prop defaults to `_blank`.
82
- */
83
- target?: string | null;
84
-
85
- /**
86
- * The `rel` attribute for anchor elements. If the `target` prop is set to
87
- * `"_blank"`, this prop defaults to `"noopener noreferrer"`.
88
- */
89
- rel?: string | null;
90
-
91
- /**
92
- * Children for the component. *
93
- */
94
- children?: React.ReactNode;
95
- } & (
52
+ LinkResolverFunction extends prismicH.LinkResolverFunction = prismicH.LinkResolverFunction,
53
+ > = Omit<
54
+ ComponentProps<InternalComponent> & ComponentProps<ExternalComponent>,
55
+ keyof LinkProps
56
+ > & {
57
+ /**
58
+ * The Link Resolver used to resolve links.
59
+ *
60
+ * @remarks
61
+ * If your app uses Route Resolvers when querying for your Prismic
62
+ * repository's content, a Link Resolver does not need to be provided.
63
+ * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
64
+ */
65
+ linkResolver?: LinkResolverFunction;
66
+
67
+ /**
68
+ * The component rendered for internal URLs. Defaults to `<a>`.
69
+ *
70
+ * If your app uses a client-side router that requires a special Link
71
+ * component, provide the Link component to this prop.
72
+ */
73
+ internalComponent?: InternalComponent;
74
+
75
+ /**
76
+ * The component rendered for external URLs. Defaults to `<a>`.
77
+ */
78
+ externalComponent?: ExternalComponent;
79
+
80
+ /**
81
+ * The `target` attribute for anchor elements. If the Prismic field is
82
+ * configured to open in a new window, this prop defaults to `_blank`.
83
+ */
84
+ target?: string | null;
85
+
86
+ /**
87
+ * The `rel` attribute for anchor elements. If the `target` prop is set to
88
+ * `"_blank"`, this prop defaults to `"noopener noreferrer"`.
89
+ */
90
+ rel?: string | null;
91
+
92
+ /**
93
+ * Children for the component. *
94
+ */
95
+ children?: React.ReactNode;
96
+ } & (
96
97
  | {
97
98
  /**
98
99
  * The Prismic Link field containing the URL or document to link.
99
100
  *
100
101
  * @see Learn about Prismic Link fields {@link https://prismic.io/docs/core-concepts/link-content-relationship}
101
102
  */
102
- field?: prismicT.LinkField;
103
+ field: prismicT.LinkField | null | undefined;
103
104
  }
104
105
  | {
105
106
  /**
106
107
  * The Prismic document to link.
107
108
  */
108
- document?: prismicT.PrismicDocument;
109
+ document: prismicT.PrismicDocument | null | undefined;
109
110
  }
110
111
  | {
111
112
  /**
112
113
  * The URL to link.
113
114
  */
114
- href?: string;
115
+ href: string | null | undefined;
115
116
  }
116
117
  );
117
118
 
@@ -141,14 +142,9 @@ const defaultExternalComponent = "a";
141
142
  * link is internal or external.
142
143
  */
143
144
  export const PrismicLink = <
144
- InternalComponent extends
145
- | string
146
- | React.ComponentType<LinkProps> = typeof defaultInternalComponent,
147
- ExternalComponent extends
148
- | string
149
- | React.ComponentType<LinkProps> = typeof defaultExternalComponent,
150
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
151
- LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
145
+ InternalComponent extends string | React.ComponentType<LinkProps>,
146
+ ExternalComponent extends string | React.ComponentType<LinkProps>,
147
+ LinkResolverFunction extends prismicH.LinkResolverFunction,
152
148
  >(
153
149
  props: PrismicLinkProps<
154
150
  InternalComponent,
@@ -1,5 +1,6 @@
1
+ import type * as prismic from "@prismicio/client";
2
+
1
3
  import * as React from "react";
2
- import * as prismic from "@prismicio/client";
3
4
  import * as prismicH from "@prismicio/helpers";
4
5
 
5
6
  import { LinkProps } from "./PrismicLink";
@@ -45,12 +46,12 @@ export type PrismicContextValue = {
45
46
  *
46
47
  * ```jsx
47
48
  * (type, node, content, children) => {
48
- * switch (type) {
49
- * case 'heading1': {
50
- * return <Heading>{chidlren}</Heading>
51
- * }
52
- * }
53
- * }
49
+ * switch (type) {
50
+ * case "heading1": {
51
+ * return <Heading>{chidlren}</Heading>;
52
+ * }
53
+ * }
54
+ * };
54
55
  * ```
55
56
  */
56
57
  richTextComponents?: JSXMapSerializer | JSXFunctionSerializer;
@@ -56,12 +56,12 @@ export type PrismicRichTextProps = {
56
56
  *
57
57
  * ```jsx
58
58
  * (type, node, content, children) => {
59
- * switch (type) {
60
- * case 'heading1': {
61
- * return <Heading>{chidlren}</Heading>
62
- * }
63
- * }
64
- * }
59
+ * switch (type) {
60
+ * case "heading1": {
61
+ * return <Heading>{chidlren}</Heading>;
62
+ * }
63
+ * }
64
+ * };
65
65
  * ```
66
66
  */
67
67
  components?: JSXMapSerializer | JSXFunctionSerializer;
@@ -193,23 +193,24 @@ const createDefaultSerializer = (
193
193
  * @example Rendering a Rich Text field using the default HTMl elements.
194
194
  *
195
195
  * ```jsx
196
- * <PrismicRichText field={document.data.content} />
196
+ * <PrismicRichText field={document.data.content} />;
197
197
  * ```
198
198
  *
199
199
  * @example Rendering a Rich Text field using a custom set of React components.
200
200
  *
201
201
  * ```jsx
202
202
  * <PrismicRichText
203
- * field={document.data.content}
204
- * components={{
205
- * heading1: ({ children }) => <Heading>{children}</Heading>
206
- * }}
207
- * />
203
+ * field={document.data.content}
204
+ * components={{
205
+ * heading1: ({ children }) => <Heading>{children}</Heading>,
206
+ * }}
207
+ * />;
208
208
  * ```
209
209
  *
210
210
  * @param props - Props for the component.
211
211
  *
212
212
  * @returns The Rich Text field's content as React components.
213
+ *
213
214
  * @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}
214
215
  * @see Learn about Rich Text serializers {@link https://prismic.io/docs/core-concepts/html-serializer}
215
216
  */
@@ -26,12 +26,13 @@ export type PrismicTextProps = {
26
26
  * @example Rendering a Rich Text field as plain text.
27
27
  *
28
28
  * ```jsx
29
- * <PrismicText field={document.data.content} />
29
+ * <PrismicText field={document.data.content} />;
30
30
  * ```
31
31
  *
32
32
  * @param props - Props for the component.
33
33
  *
34
34
  * @returns The Rich Text field's content as plain text.
35
+ *
35
36
  * @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}
36
37
  */
37
38
  export const PrismicText = (props: PrismicTextProps): JSX.Element | null => {
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,19 +228,36 @@ 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>;
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
+
194
261
  const key = JSON.stringify(slice);
195
262
 
196
263
  return (