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