@prismicio/react 3.4.0 → 3.4.1-canary.56c63d1

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 (42) hide show
  1. package/dist/PrismicImage.d.ts +20 -28
  2. package/dist/PrismicImage.d.ts.map +1 -1
  3. package/dist/PrismicImage.js +6 -8
  4. package/dist/PrismicImage.js.map +1 -1
  5. package/dist/PrismicLink.d.ts +18 -21
  6. package/dist/PrismicLink.d.ts.map +1 -1
  7. package/dist/PrismicLink.js +6 -9
  8. package/dist/PrismicLink.js.map +1 -1
  9. package/dist/PrismicRichText.d.ts +31 -37
  10. package/dist/PrismicRichText.d.ts.map +1 -1
  11. package/dist/PrismicRichText.js +5 -7
  12. package/dist/PrismicRichText.js.map +1 -1
  13. package/dist/PrismicTable.d.ts +15 -16
  14. package/dist/PrismicTable.d.ts.map +1 -1
  15. package/dist/PrismicTable.js +4 -6
  16. package/dist/PrismicTable.js.map +1 -1
  17. package/dist/PrismicText.d.ts +8 -11
  18. package/dist/PrismicText.d.ts.map +1 -1
  19. package/dist/PrismicText.js +4 -6
  20. package/dist/PrismicText.js.map +1 -1
  21. package/dist/PrismicToolbar.d.ts +11 -14
  22. package/dist/PrismicToolbar.d.ts.map +1 -1
  23. package/dist/PrismicToolbar.js +5 -8
  24. package/dist/PrismicToolbar.js.map +1 -1
  25. package/dist/SliceZone.d.ts +31 -51
  26. package/dist/SliceZone.d.ts.map +1 -1
  27. package/dist/SliceZone.js +7 -10
  28. package/dist/SliceZone.js.map +1 -1
  29. package/dist/index.js +1 -2
  30. package/dist/lib/devMsg.js +7 -10
  31. package/dist/lib/devMsg.js.map +1 -1
  32. package/dist/package.js +2 -2
  33. package/package.json +32 -34
  34. package/src/PrismicImage.tsx +107 -134
  35. package/src/PrismicLink.tsx +66 -76
  36. package/src/PrismicRichText.tsx +77 -85
  37. package/src/PrismicTable.tsx +41 -45
  38. package/src/PrismicText.tsx +20 -21
  39. package/src/PrismicToolbar.tsx +41 -45
  40. package/src/SliceZone.tsx +68 -108
  41. package/src/index.ts +15 -15
  42. package/src/lib/devMsg.ts +8 -10
@@ -1,3 +1,11 @@
1
+ import {
2
+ type LinkField,
3
+ type LinkResolverFunction,
4
+ type PrismicDocument,
5
+ asLinkAttrs,
6
+ type AsLinkAttrsConfig,
7
+ } from "@prismicio/client"
8
+ import { DEV } from "esm-env"
1
9
  import {
2
10
  type ComponentProps,
3
11
  type ComponentType,
@@ -5,109 +13,96 @@ import {
5
13
  type HTMLAttributeAnchorTarget,
6
14
  type ReactNode,
7
15
  forwardRef,
8
- } from "react";
9
- import {
10
- type LinkField,
11
- type LinkResolverFunction,
12
- type PrismicDocument,
13
- asLinkAttrs,
14
- type AsLinkAttrsConfig,
15
- } from "@prismicio/client";
16
- import { DEV } from "esm-env";
16
+ } from "react"
17
17
 
18
- import { devMsg } from "./lib/devMsg.js";
18
+ import { devMsg } from "./lib/devMsg.js"
19
19
 
20
20
  /** The default component rendered for internal and external links. */
21
- const defaultComponent = "a";
21
+ const defaultComponent = "a"
22
22
 
23
23
  /** Props provided to a component when rendered with `<PrismicLink>`. */
