@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.
- package/dist/PrismicImage.d.ts +20 -28
- package/dist/PrismicImage.d.ts.map +1 -1
- package/dist/PrismicImage.js +6 -8
- package/dist/PrismicImage.js.map +1 -1
- package/dist/PrismicLink.d.ts +18 -21
- package/dist/PrismicLink.d.ts.map +1 -1
- package/dist/PrismicLink.js +6 -9
- package/dist/PrismicLink.js.map +1 -1
- package/dist/PrismicRichText.d.ts +31 -37
- package/dist/PrismicRichText.d.ts.map +1 -1
- package/dist/PrismicRichText.js +5 -7
- package/dist/PrismicRichText.js.map +1 -1
- package/dist/PrismicTable.d.ts +15 -16
- package/dist/PrismicTable.d.ts.map +1 -1
- package/dist/PrismicTable.js +4 -6
- package/dist/PrismicTable.js.map +1 -1
- package/dist/PrismicText.d.ts +8 -11
- package/dist/PrismicText.d.ts.map +1 -1
- package/dist/PrismicText.js +4 -6
- 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 +5 -8
- package/dist/PrismicToolbar.js.map +1 -1
- package/dist/SliceZone.d.ts +31 -51
- package/dist/SliceZone.d.ts.map +1 -1
- package/dist/SliceZone.js +7 -10
- package/dist/SliceZone.js.map +1 -1
- package/dist/index.js +1 -2
- package/dist/lib/devMsg.js +7 -10
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/package.js +2 -2
- package/package.json +32 -34
- package/src/PrismicImage.tsx +107 -134
- package/src/PrismicLink.tsx +66 -76
- package/src/PrismicRichText.tsx +77 -85
- package/src/PrismicTable.tsx +41 -45
- package/src/PrismicText.tsx +20 -21
- package/src/PrismicToolbar.tsx +41 -45
- package/src/SliceZone.tsx +68 -108
- package/src/index.ts +15 -15
- package/src/lib/devMsg.ts +8 -10
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,
|
|
@@ -8,21 +7,22 @@ import {
|
|
|
8
7
|
type TableFieldBodyRow,
|
|
9
8
|
type TableFieldHeaderCell,
|
|
10
9
|
type TableFieldDataCell,
|
|
11
|
-
} from "@prismicio/client"
|
|
10
|
+
} from "@prismicio/client"
|
|
11
|
+
import type { ComponentType, FC, ReactNode } from "react"
|
|
12
12
|
|
|
13
|
-
import { type JSXMapSerializer, PrismicRichText } from "./PrismicRichText.js"
|
|
13
|
+
import { type JSXMapSerializer, PrismicRichText } from "./PrismicRichText.js"
|
|
14
14
|
|
|
15
15
|
type TableComponents = {
|
|
16
|
-
table?: ComponentType<{ table: TableField<"filled">; children: ReactNode }
|
|
17
|
-
thead?: ComponentType<{ head: TableFieldHead; children: ReactNode }
|
|
18
|
-
tbody?: ComponentType<{ body: TableFieldBody; children: ReactNode }
|
|
16
|
+
table?: ComponentType<{ table: TableField<"filled">; children: ReactNode }>
|
|
17
|
+
thead?: ComponentType<{ head: TableFieldHead; children: ReactNode }>
|
|
18
|
+
tbody?: ComponentType<{ body: TableFieldBody; children: ReactNode }>
|
|
19
19
|
tr?: ComponentType<{
|
|
20
|
-
row: TableFieldHeadRow | TableFieldBodyRow
|
|
21
|
-
children: ReactNode
|
|
22
|
-
}
|
|
23
|
-
th?: ComponentType<{ cell: TableFieldHeaderCell; children: ReactNode }
|
|
24
|
-
td?: ComponentType<{ cell: TableFieldDataCell; children: ReactNode }
|
|
25
|
-
}
|
|
20
|
+
row: TableFieldHeadRow | TableFieldBodyRow
|
|
21
|
+
children: ReactNode
|
|
22
|
+
}>
|
|
23
|
+
th?: ComponentType<{ cell: TableFieldHeaderCell; children: ReactNode }>
|
|
24
|
+
td?: ComponentType<{ cell: TableFieldDataCell; children: ReactNode }>
|
|
25
|
+
}
|
|
26
26
|
|
|
27
27
|
const defaultComponents: Required<TableComponents> = {
|
|
28
28
|
table: ({ children }) => <table>{children}</table>,
|
|
@@ -31,57 +31,53 @@ const defaultComponents: Required<TableComponents> = {
|
|
|
31
31
|
tr: ({ children }) => <tr>{children}</tr>,
|
|
32
32
|
th: ({ children }) => <th>{children}</th>,
|
|
33
33
|
td: ({ children }) => <td>{children}</td>,
|
|
34
|
-
}
|
|
34
|
+
}
|
|
35
35
|
|
|
36
36
|
/** Props for `<PrismicTable>`. */
|
|
37
37
|
export type PrismicTableProps = {
|
|
38
38
|
/** The Prismic table field to render. */
|
|
39
|
-
field: TableField
|
|
39
|
+
field: TableField
|
|
40
40
|
|
|
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
|
-
components?: JSXMapSerializer & TableComponents
|
|
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
|
-
fallback?: ReactNode
|
|
60
|
-
}
|
|
60
|
+
fallback?: ReactNode
|
|
61
|
+
}
|
|
61
62
|
|
|
62
63
|
/**
|
|
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
|
*/
|
|
73
73
|
export const PrismicTable: FC<PrismicTableProps> = (props) => {
|
|
74
|
-
const { field, components, fallback = null } = props
|
|
74
|
+
const { field, components, fallback = null } = props
|
|
75
75
|
|
|
76
76
|
if (!isFilled.table(field)) {
|
|
77
|
-
return fallback
|
|
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}>
|
|
@@ -98,18 +94,18 @@ export const PrismicTable: FC<PrismicTableProps> = (props) => {
|
|
|
98
94
|
))}
|
|
99
95
|
</Tbody>
|
|
100
96
|
</Table>
|
|
101
|
-
)
|
|
102
|
-
}
|
|
97
|
+
)
|
|
98
|
+
}
|
|
103
99
|
|
|
104
100
|
type TableRowProps = {
|
|
105
|
-
row: TableFieldHeadRow | TableFieldBodyRow
|
|
106
|
-
components?: JSXMapSerializer & TableComponents
|
|
107
|
-
}
|
|
101
|
+
row: TableFieldHeadRow | TableFieldBodyRow
|
|
102
|
+
components?: JSXMapSerializer & TableComponents
|
|
103
|
+
}
|
|
108
104
|
|
|
109
105
|
function TableRow(props: TableRowProps) {
|
|
110
|
-
const { row, components } = props
|
|
106
|
+
const { row, components } = props
|
|
111
107
|
|
|
112
|
-
const { tr: Tr, th: Th, td: Td } = { ...defaultComponents, ...components }
|
|
108
|
+
const { tr: Tr, th: Th, td: Td } = { ...defaultComponents, ...components }
|
|
113
109
|
|
|
114
110
|
return (
|
|
115
111
|
<Tr row={row}>
|
|
@@ -125,5 +121,5 @@ function TableRow(props: TableRowProps) {
|
|
|
125
121
|
),
|
|
126
122
|
)}
|
|
127
123
|
</Tr>
|
|
128
|
-
)
|
|
124
|
+
)
|
|
129
125
|
}
|
package/src/PrismicText.tsx
CHANGED
|
@@ -1,37 +1,36 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { asText, isFilled, type RichTextField } from "@prismicio/client"
|
|
2
|
+
import { DEV } from "esm-env"
|
|
3
|
+
import type { FC } from "react"
|
|
4
4
|
|
|
5
|
-
import { devMsg } from "./lib/devMsg.js"
|
|
5
|
+
import { devMsg } from "./lib/devMsg.js"
|
|
6
6
|
|
|
7
7
|
/** Props for `<PrismicText>`. */
|
|
8
8
|
export type PrismicTextProps = {
|
|
9
9
|
/** The Prismic rich text field to render. */
|
|
10
|
-
field: RichTextField | null | undefined
|
|
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
|
-
fallback?: string
|
|
16
|
+
fallback?: string
|
|
17
17
|
|
|
18
18
|
/** The separator used between blocks. Defaults to `\n`. */
|
|
19
|
-
separator?: string
|
|
20
|
-
}
|
|
19
|
+
separator?: string
|
|
20
|
+
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
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
|
*/
|
|
33
32
|
export const PrismicText: FC<PrismicTextProps> = (props) => {
|
|
34
|
-
const { field, fallback, separator } = props
|
|
33
|
+
const { field, fallback, separator } = props
|
|
35
34
|
|
|
36
35
|
if (DEV) {
|
|
37
36
|
if ("className" in props) {
|
|
@@ -40,7 +39,7 @@ export const PrismicText: FC<PrismicTextProps> = (props) => {
|
|
|
40
39
|
"classname-is-not-a-valid-prop",
|
|
41
40
|
)}.`,
|
|
42
41
|
props.field,
|
|
43
|
-
)
|
|
42
|
+
)
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
|
|
@@ -51,15 +50,15 @@ export const PrismicText: FC<PrismicTextProps> = (props) => {
|
|
|
51
50
|
"prismictext-works-only-with-rich-text-and-title-fields",
|
|
52
51
|
)}`,
|
|
53
52
|
props.field,
|
|
54
|
-
)
|
|
53
|
+
)
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
return null
|
|
56
|
+
return null
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
if (!isFilled.richText(field)) {
|
|
61
|
-
return fallback != null ? <>{fallback}</> : null
|
|
60
|
+
return fallback != null ? <>{fallback}</> : null
|
|
62
61
|
}
|
|
63
62
|
|
|
64
|
-
return <>{asText(field, { separator })}
|
|
65
|
-
}
|
|
63
|
+
return <>{asText(field, { separator })}</>
|
|
64
|
+
}
|
package/src/PrismicToolbar.tsx
CHANGED
|
@@ -1,97 +1,93 @@
|
|
|
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 = {
|
|
8
8
|
/**
|
|
9
|
-
* The name of the Prismic repository. For example, `"my-repo"` if the
|
|
10
|
-
*
|
|
9
|
+
* The name of the Prismic repository. For example, `"my-repo"` if the repository URL is
|
|
10
|
+
* `my-repo.prismic.io`.
|
|
11
11
|
*/
|
|
12
|
-
repositoryName: string
|
|
12
|
+
repositoryName: string
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Called when the Prismic toolbar triggers a preview update. This happens
|
|
16
|
-
*
|
|
15
|
+
* Called when the Prismic toolbar triggers a preview update. This happens when the previewed
|
|
16
|
+
* content changes.
|
|
17
17
|
*
|
|
18
18
|
* The new ref can be read from `event.detail.ref`.
|
|
19
19
|
*
|
|
20
|
-
* The default page reload behavior can be cancelled with
|
|
21
|
-
* `event.preventDefault()`.
|
|
20
|
+
* The default page reload behavior can be cancelled with `event.preventDefault()`.
|
|
22
21
|
*/
|
|
23
|
-
onPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void
|
|
22
|
+
onPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void
|
|
24
23
|
|
|
25
24
|
/**
|
|
26
|
-
* Called when the Prismic toolbar triggers a preview end. This happens when
|
|
27
|
-
*
|
|
25
|
+
* Called when the Prismic toolbar triggers a preview end. This happens when a preview session is
|
|
26
|
+
* closed.
|
|
28
27
|
*
|
|
29
|
-
* The default page reload behavior can be cancelled with
|
|
30
|
-
* `event.preventDefault()`.
|
|
28
|
+
* The default page reload behavior can be cancelled with `event.preventDefault()`.
|
|
31
29
|
*/
|
|
32
|
-
onPreviewEnd?: (event: CustomEvent<null>) => void
|
|
33
|
-
}
|
|
30
|
+
onPreviewEnd?: (event: CustomEvent<null>) => void
|
|
31
|
+
}
|
|
34
32
|
|
|
35
33
|
/**
|
|
36
34
|
* Renders the Prismic Toolbar script to support draft previews.
|
|
37
35
|
*
|
|
38
36
|
* @example
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* ```
|
|
37
|
+
* ;```tsx
|
|
38
|
+
* <PrismicToolbar repositoryName="my-repo" />;
|
|
39
|
+
* ```
|
|
43
40
|
*
|
|
44
41
|
* @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}
|
|
45
42
|
*/
|
|
46
43
|
export const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {
|
|
47
|
-
const { repositoryName, onPreviewUpdate, onPreviewEnd } = props
|
|
44
|
+
const { repositoryName, onPreviewUpdate, onPreviewEnd } = props
|
|
48
45
|
|
|
49
|
-
const src = getToolbarSrc(repositoryName)
|
|
46
|
+
const src = getToolbarSrc(repositoryName)
|
|
50
47
|
|
|
51
|
-
const onPreviewUpdateRef = useRef(onPreviewUpdate)
|
|
52
|
-
onPreviewUpdateRef.current = onPreviewUpdate
|
|
48
|
+
const onPreviewUpdateRef = useRef(onPreviewUpdate)
|
|
49
|
+
onPreviewUpdateRef.current = onPreviewUpdate
|
|
53
50
|
|
|
54
|
-
const onPreviewEndRef = useRef(onPreviewEnd)
|
|
55
|
-
onPreviewEndRef.current = onPreviewEnd
|
|
51
|
+
const onPreviewEndRef = useRef(onPreviewEnd)
|
|
52
|
+
onPreviewEndRef.current = onPreviewEnd
|
|
56
53
|
|
|
57
54
|
useEffect(() => {
|
|
58
|
-
const existingScript = document.querySelector(`script[src="${src}"]`)
|
|
55
|
+
const existingScript = document.querySelector(`script[src="${src}"]`)
|
|
59
56
|
|
|
60
57
|
if (!existingScript) {
|
|
61
|
-
const script = document.createElement("script")
|
|
62
|
-
script.src = src
|
|
63
|
-
script.defer = true
|
|
58
|
+
const script = document.createElement("script")
|
|
59
|
+
script.src = src
|
|
60
|
+
script.defer = true
|
|
64
61
|
|
|
65
62
|
// Used to distinguish the toolbar element from other elements.
|
|
66
|
-
script.dataset.prismicToolbar = ""
|
|
67
|
-
script.dataset.repositoryName = repositoryName
|
|
63
|
+
script.dataset.prismicToolbar = ""
|
|
64
|
+
script.dataset.repositoryName = repositoryName
|
|
68
65
|
|
|
69
66
|
// Disable Happy DOM `<script>` evaluation during tests.
|
|
70
67
|
// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
|
|
71
|
-
script._evaluateScript = false
|
|
68
|
+
script._evaluateScript = false
|
|
72
69
|
|
|
73
|
-
document.body.appendChild(script)
|
|
70
|
+
document.body.appendChild(script)
|
|
74
71
|
}
|
|
75
|
-
}, [repositoryName, src])
|
|
72
|
+
}, [repositoryName, src])
|
|
76
73
|
|
|
77
74
|
useEffect(() => {
|
|
78
|
-
const controller = new AbortController()
|
|
75
|
+
const controller = new AbortController()
|
|
79
76
|
|
|
80
77
|
window.addEventListener(
|
|
81
78
|
"prismicPreviewUpdate",
|
|
82
|
-
(event) =>
|
|
83
|
-
onPreviewUpdateRef.current?.(event as CustomEvent<{ ref: string }>),
|
|
79
|
+
(event) => onPreviewUpdateRef.current?.(event as CustomEvent<{ ref: string }>),
|
|
84
80
|
{ signal: controller.signal },
|
|
85
|
-
)
|
|
81
|
+
)
|
|
86
82
|
|
|
87
83
|
window.addEventListener(
|
|
88
84
|
"prismicPreviewEnd",
|
|
89
85
|
(event) => onPreviewEndRef.current?.(event as CustomEvent<null>),
|
|
90
86
|
{ signal: controller.signal },
|
|
91
|
-
)
|
|
87
|
+
)
|
|
92
88
|
|
|
93
|
-
return () => controller.abort()
|
|
94
|
-
}, [])
|
|
89
|
+
return () => controller.abort()
|
|
90
|
+
}, [])
|
|
95
91
|
|
|
96
|
-
return null
|
|
97
|
-
}
|
|
92
|
+
return null
|
|
93
|
+
}
|
package/src/SliceZone.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import type { Slice } from "@prismicio/client"
|
|
2
|
+
import { DEV } from "esm-env"
|
|
3
|
+
import type { ComponentType, FC, ReactNode } from "react"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Returns the type of a `SliceLike` type.
|
|
@@ -11,35 +11,34 @@ type ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice
|
|
|
11
11
|
? TSlice["slice_type"]
|
|
12
12
|
: TSlice extends SliceLikeGraphQL
|
|
13
13
|
? TSlice["type"]
|
|
14
|
-
: never
|
|
14
|
+
: never
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* The minimum required properties to represent a Prismic slice from the Prismic
|
|
18
|
-
*
|
|
17
|
+
* The minimum required properties to represent a Prismic slice from the Prismic Content API for the
|
|
18
|
+
* `mapSliceZone()` helper.
|
|
19
19
|
*
|
|
20
20
|
* @typeParam SliceType - Type name of the slice.
|
|
21
21
|
*/
|
|
22
22
|
export type SliceLikeRestV2<TSliceType extends string = string> = Pick<
|
|
23
23
|
Slice<TSliceType>,
|
|
24
24
|
"id" | "slice_type"
|
|
25
|
-
|
|
25
|
+
>
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* The minimum required properties to represent a Prismic slice from the Prismic
|
|
29
|
-
*
|
|
28
|
+
* The minimum required properties to represent a Prismic slice from the Prismic GraphQL API for the
|
|
29
|
+
* `mapSliceZone()` helper.
|
|
30
30
|
*
|
|
31
31
|
* @typeParam SliceType - Type name of the slice.
|
|
32
32
|
*/
|
|
33
33
|
export type SliceLikeGraphQL<TSliceType extends string = string> = {
|
|
34
|
-
type: Slice<TSliceType>["slice_type"]
|
|
35
|
-
}
|
|
34
|
+
type: Slice<TSliceType>["slice_type"]
|
|
35
|
+
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* The minimum required properties to represent a Prismic slice for the
|
|
39
|
-
* `mapSliceZone()` helper.
|
|
38
|
+
* The minimum required properties to represent a Prismic slice for the `mapSliceZone()` helper.
|
|
40
39
|
*
|
|
41
|
-
* If using Prismic's Content API, use the `Slice` export from
|
|
42
|
-
*
|
|
40
|
+
* If using Prismic's Content API, use the `Slice` export from `@prismicio/client` for a full
|
|
41
|
+
* interface.
|
|
43
42
|
*
|
|
44
43
|
* @typeParam SliceType - Type name of the slice.
|
|
45
44
|
*/
|
|
@@ -48,59 +47,49 @@ export type SliceLike<TSliceType extends string = string> = (
|
|
|
48
47
|
| SliceLikeGraphQL<TSliceType>
|
|
49
48
|
) & {
|
|
50
49
|
/**
|
|
51
|
-
* If `true`, this slice has been modified from its original value using a
|
|
52
|
-
*
|
|
50
|
+
* If `true`, this slice has been modified from its original value using a mapper and
|
|
51
|
+
* `@prismicio/client`'s `mapSliceZone()`.
|
|
53
52
|
*
|
|
54
53
|
* @internal
|
|
55
54
|
*/
|
|
56
|
-
__mapped?: true
|
|
57
|
-
}
|
|
55
|
+
__mapped?: true
|
|
56
|
+
}
|
|
58
57
|
|
|
59
58
|
/**
|
|
60
|
-
* A looser version of the `SliceZone` type from `@prismicio/client` using
|
|
61
|
-
* `SliceLike`.
|
|
59
|
+
* A looser version of the `SliceZone` type from `@prismicio/client` using `SliceLike`.
|
|
62
60
|
*
|
|
63
|
-
* If using Prismic's Content API, use the `SliceZone` export from
|
|
64
|
-
*
|
|
61
|
+
* If using Prismic's Content API, use the `SliceZone` export from `@prismicio/client` for the full
|
|
62
|
+
* type.
|
|
65
63
|
*
|
|
66
64
|
* @typeParam TSlice - The type(s) of a slice in the slice zone.
|
|
67
65
|
*/
|
|
68
|
-
export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
|
|
69
|
-
readonly TSlice[];
|
|
66
|
+
export type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[]
|
|
70
67
|
|
|
71
68
|
/**
|
|
72
|
-
* React props for a component rendering content from a Prismic slice using the
|
|
73
|
-
*
|
|
69
|
+
* React props for a component rendering content from a Prismic slice using the `<SliceZone>`
|
|
70
|
+
* component.
|
|
74
71
|
*
|
|
75
72
|
* @typeParam TSlice - The slice passed as a prop.
|
|
76
|
-
* @typeParam TContext - Arbitrary data passed to `<SliceZone>` and made
|
|
77
|
-
*
|
|
73
|
+
* @typeParam TContext - Arbitrary data passed to `<SliceZone>` and made available to all slice
|
|
74
|
+
* components.
|
|
78
75
|
*/
|
|
79
|
-
export type SliceComponentProps<
|
|
80
|
-
TSlice extends SliceLike = SliceLike,
|
|
81
|
-
TContext = unknown,
|
|
82
|
-
> = {
|
|
76
|
+
export type SliceComponentProps<TSlice extends SliceLike = SliceLike, TContext = unknown> = {
|
|
83
77
|
/** Slice data for this component. */
|
|
84
|
-
slice: TSlice
|
|
78
|
+
slice: TSlice
|
|
85
79
|
|
|
86
80
|
/** The index of the slice in the slice zone. */
|
|
87
|
-
index: number
|
|
81
|
+
index: number
|
|
88
82
|
|
|
89
83
|
/** All slices from the slice zone to which the slice belongs. */
|
|
90
84
|
// TODO: We have to keep this list of slices general due to circular
|
|
91
85
|
// reference limtiations. If we had another generic to determine the full
|
|
92
86
|
// union of slice types, it would include TSlice. This causes TypeScript to
|
|
93
87
|
// throw a compilation error.
|
|
94
|
-
slices: SliceZoneLike<
|
|
95
|
-
TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2
|
|
96
|
-
>;
|
|
88
|
+
slices: SliceZoneLike<TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2>
|
|
97
89
|
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
*/
|
|
102
|
-
context: TContext;
|
|
103
|
-
};
|
|
90
|
+
/** Arbitrary data passed to `<SliceZone>` and made available to all slice components. */
|
|
91
|
+
context: TContext
|
|
92
|
+
}
|
|
104
93
|
|
|
105
94
|
/**
|
|
106
95
|
* A React component to be rendered for each instance of its slice.
|
|
@@ -112,22 +101,18 @@ export type SliceComponentType<
|
|
|
112
101
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
102
|
TSlice extends SliceLike = any,
|
|
114
103
|
TContext = unknown,
|
|
115
|
-
> = ComponentType<SliceComponentProps<TSlice, TContext
|
|
104
|
+
> = ComponentType<SliceComponentProps<TSlice, TContext>>
|
|
116
105
|
|
|
117
106
|
/**
|
|
118
|
-
* A record of slice types mapped to a React component. The component will be
|
|
119
|
-
*
|
|
107
|
+
* A record of slice types mapped to a React component. The component will be rendered for each
|
|
108
|
+
* instance of its slice.
|
|
120
109
|
*
|
|
121
110
|
* @deprecated This type is no longer used by `@prismicio/react`. Prefer using
|
|
122
111
|
* `Record<string, SliceComponentType<any>>` instead.
|
|
123
|
-
*
|
|
124
112
|
* @typeParam TSlice - The type(s) of a slice in the slice zone.
|
|
125
113
|
* @typeParam TContext - Arbitrary data made available to all slice components.
|
|
126
114
|
*/
|
|
127
|
-
export type SliceZoneComponents<
|
|
128
|
-
TSlice extends SliceLike = SliceLike,
|
|
129
|
-
TContext = unknown,
|
|
130
|
-
> =
|
|
115
|
+
export type SliceZoneComponents<TSlice extends SliceLike = SliceLike, TContext = unknown> =
|
|
131
116
|
// This is purposely not wrapped in Partial to ensure a component is provided
|
|
132
117
|
// for all slice types. <SliceZone> will render a default component if one is
|
|
133
118
|
// not provided, but it *should* be a type error if an explicit component is
|
|
@@ -143,8 +128,8 @@ export type SliceZoneComponents<
|
|
|
143
128
|
? SliceLike
|
|
144
129
|
: Extract<TSlice, SliceLike<SliceType>>,
|
|
145
130
|
TContext
|
|
146
|
-
|
|
147
|
-
}
|
|
131
|
+
>
|
|
132
|
+
}
|
|
148
133
|
|
|
149
134
|
/**
|
|
150
135
|
* React props for the `<SliceZone>` component.
|
|
@@ -154,104 +139,79 @@ export type SliceZoneComponents<
|
|
|
154
139
|
*/
|
|
155
140
|
export type SliceZoneProps<TContext = unknown> = {
|
|
156
141
|
/** List of slice data from the slice zone. */
|
|
157
|
-
slices?: SliceZoneLike
|
|
142
|
+
slices?: SliceZoneLike
|
|
158
143
|
|
|
159
144
|
/** A record mapping slice types to React components. */
|
|
160
145
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
-
components?: Record<string, ComponentType<any
|
|
146
|
+
components?: Record<string, ComponentType<any>>
|
|
162
147
|
|
|
163
|
-
/**
|
|
164
|
-
* The React component rendered if a component mapping from the `components`
|
|
165
|
-
* prop cannot be found.
|
|
166
|
-
*/
|
|
148
|
+
/** The React component rendered if a component mapping from the `components` prop cannot be found. */
|
|
167
149
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
168
|
-
defaultComponent?: ComponentType<SliceComponentProps<any, TContext
|
|
150
|
+
defaultComponent?: ComponentType<SliceComponentProps<any, TContext>>
|
|
169
151
|
|
|
170
152
|
/** Arbitrary data made available to all slice components. */
|
|
171
|
-
context?: TContext
|
|
172
|
-
}
|
|
153
|
+
context?: TContext
|
|
154
|
+
}
|
|
173
155
|
|
|
174
156
|
/**
|
|
175
|
-
* This slice component can be used as a reminder to provide a proper
|
|
176
|
-
* implementation.
|
|
157
|
+
* This slice component can be used as a reminder to provide a proper implementation.
|
|
177
158
|
*
|
|
178
|
-
* This is also the default React component rendered when a component mapping
|
|
179
|
-
*
|
|
159
|
+
* This is also the default React component rendered when a component mapping cannot be found in
|
|
160
|
+
* `<SliceZone>`.
|
|
180
161
|
*/
|
|
181
162
|
export const TODOSliceComponent = <TSlice extends SliceLike>({
|
|
182
163
|
slice,
|
|
183
164
|
}: {
|
|
184
|
-
slice: TSlice
|
|
165
|
+
slice: TSlice
|
|
185
166
|
}): ReactNode => {
|
|
186
167
|
if (!DEV) {
|
|
187
|
-
return null
|
|
168
|
+
return null
|
|
188
169
|
}
|
|
189
170
|
|
|
190
|
-
const type = "slice_type" in slice ? slice.slice_type : slice.type
|
|
171
|
+
const type = "slice_type" in slice ? slice.slice_type : slice.type
|
|
191
172
|
|
|
192
|
-
console.warn(
|
|
193
|
-
`[SliceZone] Could not find a component for slice type "${type}"`,
|
|
194
|
-
slice,
|
|
195
|
-
);
|
|
173
|
+
console.warn(`[SliceZone] Could not find a component for slice type "${type}"`, slice)
|
|
196
174
|
|
|
197
175
|
return (
|
|
198
176
|
<section data-slice-zone-todo-component="" data-slice-type={type}>
|
|
199
177
|
Could not find a component for slice type “{type}
|
|
200
178
|
”
|
|
201
179
|
</section>
|
|
202
|
-
)
|
|
203
|
-
}
|
|
180
|
+
)
|
|
181
|
+
}
|
|
204
182
|
|
|
205
183
|
/**
|
|
206
184
|
* Renders slices in a slice zone as React components.
|
|
207
185
|
*
|
|
208
186
|
* @example
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
* ```
|
|
187
|
+
* ;```tsx
|
|
188
|
+
* <SliceZone slices={page.data.slices} components={components} />;
|
|
189
|
+
* ```
|
|
213
190
|
*
|
|
214
191
|
* @see Learn how to create slices, use slice variations, and display slices: {@link https://prismic.io/docs/slices}
|
|
215
192
|
*/
|
|
216
193
|
export const SliceZone: FC<SliceZoneProps> = (props) => {
|
|
217
|
-
const {
|
|
218
|
-
slices = [],
|
|
219
|
-
components = {},
|
|
220
|
-
defaultComponent,
|
|
221
|
-
context = {},
|
|
222
|
-
} = props;
|
|
194
|
+
const { slices = [], components = {}, defaultComponent, context = {} } = props
|
|
223
195
|
|
|
224
196
|
const renderedSlices = slices.map((slice, index) => {
|
|
225
|
-
const type = "slice_type" in slice ? slice.slice_type : slice.type
|
|
197
|
+
const type = "slice_type" in slice ? slice.slice_type : slice.type
|
|
226
198
|
|
|
227
|
-
const key =
|
|
228
|
-
"id" in slice && slice.id
|
|
229
|
-
? slice.id
|
|
230
|
-
: `${index}-${JSON.stringify(slice)}`;
|
|
199
|
+
const key = "id" in slice && slice.id ? slice.id : `${index}-${JSON.stringify(slice)}`
|
|
231
200
|
|
|
232
|
-
const Comp =
|
|
233
|
-
components[type as keyof typeof components] || defaultComponent;
|
|
201
|
+
const Comp = components[type as keyof typeof components] || defaultComponent
|
|
234
202
|
|
|
235
203
|
if (!Comp) {
|
|
236
|
-
return <TODOSliceComponent key={key} slice={slice}
|
|
204
|
+
return <TODOSliceComponent key={key} slice={slice} />
|
|
237
205
|
}
|
|
238
206
|
|
|
239
207
|
if (slice.__mapped) {
|
|
240
|
-
const { __mapped, ...mappedProps } = slice
|
|
208
|
+
const { __mapped, ...mappedProps } = slice
|
|
241
209
|
|
|
242
|
-
return <Comp key={key} {...mappedProps}
|
|
210
|
+
return <Comp key={key} {...mappedProps} />
|
|
243
211
|
}
|
|
244
212
|
|
|
245
|
-
return
|
|
246
|
-
|
|
247
|
-
key={key}
|
|
248
|
-
slice={slice}
|
|
249
|
-
index={index}
|
|
250
|
-
slices={slices}
|
|
251
|
-
context={context}
|
|
252
|
-
/>
|
|
253
|
-
);
|
|
254
|
-
});
|
|
213
|
+
return <Comp key={key} slice={slice} index={index} slices={slices} context={context} />
|
|
214
|
+
})
|
|
255
215
|
|
|
256
|
-
return <>{renderedSlices}
|
|
257
|
-
}
|
|
216
|
+
return <>{renderedSlices}</>
|
|
217
|
+
}
|