@prismicio/react 3.4.1 → 3.4.2-canary.bcaa9a9

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 (45) hide show
  1. package/README.md +6 -0
  2. package/dist/PrismicImage.d.ts +10 -9
  3. package/dist/PrismicImage.d.ts.map +1 -1
  4. package/dist/PrismicImage.js +3 -3
  5. package/dist/PrismicImage.js.map +1 -1
  6. package/dist/PrismicLink.d.ts +6 -7
  7. package/dist/PrismicLink.d.ts.map +1 -1
  8. package/dist/PrismicLink.js +4 -3
  9. package/dist/PrismicLink.js.map +1 -1
  10. package/dist/PrismicRichText.d.ts +8 -9
  11. package/dist/PrismicRichText.d.ts.map +1 -1
  12. package/dist/PrismicRichText.js +1 -1
  13. package/dist/PrismicRichText.js.map +1 -1
  14. package/dist/PrismicTable.d.ts +5 -6
  15. package/dist/PrismicTable.d.ts.map +1 -1
  16. package/dist/PrismicTable.js +1 -1
  17. package/dist/PrismicTable.js.map +1 -1
  18. package/dist/PrismicText.d.ts +7 -7
  19. package/dist/PrismicText.d.ts.map +1 -1
  20. package/dist/PrismicText.js +1 -1
  21. package/dist/PrismicText.js.map +1 -1
  22. package/dist/PrismicToolbar.d.ts +3 -5
  23. package/dist/PrismicToolbar.d.ts.map +1 -1
  24. package/dist/PrismicToolbar.js +5 -3
  25. package/dist/PrismicToolbar.js.map +1 -1
  26. package/dist/SliceBoundary.js +55 -0
  27. package/dist/SliceBoundary.js.map +1 -0
  28. package/dist/SliceZone.d.ts +25 -21
  29. package/dist/SliceZone.d.ts.map +1 -1
  30. package/dist/SliceZone.js +17 -9
  31. package/dist/SliceZone.js.map +1 -1
  32. package/dist/lib/devMsg.js +1 -1
  33. package/dist/lib/devMsg.js.map +1 -1
  34. package/dist/package.js +1 -1
  35. package/package.json +17 -16
  36. package/src/PrismicImage.tsx +34 -34
  37. package/src/PrismicLink.tsx +43 -43
  38. package/src/PrismicRichText.tsx +43 -43
  39. package/src/PrismicTable.tsx +29 -29
  40. package/src/PrismicText.tsx +16 -16
  41. package/src/PrismicToolbar.tsx +32 -30
  42. package/src/SliceBoundary.tsx +77 -0
  43. package/src/SliceZone.tsx +54 -43
  44. package/src/index.ts +15 -15
  45. package/src/lib/devMsg.ts +3 -3
@@ -1,7 +1,7 @@
1
- "use client";
1
+ "use client"
2
2
 
3
- import { getToolbarSrc } from "@prismicio/client";
4
- import { type FC, useEffect, useRef } from "react";
3
+ import { getToolbarSrc } from "@prismicio/client"
4
+ import { type FC, useEffect, useRef } from "react"
5
5
 
6
6
  /** Props for `<PrismicToolbar>`. */
7
7
  export type PrismicToolbarProps = {
@@ -9,7 +9,7 @@ export type PrismicToolbarProps = {
9
9
  * The name of the Prismic repository. For example, `"my-repo"` if the repository URL is
10
10
  * `my-repo.prismic.io`.
11
11
  */
12
- repositoryName: string;
12
+ repositoryName: string
13
13
 
14
14
  /**
15
15
  * Called when the Prismic toolbar triggers a preview update. This happens when the previewed
@@ -19,7 +19,7 @@ export type PrismicToolbarProps = {
19
19
  *
20
20
  * The default page reload behavior can be cancelled with `event.preventDefault()`.
21
21
  */
22
- onPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void;
22
+ onPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void
23
23
 
24
24
  /**
25
25
  * Called when the Prismic toolbar triggers a preview end. This happens when a preview session is
@@ -27,67 +27,69 @@ export type PrismicToolbarProps = {
27
27
  *
28
28
  * The default page reload behavior can be cancelled with `event.preventDefault()`.
29
29
  */
30
- onPreviewEnd?: (event: CustomEvent<null>) => void;
31
- };
30
+ onPreviewEnd?: (event: CustomEvent<null>) => void
31
+ }
32
32
 