24
24
  export interface LinkProps {
25
25
  /** The URL to link. */
26
- href: string;
26
+ href: string
27
27
 
28
28
  /**
29
- * The `target` attribute for anchor elements. If the Prismic field is
30
- * configured to open in a new window, this prop defaults to `_blank`.
29
+ * The `target` attribute for anchor elements. If the Prismic field is configured to open in a new
30
+ * window, this prop defaults to `_blank`.
31
31
  */
32
- target?: HTMLAttributeAnchorTarget;
32
+ target?: HTMLAttributeAnchorTarget
33
33
 
34
34
  /**
35
- * The `rel` attribute for anchor elements. If the `target` prop is set to
36
- * `"_blank"`, this prop defaults to `"noopener noreferrer"`.
35
+ * The `rel` attribute for anchor elements. If the `target` prop is set to `"_blank"`, this prop
36
+ * defaults to `"noopener noreferrer"`.
37
37
  */
38
- rel?: string;
38
+ rel?: string
39
39
 
40
40
  /** Children for the component. */
41
- children?: ReactNode;
41
+ children?: ReactNode
42
42
  }
43
43
 
44
44
  export type PrismicLinkProps<
45
45
  InternalComponentProps = ComponentProps<typeof defaultComponent>,
46
46
  ExternalComponentProps = ComponentProps<typeof defaultComponent>,
47
- > = Omit<
48
- InternalComponentProps & ExternalComponentProps,
49
- "rel" | "href" | "children"
50
- > & {
47
+ > = Omit<InternalComponentProps & ExternalComponentProps, "rel" | "href" | "children"> & {
51
48
  /**
52
- * The `rel` attribute for the link. By default, `"noreferrer"` is provided if
53
- * the link's URL is external. This prop can be provided a function to use the
54
- * link's metadata to determine the `rel` value.
49
+ * The `rel` attribute for the link. By default, `"noreferrer"` is provided if the link's URL is
50
+ * external. This prop can be provided a function to use the link's metadata to determine the
51
+ * `rel` value.
55
52
  */
56
- rel?: string | AsLinkAttrsConfig["rel"];
53
+ rel?: string | AsLinkAttrsConfig["rel"]
57
54
 
58
55
  /**
59
56
  * The link resolver used to resolve links.
60
57
  *
61
58
  * @remarks
62
- * If your app uses route resolvers when querying for your Prismic
63
- * repository's content, a link resolver does not need to be provided.
64
- *
59
+ * If your app uses route resolvers when querying for your Prismic repository's content, a link
60
+ * resolver does not need to be provided.
65
61
  * @see Learn about link resolvers and route resolvers {@link https://prismic.io/docs/routes}
66
62
  */
67
- linkResolver?: LinkResolverFunction;
63
+ linkResolver?: LinkResolverFunction
68
64
 
69
65
  /**
70
66
  * The component rendered for internal URLs. Defaults to `<a>`.
71
67
  *
72
- * If your app uses a client-side router that requires a special Link
73
- * component, provide the Link component to this prop.
68
+ * If your app uses a client-side router that requires a special Link component, provide the Link
69
+ * component to this prop.
74
70
  */
75
- internalComponent?: ComponentType<InternalComponentProps>;
71
+ internalComponent?: ComponentType<InternalComponentProps>
76
72
 
77
73
  /** The component rendered for external URLs. Defaults to `<a>`. */
78
- externalComponent?: ComponentType<ExternalComponentProps>;
74
+ externalComponent?: ComponentType<ExternalComponentProps>
79
75
 
80
76
  /**
81
- * The children to render for the link. If no children are provided, the
82
- * link's `text` property will be used.
77
+ * The children to render for the link. If no children are provided, the link's `text` property
78
+ * will be used.
83
79
  */
84
- children?: ReactNode;
80
+ children?: ReactNode
85
81
  } & (
86
82
  | {
87
- document: PrismicDocument | null | undefined;
88
- href?: never;
89
- field?: never;
83
+ document: PrismicDocument | null | undefined
84
+ href?: never
85
+ field?: never
90
86
  }
91
87
  | {
92
- field: LinkField | null | undefined;
93
- href?: never;
94
- document?: never;
88
+ field: LinkField | null | undefined
89
+ href?: never
90
+ document?: never
95
91
  }
96
92
  | {
97
- href: LinkProps["href"];
98
- field?: LinkField | null | undefined;
99
- document?: never;
93
+ href: LinkProps["href"]
94
+ field?: LinkField | null | undefined
95
+ document?: never
100
96
  }
101
- );
97
+ )
102
98
 
