@prismicio/react 2.9.1 → 3.0.0-alpha.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.
Files changed (191) hide show
  1. package/README.md +10 -11
  2. package/dist/PrismicImage.d.ts +18 -633
  3. package/dist/PrismicImage.js +32 -33
  4. package/dist/PrismicImage.js.map +1 -1
  5. package/dist/PrismicLink.d.ts +77 -6
  6. package/dist/PrismicLink.js +42 -7
  7. package/dist/PrismicLink.js.map +1 -1
  8. package/dist/PrismicRichText.d.ts +117 -4
  9. package/dist/PrismicRichText.js +74 -8
  10. package/dist/PrismicRichText.js.map +1 -1
  11. package/dist/PrismicText.d.ts +8 -13
  12. package/dist/PrismicText.js +15 -16
  13. package/dist/PrismicText.js.map +1 -1
  14. package/dist/PrismicToolbar.d.ts +2 -12
  15. package/dist/PrismicToolbar.js +8 -10
  16. package/dist/PrismicToolbar.js.map +1 -1
  17. package/dist/SliceZone.d.ts +16 -70
  18. package/dist/SliceZone.js +16 -37
  19. package/dist/SliceZone.js.map +1 -1
  20. package/dist/index.d.ts +13 -21
  21. package/dist/index.js +2 -29
  22. package/dist/index.js.map +1 -1
  23. package/dist/lib/devMsg.d.ts +2 -2
  24. package/dist/lib/devMsg.js +2 -2
  25. package/dist/lib/devMsg.js.map +1 -1
  26. package/dist/package.json.js +1 -1
  27. package/package.json +53 -70
  28. package/src/PrismicImage.tsx +80 -65
  29. package/src/PrismicLink.tsx +196 -26
  30. package/src/PrismicRichText.tsx +333 -33
  31. package/src/PrismicText.tsx +25 -30
  32. package/src/PrismicToolbar.tsx +10 -30
  33. package/src/SliceZone.tsx +58 -150
  34. package/src/index.ts +17 -53
  35. package/src/lib/devMsg.ts +3 -3
  36. package/dist/PrismicImage.cjs +0 -68
  37. package/dist/PrismicImage.cjs.map +0 -1
  38. package/dist/PrismicLink.cjs +0 -29
  39. package/dist/PrismicLink.cjs.map +0 -1
  40. package/dist/PrismicProvider.cjs +0 -41
  41. package/dist/PrismicProvider.cjs.map +0 -1
  42. package/dist/PrismicProvider.d.ts +0 -83
  43. package/dist/PrismicProvider.js +0 -24
  44. package/dist/PrismicProvider.js.map +0 -1
  45. package/dist/PrismicRichText.cjs +0 -30
  46. package/dist/PrismicRichText.cjs.map +0 -1
  47. package/dist/PrismicText.cjs +0 -44
  48. package/dist/PrismicText.cjs.map +0 -1
  49. package/dist/PrismicToolbar.cjs +0 -43
  50. package/dist/PrismicToolbar.cjs.map +0 -1
  51. package/dist/SliceZone.cjs +0 -53
  52. package/dist/SliceZone.cjs.map +0 -1
  53. package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs +0 -16
  54. package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs.map +0 -1
  55. package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js +0 -16
  56. package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js.map +0 -1
  57. package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs +0 -13
  58. package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs.map +0 -1
  59. package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js +0 -13
  60. package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js.map +0 -1
  61. package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.cjs +0 -22
  62. package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.cjs.map +0 -1
  63. package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.js +0 -22
  64. package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.js.map +0 -1
  65. package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.cjs +0 -46
  66. package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.cjs.map +0 -1
  67. package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.js +0 -46
  68. package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.js.map +0 -1
  69. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs +0 -47
  70. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs.map +0 -1
  71. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js +0 -47
  72. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js.map +0 -1
  73. package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs +0 -28
  74. package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs.map +0 -1
  75. package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js +0 -28
  76. package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js.map +0 -1
  77. package/dist/_node_modules/@prismicio/client/dist/helpers/asText.cjs +0 -21
  78. package/dist/_node_modules/@prismicio/client/dist/helpers/asText.cjs.map +0 -1
  79. package/dist/_node_modules/@prismicio/client/dist/helpers/asText.js +0 -21
  80. package/dist/_node_modules/@prismicio/client/dist/helpers/asText.js.map +0 -1
  81. package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs +0 -27
  82. package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs.map +0 -1
  83. package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js +0 -27
  84. package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js.map +0 -1
  85. package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs +0 -24
  86. package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs.map +0 -1
  87. package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js +0 -24
  88. package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js.map +0 -1
  89. package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs +0 -7
  90. package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs.map +0 -1
  91. package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js +0 -7
  92. package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js.map +0 -1
  93. package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs +0 -9
  94. package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs.map +0 -1
  95. package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js +0 -9
  96. package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js.map +0 -1
  97. package/dist/_node_modules/@prismicio/client/dist/richtext/asText.cjs +0 -13
  98. package/dist/_node_modules/@prismicio/client/dist/richtext/asText.cjs.map +0 -1
  99. package/dist/_node_modules/@prismicio/client/dist/richtext/asText.js +0 -13
  100. package/dist/_node_modules/@prismicio/client/dist/richtext/asText.js.map +0 -1
  101. package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs +0 -10
  102. package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs.map +0 -1
  103. package/dist/_node_modules/@prismicio/client/dist/types/value/link.js +0 -10
  104. package/dist/_node_modules/@prismicio/client/dist/types/value/link.js.map +0 -1
  105. package/dist/_node_modules/imgix-url-builder/dist/buildPixelDensitySrcSet.cjs +0 -10
  106. package/dist/_node_modules/imgix-url-builder/dist/buildPixelDensitySrcSet.cjs.map +0 -1
  107. package/dist/_node_modules/imgix-url-builder/dist/buildPixelDensitySrcSet.js +0 -10
  108. package/dist/_node_modules/imgix-url-builder/dist/buildPixelDensitySrcSet.js.map +0 -1
  109. package/dist/_node_modules/imgix-url-builder/dist/buildURL.cjs +0 -29
  110. package/dist/_node_modules/imgix-url-builder/dist/buildURL.cjs.map +0 -1
  111. package/dist/_node_modules/imgix-url-builder/dist/buildURL.js +0 -29
  112. package/dist/_node_modules/imgix-url-builder/dist/buildURL.js.map +0 -1
  113. package/dist/_node_modules/imgix-url-builder/dist/buildWidthSrcSet.cjs +0 -10
  114. package/dist/_node_modules/imgix-url-builder/dist/buildWidthSrcSet.cjs.map +0 -1
  115. package/dist/_node_modules/imgix-url-builder/dist/buildWidthSrcSet.js +0 -10
  116. package/dist/_node_modules/imgix-url-builder/dist/buildWidthSrcSet.js.map +0 -1
  117. package/dist/clientHooks.cjs +0 -40
  118. package/dist/clientHooks.cjs.map +0 -1
  119. package/dist/clientHooks.d.ts +0 -321
  120. package/dist/clientHooks.js +0 -40
  121. package/dist/clientHooks.js.map +0 -1
  122. package/dist/index.cjs +0 -48
  123. package/dist/index.cjs.map +0 -1
  124. package/dist/lib/devMsg.cjs +0 -8
  125. package/dist/lib/devMsg.cjs.map +0 -1
  126. package/dist/lib/isInternalURL.cjs +0 -9
  127. package/dist/lib/isInternalURL.cjs.map +0 -1
  128. package/dist/lib/isInternalURL.d.ts +0 -8
  129. package/dist/lib/isInternalURL.js +0 -9
  130. package/dist/lib/isInternalURL.js.map +0 -1
  131. package/dist/lib/pascalCase.cjs +0 -10
  132. package/dist/lib/pascalCase.cjs.map +0 -1
  133. package/dist/lib/pascalCase.d.ts +0 -26
  134. package/dist/lib/pascalCase.js +0 -10
  135. package/dist/lib/pascalCase.js.map +0 -1
  136. package/dist/package.json.cjs +0 -5
  137. package/dist/package.json.cjs.map +0 -1
  138. package/dist/react-server/PrismicLink.cjs +0 -59
  139. package/dist/react-server/PrismicLink.cjs.map +0 -1
  140. package/dist/react-server/PrismicLink.d.ts +0 -77
  141. package/dist/react-server/PrismicLink.js +0 -42
  142. package/dist/react-server/PrismicLink.js.map +0 -1
  143. package/dist/react-server/PrismicRichText.cjs +0 -102
  144. package/dist/react-server/PrismicRichText.cjs.map +0 -1
  145. package/dist/react-server/PrismicRichText.d.ts +0 -104
  146. package/dist/react-server/PrismicRichText.js +0 -84
  147. package/dist/react-server/PrismicRichText.js.map +0 -1
  148. package/dist/react-server/index.d.ts +0 -4
  149. package/dist/react-server/unsupported.cjs +0 -97
  150. package/dist/react-server/unsupported.cjs.map +0 -1
  151. package/dist/react-server/unsupported.d.ts +0 -22
  152. package/dist/react-server/unsupported.js +0 -97
  153. package/dist/react-server/unsupported.js.map +0 -1
  154. package/dist/react-server.cjs +0 -44
  155. package/dist/react-server.cjs.map +0 -1
  156. package/dist/react-server.js +0 -41
  157. package/dist/react-server.js.map +0 -1
  158. package/dist/types.d.ts +0 -19
  159. package/dist/usePrismicClient.cjs +0 -13
  160. package/dist/usePrismicClient.cjs.map +0 -1
  161. package/dist/usePrismicClient.d.ts +0 -11
  162. package/dist/usePrismicClient.js +0 -13
  163. package/dist/usePrismicClient.js.map +0 -1
  164. package/dist/usePrismicContext.cjs +0 -26
  165. package/dist/usePrismicContext.cjs.map +0 -1
  166. package/dist/usePrismicContext.d.ts +0 -8
  167. package/dist/usePrismicContext.js +0 -9
  168. package/dist/usePrismicContext.js.map +0 -1
  169. package/dist/usePrismicPreviewResolver.cjs +0 -44
  170. package/dist/usePrismicPreviewResolver.cjs.map +0 -1
  171. package/dist/usePrismicPreviewResolver.d.ts +0 -45
  172. package/dist/usePrismicPreviewResolver.js +0 -27
  173. package/dist/usePrismicPreviewResolver.js.map +0 -1
  174. package/dist/useStatefulPrismicClientMethod.cjs +0 -91
  175. package/dist/useStatefulPrismicClientMethod.cjs.map +0 -1
  176. package/dist/useStatefulPrismicClientMethod.d.ts +0 -44
  177. package/dist/useStatefulPrismicClientMethod.js +0 -74
  178. package/dist/useStatefulPrismicClientMethod.js.map +0 -1
  179. package/src/PrismicProvider.tsx +0 -132
  180. package/src/clientHooks.ts +0 -472
  181. package/src/lib/isInternalURL.ts +0 -14
  182. package/src/lib/pascalCase.ts +0 -79
  183. package/src/react-server/PrismicLink.tsx +0 -197
  184. package/src/react-server/PrismicRichText.tsx +0 -326
  185. package/src/react-server/index.ts +0 -38
  186. package/src/react-server/unsupported.ts +0 -184
  187. package/src/types.ts +0 -23
  188. package/src/usePrismicClient.ts +0 -30
  189. package/src/usePrismicContext.ts +0 -13
  190. package/src/usePrismicPreviewResolver.ts +0 -93
  191. package/src/useStatefulPrismicClientMethod.ts +0 -196