33
33
  /**
34
34
  * Renders the Prismic Toolbar script to support draft previews.
35
35
  *
36
36
  * @example
37
- * ```tsx
37
+ * ;```tsx
38
38
  * <PrismicToolbar repositoryName="my-repo" />;
39
39
  * ```
40
40
  *
41
41
  * @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}
42
42
  */
43
43
  export const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {
44
- const { repositoryName, onPreviewUpdate, onPreviewEnd } = props;
44
+ const { repositoryName, onPreviewUpdate, onPreviewEnd } = props
45
45
 
46
- const src = getToolbarSrc(repositoryName);
46
+ const src = getToolbarSrc(repositoryName)
47
47
 
48
- const onPreviewUpdateRef = useRef(onPreviewUpdate);
49
- onPreviewUpdateRef.current = onPreviewUpdate;
48
+ const onPreviewUpdateRef = useRef(onPreviewUpdate)
49
+ const onPreviewEndRef = useRef(onPreviewEnd)
50
50
 
51
- const onPreviewEndRef = useRef(onPreviewEnd);
52
- onPreviewEndRef.current = onPreviewEnd;
51
+ useEffect(() => {
52
+ onPreviewUpdateRef.current = onPreviewUpdate
53
+ onPreviewEndRef.current = onPreviewEnd
54
+ })
53
55
 
54
56
  useEffect(() => {
55
- const existingScript = document.querySelector(`script[src="${src}"]`);
57
+ const existingScript = document.querySelector(`script[src="${src}"]`)
56
58
 
57
59
  if (!existingScript) {
58
- const script = document.createElement("script");
59
- script.src = src;
60
- script.defer = true;
60
+ const script = document.createElement("script")
61
+ script.src = src
62
+ script.defer = true
61
63
 
62
64
  // Used to distinguish the toolbar element from other elements.
63
- script.dataset.prismicToolbar = "";
64
- script.dataset.repositoryName = repositoryName;
65
+ script.dataset.prismicToolbar = ""
66
+ script.dataset.repositoryName = repositoryName
65
67
 
66
68
  // Disable Happy DOM `<script>` evaluation during tests.
67
69
  // @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
68
- script._evaluateScript = false;
70
+ script._evaluateScript = false
69
71
 
70
- document.body.appendChild(script);
72
+ document.body.appendChild(script)
71
73
  }
72
- }, [repositoryName, src]);
74
+ }, [repositoryName, src])
73
75
 
74
76
  useEffect(() => {
75
- const controller = new AbortController();
77
+ const controller = new AbortController()
76
78
 
77
79
  window.addEventListener(
78
80
  "prismicPreviewUpdate",
79
81
  (event) => onPreviewUpdateRef.current?.(event as CustomEvent<{ ref: string }>),
80
82
  { signal: controller.signal },
81
- );
83
+ )
82
84
 
83
85
  window.addEventListener(
84
86
  "prismicPreviewEnd",
85
87
  (event) => onPreviewEndRef.current?.(event as CustomEvent<null>),
86
88
  { signal: controller.signal },
87
- );
89
+ )
88
90
 
89
- return () => controller.abort();
90
- }, []);
91
+ return () => controller.abort()
92
+ }, [])
91
93
 
92
- return null;
93
- };
94
+ return null
95
+ }
@@ -0,0 +1,77 @@
1
+ "use client"
2
+
3
+ import { type ReactNode, useId, useLayoutEffect, useRef, useSyncExternalStore } from "react"
4
+
5
+ type SliceBoundaryProps = {
6
+ children: ReactNode
7
+ sliceId: string
8
+ }
9
+
10
+ export function SliceBoundary(props: SliceBoundaryProps): ReactNode {
11
+ const { children, sliceId } = props
12
+
13
+ return (
14
+ <>
15
+ <CommentMarker value={`prismic-slice-start:${sliceId}`} />
16
+ {children}
17
+ <CommentMarker value={`prismic-slice-end:${sliceId}`} />
18
+ </>
19
+ )
20
+ }
21
+
22
+ type CommentMarkerProps = {
23
+ value: string
24
+ }
25
+
26
+ function CommentMarker(props: CommentMarkerProps) {
27
+ const { value } = props
28
+ const id = useId()
29
+ const isClient = useIsClient()
30
+ const anchorRef = useRef<Text>(null)
31
+
32
+ useLayoutEffect(() => {
33
+ let anchor = anchorRef.current
34
+
35
+ if (!anchor?.isConnected) {
36
+ anchor = findTextNode(id)
37
+ if (!anchor) return
38
+
39
+ anchor.data = ""
40
+ anchorRef.current = anchor
41
+ }
42
+
43
+ const comment = document.createComment(value)
44
+ anchor.after(comment)
45
+
46
+ return () => comment.remove()
47
+ })
48
+
49
+ return isClient ? id : null
50
+ }
51
+
52
+ function useIsClient() {
53
+ return useSyncExternalStore(subscribe, getClientSnapshot, getServerSnapshot)
54
+ }
55
+
56
+ function subscribe() {
57
+ return () => {}
58
+ }
59
+
60
+ function getClientSnapshot() {
61
+ return true
62
+ }
63
+
64
+ function getServerSnapshot() {
65
+ return false
66
+ }
67
+
68
+ function findTextNode(value: string) {
69
+ const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT)
70
+
71
+ while (walker.nextNode()) {
72
+ const node = walker.currentNode
73
+ if (node instanceof Text && node.data === value) return node
74
+ }
75
+
76
+ return null
77
+ }
package/src/SliceZone.tsx CHANGED
@@ -1,6 +1,8 @@
1
- import type { Slice } from "@prismicio/client";
2
- import { DEV } from "esm-env";
3
- import type { ComponentType, FC, ReactNode } from "react";
1
+ import type { Slice } from "@prismicio/client"
2
+ import { DEV } from "esm-env"
3
+ import { Fragment, type ComponentType, type FC, type ReactNode } from "react"
4
+
5
+ import { SliceBoundary } from "./SliceBoundary.js"
4
6
 
