@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.
- package/dist/index.cjs +63 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +190 -48
- package/dist/index.mjs +62 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +22 -17
- package/src/PrismicLink.tsx +51 -55
- package/src/PrismicProvider.tsx +8 -7
- package/src/PrismicRichText.tsx +13 -12
- package/src/PrismicText.tsx +2 -1
- package/src/SliceZone.tsx +68 -1
- package/src/{hooks.ts → clientHooks.ts} +78 -24
- package/src/index.ts +6 -2
- package/src/usePrismicClient.ts +1 -1
- package/src/usePrismicPreviewResolver.ts +88 -0
- package/src/useStatefulPrismicClientMethod.ts +45 -21
package/src/PrismicLink.tsx
CHANGED
|
@@ -49,69 +49,70 @@ export type PrismicLinkProps<
|
|
|
49
49
|
ExternalComponent extends string | React.ComponentType<LinkProps> =
|
|
50
50
|
| string
|
|
51
51
|
| React.ComponentType<LinkProps>,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
|
103
|
+
field: prismicT.LinkField | null | undefined;
|
|
103
104
|
}
|
|
104
105
|
| {
|
|
105
106
|
/**
|
|
106
107
|
* The Prismic document to link.
|
|
107
108
|
*/
|
|
108
|
-
document
|
|
109
|
+
document: prismicT.PrismicDocument | null | undefined;
|
|
109
110
|
}
|
|
110
111
|
| {
|
|
111
112
|
/**
|
|
112
113
|
* The URL to link.
|
|
113
114
|
*/
|
|
114
|
-
href
|
|
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
|
-
|
|
146
|
-
|
|
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,
|
package/src/PrismicProvider.tsx
CHANGED
|
@@ -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
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
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;
|
package/src/PrismicRichText.tsx
CHANGED
|
@@ -56,12 +56,12 @@ export type PrismicRichTextProps = {
|
|
|
56
56
|
*
|
|
57
57
|
* ```jsx
|
|
58
58
|
* (type, node, content, children) => {
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
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
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
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
|
*/
|
package/src/PrismicText.tsx
CHANGED
|
@@ -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
|
-
|
|
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 (
|