@@ -1,38 +1,338 @@
1
- "use client";
1
+ import {
2
+ cloneElement,
3
+ ComponentType,
4
+ Fragment,
5
+ isValidElement,
6
+ ReactNode,
7
+ } from "react";
8
+ import {
9
+ isFilled,
10
+ LinkResolverFunction,
11
+ RichTextField,
12
+ RTAnyNode,
13
+ } from "@prismicio/client";
14
+ import {
15
+ composeSerializers,
16
+ serialize,
17
+ wrapMapSerializer,
18
+ RichTextFunctionSerializer,
19
+ RichTextMapSerializer,
20
+ } from "@prismicio/client/richtext";
21
+ import { DEV } from "esm-env";
2
22
 
3
- import * as prismic from "@prismicio/client";
4
- import * as prismicR from "@prismicio/richtext";
23
+ import { devMsg } from "./lib/devMsg.js";
5
24
 
6
- import {
7
- PrismicRichText as ServerPrismicRichText,
8
- PrismicRichTextProps,
9
- } from "./react-server/PrismicRichText";
10
-
11
- import { usePrismicContext } from "./usePrismicContext";
12
-
13
- export type { PrismicRichTextProps };
14
-
15
- export const PrismicRichText = function PrismicRichText<
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- LinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,
18
- >({ components, ...restProps }: PrismicRichTextProps<LinkResolverFunction>) {
19
- const context = usePrismicContext();
20
-
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,
25
+ import { LinkProps, PrismicLink } from "./PrismicLink.js";
26
+
27
+ /**
28
+ * A function mapping Rich Text block types to React Components. It is used to
29
+ * render Rich Text or Title fields.
30
+ *
31
+ * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
32
+ */
33
+ export type JSXFunctionSerializer = RichTextFunctionSerializer<ReactNode>;
34
+
35
+ /**
36
+ * A map of Rich Text block types to React Components. It is used to render Rich
37
+ * Text or Title fields.
38
+ *
39
+ * @see Templating Rich Text and Title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
40
+ */
41
+ export type JSXMapSerializer = RichTextMapSerializer<ReactNode>;
42
+
43
+ /** Props for `<PrismicRichText>`. */
44
+ export type PrismicRichTextProps = {
45
+ /** The Prismic Rich Text field to render. */
46
+ field: RichTextField | null | undefined;
47
+
48
+ /**
49
+ * The Link Resolver used to resolve links.
50
+ *
51
+ * @remarks
52
+ * If your app uses Route Resolvers when querying for your Prismic
53
+ * repository's content, a Link Resolver does not need to be provided.
54
+ *
55
+ * @see Learn about Link Resolvers and Route Resolvers {@link https://io/docs/core-concepts/link-resolver-route-resolver}
56
+ */
57
+ linkResolver?: LinkResolverFunction;
58
+
59
+ /**
60
+ * A map or function that maps a Rich Text block to a React component.
61
+ *
62
+ * @remarks
63
+ * Prefer using a map serializer over the function serializer when possible.
64
+ * The map serializer is simpler to maintain.
65
+ *
66
+ * @example A map serializer.
67
+ *
68
+ * ```jsx
69
+ * {
70
+ * heading1: ({children}) => <Heading>{children}</Heading>
71
+ * }
72
+ * ```
73
+ *
74
+ * @example A function serializer.
75
+ *
76
+ * ```jsx
77
+ * (type, node, content, children) => {
78
+ * switch (type) {
79
+ * case "heading1": {
80
+ * return <Heading>{children}</Heading>;
81
+ * }
82
+ * }
83
+ * };
84
+ * ```
85
+ */
86
+ components?: JSXMapSerializer | JSXFunctionSerializer;
87
+
88
+ /**
89
+ * The React component rendered for links when the URL is internal.
90
+ *
91
+ * @defaultValue `<a>`
92
+ */
93
+ internalLinkComponent?: ComponentType<LinkProps>;
94
+
95
+ /**
96
+ * The React component rendered for links when the URL is external.
97
+ *
98
+ * @defaultValue `<a>`
99
+ */
100
+ externalLinkComponent?: ComponentType<LinkProps>;
101
+
102
+ /**
103
+ * The value to be rendered when the field is empty. If a fallback is not
104
+ * given, `null` will be rendered.
105
+ */
106
+ fallback?: ReactNode;
107
+ };
108
+
109
+ type CreateDefaultSerializerArgs = {
110
+ linkResolver: LinkResolverFunction | undefined;
111
+ internalLinkComponent?: ComponentType<LinkProps>;
112
+ externalLinkComponent?: ComponentType<LinkProps>;
113
+ };
114
+
115
+ const getDir = (node: RTAnyNode): "rtl" | undefined => {
116
+ if ("direction" in node && node.direction === "rtl") {
117
+ return "rtl";
118
+ }
119
+ };
120
+
121
+ const createDefaultSerializer = (
122
+ args: CreateDefaultSerializerArgs,
123
+ ): JSXFunctionSerializer =>
124
+ wrapMapSerializer<ReactNode>({
125
+ heading1: ({ node, children, key }) => (
126
+ <h1 key={key} dir={getDir(node)}>
127
+ {children}
128
+ </h1>
129
+ ),
130
+ heading2: ({ node, children, key }) => (
131
+ <h2 key={key} dir={getDir(node)}>
132
+ {children}
133
+ </h2>
134
+ ),
135
+ heading3: ({ node, children, key }) => (
136
+ <h3 key={key} dir={getDir(node)}>
137
+ {children}
138
+ </h3>
139
+ ),
140
+ heading4: ({ node, children, key }) => (
141
+ <h4 key={key} dir={getDir(node)}>
142
+ {children}
143
+ </h4>
144
+ ),
145
+ heading5: ({ node, children, key }) => (
146
+ <h5 key={key} dir={getDir(node)}>
147
+ {children}
148
+ </h5>
149
+ ),
150
+ heading6: ({ node, children, key }) => (
151
+ <h6 key={key} dir={getDir(node)}>
152
+ {children}
153
+ </h6>
154
+ ),
155
+ paragraph: ({ node, children, key }) => (
156
+ <p key={key} dir={getDir(node)}>
157
+ {children}
158
+ </p>
159
+ ),
160
+ preformatted: ({ node, key }) => <pre key={key}>{node.text}</pre>,
161
+ strong: ({ children, key }) => <strong key={key}>{children}</strong>,
162
+ em: ({ children, key }) => <em key={key}>{children}</em>,
163
+ listItem: ({ node, children, key }) => (
164
+ <li key={key} dir={getDir(node)}>
165
+ {children}
166
+ </li>
167
+ ),
168
+ oListItem: ({ node, children, key }) => (
169
+ <li key={key} dir={getDir(node)}>
170
+ {children}
171
+ </li>
172
+ ),
173
+ list: ({ children, key }) => <ul key={key}>{children}</ul>,
174
+ oList: ({ children, key }) => <ol key={key}>{children}</ol>,
175
+ image: ({ node, key }) => {
176
+ const img = (
177
+ <img
178
+ src={node.url}
179
+ alt={node.alt ?? undefined}
180
+ data-copyright={node.copyright ? node.copyright : undefined}
181
+ />
182
+ );
183
+
184
+ return (
185
+ <p key={key} className="block-img">
186
+ {node.linkTo ? (
187
+ <PrismicLink
188
+ linkResolver={args.linkResolver}
189
+ internalComponent={args.internalLinkComponent}
190
+ externalComponent={args.externalLinkComponent}
191
+ field={node.linkTo}
192
+ >
193
+ {img}
194
+ </PrismicLink>
195
+ ) : (
196
+ img
197
+ )}
198
+ </p>
199
+ );
200
+ },
201
+ embed: ({ node, key }) => (
202
+ <div
203
+ key={key}
204
+ data-oembed={node.oembed.embed_url}
205
+ data-oembed-type={node.oembed.type}
206
+ data-oembed-provider={node.oembed.provider_name}
207
+ dangerouslySetInnerHTML={{ __html: node.oembed.html ?? "" }}
208
+ />
209
+ ),
210
+ hyperlink: ({ node, children, key }) => (
211
+ <PrismicLink
212
+ key={key}
213
+ field={node.data}
214
+ linkResolver={args.linkResolver}
215
+ internalComponent={args.internalLinkComponent}
216
+ externalComponent={args.externalLinkComponent}
217
+ >
218
+ {children}
219
+ </PrismicLink>
220
+ ),
221
+ label: ({ node, children, key }) => (
222
+ <span key={key} className={node.data.label}>
223
+ {children}
224
+ </span>
225
+ ),
226
+ span: ({ text, key }) => {
227
+ const result: ReactNode[] = [];
228
+
229
+ let i = 0;
230
+ for (const line of text.split("\n")) {
231
+ if (i > 0) {
232
+ result.push(<br key={`${i}__break`} />);
233
+ }
234
+
235
+ result.push(<Fragment key={`${i}__line`}>{line}</Fragment>);
236
+
237
+ i++;
238
+ }
239
+
240
+ return <Fragment key={key}>{result}</Fragment>;
241
+ },
242
+ });
243
+
244
+ /**
245
+ * React component that renders content from a Prismic Rich Text field. By
246
+ * default, HTML elements are rendered for each piece of content. A `heading1`
247
+ * block will render an `<h1>` HTML element, for example. Links will use
248
+ * `<PrismicLink>` by default which can be customized using the
249
+ * `internalLinkComponent` and `externalLinkComponent` props.
250
+ *
251
+ * To customize the components that are rendered, provide a map or function
252
+ * serializer to the `components` prop.
253
+ *
254
+ * @remarks
255
+ * This component returns a React fragment with no wrapping element around the
256
+ * content. If you need a wrapper, add a component around `<PrismicRichText>`.
257
+ *
258
+ * @example Rendering a Rich Text field using the default HTMl elements.
259
+ *
260
+ * ```jsx
261
+ * <PrismicRichText field={document.data.content} />;
262
+ * ```
263
+ *
264
+ * @example Rendering a Rich Text field using a custom set of React components.
265
+ *
266
+ * ```jsx
267
+ * <PrismicRichText
268
+ * field={document.data.content}
269
+ * components={{
270
+ * heading1: ({ children }) => <Heading>{children}</Heading>,
271
+ * }}
272
+ * />;
273
+ * ```
274
+ *
275
+ * @param props - Props for the component.
276
+ *
277
+ * @returns The Rich Text field's content as React components.
278
+ *
279
+ * @see Learn about Rich Text fields {@link https://io/docs/core-concepts/rich-text-title}
280
+ * @see Learn about Rich Text serializers {@link https://io/docs/core-concepts/html-serializer}
281
+ */
282
+ export function PrismicRichText(props: PrismicRichTextProps) {
283
+ const {
284
+ linkResolver,
285
+ field,
286
+ fallback,
287
+ components,
288
+ externalLinkComponent,
289
+ internalLinkComponent,
290
+ ...restProps
291
+ } = props;
292
+
293
+ if (DEV) {
294
+ if ("className" in restProps) {
295
+ console.warn(
296
+ `[PrismicRichText] className cannot be passed to <PrismicRichText> since it renders an array without a wrapping component. For more details, see ${devMsg(
297
+ "classname-is-not-a-valid-prop",
298
+ )}.`,
299
+ field,
300
+ );
301
+ }
302
+ }
303
+
304
+ if (!isFilled.richText(field)) {
305
+ return fallback != null ? <>{fallback}</> : null;
306
+ }
307
+
308
+ const serializer = composeSerializers<ReactNode>(
309
+ typeof components === "object" ? wrapMapSerializer(components) : components,
310
+ createDefaultSerializer({
311
+ linkResolver,
312
+ internalLinkComponent,
313
+ externalLinkComponent,
314
+ }),
28
315
  );
29
316
 
30
- return (
31
- <ServerPrismicRichText
32
- components={serializer}
33
- internalLinkComponent={context.internalLinkComponent}
34
- externalLinkComponent={context.externalLinkComponent}
35
- {...restProps}
36
- />
317
+ // The serializer is wrapped in a higher-order function that
318
+ // automatically applies a key to React Elements if one is not already
319
+ // given.
320
+ const serialized = serialize<ReactNode>(
321
+ field,
322
+ (type, node, text, children, key) => {
323
+ const result = serializer(type, node, text, children, key);
324
+
325
+ if (isValidElement(result) && result.key == null) {
326
+ return cloneElement(result, { key });
327
+ } else {
328
+ return result;
329
+ }
330
+ },
37
331
  );
38
- };
332
+
333
+ if (!serialized) {
334
+ return fallback != null ? <>{fallback}</> : null;
335
+ }
336
+
337
+ return <>{serialized}</>;
338
+ }
@@ -1,16 +1,12 @@
1
- import * as React from "react";
2
- import * as prismic from "@prismicio/client";
1
+ import { asText, isFilled, RichTextField } from "@prismicio/client";
2
+ import { DEV } from "esm-env";
3
3
 
4
- import { devMsg } from "./lib/devMsg";
4
+ import { devMsg } from "./lib/devMsg.js";
5
5
 
6
- /**
7
- * Props for `<PrismicText>`.
8
- */
6
+ /** Props for `<PrismicText>`. */
9
7
  export type PrismicTextProps = {
10
- /**
11
- * The Prismic Rich Text field to render.
12
- */
13
- field: prismic.RichTextField | null | undefined;
8
+ /** The Prismic Rich Text field to render. */
9
+ field: RichTextField | null | undefined;
14
10
 
15
11
  /**
16
12
  * The string rendered when the field is empty. If a fallback is not given,
@@ -18,9 +14,7 @@ export type PrismicTextProps = {
18
14
  */
19
15
  fallback?: string;
20
16
 
21
- /**
22
- * The separator used between blocks. Defaults to `\n`.
23
- */
17
+ /** The separator used between blocks. Defaults to `\n`. */
24
18
  separator?: string;
25
19
  };
26
20
 
@@ -31,6 +25,7 @@ export type PrismicTextProps = {
31
25
  * @remarks
32
26
  * This component returns a React fragment with no wrapping element around the
33
27
  * content. If you need a wrapper, add a component around `<PrismicText>`.
28
+ *
34
29
  * @example Rendering a Rich Text field as plain text.
35
30
  *
36
31
  * ```jsx
@@ -41,13 +36,12 @@ export type PrismicTextProps = {
41
36
  *
42
37
  * @returns The Rich Text field's content as plain text.
43
38
  *
44
- * @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}
39
+ * @see Learn about Rich Text fields {@link https://io/docs/core-concepts/rich-text-title}
45
40
  */
46
- export const PrismicText = (props: PrismicTextProps): JSX.Element | null => {
47
- if (
48
- typeof process !== "undefined" &&
49
- process.env.NODE_ENV === "development"
50
- ) {
41
+ export function PrismicText(props: PrismicTextProps) {
42
+ const { field, fallback, separator } = props;
43
+
44
+ if (DEV) {
51
45
  if ("className" in props) {
52
46
  console.warn(
53
47
  `[PrismicText] className cannot be passed to <PrismicText> since it renders plain text without a wrapping component. For more details, see ${devMsg(
@@ -56,23 +50,24 @@ export const PrismicText = (props: PrismicTextProps): JSX.Element | null => {
56
50
  props.field,
57
51
  );
58
52
  }
53
+ }
59
54
 
60
- if (typeof props.field === "string") {
61
- throw new Error(
55
+ if (typeof props.field === "string") {
56
+ if (DEV) {
57
+ console.error(
62
58
  `[PrismicText] The "field" prop only accepts a Rich Text or Title field's value but was provided a different type of field instead (e.g. a Key Text or Select field). You can resolve this error by rendering the field value inline without <PrismicText>. For more details, see ${devMsg(
63
59
  "prismictext-works-only-with-rich-text-and-title-fields",
64
60
  )}`,
61
+ props.field,
65
62
  );
66
63
  }
64
+
65
+ return null;
67
66
  }
68
67
 
69
- return React.useMemo(() => {
70
- if (prismic.isFilled.richText(props.field)) {
71
- const text = prismic.asText(props.field, props.separator);
68
+ if (!isFilled.richText(field)) {
69
+ return fallback != null ? <>{fallback}</> : null;
70
+ }
72
71
 
73
- return <>{text}</>;
74
- } else {
75
- return props.fallback != null ? <>{props.fallback}</> : null;
76
- }
77
- }, [props.field, props.fallback, props.separator]);
78
- };
72
+ return <>{asText(field, { separator })}</>;
73
+ }
@@ -1,38 +1,27 @@
1
1
  "use client";
2
2
 
3
- import * as React from "react";
3
+ import { useEffect } from "react";
4
4
  import { getToolbarSrc } from "@prismicio/client";
5
5
 
6
- /**
7
- * Props for `<PrismicToolbar>`.
8
- */
6
+ /** Props for `<PrismicToolbar>`. */
9
7
  export type PrismicToolbarProps = {
10
8
  /**
11
9
  * The name of the Prismic repository. For example, `"my-repo"` if the
12
10
  * repository URL is `my-repo.prismic.io`.
13
11
  */
14
12
  repositoryName: string;
15
-
16
- /**
17
- * The type of toolbar needed for the repository. Defaults to `"new"`.
18
- *
19
- * @deprecated All repositories use the "new"-type toolbar. This option can be
20
- * removed.
21
- * @see To check which version you need, view the Prismic Toolbar documentation {@link https://prismic.io/docs/technologies/previews-and-the-prismic-toolbar-reactjs}
22
- */
23
- type?: "new" | "legacy";
24
13
  };
25
14
 
26
15
  /**
27
16
  * React component that injects the Prismic Toolbar into the app. This component
28
17
  * can be placed anywhere in the React tree.
29
18
  */
30
- export const PrismicToolbar = ({
31
- repositoryName,
32
- }: PrismicToolbarProps): null => {
19
+ export function PrismicToolbar(props: PrismicToolbarProps) {
20
+ const { repositoryName } = props;
21
+
33
22
  const src = getToolbarSrc(repositoryName);
34
23
 
35
- React.useEffect(() => {
24
+ useEffect(() => {
36
25
  const existingScript = document.querySelector(`script[src="${src}"]`);
37
26
 
38
27
  if (!existingScript) {
@@ -44,22 +33,13 @@ export const PrismicToolbar = ({
44
33
  script.dataset.prismicToolbar = "";
45
34
  script.dataset.repositoryName = repositoryName;
46
35
 
47
- // Disable Happy DOM `<script>` evaluation during
48
- // tests.
49
- //
50
- // This is a patch ONLY INCLUDED DURING TESTS. It will
51
- // be pruned during code minification in non-test
52
- // environments.
53
- //
54
- // @see https://github.com/capricorn86/happy-dom/blob/02ae081e36f990c06171eda44f9d885fd9413d73/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElement.ts#L191-L209
55
- if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
56
- // @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
57
- script._evaluateScript = false;
58
- }
36
+ // Disable Happy DOM `<script>` evaluation during tests.
37
+ // @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
38
+ script._evaluateScript = false;
59
39
 
60
40
  document.body.appendChild(script);
61
41
  }
62
42
  }, [repositoryName, src]);
63
43
 
64
44
  return null;
65
- };
45
+ }