5
7
  /**
6
8
  * Returns the type of a `SliceLike` type.
@@ -11,7 +13,7 @@ type ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice
11
13
  ? TSlice["slice_type"]
12
14
  : TSlice extends SliceLikeGraphQL
13
15
  ? TSlice["type"]
14
- : never;
16
+ : never
15
17
 
16
18
  /**
17
19
  * The minimum required properties to represent a Prismic slice from the Prismic Content API for the
@@ -22,7 +24,7 @@ type ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice
22
24
  export type SliceLikeRestV2<TSliceType extends string = string> = Pick<
23
25
  Slice<TSliceType>,
24
26
  "id" | "slice_type"
25
- >;
27
+ >
26
28
 
27
29
  /**
28
30
  * The minimum required properties to represent a Prismic slice from the Prismic GraphQL API for the
@@ -31,8 +33,8 @@ export type SliceLikeRestV2<TSliceType extends string = string> = Pick<
31
33
  * @typeParam SliceType - Type name of the slice.
32
34
  */
33
35
  export type SliceLikeGraphQL<TSliceType extends string = string> = {
34
- type: Slice<TSliceType>["slice_type"];
35
- };
36
+ type: Slice<TSliceType>["slice_type"]
37
+ }
36
38
 
37
39
  /**
38
40
  * The minimum required properties to represent a Prismic slice for the `mapSliceZone()` helper.
@@ -52,8 +54,8 @@ export type SliceLike<TSliceType extends string = string> = (
52
54
  *
53
55
  * @internal
54
56
  */
55
- __mapped?: true;
56
- };
57
+ __mapped?: true
58
+ }
57
59
 
58
60
  /**
59
61
  * A looser version of the `SliceZone` type from `@prismicio/client` using `SliceLike`.
@@ -63,7 +65,7 @@ export type SliceLike<TSliceType extends string = string> = (
63
65
  *
64
66
  * @typeParam TSlice - The type(s) of a slice in the slice zone.
65
67
  */
66
- export type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[];
68
+ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[]
67
69
 
68
70
  /**
69
71
  * React props for a component rendering content from a Prismic slice using the `<SliceZone>`
@@ -75,21 +77,21 @@ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlic
75
77
  */
76
78
  export type SliceComponentProps<TSlice extends SliceLike = SliceLike, TContext = unknown> = {
77
79
  /** Slice data for this component. */
78
- slice: TSlice;
80
+ slice: TSlice
79
81
 
80
82
  /** The index of the slice in the slice zone. */
81
- index: number;
83
+ index: number
82
84
 
83
85
  /** All slices from the slice zone to which the slice belongs. */
84
86
  // TODO: We have to keep this list of slices general due to circular
85
87
  // reference limtiations. If we had another generic to determine the full
86
88
  // union of slice types, it would include TSlice. This causes TypeScript to
87
89
  // throw a compilation error.
88
- slices: SliceZoneLike<TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2>;
90
+ slices: SliceZoneLike<TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2>
89
91
 
90
92
  /** Arbitrary data passed to `<SliceZone>` and made available to all slice components. */
91
- context: TContext;
92
- };
93
+ context: TContext
94
+ }
93
95
 