103
99
  /**
104
100
  * Renders a link from a Prismic link field or page.
105
101
  *
106
102
  * @example
107
- *
108
- * ```tsx
109
- * <PrismicLink field={slice.primary.link}>Click here</PrismicLink>;
110
- * ```
103
+ * ;```tsx
104
+ * <PrismicLink field={slice.primary.link}>Click here</PrismicLink>;
105
+ * ```
111
106
  *
112
107
  * @see Learn how to display links and use variants for styling: {@link https://prismic.io/docs/fields/link}
113
108
  */
@@ -126,7 +121,7 @@ export const PrismicLink = forwardRef(function PrismicLink<
126
121
  externalComponent,
127
122
  children,
128
123
  ...restProps
129
- } = props;
124
+ } = props
130
125
 
131
126
  if (DEV) {
132
127
  if (field) {
@@ -134,16 +129,14 @@ export const PrismicLink = forwardRef(function PrismicLink<
134
129
  console.error(
135
130
  `[PrismicLink] This "field" prop value caused an error to be thrown.\n`,
136
131
  field,
137
- );
132
+ )
138
133
  throw new Error(
139
134
  `[PrismicLink] The provided field is missing required properties to properly render a link. The link will not render. For more details, see ${devMsg(
140
135
  "missing-link-properties",
141
136
  )}`,
142
- );
137
+ )
143
138
  } else if (
144
- ("text" in field
145
- ? Object.keys(field).length > 2
146
- : Object.keys(field).length > 1) &&
139
+ ("text" in field ? Object.keys(field).length > 2 : Object.keys(field).length > 1) &&
147
140
  !("url" in field || "uid" in field || "id" in field)
148
141
  ) {
149
142
  console.warn(
@@ -151,7 +144,7 @@ export const PrismicLink = forwardRef(function PrismicLink<
151
144
  "missing-link-properties",
152
145
  )}`,
153
146
  field,
154
- );
147
+ )
155
148
  }
156
149
  } else if (doc) {
157
150
  if (!("url" in doc || "id" in doc)) {
@@ -160,7 +153,7 @@ export const PrismicLink = forwardRef(function PrismicLink<
160
153
  "missing-link-properties",
161
154
  )}`,
162
155
  doc,
163
- );
156
+ )
164
157
  }
165
158
  }
166
159
  }
@@ -172,50 +165,47 @@ export const PrismicLink = forwardRef(function PrismicLink<
172
165
  } = asLinkAttrs(field ?? doc, {
173
166
  linkResolver,
174
167
  rel: typeof restProps.rel === "function" ? restProps.rel : undefined,
175
- });
168
+ })
176
169
 
177
- let rel: string | undefined = computedRel;
170
+ let rel: string | undefined = computedRel
178
171
  if ("rel" in restProps && typeof restProps.rel !== "function") {
179
- rel = restProps.rel;
172
+ rel = restProps.rel
180
173
  }
181
174
 
182
- const href = ("href" in restProps ? restProps.href : computedHref) || "";
175
+ const href = ("href" in restProps ? restProps.href : computedHref) || ""
183
176
 
184
- const InternalComponent = (internalComponent ||
185
- defaultComponent) as ComponentType<LinkProps>;
186
- const ExternalComponent = (externalComponent ||
187
- defaultComponent) as ComponentType<LinkProps>;
177
+ const InternalComponent = (internalComponent || defaultComponent) as ComponentType<LinkProps>
178
+ const ExternalComponent = (externalComponent || defaultComponent) as ComponentType<LinkProps>
188
179
  const Component = href
189
180
  ? isInternalURL(href)
190
181
  ? InternalComponent
191
182
  : ExternalComponent
192
- : InternalComponent;
183
+ : InternalComponent
193
184
 
194
185
  return (
195
186
  <Component ref={ref} {...attrs} {...restProps} href={href} rel={rel}>
196
187
  {"children" in props ? children : field?.text}
197
188
  </Component>
198
- );
189
+ )
199
190
  }) as <
200
191
  InternalComponentProps = ComponentProps<typeof defaultComponent>,
201
192
  ExternalComponentProps = ComponentProps<typeof defaultComponent>,
