@prismicio/react 3.4.0-pr.257.68f2f80 → 3.4.0-pr.257.71ee28e
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.d.ts +22 -17
- package/dist/PrismicImage.d.ts.map +1 -1
- package/dist/PrismicImage.js +5 -4
- package/dist/PrismicImage.js.map +1 -1
- package/dist/PrismicLink.d.ts +19 -17
- package/dist/PrismicLink.d.ts.map +1 -1
- package/dist/PrismicLink.js +6 -4
- package/dist/PrismicLink.js.map +1 -1
- package/dist/PrismicRichText.d.ts +34 -30
- package/dist/PrismicRichText.d.ts.map +1 -1
- package/dist/PrismicRichText.js +5 -4
- package/dist/PrismicRichText.js.map +1 -1
- package/dist/PrismicTable.d.ts +14 -14
- package/dist/PrismicTable.d.ts.map +1 -1
- package/dist/PrismicTable.js +4 -3
- package/dist/PrismicTable.js.map +1 -1
- package/dist/PrismicText.d.ts +7 -6
- package/dist/PrismicText.d.ts.map +1 -1
- package/dist/PrismicText.js +4 -3
- package/dist/PrismicText.js.map +1 -1
- package/dist/PrismicToolbar.d.ts +14 -11
- package/dist/PrismicToolbar.d.ts.map +1 -1
- package/dist/PrismicToolbar.js +5 -4
- package/dist/PrismicToolbar.js.map +1 -1
- package/dist/SliceZone.d.ts +40 -27
- package/dist/SliceZone.d.ts.map +1 -1
- package/dist/SliceZone.js +8 -6
- package/dist/SliceZone.js.map +1 -1
- package/dist/lib/devMsg.js +8 -6
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/package.js +1 -1
- package/package.json +28 -26
- package/src/PrismicImage.tsx +122 -95
- package/src/PrismicLink.tsx +38 -28
- package/src/PrismicRichText.tsx +49 -41
- package/src/PrismicTable.tsx +19 -15
- package/src/PrismicText.tsx +7 -6
- package/src/PrismicToolbar.tsx +16 -12
- package/src/SliceZone.tsx +76 -36
- package/src/lib/devMsg.ts +8 -6
package/src/PrismicImage.tsx
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ForwardedRef,
|
|
3
|
+
forwardRef,
|
|
4
|
+
type ComponentProps,
|
|
5
|
+
type FC,
|
|
6
|
+
type ReactNode,
|
|
7
|
+
} from "react";
|
|
1
8
|
import {
|
|
2
9
|
type ImageFieldImage,
|
|
3
10
|
asImagePixelDensitySrcSet,
|
|
@@ -5,15 +12,23 @@ import {
|
|
|
5
12
|
isFilled,
|
|
6
13
|
} from "@prismicio/client";
|
|
7
14
|
import { DEV } from "esm-env";
|
|
8
|
-
import { type ForwardedRef, forwardRef, type ComponentProps, type FC, type ReactNode } from "react";
|
|
9
15
|
|
|
10
16
|
import { devMsg } from "./lib/devMsg.js";
|
|
11
17
|
|
|
12
|
-
type ImgixURLParams = Omit<
|
|
13
|
-
|
|
18
|
+
type ImgixURLParams = Omit<
|
|
19
|
+
NonNullable<Parameters<typeof asImageWidthSrcSet>[1]>,
|
|
20
|
+
"widths"
|
|
21
|
+
> &
|
|
22
|
+
Omit<
|
|
23
|
+
NonNullable<Parameters<typeof asImagePixelDensitySrcSet>[1]>,
|
|
24
|
+
"pixelDensities"
|
|
25
|
+
>;
|
|
14
26
|
|
|
15
27
|
/** Props for `<PrismicImage>`. */
|
|
16
|
-
export type PrismicImageProps = Omit<
|
|
28
|
+
export type PrismicImageProps = Omit<
|
|
29
|
+
ComponentProps<"img">,
|
|
30
|
+
"src" | "srcset" | "alt"
|
|
31
|
+
> & {
|
|
17
32
|
/** The Prismic image field or thumbnail to render. */
|
|
18
33
|
field: ImageFieldImage | null | undefined;
|
|
19
34
|
|
|
@@ -27,35 +42,41 @@ export type PrismicImageProps = Omit<ComponentProps<"img">, "src" | "srcset" | "
|
|
|
27
42
|
/**
|
|
28
43
|
* Declare an image as decorative by providing `alt=""`.
|
|
29
44
|
*
|
|
30
|
-
* See:
|
|
45
|
+
* See:
|
|
46
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
31
47
|
*/
|
|
32
48
|
alt?: "";
|
|
33
49
|
|
|
34
50
|
/**
|
|
35
|
-
* Declare an image as decorative only if the image field does not have
|
|
36
|
-
* providing `fallbackAlt=""`.
|
|
51
|
+
* Declare an image as decorative only if the image field does not have
|
|
52
|
+
* alternative text by providing `fallbackAlt=""`.
|
|
37
53
|
*
|
|
38
|
-
* See:
|
|
54
|
+
* See:
|
|
55
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
39
56
|
*/
|
|
40
57
|
fallbackAlt?: "";
|
|
41
58
|
|
|
42
59
|
/**
|
|
43
|
-
* The value to be rendered when the field is empty. If a fallback is not
|
|
44
|
-
* rendered.
|
|
60
|
+
* The value to be rendered when the field is empty. If a fallback is not
|
|
61
|
+
* given, `null` will be rendered.
|
|
45
62
|
*/
|
|
46
63
|
fallback?: ReactNode;
|
|
47
64
|
} & (
|
|
48
65
|
| {
|
|
49
66
|
/**
|
|
50
|
-
* Widths (in pixels) used to build a `srcset` value for the image
|
|
67
|
+
* Widths (in pixels) used to build a `srcset` value for the image
|
|
68
|
+
* field.
|
|
51
69
|
*
|
|
52
|
-
* If omitted or set to `"defaults"`, the following widths will be used:
|
|
53
|
-
* 1080, 1200, 1920, 2048, 3840.
|
|
70
|
+
* If omitted or set to `"defaults"`, the following widths will be used:
|
|
71
|
+
* 640, 750, 828, 1080, 1200, 1920, 2048, 3840.
|
|
54
72
|
*
|
|
55
|
-
* If the image field contains responsive views, each responsive view
|
|
56
|
-
* in the resulting `srcset` by passing
|
|
73
|
+
* If the image field contains responsive views, each responsive view
|
|
74
|
+
* can be used as a width in the resulting `srcset` by passing
|
|
75
|
+
* `"thumbnails"` as the `widths` prop.
|
|
57
76
|
*/
|
|
58
|
-
widths?:
|
|
77
|
+
widths?:
|
|
78
|
+
| NonNullable<Parameters<typeof asImageWidthSrcSet>[1]>["widths"]
|
|
79
|
+
| "defaults";
|
|
59
80
|
/** Not used when the `widths` prop is used. */
|
|
60
81
|
pixelDensities?: never;
|
|
61
82
|
}
|
|
@@ -65,11 +86,13 @@ export type PrismicImageProps = Omit<ComponentProps<"img">, "src" | "srcset" | "
|
|
|
65
86
|
/**
|
|
66
87
|
* Pixel densities used to build a `srcset` value for the image field.
|
|
67
88
|
*
|
|
68
|
-
* If a `pixelDensities` prop is passed `"defaults"`, the following
|
|
69
|
-
* used: 1, 2, 3.
|
|
89
|
+
* If a `pixelDensities` prop is passed `"defaults"`, the following
|
|
90
|
+
* pixel densities will be used: 1, 2, 3.
|
|
70
91
|
*/
|
|
71
92
|
pixelDensities:
|
|
72
|
-
| NonNullable<
|
|
93
|
+
| NonNullable<
|
|
94
|
+
Parameters<typeof asImagePixelDensitySrcSet>[1]
|
|
95
|
+
>["pixelDensities"]
|
|
73
96
|
| "defaults";
|
|
74
97
|
}
|
|
75
98
|
);
|
|
@@ -78,90 +101,94 @@ export type PrismicImageProps = Omit<ComponentProps<"img">, "src" | "srcset" | "
|
|
|
78
101
|
* Renders an optimized image from a Prismic image field.
|
|
79
102
|
*
|
|
80
103
|
* @example
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
104
|
+
*
|
|
105
|
+
* ```tsx
|
|
106
|
+
* <PrismicImage field={slice.primary.photo} />;
|
|
107
|
+
* ```
|
|
84
108
|
*
|
|
85
109
|
* @see Learn how to optimize images with imgix, use responsive images, and use framework-specific image components: {@link https://prismic.io/docs/fields/image}
|
|
86
110
|
*/
|
|
87
|
-
export const PrismicImage: FC<PrismicImageProps> = forwardRef(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
"alt
|
|
107
|
-
|
|
108
|
-
|
|
111
|
+
export const PrismicImage: FC<PrismicImageProps> = forwardRef(
|
|
112
|
+
function PrismicImage(
|
|
113
|
+
props: PrismicImageProps,
|
|
114
|
+
ref: ForwardedRef<HTMLImageElement>,
|
|
115
|
+
) {
|
|
116
|
+
const {
|
|
117
|
+
field,
|
|
118
|
+
alt,
|
|
119
|
+
fallbackAlt,
|
|
120
|
+
imgixParams = {},
|
|
121
|
+
widths,
|
|
122
|
+
pixelDensities,
|
|
123
|
+
fallback,
|
|
124
|
+
...restProps
|
|
125
|
+
} = props;
|
|
126
|
+
|
|
127
|
+
if (DEV) {
|
|
128
|
+
if (typeof alt === "string" && alt !== "") {
|
|
129
|
+
console.warn(
|
|
130
|
+
`[PrismicImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="". For more details, see ${devMsg(
|
|
131
|
+
"alt-must-be-an-empty-string",
|
|
132
|
+
)}`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
|
|
137
|
+
console.warn(
|
|
138
|
+
`[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="". For more details, see ${devMsg(
|
|
139
|
+
"alt-must-be-an-empty-string",
|
|
140
|
+
)}`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (widths && pixelDensities) {
|
|
145
|
+
console.warn(
|
|
146
|
+
`[PrismicImage] Only one of "widths" or "pixelDensities" props can be provided. You can resolve this warning by removing either the "widths" or "pixelDensities" prop. "widths" will be used in this case.`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
109
149
|
}
|
|
110
150
|
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
`[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="". For more details, see ${devMsg(
|
|
114
|
-
"alt-must-be-an-empty-string",
|
|
115
|
-
)}`,
|
|
116
|
-
);
|
|
151
|
+
if (!isFilled.imageThumbnail(field)) {
|
|
152
|
+
return <>{fallback}</>;
|
|
117
153
|
}
|
|
118
154
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
155
|
+
const resolvedImgixParams = imgixParams;
|
|
156
|
+
for (const x in imgixParams) {
|
|
157
|
+
if (resolvedImgixParams[x as keyof typeof resolvedImgixParams] === null) {
|
|
158
|
+
resolvedImgixParams[x as keyof typeof resolvedImgixParams] = undefined;
|
|
159
|
+
}
|
|
123
160
|
}
|
|
124
|
-
}
|
|
125
161
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
162
|
+
let src: string | undefined;
|
|
163
|
+
let srcSet: string | undefined;
|
|
164
|
+
|
|
165
|
+
if (widths || !pixelDensities) {
|
|
166
|
+
const res = asImageWidthSrcSet(field, {
|
|
167
|
+
...resolvedImgixParams,
|
|
168
|
+
widths: widths === "defaults" ? undefined : widths,
|
|
169
|
+
} as ImgixURLParams);
|
|
129
170
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (
|
|
133
|
-
|
|
171
|
+
src = res.src;
|
|
172
|
+
srcSet = res.srcset;
|
|
173
|
+
} else if (pixelDensities) {
|
|
174
|
+
const res = asImagePixelDensitySrcSet(field, {
|
|
175
|
+
...resolvedImgixParams,
|
|
176
|
+
pixelDensities:
|
|
177
|
+
pixelDensities === "defaults" ? undefined : pixelDensities,
|
|
178
|
+
} as ImgixURLParams);
|
|
179
|
+
|
|
180
|
+
src = res.src;
|
|
181
|
+
srcSet = res.srcset;
|
|
134
182
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
srcSet = res.srcset;
|
|
148
|
-
} else if (pixelDensities) {
|
|
149
|
-
const res = asImagePixelDensitySrcSet(field, {
|
|
150
|
-
...resolvedImgixParams,
|
|
151
|
-
pixelDensities: pixelDensities === "defaults" ? undefined : pixelDensities,
|
|
152
|
-
} as ImgixURLParams);
|
|
153
|
-
|
|
154
|
-
src = res.src;
|
|
155
|
-
srcSet = res.srcset;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return (
|
|
159
|
-
<img
|
|
160
|
-
ref={ref}
|
|
161
|
-
src={src}
|
|
162
|
-
srcSet={srcSet}
|
|
163
|
-
alt={alt ?? (field.alt || fallbackAlt)}
|
|
164
|
-
{...restProps}
|
|
165
|
-
/>
|
|
166
|
-
);
|
|
167
|
-
});
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<img
|
|
186
|
+
ref={ref}
|
|
187
|
+
src={src}
|
|
188
|
+
srcSet={srcSet}
|
|
189
|
+
alt={alt ?? (field.alt || fallbackAlt)}
|
|
190
|
+
{...restProps}
|
|
191
|
+
/>
|
|
192
|
+
);
|
|
193
|
+
},
|
|
194
|
+
);
|
package/src/PrismicLink.tsx
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
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";
|
|
9
1
|
import {
|
|
10
2
|
type ComponentProps,
|
|
11
3
|
type ComponentType,
|
|
@@ -14,6 +6,14 @@ import {
|
|
|
14
6
|
type ReactNode,
|
|
15
7
|
forwardRef,
|
|
16
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";
|
|
17
17
|
|
|
18
18
|
import { devMsg } from "./lib/devMsg.js";
|
|
19
19
|
|
|
@@ -26,14 +26,14 @@ export interface LinkProps {
|
|
|
26
26
|
href: string;
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* The `target` attribute for anchor elements. If the Prismic field is
|
|
30
|
-
* window, this prop defaults to `_blank`.
|
|
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`.
|
|
31
31
|
*/
|
|
32
32
|
target?: HTMLAttributeAnchorTarget;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
* The `rel` attribute for anchor elements. If the `target` prop is set to
|
|
36
|
-
* defaults to `"noopener noreferrer"`.
|
|
35
|
+
* The `rel` attribute for anchor elements. If the `target` prop is set to
|
|
36
|
+
* `"_blank"`, this prop defaults to `"noopener noreferrer"`.
|
|
37
37
|
*/
|
|
38
38
|
rel?: string;
|
|
39
39
|
|
|
@@ -44,11 +44,14 @@ export interface LinkProps {
|
|
|
44
44
|
export type PrismicLinkProps<
|
|
45
45
|
InternalComponentProps = ComponentProps<typeof defaultComponent>,
|
|
46
46
|
ExternalComponentProps = ComponentProps<typeof defaultComponent>,
|
|
47
|
-
> = Omit<
|
|
47
|
+
> = Omit<
|
|
48
|
+
InternalComponentProps & ExternalComponentProps,
|
|
49
|
+
"rel" | "href" | "children"
|
|
50
|
+
> & {
|
|
48
51
|
/**
|
|
49
|
-
* The `rel` attribute for the link. By default, `"noreferrer"` is provided if
|
|
50
|
-
* external. This prop can be provided a function to use the
|
|
51
|
-
* `rel` value.
|
|
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.
|
|
52
55
|
*/
|
|
53
56
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
54
57
|
|
|
@@ -56,8 +59,9 @@ export type PrismicLinkProps<
|
|
|
56
59
|
* The link resolver used to resolve links.
|
|
57
60
|
*
|
|
58
61
|
* @remarks
|
|
59
|
-
*
|
|
60
|
-
*
|
|
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
|
+
*
|
|
61
65
|
* @see Learn about link resolvers and route resolvers {@link https://prismic.io/docs/routes}
|
|
62
66
|
*/
|
|
63
67
|
linkResolver?: LinkResolverFunction;
|
|
@@ -65,8 +69,8 @@ export type PrismicLinkProps<
|
|
|
65
69
|
/**
|
|
66
70
|
* The component rendered for internal URLs. Defaults to `<a>`.
|
|
67
71
|
*
|
|
68
|
-
* If your app uses a client-side router that requires a special Link
|
|
69
|
-
* component to this prop.
|
|
72
|
+
* If your app uses a client-side router that requires a special Link
|
|
73
|
+
* component, provide the Link component to this prop.
|
|
70
74
|
*/
|
|
71
75
|
internalComponent?: ComponentType<InternalComponentProps>;
|
|
72
76
|
|
|
@@ -74,8 +78,8 @@ export type PrismicLinkProps<
|
|
|
74
78
|
externalComponent?: ComponentType<ExternalComponentProps>;
|
|
75
79
|
|
|
76
80
|
/**
|
|
77
|
-
* The children to render for the link. If no children are provided, the
|
|
78
|
-
* will be used.
|
|
81
|
+
* The children to render for the link. If no children are provided, the
|
|
82
|
+
* link's `text` property will be used.
|
|
79
83
|
*/
|
|
80
84
|
children?: ReactNode;
|
|
81
85
|
} & (
|
|
@@ -100,9 +104,10 @@ export type PrismicLinkProps<
|
|
|
100
104
|
* Renders a link from a Prismic link field or page.
|
|
101
105
|
*
|
|
102
106
|
* @example
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
107
|
+
*
|
|
108
|
+
* ```tsx
|
|
109
|
+
* <PrismicLink field={slice.primary.link}>Click here</PrismicLink>;
|
|
110
|
+
* ```
|
|
106
111
|
*
|
|
107
112
|
* @see Learn how to display links and use variants for styling: {@link https://prismic.io/docs/fields/link}
|
|
108
113
|
*/
|
|
@@ -136,7 +141,9 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
136
141
|
)}`,
|
|
137
142
|
);
|
|
138
143
|
} else if (
|
|
139
|
-
("text" in field
|
|
144
|
+
("text" in field
|
|
145
|
+
? Object.keys(field).length > 2
|
|
146
|
+
: Object.keys(field).length > 1) &&
|
|
140
147
|
!("url" in field || "uid" in field || "id" in field)
|
|
141
148
|
) {
|
|
142
149
|
console.warn(
|
|
@@ -174,8 +181,10 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
174
181
|
|
|
175
182
|
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
176
183
|
|
|
177
|
-
const InternalComponent = (internalComponent ||
|
|
178
|
-
|
|
184
|
+
const InternalComponent = (internalComponent ||
|
|
185
|
+
defaultComponent) as ComponentType<LinkProps>;
|
|
186
|
+
const ExternalComponent = (externalComponent ||
|
|
187
|
+
defaultComponent) as ComponentType<LinkProps>;
|
|
179
188
|
const Component = href
|
|
180
189
|
? isInternalURL(href)
|
|
181
190
|
? InternalComponent
|
|
@@ -200,6 +209,7 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
200
209
|
* Determines if a URL is internal or external.
|
|
201
210
|
*
|
|
202
211
|
* @param url - The URL to check if internal or external.
|
|
212
|
+
*
|
|
203
213
|
* @returns `true` if `url` is internal, `false` otherwise.
|
|
204
214
|
*/
|
|
205
215
|
// TODO: This does not detect all relative URLs as internal such as `about` or `./about`. This function assumes relative URLs start with a "/" or "#"`.
|
package/src/PrismicRichText.tsx
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type LinkProps, PrismicLink } from "./PrismicLink.js";
|
|
2
|
+
import { devMsg } from "./lib/devMsg.js";
|
|
1
3
|
import {
|
|
2
4
|
isFilled,
|
|
3
5
|
type LinkResolverFunction,
|
|
@@ -21,25 +23,25 @@ import {
|
|
|
21
23
|
type ReactNode,
|
|
22
24
|
} from "react";
|
|
23
25
|
|
|
24
|
-
import { devMsg } from "./lib/devMsg.js";
|
|
25
|
-
import { type LinkProps, PrismicLink } from "./PrismicLink.js";
|
|
26
|
-
|
|
27
26
|
/**
|
|
28
|
-
* A function mapping rich text block types to React Components. It is used to
|
|
29
|
-
* fields.
|
|
27
|
+
* A function mapping rich text block types to React Components. It is used to
|
|
28
|
+
* render rich text fields.
|
|
30
29
|
*
|
|
31
30
|
* @see Templating rich text fields {@link https://prismic.io/docs/fields/rich-text}
|
|
32
31
|
*/
|
|
33
32
|
export type JSXFunctionSerializer = RichTextFunctionSerializer<ReactNode>;
|
|
34
33
|
|
|
35
34
|
/**
|
|
36
|
-
* A map of rich text block types to React Components. It is used to render rich
|
|
35
|
+
* A map of rich text block types to React Components. It is used to render rich
|
|
36
|
+
* text fields.
|
|
37
37
|
*
|
|
38
38
|
* @see Templating rich text fields {@link https://prismic.io/docs/fields/rich-text}
|
|
39
39
|
*/
|
|
40
40
|
export type RichTextComponents = RichTextMapSerializer<ReactNode>;
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated Use `RichTextComponents` instead.
|
|
44
|
+
*/
|
|
43
45
|
export type JSXMapSerializer = RichTextComponents;
|
|
44
46
|
|
|
45
47
|
/** Props for `<PrismicRichText>`. */
|
|
@@ -51,8 +53,9 @@ export type PrismicRichTextProps = {
|
|
|
51
53
|
* The link resolver used to resolve links.
|
|
52
54
|
*
|
|
53
55
|
* @remarks
|
|
54
|
-
*
|
|
55
|
-
*
|
|
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
|
+
*
|
|
56
59
|
* @see Learn about link resolvers and route resolvers {@link https://prismic.io/docs/routes}
|
|
57
60
|
*/
|
|
58
61
|
linkResolver?: LinkResolverFunction;
|
|
@@ -61,29 +64,28 @@ export type PrismicRichTextProps = {
|
|
|
61
64
|
* A map or function that maps a rich text block to a React component.
|
|
62
65
|
*
|
|
63
66
|
* @remarks
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* A map serializer.
|
|
67
|
+
* Prefer using a map serializer over the function serializer when possible.
|
|
68
|
+
* The map serializer is simpler to maintain.
|
|
68
69
|
*
|
|
69
|
-
*
|
|
70
|
-
* {
|
|
71
|
-
* heading1: ({children}) => <Heading>{children}</Heading>
|
|
72
|
-
* }
|
|
73
|
-
* ```
|
|
70
|
+
* @example A map serializer.
|
|
74
71
|
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
72
|
+
* ```jsx
|
|
73
|
+
* {
|
|
74
|
+
* heading1: ({children}) => <Heading>{children}</Heading>
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
77
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
78
|
+
* @example A function serializer.
|
|
79
|
+
*
|
|
80
|
+
* ```jsx
|
|
81
|
+
* (type, node, content, children) => {
|
|
80
82
|
* switch (type) {
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
83
|
+
* case "heading1": {
|
|
84
|
+
* return <Heading>{children}</Heading>;
|
|
85
|
+
* }
|
|
84
86
|
* }
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
+
* };
|
|
88
|
+
* ```
|
|
87
89
|
*/
|
|
88
90
|
components?: RichTextComponents | JSXFunctionSerializer;
|
|
89
91
|
|
|
@@ -102,8 +104,8 @@ export type PrismicRichTextProps = {
|
|
|
102
104
|
externalLinkComponent?: ComponentType<LinkProps>;
|
|
103
105
|
|
|
104
106
|
/**
|
|
105
|
-
* The value to be rendered when the field is empty. If a fallback is not
|
|
106
|
-
* rendered.
|
|
107
|
+
* The value to be rendered when the field is empty. If a fallback is not
|
|
108
|
+
* given, `null` will be rendered.
|
|
107
109
|
*/
|
|
108
110
|
fallback?: ReactNode;
|
|
109
111
|
};
|
|
@@ -120,7 +122,9 @@ const getDir = (node: RTAnyNode): "rtl" | undefined => {
|
|
|
120
122
|
}
|
|
121
123
|
};
|
|
122
124
|
|
|
123
|
-
const createDefaultSerializer = (
|
|
125
|
+
const createDefaultSerializer = (
|
|
126
|
+
args: CreateDefaultSerializerArgs,
|
|
127
|
+
): JSXFunctionSerializer =>
|
|
124
128
|
wrapMapSerializer<ReactNode>({
|
|
125
129
|
heading1: ({ node, children, key }) => (
|
|
126
130
|
<h1 key={key} dir={getDir(node)}>
|
|
@@ -245,9 +249,10 @@ const createDefaultSerializer = (args: CreateDefaultSerializerArgs): JSXFunction
|
|
|
245
249
|
* Renders content from a Prismic rich text field as React components.
|
|
246
250
|
*
|
|
247
251
|
* @example
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
252
|
+
*
|
|
253
|
+
* ```tsx
|
|
254
|
+
* <PrismicRichText field={slice.primary.text} />;
|
|
255
|
+
* ```
|
|
251
256
|
*
|
|
252
257
|
* @see Learn how to style rich text, use custom components, and use labels for custom formatting: {@link https://prismic.io/docs/fields/rich-text}
|
|
253
258
|
*/
|
|
@@ -289,15 +294,18 @@ export const PrismicRichText: FC<PrismicRichTextProps> = (props) => {
|
|
|
289
294
|
// The serializer is wrapped in a higher-order function that
|
|
290
295
|
// automatically applies a key to React Elements if one is not already
|
|
291
296
|
// given.
|
|
292
|
-
const serialized = serialize<ReactNode>(
|
|
293
|
-
|
|
297
|
+
const serialized = serialize<ReactNode>(
|
|
298
|
+
field,
|
|
299
|
+
(type, node, text, children, key) => {
|
|
300
|
+
const result = serializer(type, node, text, children, key);
|
|
294
301
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
302
|
+
if (isValidElement(result) && result.key == null) {
|
|
303
|
+
return cloneElement(result, { key });
|
|
304
|
+
} else {
|
|
305
|
+
return result;
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
);
|
|
301
309
|
|
|
302
310
|
if (!serialized) {
|
|
303
311
|
return fallback != null ? <>{fallback}</> : null;
|
package/src/PrismicTable.tsx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ComponentType, FC, ReactNode } from "react";
|
|
1
2
|
import {
|
|
2
3
|
isFilled,
|
|
3
4
|
type TableField,
|
|
@@ -8,7 +9,6 @@ import {
|
|
|
8
9
|
type TableFieldHeaderCell,
|
|
9
10
|
type TableFieldDataCell,
|
|
10
11
|
} from "@prismicio/client";
|
|
11
|
-
import type { ComponentType, FC, ReactNode } from "react";
|
|
12
12
|
|
|
13
13
|
import { type JSXMapSerializer, PrismicRichText } from "./PrismicRichText.js";
|
|
14
14
|
|
|
@@ -41,21 +41,20 @@ export type PrismicTableProps = {
|
|
|
41
41
|
/**
|
|
42
42
|
* An object that maps a table block to a React component.
|
|
43
43
|
*
|
|
44
|
-
* @example
|
|
45
|
-
* A map serializer.
|
|
44
|
+
* @example A map serializer.
|
|
46
45
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
46
|
+
* ```jsx
|
|
47
|
+
* {
|
|
48
|
+
* table: ({children}) => <table>{children}</table>
|
|
49
|
+
* thead: ({children}) => <thead>{children}</thead>
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
53
52
|
*/
|
|
54
53
|
components?: JSXMapSerializer & TableComponents;
|
|
55
54
|
|
|
56
55
|
/**
|
|
57
|
-
* The value to be rendered when the field is empty. If a fallback is not
|
|
58
|
-
* rendered.
|
|
56
|
+
* The value to be rendered when the field is empty. If a fallback is not
|
|
57
|
+
* given, `null` will be rendered.
|
|
59
58
|
*/
|
|
60
59
|
fallback?: ReactNode;
|
|
61
60
|
};
|
|
@@ -64,9 +63,10 @@ export type PrismicTableProps = {
|
|
|
64
63
|
* Renders content from a Prismic table field as React components.
|
|
65
64
|
*
|
|
66
65
|
* @example
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
66
|
+
*
|
|
67
|
+
* ```tsx
|
|
68
|
+
* <PrismicTable field={slice.primary.pricing_table} />;
|
|
69
|
+
* ```
|
|
70
70
|
*
|
|
71
71
|
* @see Learn how to style tables and customize table element components: {@link https://prismic.io/docs/fields/table}
|
|
72
72
|
*/
|
|
@@ -77,7 +77,11 @@ export const PrismicTable: FC<PrismicTableProps> = (props) => {
|
|
|
77
77
|
return fallback;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
const {
|
|
80
|
+
const {
|
|
81
|
+
table: Table,
|
|
82
|
+
thead: Thead,
|
|
83
|
+
tbody: Tbody,
|
|
84
|
+
} = { ...defaultComponents, ...components };
|
|
81
85
|
|
|
82
86
|
return (
|
|
83
87
|
<Table table={field}>
|
package/src/PrismicText.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { FC } from "react";
|
|
1
2
|
import { asText, isFilled, type RichTextField } from "@prismicio/client";
|
|
2
3
|
import { DEV } from "esm-env";
|
|
3
|
-
import type { FC } from "react";
|
|
4
4
|
|
|
5
5
|
import { devMsg } from "./lib/devMsg.js";
|
|
6
6
|
|
|
@@ -10,8 +10,8 @@ export type PrismicTextProps = {
|
|
|
10
10
|
field: RichTextField | null | undefined;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* The string rendered when the field is empty. If a fallback is not given,
|
|
14
|
-
* rendered.
|
|
13
|
+
* The string rendered when the field is empty. If a fallback is not given,
|
|
14
|
+
* `null` will be rendered.
|
|
15
15
|
*/
|
|
16
16
|
fallback?: string;
|
|
17
17
|
|
|
@@ -23,9 +23,10 @@ export type PrismicTextProps = {
|
|
|
23
23
|
* Renders content from a Prismic rich text field as plain text (no HTML).
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* <PrismicText field={slice.primary.text} />;
|
|
29
|
+
* ```
|
|
29
30
|
*
|
|
30
31
|
* @see Learn how to display rich text as plain text or React components: {@link https://prismic.io/docs/fields/rich-text}
|
|
31
32
|
*/
|