94
96
  /**
95
97
  * A React component to be rendered for each instance of its slice.
@@ -101,7 +103,7 @@ export type SliceComponentType<
101
103
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
104
  TSlice extends SliceLike = any,
103
105
  TContext = unknown,
104
- > = ComponentType<SliceComponentProps<TSlice, TContext>>;
106
+ > = ComponentType<SliceComponentProps<TSlice, TContext>>
105
107
 
106
108
  /**
107
109
  * A record of slice types mapped to a React component. The component will be rendered for each
@@ -128,8 +130,8 @@ export type SliceZoneComponents<TSlice extends SliceLike = SliceLike, TContext =
128
130
  ? SliceLike
129
131
  : Extract<TSlice, SliceLike<SliceType>>,
130
132
  TContext
131
- >;
132
- };
133
+ >
134
+ }
133
135
 
134
136
  /**
135
137
  * React props for the `<SliceZone>` component.
@@ -139,19 +141,19 @@ export type SliceZoneComponents<TSlice extends SliceLike = SliceLike, TContext =
139
141
  */
140
142
  export type SliceZoneProps<TContext = unknown> = {
141
143
  /** List of slice data from the slice zone. */
142
- slices?: SliceZoneLike;
144
+ slices?: SliceZoneLike
143
145
 
144
146
  /** A record mapping slice types to React components. */
145
147
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
146
- components?: Record<string, ComponentType<any>>;
148
+ components?: Record<string, ComponentType<any>>
147
149
 
148
150
  /** The React component rendered if a component mapping from the `components` prop cannot be found. */
149
151
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
150
- defaultComponent?: ComponentType<SliceComponentProps<any, TContext>>;
152
+ defaultComponent?: ComponentType<SliceComponentProps<any, TContext>>
151
153
 
152
154
  /** Arbitrary data made available to all slice components. */
153
- context?: TContext;
154
- };
155
+ context?: TContext
156
+ }
155
157
 
156
158
  /**
157
159
  * This slice component can be used as a reminder to provide a proper implementation.
@@ -162,56 +164,65 @@ export type SliceZoneProps<TContext = unknown> = {
162
164
  export const TODOSliceComponent = <TSlice extends SliceLike>({
163
165
  slice,
164
166
  }: {
165
- slice: TSlice;
167
+ slice: TSlice
166
168
  }): ReactNode => {
167
169
  if (!DEV) {
168
- return null;
170
+ return null
169
171
  }
170
172
 
171
- const type = "slice_type" in slice ? slice.slice_type : slice.type;
173
+ const type = "slice_type" in slice ? slice.slice_type : slice.type
172
174
 
173
- console.warn(`[SliceZone] Could not find a component for slice type "${type}"`, slice);
175
+ console.warn(`[SliceZone] Could not find a component for slice type "${type}"`, slice)
174
176
 
175
177
  return (
176
178
  <section data-slice-zone-todo-component="" data-slice-type={type}>
177
179
  Could not find a component for slice type &ldquo;{type}
178
180
  &rdquo;
179
181
  </section>
180
- );
181
- };
182
+ )
183
+ }
182
184
 
183
185
  /**
184
186
  * Renders slices in a slice zone as React components.
185
187
  *
186
188
  * @example
187
- * ```tsx
189
+ * ;```tsx
188
190
  * <SliceZone slices={page.data.slices} components={components} />;
189
191
  * ```
190
192
  *
191
193
  * @see Learn how to create slices, use slice variations, and display slices: {@link https://prismic.io/docs/slices}
192
194
  */
193
195
  export const SliceZone: FC<SliceZoneProps> = (props) => {
194
- const { slices = [], components = {}, defaultComponent, context = {} } = props;
196
+ const { slices = [], components = {}, defaultComponent, context = {} } = props
195
197
 
196
198
  const renderedSlices = slices.map((slice, index) => {
197
- const type = "slice_type" in slice ? slice.slice_type : slice.type;
199
+ const type = "slice_type" in slice ? slice.slice_type : slice.type
198
200
 
199
- const key = "id" in slice && slice.id ? slice.id : `${index}-${JSON.stringify(slice)}`;
201
+ const key = "id" in slice && slice.id ? slice.id : `${index}-${JSON.stringify(slice)}`
200
202
 
201
- const Comp = components[type as keyof typeof components] || defaultComponent;
203
+ const Comp = components[type as keyof typeof components] || defaultComponent
204
+ let renderedSlice: ReactNode
202
205
 
203
206
  if (!Comp) {
204
- return <TODOSliceComponent key={key} slice={slice} />;
205
- }
207
+ renderedSlice = <TODOSliceComponent slice={slice} />
208
+ } else if (slice.__mapped) {
209
+ const { __mapped, ...mappedProps } = slice
206
210
 
207
- if (slice.__mapped) {
208
- const { __mapped, ...mappedProps } = slice;
211
+ renderedSlice = <Comp {...mappedProps} />
212
+ } else {
213
+ renderedSlice = <Comp slice={slice} index={index} slices={slices} context={context} />
214
+ }
209
215
 
210
- return <Comp key={key} {...mappedProps} />;
216
+ if ("id" in slice && slice.id) {
217
+ return (
218
+ <SliceBoundary key={key} sliceId={slice.id}>
219
+ {renderedSlice}
220
+ </SliceBoundary>
221
+ )
211
222
  }
212
223
 
213
- return <Comp key={key} slice={slice} index={index} slices={slices} context={context} />;
214
- });
224
+ return <Fragment key={key}>{renderedSlice}</Fragment>
225
+ })
215
226
 