202
193
  >(
203
194
  props: PrismicLinkProps<InternalComponentProps, ExternalComponentProps> & {
204
- ref?: ForwardedRef<Element>;
195
+ ref?: ForwardedRef<Element>
205
196
  },
206
- ) => ReactNode;
197
+ ) => ReactNode
207
198
 
208
199
  /**
209
200
  * Determines if a URL is internal or external.
210
201
  *
211
202
  * @param url - The URL to check if internal or external.
212
- *
213
203
  * @returns `true` if `url` is internal, `false` otherwise.
214
204
  */
215
205
  // TODO: This does not detect all relative URLs as internal such as `about` or `./about`. This function assumes relative URLs start with a "/" or "#"`.
216
206
  export function isInternalURL(url: string): boolean {
217
- const isInternal = /^(\/(?!\/)|#)/.test(url);
218
- const isSpecialLink = !isInternal && !/^https?:\/\//.test(url);
207
+ const isInternal = /^(\/(?!\/)|#)/.test(url)
208
+ const isSpecialLink = !isInternal && !/^https?:\/\//.test(url)
219
209
 
220
- return isInternal && !isSpecialLink;
210
+ return isInternal && !isSpecialLink
221
211
  }
@@ -1,19 +1,17 @@
1
- import { type LinkProps, PrismicLink } from "./PrismicLink.js";
2
- import { devMsg } from "./lib/devMsg.js";
3
1
  import {
4
2
  isFilled,
5
3
  type LinkResolverFunction,
6
4
  type RichTextField,
7
5
  type RTAnyNode,
8
- } from "@prismicio/client";
6
+ } from "@prismicio/client"
9
7
  import {
10
8
  composeSerializers,
11
9
  serialize,
12
10
  wrapMapSerializer,
13
11
  type RichTextFunctionSerializer,
14
12
  type RichTextMapSerializer,
15
- } from "@prismicio/client/richtext";
16
- import { DEV } from "esm-env";
13
+ } from "@prismicio/client/richtext"
14
+ import { DEV } from "esm-env"
17
15
  import {
18
16
  cloneElement,
19
17
  type ComponentType,
@@ -21,110 +19,108 @@ import {
21
19
  Fragment,
22
20
  isValidElement,
23
21
  type ReactNode,
24
- } from "react";
22
+ } from "react"
23
+
24
+ import { devMsg } from "./lib/devMsg.js"
25
+ import { type LinkProps, PrismicLink } from "./PrismicLink.js"
25
26
 
26
27
  /**
27
- * A function mapping rich text block types to React Components. It is used to
28
- * render rich text fields.
28
+ * A function mapping rich text block types to React Components. It is used to render rich text
29
+ * fields.
29
30
  *
30
31
  * @see Templating rich text fields {@link https://prismic.io/docs/fields/rich-text}
31
32
  */
32
- export type JSXFunctionSerializer = RichTextFunctionSerializer<ReactNode>;
33
+ export type JSXFunctionSerializer = RichTextFunctionSerializer<ReactNode>
33
34
 
34
35
  /**
35
- * A map of rich text block types to React Components. It is used to render rich
36
- * text fields.
36
+ * A map of rich text block types to React Components. It is used to render rich text fields.
37
37
  *
38
38
  * @see Templating rich text fields {@link https://prismic.io/docs/fields/rich-text}
39
39
  */
40
- export type RichTextComponents = RichTextMapSerializer<ReactNode>;
40
+ export type RichTextComponents = RichTextMapSerializer<ReactNode>
41
41
 
42
- /**
43
- * @deprecated Use `RichTextComponents` instead.
44
- */
45
- export type JSXMapSerializer = RichTextComponents;
42
+ /** @deprecated Use `RichTextComponents` instead. */
43
+ export type JSXMapSerializer = RichTextComponents
46
44
 
47
45
  /** Props for `<PrismicRichText>`. */
