@prismicio/react 3.4.0-pr.257.5417830 → 3.4.0-pr.257.68f2f80
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 +17 -22
- package/dist/PrismicImage.d.ts.map +1 -1
- package/dist/PrismicImage.js +4 -5
- package/dist/PrismicImage.js.map +1 -1
- package/dist/PrismicLink.d.ts +17 -19
- package/dist/PrismicLink.d.ts.map +1 -1
- package/dist/PrismicLink.js +4 -6
- package/dist/PrismicLink.js.map +1 -1
- package/dist/PrismicRichText.d.ts +30 -34
- package/dist/PrismicRichText.d.ts.map +1 -1
- package/dist/PrismicRichText.js +4 -5
- 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 +3 -4
- package/dist/PrismicTable.js.map +1 -1
- package/dist/PrismicText.d.ts +6 -7
- package/dist/PrismicText.d.ts.map +1 -1
- package/dist/PrismicText.js +3 -4
- package/dist/PrismicText.js.map +1 -1
- package/dist/PrismicToolbar.d.ts +11 -14
- package/dist/PrismicToolbar.d.ts.map +1 -1
- package/dist/PrismicToolbar.js +4 -5
- package/dist/PrismicToolbar.js.map +1 -1
- package/dist/SliceZone.d.ts +27 -40
- package/dist/SliceZone.d.ts.map +1 -1
- package/dist/SliceZone.js +6 -8
- package/dist/SliceZone.js.map +1 -1
- package/dist/lib/devMsg.js +6 -8
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/package.js +1 -1
- package/package.json +27 -29
- package/src/PrismicImage.tsx +95 -122
- package/src/PrismicLink.tsx +28 -38
- package/src/PrismicRichText.tsx +41 -49
- package/src/PrismicTable.tsx +15 -19
- package/src/PrismicText.tsx +6 -7
- package/src/PrismicToolbar.tsx +12 -16
- package/src/SliceZone.tsx +36 -76
- package/src/lib/devMsg.ts +6 -8
package/src/PrismicImage.tsx
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ForwardedRef,
|
|
3
|
-
forwardRef,
|
|
4
|
-
type ComponentProps,
|
|
5
|
-
type FC,
|
|
6
|
-
type ReactNode,
|
|
7
|
-
} from "react";
|
|
8
1
|
import {
|
|
9
2
|
type ImageFieldImage,
|
|
10
3
|
asImagePixelDensitySrcSet,
|
|
@@ -12,23 +5,15 @@ import {
|
|
|
12
5
|
isFilled,
|
|
13
6
|
} from "@prismicio/client";
|
|
14
7
|
import { DEV } from "esm-env";
|
|
8
|
+
import { type ForwardedRef, forwardRef, type ComponentProps, type FC, type ReactNode } from "react";
|
|
15
9
|
|
|
16
10
|
import { devMsg } from "./lib/devMsg.js";
|
|
17
11
|
|
|
18
|
-
type ImgixURLParams = Omit<
|
|
19
|
-
NonNullable<Parameters<typeof
|
|
20
|
-
"widths"
|
|
21
|
-
> &
|
|
22
|
-
Omit<
|
|
23
|
-
NonNullable<Parameters<typeof asImagePixelDensitySrcSet>[1]>,
|
|
24
|
-
"pixelDensities"
|
|
25
|
-
>;
|
|
12
|
+
type ImgixURLParams = Omit<NonNullable<Parameters<typeof asImageWidthSrcSet>[1]>, "widths"> &
|
|
13
|
+
Omit<NonNullable<Parameters<typeof asImagePixelDensitySrcSet>[1]>, "pixelDensities">;
|
|
26
14
|
|
|
27
15
|
/** Props for `<PrismicImage>`. */
|
|
28
|
-
export type PrismicImageProps = Omit<
|
|
29
|
-
ComponentProps<"img">,
|
|
30
|
-
"src" | "srcset" | "alt"
|
|
31
|
-
> & {
|
|
16
|
+
export type PrismicImageProps = Omit<ComponentProps<"img">, "src" | "srcset" | "alt"> & {
|
|
32
17
|
/** The Prismic image field or thumbnail to render. */
|
|
33
18
|
field: ImageFieldImage | null | undefined;
|
|
34
19
|
|
|
@@ -42,41 +27,35 @@ export type PrismicImageProps = Omit<
|
|
|
42
27
|
/**
|
|
43
28
|
* Declare an image as decorative by providing `alt=""`.
|
|
44
29
|
*
|
|
45
|
-
* See:
|
|
46
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
30
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
47
31
|
*/
|
|
48
32
|
alt?: "";
|
|
49
33
|
|
|
50
34
|
/**
|
|
51
|
-
* Declare an image as decorative only if the image field does not have
|
|
52
|
-
*
|
|
35
|
+
* Declare an image as decorative only if the image field does not have alternative text by
|
|
36
|
+
* providing `fallbackAlt=""`.
|
|
53
37
|
*
|
|
54
|
-
* See:
|
|
55
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
38
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
56
39
|
*/
|
|
57
40
|
fallbackAlt?: "";
|
|
58
41
|
|
|
59
42
|
/**
|
|
60
|
-
* The value to be rendered when the field is empty. If a fallback is not
|
|
61
|
-
*
|
|
43
|
+
* The value to be rendered when the field is empty. If a fallback is not given, `null` will be
|
|
44
|
+
* rendered.
|
|
62
45
|
*/
|
|
63
46
|
fallback?: ReactNode;
|
|
64
47
|
} & (
|
|
65
48
|
| {
|
|
66
49
|
/**
|
|
67
|
-
* Widths (in pixels) used to build a `srcset` value for the image
|
|
68
|
-
* field.
|
|
50
|
+
* Widths (in pixels) used to build a `srcset` value for the image field.
|
|
69
51
|
*
|
|
70
|
-
* If omitted or set to `"defaults"`, the following widths will be used:
|
|
71
|
-
*
|
|
52
|
+
* If omitted or set to `"defaults"`, the following widths will be used: 640, 750, 828,
|
|
53
|
+
* 1080, 1200, 1920, 2048, 3840.
|
|
72
54
|
*
|
|
73
|
-
* If the image field contains responsive views, each responsive view
|
|
74
|
-
*
|
|
75
|
-
* `"thumbnails"` as the `widths` prop.
|
|
55
|
+
* If the image field contains responsive views, each responsive view can be used as a width
|
|
56
|
+
* in the resulting `srcset` by passing `"thumbnails"` as the `widths` prop.
|
|
76
57
|
*/
|
|
77
|
-
widths?:
|
|
78
|
-
| NonNullable<Parameters<typeof asImageWidthSrcSet>[1]>["widths"]
|
|
79
|
-
| "defaults";
|
|
58
|
+
widths?: NonNullable<Parameters<typeof asImageWidthSrcSet>[1]>["widths"] | "defaults";
|
|
80
59
|
/** Not used when the `widths` prop is used. */
|
|
81
60
|
pixelDensities?: never;
|
|
82
61
|
}
|
|
@@ -86,13 +65,11 @@ export type PrismicImageProps = Omit<
|
|
|
86
65
|
/**
|
|
87
66
|
* Pixel densities used to build a `srcset` value for the image field.
|
|
88
67
|
*
|
|
89
|
-
* If a `pixelDensities` prop is passed `"defaults"`, the following
|
|
90
|
-
*
|
|
68
|
+
* If a `pixelDensities` prop is passed `"defaults"`, the following pixel densities will be
|
|
69
|
+
* used: 1, 2, 3.
|
|
91
70
|
*/
|
|
92
71
|
pixelDensities:
|
|
93
|
-
| NonNullable<
|
|
94
|
-
Parameters<typeof asImagePixelDensitySrcSet>[1]
|
|
95
|
-
>["pixelDensities"]
|
|
72
|
+
| NonNullable<Parameters<typeof asImagePixelDensitySrcSet>[1]>["pixelDensities"]
|
|
96
73
|
| "defaults";
|
|
97
74
|
}
|
|
98
75
|
);
|
|
@@ -101,94 +78,90 @@ export type PrismicImageProps = Omit<
|
|
|
101
78
|
* Renders an optimized image from a Prismic image field.
|
|
102
79
|
*
|
|
103
80
|
* @example
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* ```
|
|
81
|
+
* ```tsx
|
|
82
|
+
* <PrismicImage field={slice.primary.photo} />;
|
|
83
|
+
* ```
|
|
108
84
|
*
|
|
109
85
|
* @see Learn how to optimize images with imgix, use responsive images, and use framework-specific image components: {@link https://prismic.io/docs/fields/image}
|
|
110
86
|
*/
|
|
111
|
-
export const PrismicImage: FC<PrismicImageProps> = forwardRef(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
}
|
|
87
|
+
export const PrismicImage: FC<PrismicImageProps> = forwardRef(function PrismicImage(
|
|
88
|
+
props: PrismicImageProps,
|
|
89
|
+
ref: ForwardedRef<HTMLImageElement>,
|
|
90
|
+
) {
|
|
91
|
+
const {
|
|
92
|
+
field,
|
|
93
|
+
alt,
|
|
94
|
+
fallbackAlt,
|
|
95
|
+
imgixParams = {},
|
|
96
|
+
widths,
|
|
97
|
+
pixelDensities,
|
|
98
|
+
fallback,
|
|
99
|
+
...restProps
|
|
100
|
+
} = props;
|
|
101
|
+
|
|
102
|
+
if (DEV) {
|
|
103
|
+
if (typeof alt === "string" && alt !== "") {
|
|
104
|
+
console.warn(
|
|
105
|
+
`[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(
|
|
106
|
+
"alt-must-be-an-empty-string",
|
|
107
|
+
)}`,
|
|
108
|
+
);
|
|
149
109
|
}
|
|
150
110
|
|
|
151
|
-
if (
|
|
152
|
-
|
|
111
|
+
if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
|
|
112
|
+
console.warn(
|
|
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
|
+
);
|
|
153
117
|
}
|
|
154
118
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
119
|
+
if (widths && pixelDensities) {
|
|
120
|
+
console.warn(
|
|
121
|
+
`[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.`,
|
|
122
|
+
);
|
|
160
123
|
}
|
|
124
|
+
}
|
|
161
125
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (widths || !pixelDensities) {
|
|
166
|
-
const res = asImageWidthSrcSet(field, {
|
|
167
|
-
...resolvedImgixParams,
|
|
168
|
-
widths: widths === "defaults" ? undefined : widths,
|
|
169
|
-
} as ImgixURLParams);
|
|
126
|
+
if (!isFilled.imageThumbnail(field)) {
|
|
127
|
+
return <>{fallback}</>;
|
|
128
|
+
}
|
|
170
129
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
...resolvedImgixParams,
|
|
176
|
-
pixelDensities:
|
|
177
|
-
pixelDensities === "defaults" ? undefined : pixelDensities,
|
|
178
|
-
} as ImgixURLParams);
|
|
179
|
-
|
|
180
|
-
src = res.src;
|
|
181
|
-
srcSet = res.srcset;
|
|
130
|
+
const resolvedImgixParams = imgixParams;
|
|
131
|
+
for (const x in imgixParams) {
|
|
132
|
+
if (resolvedImgixParams[x as keyof typeof resolvedImgixParams] === null) {
|
|
133
|
+
resolvedImgixParams[x as keyof typeof resolvedImgixParams] = undefined;
|
|
182
134
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let src: string | undefined;
|
|
138
|
+
let srcSet: string | undefined;
|
|
139
|
+
|
|
140
|
+
if (widths || !pixelDensities) {
|
|
141
|
+
const res = asImageWidthSrcSet(field, {
|
|
142
|
+
...resolvedImgixParams,
|
|
143
|
+
widths: widths === "defaults" ? undefined : widths,
|
|
144
|
+
} as ImgixURLParams);
|
|
145
|
+
|
|
146
|
+
src = res.src;
|
|
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
|
+
});
|
package/src/PrismicLink.tsx
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ComponentProps,
|
|
3
|
-
type ComponentType,
|
|
4
|
-
type ForwardedRef,
|
|
5
|
-
type HTMLAttributeAnchorTarget,
|
|
6
|
-
type ReactNode,
|
|
7
|
-
forwardRef,
|
|
8
|
-
} from "react";
|
|
9
1
|
import {
|
|
10
2
|
type LinkField,
|
|
11
3
|
type LinkResolverFunction,
|
|
@@ -14,6 +6,14 @@ import {
|
|
|
14
6
|
type AsLinkAttrsConfig,
|
|
15
7
|
} from "@prismicio/client";
|
|
16
8
|
import { DEV } from "esm-env";
|
|
9
|
+
import {
|
|
10
|
+
type ComponentProps,
|
|
11
|
+
type ComponentType,
|
|
12
|
+
type ForwardedRef,
|
|
13
|
+
type HTMLAttributeAnchorTarget,
|
|
14
|
+
type ReactNode,
|
|
15
|
+
forwardRef,
|
|
16
|
+
} from "react";
|
|
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
|
-
*
|
|
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
32
|
target?: HTMLAttributeAnchorTarget;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
* The `rel` attribute for anchor elements. If the `target` prop is set to
|
|
36
|
-
*
|
|
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
38
|
rel?: string;
|
|
39
39
|
|
|
@@ -44,14 +44,11 @@ export interface LinkProps {
|
|
|
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
|
-
*
|
|
54
|
-
*
|
|
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
53
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
57
54
|
|
|
@@ -59,9 +56,8 @@ export type PrismicLinkProps<
|
|
|
59
56
|
* The link resolver used to resolve links.
|
|
60
57
|
*
|
|
61
58
|
* @remarks
|
|
62
|
-
*
|
|
63
|
-
*
|
|
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
63
|
linkResolver?: LinkResolverFunction;
|
|
@@ -69,8 +65,8 @@ export type PrismicLinkProps<
|
|
|
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
|
|
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
71
|
internalComponent?: ComponentType<InternalComponentProps>;
|
|
76
72
|
|
|
@@ -78,8 +74,8 @@ export type PrismicLinkProps<
|
|
|
78
74
|
externalComponent?: ComponentType<ExternalComponentProps>;
|
|
79
75
|
|
|
80
76
|
/**
|
|
81
|
-
* The children to render for the link. If no children are provided, the
|
|
82
|
-
*
|
|
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
80
|
children?: ReactNode;
|
|
85
81
|
} & (
|
|
@@ -104,10 +100,9 @@ export type PrismicLinkProps<
|
|
|
104
100
|
* Renders a link from a Prismic link field or page.
|
|
105
101
|
*
|
|
106
102
|
* @example
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
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
|
*/
|
|
@@ -141,9 +136,7 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
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(
|
|
@@ -181,10 +174,8 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
181
174
|
|
|
182
175
|
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
183
176
|
|
|
184
|
-
const InternalComponent = (internalComponent ||
|
|
185
|
-
|
|
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
|
|
@@ -209,7 +200,6 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
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 "#"`.
|
package/src/PrismicRichText.tsx
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
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,
|
|
@@ -23,25 +21,25 @@ import {
|
|
|
23
21
|
type ReactNode,
|
|
24
22
|
} from "react";
|
|
25
23
|
|
|
24
|
+
import { devMsg } from "./lib/devMsg.js";
|
|
25
|
+
import { type LinkProps, PrismicLink } from "./PrismicLink.js";
|
|
26
|
+
|
|
26
27
|
/**
|
|
27
|
-
* A function mapping rich text block types to React Components. It is used to
|
|
28
|
-
*
|
|
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
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
40
|
export type RichTextComponents = RichTextMapSerializer<ReactNode>;
|
|
41
41
|
|
|
42
|
-
/**
|
|
43
|
-
* @deprecated Use `RichTextComponents` instead.
|
|
44
|
-
*/
|
|
42
|
+
/** @deprecated Use `RichTextComponents` instead. */
|
|
45
43
|
export type JSXMapSerializer = RichTextComponents;
|
|
46
44
|
|
|
47
45
|
/** Props for `<PrismicRichText>`. */
|
|
@@ -53,9 +51,8 @@ export type PrismicRichTextProps = {
|
|
|
53
51
|
* The link resolver used to resolve links.
|
|
54
52
|
*
|
|
55
53
|
* @remarks
|
|
56
|
-
*
|
|
57
|
-
*
|
|
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
58
|
linkResolver?: LinkResolverFunction;
|
|
@@ -64,28 +61,29 @@ export type PrismicRichTextProps = {
|
|
|
64
61
|
* A map or function that maps a rich text block to a React component.
|
|
65
62
|
*
|
|
66
63
|
* @remarks
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
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
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
69
|
+
* ```jsx
|
|
70
|
+
* {
|
|
71
|
+
* heading1: ({children}) => <Heading>{children}</Heading>
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
77
74
|
*
|
|
78
|
-
* @example
|
|
75
|
+
* @example
|
|
76
|
+
* A function serializer.
|
|
79
77
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
78
|
+
* ```jsx
|
|
79
|
+
* (type, node, content, children) => {
|
|
82
80
|
* switch (type) {
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
81
|
+
* case "heading1": {
|
|
82
|
+
* return <Heading>{children}</Heading>;
|
|
83
|
+
* }
|
|
86
84
|
* }
|
|
87
|
-
*
|
|
88
|
-
*
|
|
85
|
+
* };
|
|
86
|
+
* ```
|
|
89
87
|
*/
|
|
90
88
|
components?: RichTextComponents | JSXFunctionSerializer;
|
|
91
89
|
|
|
@@ -104,8 +102,8 @@ export type PrismicRichTextProps = {
|
|
|
104
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
|
-
*
|
|
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
108
|
fallback?: ReactNode;
|
|
111
109
|
};
|
|
@@ -122,9 +120,7 @@ const getDir = (node: RTAnyNode): "rtl" | undefined => {
|
|
|
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)}>
|
|
@@ -249,10 +245,9 @@ const createDefaultSerializer = (
|
|
|
249
245
|
* Renders content from a Prismic rich text field as React components.
|
|
250
246
|
*
|
|
251
247
|
* @example
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
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
|
*/
|
|
@@ -294,18 +289,15 @@ export const PrismicRichText: FC<PrismicRichTextProps> = (props) => {
|
|
|
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
|
-
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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
303
|
return fallback != null ? <>{fallback}</> : null;
|
package/src/PrismicTable.tsx
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { ComponentType, FC, ReactNode } from "react";
|
|
2
1
|
import {
|
|
3
2
|
isFilled,
|
|
4
3
|
type TableField,
|
|
@@ -9,6 +8,7 @@ import {
|
|
|
9
8
|
type TableFieldHeaderCell,
|
|
10
9
|
type TableFieldDataCell,
|
|
11
10
|
} 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,20 +41,21 @@ export type PrismicTableProps = {
|
|
|
41
41
|
/**
|
|
42
42
|
* An object that maps a table block to a React component.
|
|
43
43
|
*
|
|
44
|
-
* @example
|
|
44
|
+
* @example
|
|
45
|
+
* A map serializer.
|
|
45
46
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
47
|
+
* ```jsx
|
|
48
|
+
* {
|
|
49
|
+
* table: ({children}) => <table>{children}</table>
|
|
50
|
+
* thead: ({children}) => <thead>{children}</thead>
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
52
53
|
*/
|
|
53
54
|
components?: JSXMapSerializer & TableComponents;
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
|
-
* The value to be rendered when the field is empty. If a fallback is not
|
|
57
|
-
*
|
|
57
|
+
* The value to be rendered when the field is empty. If a fallback is not given, `null` will be
|
|
58
|
+
* rendered.
|
|
58
59
|
*/
|
|
59
60
|
fallback?: ReactNode;
|
|
60
61
|
};
|
|
@@ -63,10 +64,9 @@ export type PrismicTableProps = {
|
|
|
63
64
|
* Renders content from a Prismic table field as React components.
|
|
64
65
|
*
|
|
65
66
|
* @example
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* ```
|
|
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,11 +77,7 @@ export const PrismicTable: FC<PrismicTableProps> = (props) => {
|
|
|
77
77
|
return fallback;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
const {
|
|
81
|
-
table: Table,
|
|
82
|
-
thead: Thead,
|
|
83
|
-
tbody: Tbody,
|
|
84
|
-
} = { ...defaultComponents, ...components };
|
|
80
|
+
const { table: Table, thead: Thead, tbody: Tbody } = { ...defaultComponents, ...components };
|
|
85
81
|
|
|
86
82
|
return (
|
|
87
83
|
<Table table={field}>
|
package/src/PrismicText.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { FC } from "react";
|
|
2
1
|
import { asText, isFilled, type RichTextField } from "@prismicio/client";
|
|
3
2
|
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
|
-
*
|
|
13
|
+
* The string rendered when the field is empty. If a fallback is not given, `null` will be
|
|
14
|
+
* rendered.
|
|
15
15
|
*/
|
|
16
16
|
fallback?: string;
|
|
17
17
|
|
|
@@ -23,10 +23,9 @@ 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
|
-
*
|
|
29
|
-
* ```
|
|
26
|
+
* ```tsx
|
|
27
|
+
* <PrismicText field={slice.primary.text} />;
|
|
28
|
+
* ```
|
|
30
29
|
*
|
|
31
30
|
* @see Learn how to display rich text as plain text or React components: {@link https://prismic.io/docs/fields/rich-text}
|
|
32
31
|
*/
|