216
- return <>{renderedSlices}</>;
217
- };
227
+ return <>{renderedSlices}</>
228
+ }
package/src/index.ts CHANGED
@@ -1,26 +1,26 @@
1
- export { PrismicLink } from "./PrismicLink.js";
2
- export type { PrismicLinkProps, LinkProps } from "./PrismicLink.js";
1
+ export { PrismicLink } from "./PrismicLink.js"
2
+ export type { PrismicLinkProps, LinkProps } from "./PrismicLink.js"
3
3
 
4
- export { PrismicTable } from "./PrismicTable.js";
5
- export type { PrismicTableProps } from "./PrismicTable.js";
4
+ export { PrismicTable } from "./PrismicTable.js"
5
+ export type { PrismicTableProps } from "./PrismicTable.js"
6
6
 
7
- export { PrismicText } from "./PrismicText.js";
8
- export type { PrismicTextProps } from "./PrismicText.js";
7
+ export { PrismicText } from "./PrismicText.js"
8
+ export type { PrismicTextProps } from "./PrismicText.js"
9
9
 
10
- export { PrismicRichText } from "./PrismicRichText.js";
10
+ export { PrismicRichText } from "./PrismicRichText.js"
11
11
  export type {
12
12
  PrismicRichTextProps,
13
13
  RichTextComponents,
14
14
  JSXMapSerializer,
15
15
  JSXFunctionSerializer,
16
- } from "./PrismicRichText.js";
16
+ } from "./PrismicRichText.js"
17
17
 
18
- export { Element } from "@prismicio/client/richtext";
18
+ export { Element } from "@prismicio/client/richtext"
19
19
 
20
- export { PrismicImage } from "./PrismicImage.js";
21
- export type { PrismicImageProps } from "./PrismicImage.js";
20
+ export { PrismicImage } from "./PrismicImage.js"
21
+ export type { PrismicImageProps } from "./PrismicImage.js"
22
22
 
23
- export { SliceZone, TODOSliceComponent } from "./SliceZone.js";
23
+ export { SliceZone, TODOSliceComponent } from "./SliceZone.js"
24
24
  export type {
25
25
  SliceComponentProps,
26
26
  SliceComponentType,
@@ -30,7 +30,7 @@ export type {
30
30
  SliceZoneComponents,
31
31
  SliceZoneLike,
32
32
  SliceZoneProps,
33
- } from "./SliceZone.js";
33
+ } from "./SliceZone.js"
34
34
 
35
- export { PrismicToolbar } from "./PrismicToolbar.js";
36
- export type { PrismicToolbarProps } from "./PrismicToolbar.js";
35
+ export { PrismicToolbar } from "./PrismicToolbar.js"
36
+ export type { PrismicToolbarProps } from "./PrismicToolbar.js"
package/src/lib/devMsg.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { version } from "../../package.json";
1
+ import { version } from "../../package.json"
2
2
 
3
3
  /**
4
4
  * Returns a `prismic.dev/msg` URL for a given message slug.
5
5
  *
6
6
  * @example
7
- * ```ts
7
+ * ;```ts
8
8
  * devMsg("missing-param");
9
9
  * // => "https://prismic.dev/msg/react/v1.2.3/missing-param"
10
10
  * ```
@@ -14,5 +14,5 @@ import { version } from "../../package.json";
14
14
  * @returns The `prismic.dev/msg` URL for the given slug.
15
15
  */
16
16
  export function devMsg(slug: string): string {
17
- return `https://prismic.dev/msg/react/v${version}/${slug}`;
17
+ return `https://prismic.dev/msg/react/v${version}/${slug}`
18
18
  }