48
46
  export type PrismicRichTextProps = {
49
47
  /** The Prismic rich text field to render. */
50
- field: RichTextField | null | undefined;
48
+ field: RichTextField | null | undefined
51
49
 
52
50
  /**
53
51
  * The link resolver used to resolve links.
54
52
  *
55
53
  * @remarks
56
- * If your app uses route resolvers when querying for your Prismic
57
- * repository's content, a link resolver does not need to be provided.
58
- *
54
+ * If your app uses route resolvers when querying for your Prismic repository's content, a link
55
+ * resolver does not need to be provided.
59
56
  * @see Learn about link resolvers and route resolvers {@link https://prismic.io/docs/routes}
60
57
  */
61
- linkResolver?: LinkResolverFunction;
58
+ linkResolver?: LinkResolverFunction
62
59
 
63
60
  /**
64
61
  * A map or function that maps a rich text block to a React component.
65
62
  *
66
63
  * @remarks
67
- * Prefer using a map serializer over the function serializer when possible.
68
- * The map serializer is simpler to maintain.
69
- *
70
- * @example A map serializer.
64
+ * Prefer using a map serializer over the function serializer when possible. The map serializer
65
+ * is simpler to maintain.
66
+ * @example
67
+ * A map serializer.
71
68
  *
72
- * ```jsx
73
- * {
74
- * heading1: ({children}) => <Heading>{children}</Heading>
75
- * }
76
- * ```
69
+ * ```jsx
70
+ * {
71
+ * heading1: ({children}) => <Heading>{children}</Heading>
72
+ * }
73
+ * ```
77
74
  *
78
- * @example A function serializer.
75
+ * @example
76
+ * A function serializer.
79
77
  *
80
- * ```jsx
81
- * (type, node, content, children) => {
78
+ * ```jsx
79
+ * (type, node, content, children) => {
82
80
  * switch (type) {
83
- * case "heading1": {
84
- * return <Heading>{children}</Heading>;
85
- * }
81
+ * case "heading1": {
82
+ * return <Heading>{children}</Heading>;
86
83
  * }
87
- * };
88
- * ```
84
+ * }
85
+ * };
86
+ * ```
89
87
  */
90
- components?: RichTextComponents | JSXFunctionSerializer;
88
+ components?: RichTextComponents | JSXFunctionSerializer
91
89
 
92
90
  /**
93
91
  * The React component rendered for links when the URL is internal.
94
92
  *
95
93
  * @defaultValue `<a>`
96
94
  */
97
- internalLinkComponent?: ComponentType<LinkProps>;
95
+ internalLinkComponent?: ComponentType<LinkProps>
98
96
 
99
97
  /**
100
98
  * The React component rendered for links when the URL is external.
101
99
  *
102
100
  * @defaultValue `<a>`
103
101
  */
104
- externalLinkComponent?: ComponentType<LinkProps>;
102
+ externalLinkComponent?: ComponentType<LinkProps>
105
103
 
106
104
  /**
107
- * The value to be rendered when the field is empty. If a fallback is not
108
- * given, `null` will be rendered.
105
+ * The value to be rendered when the field is empty. If a fallback is not given, `null` will be
106
+ * rendered.
109
107
  */
110
- fallback?: ReactNode;
111
- };
108
+ fallback?: ReactNode
109
+ }
112
110
 
113
111
  type CreateDefaultSerializerArgs = {
114
- linkResolver: LinkResolverFunction | undefined;
115
- internalLinkComponent?: ComponentType<LinkProps>;
116
- externalLinkComponent?: ComponentType<LinkProps>;
117
- };
112
+ linkResolver: LinkResolverFunction | undefined
113
+ internalLinkComponent?: ComponentType<LinkProps>
114
+ externalLinkComponent?: ComponentType<LinkProps>
115
+ }
118
116
 
119
117
  const getDir = (node: RTAnyNode): "rtl" | undefined => {
120
118
  if ("direction" in node && node.direction === "rtl") {
121
- return "rtl";
119
+ return "rtl"
122
120
  }
123
- };
121
+ }
124
122
 
125
- const createDefaultSerializer = (
126
- args: CreateDefaultSerializerArgs,
127
- ): JSXFunctionSerializer =>
123
+ const createDefaultSerializer = (args: CreateDefaultSerializerArgs): JSXFunctionSerializer =>
128
124
  wrapMapSerializer<ReactNode>({
129
125
  heading1: ({ node, children, key }) => (
130
126
  <h1 key={key} dir={getDir(node)}>
@@ -183,7 +179,7 @@ const createDefaultSerializer = (
183
179
  alt={node.alt ?? undefined}
184
180
  data-copyright={node.copyright ? node.copyright : undefined}
185
181
  />
186
- );
182
+ )
187
183
 
188
184
  return (
189
185
  <p key={key} className="block-img">
@@ -200,7 +196,7 @@ const createDefaultSerializer = (
200
196
  img
201
197
  )}
202
198
  </p>
203
- );
199
+ )
204
200
  },
