@prismicio/react 2.5.2 → 2.6.0-alpha.1
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/PrismicImage.cjs +7 -11
- package/dist/PrismicImage.cjs.map +1 -1
- package/dist/PrismicImage.d.ts +14 -15
- package/dist/PrismicImage.js +6 -10
- package/dist/PrismicImage.js.map +1 -1
- package/dist/PrismicLink.cjs +4 -66
- package/dist/PrismicLink.cjs.map +1 -1
- package/dist/PrismicLink.d.ts +5 -104
- package/dist/PrismicLink.js +4 -65
- package/dist/PrismicLink.js.map +1 -1
- package/dist/PrismicProvider.cjs.map +1 -1
- package/dist/PrismicProvider.d.ts +8 -9
- package/dist/PrismicProvider.js.map +1 -1
- package/dist/PrismicRichText.cjs +4 -71
- package/dist/PrismicRichText.cjs.map +1 -1
- package/dist/PrismicRichText.d.ts +4 -116
- package/dist/PrismicRichText.js +5 -70
- package/dist/PrismicRichText.js.map +1 -1
- package/dist/PrismicText.cjs +4 -4
- package/dist/PrismicText.cjs.map +1 -1
- package/dist/PrismicText.d.ts +2 -2
- package/dist/PrismicText.js +3 -3
- package/dist/PrismicText.js.map +1 -1
- package/dist/PrismicToolbar.cjs +6 -4
- package/dist/PrismicToolbar.cjs.map +1 -1
- package/dist/PrismicToolbar.d.ts +3 -1
- package/dist/PrismicToolbar.js +6 -4
- package/dist/PrismicToolbar.js.map +1 -1
- package/dist/SliceZone.cjs +4 -17
- package/dist/SliceZone.cjs.map +1 -1
- package/dist/SliceZone.d.ts +4 -65
- package/dist/SliceZone.js +4 -17
- package/dist/SliceZone.js.map +1 -1
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -27
- package/dist/index.js +1 -4
- package/dist/index.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/react-server/PrismicLink.cjs +59 -0
- package/dist/react-server/PrismicLink.cjs.map +1 -0
- package/dist/react-server/PrismicLink.d.ts +72 -0
- package/dist/react-server/PrismicLink.js +42 -0
- package/dist/react-server/PrismicLink.js.map +1 -0
- package/dist/react-server/PrismicRichText.cjs +92 -0
- package/dist/react-server/PrismicRichText.cjs.map +1 -0
- package/dist/react-server/PrismicRichText.d.ts +104 -0
- package/dist/react-server/PrismicRichText.js +73 -0
- package/dist/react-server/PrismicRichText.js.map +1 -0
- package/dist/react-server/index.d.ts +4 -0
- package/dist/react-server/unsupported.cjs +97 -0
- package/dist/react-server/unsupported.cjs.map +1 -0
- package/dist/react-server/unsupported.d.ts +22 -0
- package/dist/react-server/unsupported.js +97 -0
- package/dist/react-server/unsupported.js.map +1 -0
- package/dist/react-server.cjs +44 -0
- package/dist/react-server.cjs.map +1 -0
- package/dist/react-server.js +41 -0
- package/dist/react-server.js.map +1 -0
- package/dist/useStatefulPrismicClientMethod.cjs +6 -0
- package/dist/useStatefulPrismicClientMethod.cjs.map +1 -1
- package/dist/useStatefulPrismicClientMethod.d.ts +1 -1
- package/dist/useStatefulPrismicClientMethod.js +6 -0
- package/dist/useStatefulPrismicClientMethod.js.map +1 -1
- package/package.json +42 -47
- package/src/PrismicImage.tsx +30 -37
- package/src/PrismicLink.tsx +29 -258
- package/src/PrismicProvider.tsx +9 -8
- package/src/PrismicRichText.tsx +28 -275
- package/src/PrismicText.tsx +4 -5
- package/src/PrismicToolbar.tsx +7 -6
- package/src/SliceZone.tsx +8 -129
- package/src/index.ts +1 -9
- package/src/react-server/PrismicLink.tsx +179 -0
- package/src/react-server/PrismicRichText.tsx +267 -0
- package/src/react-server/index.ts +38 -0
- package/src/react-server/unsupported.ts +98 -0
- package/dist/lib/pascalCase.cjs +0 -10
- package/dist/lib/pascalCase.cjs.map +0 -1
- package/dist/lib/pascalCase.js +0 -10
- package/dist/lib/pascalCase.js.map +0 -1
package/src/PrismicLink.tsx
CHANGED
|
@@ -1,270 +1,41 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
1
3
|
import * as React from "react";
|
|
2
|
-
import * as prismicH from "@prismicio/helpers";
|
|
3
|
-
import * as prismicT from "@prismicio/types";
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import {
|
|
6
|
+
PrismicLink as ServerPrismicLink,
|
|
7
|
+
LinkProps,
|
|
8
|
+
PrismicLinkProps,
|
|
9
|
+
defaultComponent,
|
|
10
|
+
} from "./react-server/PrismicLink";
|
|
8
11
|
|
|
9
12
|
import { usePrismicContext } from "./usePrismicContext";
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
* Props provided to a component when rendered with `<PrismicLink>`.
|
|
13
|
-
*/
|
|
14
|
-
export interface LinkProps {
|
|
15
|
-
/**
|
|
16
|
-
* The URL to link.
|
|
17
|
-
*/
|
|
18
|
-
href: string;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The `target` attribute for anchor elements. If the Prismic field is
|
|
22
|
-
* configured to open in a new window, this prop defaults to `_blank`.
|
|
23
|
-
*/
|
|
24
|
-
target?: string;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The `rel` attribute for anchor elements. If the `target` prop is set to
|
|
28
|
-
* `"_blank"`, this prop defaults to `"noopener noreferrer"`.
|
|
29
|
-
*/
|
|
30
|
-
rel?: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Children for the component. *
|
|
34
|
-
*/
|
|
35
|
-
children?: React.ReactNode;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Props for `<PrismicLink>`.
|
|
40
|
-
*/
|
|
41
|
-
export type PrismicLinkProps<
|
|
42
|
-
InternalComponent extends React.ElementType<LinkProps> = typeof defaultInternalComponent,
|
|
43
|
-
ExternalComponent extends React.ElementType<LinkProps> = typeof defaultInternalComponent,
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
-
LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
|
|
46
|
-
> = Omit<
|
|
47
|
-
React.ComponentPropsWithoutRef<InternalComponent> &
|
|
48
|
-
React.ComponentPropsWithoutRef<ExternalComponent>,
|
|
49
|
-
keyof LinkProps
|
|
50
|
-
> & {
|
|
51
|
-
/**
|
|
52
|
-
* The Link Resolver used to resolve links.
|
|
53
|
-
*
|
|
54
|
-
* @remarks
|
|
55
|
-
* If your app uses Route Resolvers when querying for your Prismic
|
|
56
|
-
* repository's content, a Link Resolver does not need to be provided.
|
|
57
|
-
* @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
58
|
-
*/
|
|
59
|
-
linkResolver?: LinkResolverFunction;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The component rendered for internal URLs. Defaults to `<a>`.
|
|
63
|
-
*
|
|
64
|
-
* If your app uses a client-side router that requires a special Link
|
|
65
|
-
* component, provide the Link component to this prop.
|
|
66
|
-
*/
|
|
67
|
-
internalComponent?: InternalComponent;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* The component rendered for external URLs. Defaults to `<a>`.
|
|
71
|
-
*/
|
|
72
|
-
externalComponent?: ExternalComponent;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The `target` attribute for anchor elements. If the Prismic field is
|
|
76
|
-
* configured to open in a new window, this prop defaults to `_blank`.
|
|
77
|
-
*/
|
|
78
|
-
target?: string | null;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The `rel` attribute for anchor elements. If the `target` prop is set to
|
|
82
|
-
* `"_blank"`, this prop defaults to `"noopener noreferrer"`.
|
|
83
|
-
*/
|
|
84
|
-
rel?: string | null;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Children for the component. *
|
|
88
|
-
*/
|
|
89
|
-
children?: React.ReactNode;
|
|
90
|
-
} & (
|
|
91
|
-
| {
|
|
92
|
-
/**
|
|
93
|
-
* The Prismic Link field containing the URL or document to link.
|
|
94
|
-
*
|
|
95
|
-
* @see Learn about Prismic Link fields {@link https://prismic.io/docs/core-concepts/link-content-relationship}
|
|
96
|
-
*/
|
|
97
|
-
field: prismicT.LinkField | null | undefined;
|
|
98
|
-
}
|
|
99
|
-
| {
|
|
100
|
-
/**
|
|
101
|
-
* The Prismic document to link.
|
|
102
|
-
*/
|
|
103
|
-
document: prismicT.PrismicDocument | null | undefined;
|
|
104
|
-
}
|
|
105
|
-
| {
|
|
106
|
-
/**
|
|
107
|
-
* The URL to link.
|
|
108
|
-
*/
|
|
109
|
-
href: string | null | undefined;
|
|
110
|
-
}
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* The default component rendered for internal URLs.
|
|
115
|
-
*/
|
|
116
|
-
const defaultInternalComponent = "a";
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The default component rendered for external URLs.
|
|
120
|
-
*/
|
|
121
|
-
const defaultExternalComponent = "a";
|
|
14
|
+
export { LinkProps, PrismicLinkProps };
|
|
122
15
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
127
|
-
LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
|
|
16
|
+
export const PrismicLink = React.forwardRef(function PrismicLink<
|
|
17
|
+
InternalComponentProps = React.ComponentProps<typeof defaultComponent>,
|
|
18
|
+
ExternalComponentProps = React.ComponentProps<typeof defaultComponent>,
|
|
128
19
|
>(
|
|
129
|
-
props: PrismicLinkProps<
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
LinkResolverFunction
|
|
133
|
-
>,
|
|
134
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
135
|
-
ref: React.Ref<any>,
|
|
136
|
-
): JSX.Element | null => {
|
|
20
|
+
props: PrismicLinkProps<InternalComponentProps, ExternalComponentProps>,
|
|
21
|
+
ref: React.ForwardedRef<Element>,
|
|
22
|
+
) {
|
|
137
23
|
const context = usePrismicContext();
|
|
138
24
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (!props.field.link_type) {
|
|
142
|
-
console.error(
|
|
143
|
-
`[PrismicLink] This "field" prop value caused an error to be thrown.\n`,
|
|
144
|
-
props.field,
|
|
145
|
-
);
|
|
146
|
-
throw new Error(
|
|
147
|
-
`[PrismicLink] The provided field is missing required properties to properly render a link. The link will not render. For more details, see ${devMsg(
|
|
148
|
-
"missing-link-properties",
|
|
149
|
-
)}`,
|
|
150
|
-
);
|
|
151
|
-
} else if (
|
|
152
|
-
Object.keys(props.field).length > 1 &&
|
|
153
|
-
!("url" in props.field || "uid" in props.field || "id" in props.field)
|
|
154
|
-
) {
|
|
155
|
-
console.warn(
|
|
156
|
-
`[PrismicLink] The provided field is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(
|
|
157
|
-
"missing-link-properties",
|
|
158
|
-
)}`,
|
|
159
|
-
props.field,
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
} else if ("document" in props && props.document) {
|
|
163
|
-
if (!("url" in props.document || "id" in props.document)) {
|
|
164
|
-
console.warn(
|
|
165
|
-
`[PrismicLink] The provided document is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(
|
|
166
|
-
"missing-link-properties",
|
|
167
|
-
)}`,
|
|
168
|
-
props.document,
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const linkResolver = props.linkResolver || context.linkResolver;
|
|
175
|
-
|
|
176
|
-
let href: string | null | undefined;
|
|
177
|
-
if ("href" in props) {
|
|
178
|
-
href = props.href;
|
|
179
|
-
} else if ("document" in props && props.document) {
|
|
180
|
-
href = prismicH.asLink(props.document, linkResolver);
|
|
181
|
-
} else if ("field" in props && props.field) {
|
|
182
|
-
href = prismicH.asLink(props.field, linkResolver);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const isInternal = href && isInternalURL(href);
|
|
186
|
-
|
|
187
|
-
const target =
|
|
188
|
-
props.target ||
|
|
189
|
-
("field" in props &&
|
|
190
|
-
props.field &&
|
|
191
|
-
"target" in props.field &&
|
|
192
|
-
props.field.target) ||
|
|
193
|
-
undefined;
|
|
194
|
-
|
|
195
|
-
const rel =
|
|
196
|
-
props.rel || (target === "_blank" ? "noopener noreferrer" : undefined);
|
|
197
|
-
|
|
198
|
-
const InternalComponent: React.ElementType<LinkProps> =
|
|
199
|
-
props.internalComponent ||
|
|
200
|
-
context.internalLinkComponent ||
|
|
201
|
-
defaultInternalComponent;
|
|
202
|
-
|
|
203
|
-
const ExternalComponent: React.ElementType<LinkProps> =
|
|
204
|
-
props.externalComponent ||
|
|
205
|
-
context.externalLinkComponent ||
|
|
206
|
-
defaultExternalComponent;
|
|
207
|
-
|
|
208
|
-
const Component = isInternal ? InternalComponent : ExternalComponent;
|
|
209
|
-
|
|
210
|
-
const passthroughProps: typeof props = Object.assign({}, props);
|
|
211
|
-
delete passthroughProps.linkResolver;
|
|
212
|
-
delete passthroughProps.internalComponent;
|
|
213
|
-
delete passthroughProps.externalComponent;
|
|
214
|
-
delete passthroughProps.rel;
|
|
215
|
-
delete passthroughProps.target;
|
|
216
|
-
if ("field" in passthroughProps) {
|
|
217
|
-
delete passthroughProps.field;
|
|
218
|
-
} else if ("document" in passthroughProps) {
|
|
219
|
-
delete passthroughProps.document;
|
|
220
|
-
} else if ("href" in passthroughProps) {
|
|
221
|
-
delete passthroughProps.href;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return href ? (
|
|
225
|
-
<Component
|
|
226
|
-
// @ts-expect-error - Expression produces a union type
|
|
227
|
-
// that is too complex to represent. This most likely
|
|
228
|
-
// happens due to the polymorphic nature of this
|
|
229
|
-
// component, passing of "extra" props, and ref
|
|
230
|
-
// forwarding support.
|
|
231
|
-
{...passthroughProps}
|
|
25
|
+
return (
|
|
26
|
+
<ServerPrismicLink
|
|
232
27
|
ref={ref}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
28
|
+
linkResolver={context.linkResolver}
|
|
29
|
+
internalComponent={context.internalLinkComponent}
|
|
30
|
+
externalComponent={context.externalLinkComponent}
|
|
31
|
+
{...props}
|
|
236
32
|
/>
|
|
237
|
-
)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
_PrismicLink.displayName = "PrismicLink";
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* React component that renders a link from a Prismic Link field.
|
|
246
|
-
*
|
|
247
|
-
* Different components can be rendered depending on whether the link is
|
|
248
|
-
* internal or external. This is helpful when integrating with client-side
|
|
249
|
-
* routers, such as a router-specific Link component.
|
|
250
|
-
*
|
|
251
|
-
* If a link is configured to open in a new window using `target="_blank"`,
|
|
252
|
-
* `rel="noopener noreferrer"` is set by default.
|
|
253
|
-
*
|
|
254
|
-
* @param props - Props for the component.
|
|
255
|
-
*
|
|
256
|
-
* @returns The internal or external link component depending on whether the
|
|
257
|
-
* link is internal or external.
|
|
258
|
-
*/
|
|
259
|
-
export const PrismicLink = React.forwardRef(_PrismicLink) as <
|
|
260
|
-
InternalComponent extends React.ElementType<LinkProps> = typeof defaultInternalComponent,
|
|
261
|
-
ExternalComponent extends React.ElementType<LinkProps> = typeof defaultExternalComponent,
|
|
262
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
263
|
-
LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
|
|
33
|
+
);
|
|
34
|
+
}) as <
|
|
35
|
+
InternalComponentProps = React.ComponentProps<typeof defaultComponent>,
|
|
36
|
+
ExternalComponentProps = React.ComponentProps<typeof defaultComponent>,
|
|
264
37
|
>(
|
|
265
|
-
props: PrismicLinkProps<
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
> & { ref?: React.Ref<Element> },
|
|
270
|
-
) => JSX.Element | null;
|
|
38
|
+
props: PrismicLinkProps<InternalComponentProps, ExternalComponentProps> & {
|
|
39
|
+
ref?: React.ForwardedRef<Element>;
|
|
40
|
+
},
|
|
41
|
+
) => JSX.Element;
|
package/src/PrismicProvider.tsx
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
"use client";
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
|
-
import * as
|
|
4
|
+
import * as prismic from "@prismicio/client";
|
|
5
5
|
|
|
6
|
-
import { LinkProps } from "./PrismicLink";
|
|
7
6
|
import { JSXFunctionSerializer, JSXMapSerializer } from "./types";
|
|
7
|
+
import { LinkProps } from "./PrismicLink";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* React context value containing shared configuration for `@prismicio/react`
|
|
@@ -12,7 +12,7 @@ import { JSXFunctionSerializer, JSXMapSerializer } from "./types";
|
|
|
12
12
|
*/
|
|
13
13
|
export type PrismicContextValue<
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
-
LinkResolverFunction extends
|
|
15
|
+
LinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,
|
|
16
16
|
> = {
|
|
17
17
|
/**
|
|
18
18
|
* A `@prismicio/client` instance used to fetch content from a Prismic
|
|
@@ -64,13 +64,13 @@ export type PrismicContextValue<
|
|
|
64
64
|
* The component rendered by `<PrismicLink>` for internal URLs. Defaults to
|
|
65
65
|
* `<a>`.
|
|
66
66
|
*/
|
|
67
|
-
internalLinkComponent?: React.
|
|
67
|
+
internalLinkComponent?: React.ComponentType<LinkProps>;
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* The component rendered by `<PrismicLink>` for external URLs. Defaults to
|
|
71
71
|
* `<a>`.
|
|
72
72
|
*/
|
|
73
|
-
externalLinkComponent?: React.
|
|
73
|
+
externalLinkComponent?: React.ComponentType<LinkProps>;
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* Children for the component.
|
|
@@ -88,7 +88,8 @@ export const PrismicContext = React.createContext<PrismicContextValue>({});
|
|
|
88
88
|
* Props for `<PrismicProvider>`.
|
|
89
89
|
*/
|
|
90
90
|
export type PrismicProviderProps<
|
|
91
|
-
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
+
LinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction<any>,
|
|
92
93
|
> = PrismicContextValue<LinkResolverFunction>;
|
|
93
94
|
|
|
94
95
|
/**
|
|
@@ -99,7 +100,7 @@ export type PrismicProviderProps<
|
|
|
99
100
|
*/
|
|
100
101
|
export const PrismicProvider = <
|
|
101
102
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
|
-
LinkResolverFunction extends
|
|
103
|
+
LinkResolverFunction extends prismic.LinkResolverFunction<any>,
|
|
103
104
|
>({
|
|
104
105
|
client,
|
|
105
106
|
linkResolver,
|
package/src/PrismicRichText.tsx
CHANGED
|
@@ -1,285 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
/* eslint-disable react/prop-types */
|
|
1
|
+
"use client";
|
|
3
2
|
|
|
4
|
-
import * as
|
|
5
|
-
import * as prismicT from "@prismicio/types";
|
|
6
|
-
import * as prismicH from "@prismicio/helpers";
|
|
3
|
+
import * as prismic from "@prismicio/client";
|
|
7
4
|
import * as prismicR from "@prismicio/richtext";
|
|
8
5
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Props for `<PrismicRichText>`.
|
|
15
|
-
*/
|
|
16
|
-
export type PrismicRichTextProps<
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
|
|
19
|
-
> = {
|
|
20
|
-
/**
|
|
21
|
-
* The Prismic Rich Text field to render.
|
|
22
|
-
*/
|
|
23
|
-
field: prismicT.RichTextField | null | undefined;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The Link Resolver used to resolve links.
|
|
27
|
-
*
|
|
28
|
-
* @remarks
|
|
29
|
-
* If your app uses Route Resolvers when querying for your Prismic
|
|
30
|
-
* repository's content, a Link Resolver does not need to be provided.
|
|
31
|
-
* @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
32
|
-
*/
|
|
33
|
-
linkResolver?: LinkResolverFunction;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* A function that maps a Rich Text block to a React component.
|
|
37
|
-
*
|
|
38
|
-
* @deprecated Use the `components` prop instead. Prefer using a map
|
|
39
|
-
* serializer when possible.
|
|
40
|
-
* @see Learn about Rich Text serializers {@link https://prismic.io/docs/core-concepts/html-serializer}
|
|
41
|
-
*/
|
|
42
|
-
htmlSerializer?: JSXFunctionSerializer;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* A map or function that maps a Rich Text block to a React component.
|
|
46
|
-
*
|
|
47
|
-
* @remarks
|
|
48
|
-
* Prefer using a map serializer over the function serializer when possible.
|
|
49
|
-
* The map serializer is simpler to maintain.
|
|
50
|
-
* @example A map serializer.
|
|
51
|
-
*
|
|
52
|
-
* ```jsx
|
|
53
|
-
* {
|
|
54
|
-
* heading1: ({children}) => <Heading>{children}</Heading>
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @example A function serializer.
|
|
59
|
-
*
|
|
60
|
-
* ```jsx
|
|
61
|
-
* (type, node, content, children) => {
|
|
62
|
-
* switch (type) {
|
|
63
|
-
* case "heading1": {
|
|
64
|
-
* return <Heading>{children}</Heading>;
|
|
65
|
-
* }
|
|
66
|
-
* }
|
|
67
|
-
* };
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
components?: JSXMapSerializer | JSXFunctionSerializer;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The React component rendered for links when the URL is internal.
|
|
74
|
-
*
|
|
75
|
-
* @defaultValue `<a>`
|
|
76
|
-
*/
|
|
77
|
-
internalLinkComponent?: PrismicLinkProps["internalComponent"];
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* The React component rendered for links when the URL is external.
|
|
81
|
-
*
|
|
82
|
-
* @defaultValue `<a>`
|
|
83
|
-
*/
|
|
84
|
-
externalLinkComponent?: PrismicLinkProps["externalComponent"];
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The value to be rendered when the field is empty. If a fallback is not
|
|
88
|
-
* given, `null` will be rendered.
|
|
89
|
-
*/
|
|
90
|
-
fallback?: React.ReactNode;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
type CreateDefaultSerializerArgs<
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
-
LinkResolverFunction extends prismicH.LinkResolverFunction<any> = prismicH.LinkResolverFunction,
|
|
96
|
-
> = {
|
|
97
|
-
linkResolver: LinkResolverFunction | undefined;
|
|
98
|
-
internalLinkComponent: PrismicRichTextProps["internalLinkComponent"];
|
|
99
|
-
externalLinkComponent: PrismicRichTextProps["externalLinkComponent"];
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const createDefaultSerializer = (
|
|
103
|
-
args: CreateDefaultSerializerArgs,
|
|
104
|
-
): JSXFunctionSerializer =>
|
|
105
|
-
prismicR.wrapMapSerializer({
|
|
106
|
-
heading1: ({ children, key }) => <h1 key={key}>{children}</h1>,
|
|
107
|
-
heading2: ({ children, key }) => <h2 key={key}>{children}</h2>,
|
|
108
|
-
heading3: ({ children, key }) => <h3 key={key}>{children}</h3>,
|
|
109
|
-
heading4: ({ children, key }) => <h4 key={key}>{children}</h4>,
|
|
110
|
-
heading5: ({ children, key }) => <h5 key={key}>{children}</h5>,
|
|
111
|
-
heading6: ({ children, key }) => <h6 key={key}>{children}</h6>,
|
|
112
|
-
paragraph: ({ children, key }) => <p key={key}>{children}</p>,
|
|
113
|
-
preformatted: ({ node, key }) => <pre key={key}>{node.text}</pre>,
|
|
114
|
-
strong: ({ children, key }) => <strong key={key}>{children}</strong>,
|
|
115
|
-
em: ({ children, key }) => <em key={key}>{children}</em>,
|
|
116
|
-
listItem: ({ children, key }) => <li key={key}>{children}</li>,
|
|
117
|
-
oListItem: ({ children, key }) => <li key={key}>{children}</li>,
|
|
118
|
-
list: ({ children, key }) => <ul key={key}>{children}</ul>,
|
|
119
|
-
oList: ({ children, key }) => <ol key={key}>{children}</ol>,
|
|
120
|
-
image: ({ node, key }) => {
|
|
121
|
-
const img = (
|
|
122
|
-
<img
|
|
123
|
-
src={node.url}
|
|
124
|
-
alt={node.alt ?? undefined}
|
|
125
|
-
data-copyright={node.copyright ? node.copyright : undefined}
|
|
126
|
-
/>
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
return (
|
|
130
|
-
<p key={key} className="block-img">
|
|
131
|
-
{node.linkTo ? (
|
|
132
|
-
<PrismicLink
|
|
133
|
-
linkResolver={args.linkResolver}
|
|
134
|
-
internalComponent={args.internalLinkComponent}
|
|
135
|
-
externalComponent={args.externalLinkComponent}
|
|
136
|
-
field={node.linkTo}
|
|
137
|
-
>
|
|
138
|
-
{img}
|
|
139
|
-
</PrismicLink>
|
|
140
|
-
) : (
|
|
141
|
-
img
|
|
142
|
-
)}
|
|
143
|
-
</p>
|
|
144
|
-
);
|
|
145
|
-
},
|
|
146
|
-
embed: ({ node, key }) => (
|
|
147
|
-
<div
|
|
148
|
-
key={key}
|
|
149
|
-
data-oembed={node.oembed.embed_url}
|
|
150
|
-
data-oembed-type={node.oembed.type}
|
|
151
|
-
data-oembed-provider={node.oembed.provider_name}
|
|
152
|
-
dangerouslySetInnerHTML={{ __html: node.oembed.html ?? "" }}
|
|
153
|
-
/>
|
|
154
|
-
),
|
|
155
|
-
hyperlink: ({ node, children, key }) => (
|
|
156
|
-
<PrismicLink
|
|
157
|
-
key={key}
|
|
158
|
-
field={node.data}
|
|
159
|
-
linkResolver={args.linkResolver}
|
|
160
|
-
internalComponent={args.internalLinkComponent}
|
|
161
|
-
externalComponent={args.externalLinkComponent}
|
|
162
|
-
>
|
|
163
|
-
{children}
|
|
164
|
-
</PrismicLink>
|
|
165
|
-
),
|
|
166
|
-
label: ({ node, children, key }) => (
|
|
167
|
-
<span key={key} className={node.data.label}>
|
|
168
|
-
{children}
|
|
169
|
-
</span>
|
|
170
|
-
),
|
|
171
|
-
span: ({ text, key }) => {
|
|
172
|
-
const result: React.ReactNode[] = [];
|
|
173
|
-
|
|
174
|
-
let i = 0;
|
|
175
|
-
for (const line of text.split("\n")) {
|
|
176
|
-
if (i > 0) {
|
|
177
|
-
result.push(<br key={`${i}__break`} />);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
result.push(<React.Fragment key={`${i}__line`}>{line}</React.Fragment>);
|
|
6
|
+
import {
|
|
7
|
+
PrismicRichText as ServerPrismicRichText,
|
|
8
|
+
PrismicRichTextProps,
|
|
9
|
+
} from "./react-server/PrismicRichText";
|
|
181
10
|
|
|
182
|
-
|
|
183
|
-
}
|
|
11
|
+
import { usePrismicContext } from "./usePrismicContext";
|
|
184
12
|
|
|
185
|
-
|
|
186
|
-
},
|
|
187
|
-
});
|
|
13
|
+
export { PrismicRichTextProps };
|
|
188
14
|
|
|
189
|
-
|
|
190
|
-
* React component that renders content from a Prismic Rich Text field. By
|
|
191
|
-
* default, HTML elements are rendered for each piece of content. A `heading1`
|
|
192
|
-
* block will render an `<h1>` HTML element, for example. Links will use
|
|
193
|
-
* `<PrismicLink>` by default which can be customized using the
|
|
194
|
-
* `internalLinkComponent` and `externalLinkComponent` props.
|
|
195
|
-
*
|
|
196
|
-
* To customize the components that are rendered, provide a map or function
|
|
197
|
-
* serializer to the `components` prop.
|
|
198
|
-
*
|
|
199
|
-
* Components can also be provided in a centralized location using the
|
|
200
|
-
* `<PrismicProvider>` React context provider.
|
|
201
|
-
*
|
|
202
|
-
* @remarks
|
|
203
|
-
* This component returns a React fragment with no wrapping element around the
|
|
204
|
-
* content. If you need a wrapper, add a component around `<PrismicRichText>`.
|
|
205
|
-
* @example Rendering a Rich Text field using the default HTMl elements.
|
|
206
|
-
*
|
|
207
|
-
* ```jsx
|
|
208
|
-
* <PrismicRichText field={document.data.content} />;
|
|
209
|
-
* ```
|
|
210
|
-
*
|
|
211
|
-
* @example Rendering a Rich Text field using a custom set of React components.
|
|
212
|
-
*
|
|
213
|
-
* ```jsx
|
|
214
|
-
* <PrismicRichText
|
|
215
|
-
* field={document.data.content}
|
|
216
|
-
* components={{
|
|
217
|
-
* heading1: ({ children }) => <Heading>{children}</Heading>,
|
|
218
|
-
* }}
|
|
219
|
-
* />;
|
|
220
|
-
* ```
|
|
221
|
-
*
|
|
222
|
-
* @param props - Props for the component.
|
|
223
|
-
*
|
|
224
|
-
* @returns The Rich Text field's content as React components.
|
|
225
|
-
*
|
|
226
|
-
* @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}
|
|
227
|
-
* @see Learn about Rich Text serializers {@link https://prismic.io/docs/core-concepts/html-serializer}
|
|
228
|
-
*/
|
|
229
|
-
export const PrismicRichText = <
|
|
15
|
+
export const PrismicRichText = function PrismicRichText<
|
|
230
16
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
231
|
-
LinkResolverFunction extends
|
|
232
|
-
>(
|
|
233
|
-
props: PrismicRichTextProps<LinkResolverFunction>,
|
|
234
|
-
): JSX.Element | null => {
|
|
17
|
+
LinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,
|
|
18
|
+
>({ components, ...restProps }: PrismicRichTextProps<LinkResolverFunction>) {
|
|
235
19
|
const context = usePrismicContext();
|
|
236
20
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
// The serializer is wrapped in a higher-order function
|
|
256
|
-
// that automatically applies a key to React Elements
|
|
257
|
-
// if one is not already given.
|
|
258
|
-
const serialized = prismicR.serialize<JSX.Element>(
|
|
259
|
-
props.field,
|
|
260
|
-
(type, node, text, children, key) => {
|
|
261
|
-
const result = serializer(type, node, text, children, key);
|
|
262
|
-
|
|
263
|
-
if (React.isValidElement(result) && result.key == null) {
|
|
264
|
-
return React.cloneElement(result, { key });
|
|
265
|
-
} else {
|
|
266
|
-
return result;
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
return <>{serialized}</>;
|
|
272
|
-
} else {
|
|
273
|
-
return props.fallback != null ? <>{props.fallback}</> : null;
|
|
274
|
-
}
|
|
275
|
-
}, [
|
|
276
|
-
props.field,
|
|
277
|
-
props.internalLinkComponent,
|
|
278
|
-
props.externalLinkComponent,
|
|
279
|
-
props.components,
|
|
280
|
-
props.linkResolver,
|
|
281
|
-
props.fallback,
|
|
282
|
-
context.linkResolver,
|
|
283
|
-
context.richTextComponents,
|
|
284
|
-
]);
|
|
21
|
+
const serializer = prismicR.composeSerializers(
|
|
22
|
+
typeof components === "object"
|
|
23
|
+
? prismicR.wrapMapSerializer(components)
|
|
24
|
+
: components,
|
|
25
|
+
typeof context.richTextComponents === "object"
|
|
26
|
+
? prismicR.wrapMapSerializer(context.richTextComponents)
|
|
27
|
+
: context.richTextComponents,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<ServerPrismicRichText
|
|
32
|
+
components={serializer}
|
|
33
|
+
internalLinkComponent={context.internalLinkComponent}
|
|
34
|
+
externalLinkComponent={context.externalLinkComponent}
|
|
35
|
+
{...restProps}
|
|
36
|
+
/>
|
|
37
|
+
);
|
|
285
38
|
};
|
package/src/PrismicText.tsx
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import * as
|
|
3
|
-
import * as prismicH from "@prismicio/helpers";
|
|
2
|
+
import * as prismic from "@prismicio/client";
|
|
4
3
|
|
|
5
4
|
import { __PRODUCTION__ } from "./lib/__PRODUCTION__";
|
|
6
5
|
import { devMsg } from "./lib/devMsg";
|
|
@@ -12,7 +11,7 @@ export type PrismicTextProps = {
|
|
|
12
11
|
/**
|
|
13
12
|
* The Prismic Rich Text field to render.
|
|
14
13
|
*/
|
|
15
|
-
field:
|
|
14
|
+
field: prismic.RichTextField | null | undefined;
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
17
|
* The string rendered when the field is empty. If a fallback is not given,
|
|
@@ -57,8 +56,8 @@ export const PrismicText = (props: PrismicTextProps): JSX.Element | null => {
|
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
return React.useMemo(() => {
|
|
60
|
-
if (
|
|
61
|
-
const text =
|
|
59
|
+
if (prismic.isFilled.richText(props.field)) {
|
|
60
|
+
const text = prismic.asText(props.field, props.separator);
|
|
62
61
|
|
|
63
62
|
return <>{text}</>;
|
|
64
63
|
} else {
|