205
201
  embed: ({ node, key }) => (
206
202
  <div
@@ -228,31 +224,30 @@ const createDefaultSerializer = (
228
224
  </span>
229
225
  ),
230
226
  span: ({ text, key }) => {
231
- const result: ReactNode[] = [];
227
+ const result: ReactNode[] = []
232
228
 
233
- let i = 0;
229
+ let i = 0
234
230
  for (const line of text.split("\n")) {
235
231
  if (i > 0) {
236
- result.push(<br key={`${i}__break`} />);
232
+ result.push(<br key={`${i}__break`} />)
237
233
  }
238
234
 
239
- result.push(<Fragment key={`${i}__line`}>{line}</Fragment>);
235
+ result.push(<Fragment key={`${i}__line`}>{line}</Fragment>)
240
236
 
241
- i++;
237
+ i++
242
238
  }
243
239
 
244
- return <Fragment key={key}>{result}</Fragment>;
240
+ return <Fragment key={key}>{result}</Fragment>
245
241
  },
246
- });
242
+ })
247
243
 
248
244
  /**
249
245
  * Renders content from a Prismic rich text field as React components.
250
246
  *
251
247
  * @example
252
- *
253
- * ```tsx
254
- * <PrismicRichText field={slice.primary.text} />;
255
- * ```
248
+ * ;```tsx
249
+ * <PrismicRichText field={slice.primary.text} />;
250
+ * ```
256
251
  *
257
252
  * @see Learn how to style rich text, use custom components, and use labels for custom formatting: {@link https://prismic.io/docs/fields/rich-text}
258
253
  */
@@ -265,7 +260,7 @@ export const PrismicRichText: FC<PrismicRichTextProps> = (props) => {
265
260
  externalLinkComponent,
266
261
  internalLinkComponent,
267
262
  ...restProps
268
- } = props;
263
+ } = props
269
264
 
270
265
  if (DEV) {
271
266
  if ("className" in restProps) {
@@ -274,12 +269,12 @@ export const PrismicRichText: FC<PrismicRichTextProps> = (props) => {
274
269
  "classname-is-not-a-valid-prop",
275
270
  )}.`,
276
271
  field,
277
- );
272
+ )
278
273
  }
279
274
  }
280
275
 
281
276
  if (!isFilled.richText(field)) {
282
- return fallback != null ? <>{fallback}</> : null;
277
+ return fallback != null ? <>{fallback}</> : null
283
278
  }
284
279
 
285
280
  const serializer = composeSerializers<ReactNode>(
@@ -289,27 +284,24 @@ export const PrismicRichText: FC<PrismicRichTextProps> = (props) => {
289
284
  internalLinkComponent,
290
285
  externalLinkComponent,
291
286
  }),
292
- );
287
+ )
293
288
 
294
289
  // The serializer is wrapped in a higher-order function that
295
290
  // automatically applies a key to React Elements if one is not already
296
291
  // given.
297
- const serialized = serialize<ReactNode>(
298
- field,
299
- (type, node, text, children, key) => {
300
- const result = serializer(type, node, text, children, key);
292
+ const serialized = serialize<ReactNode>(field, (type, node, text, children, key) => {
293
+ const result = serializer(type, node, text, children, key)
301
294
 
302
- if (isValidElement(result) && result.key == null) {
303
- return cloneElement(result, { key });
304
- } else {
305
- return result;
306
- }
307
- },
308
- );
295
+ if (isValidElement(result) && result.key == null) {
296
+ return cloneElement(result, { key })
297
+ } else {
298
+ return result
299
+ }
300
+ })
309
301
 
310
302
  if (!serialized) {
311
- return fallback != null ? <>{fallback}</> : null;
303
+ return fallback != null ? <>{fallback}</> : null
312
304
  }
313
305
 
314
- return <>{serialized}</>;
315
- };
306
+ return <>{serialized}</>
307
+ }