@prismicio/react 2.6.1 → 2.7.0
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.cjs +1 -1
- package/dist/PrismicImage.cjs.map +1 -1
- package/dist/PrismicImage.js +1 -1
- package/dist/PrismicImage.js.map +1 -1
- package/dist/PrismicRichText.d.ts +1 -1
- package/dist/PrismicText.cjs +1 -1
- package/dist/PrismicText.cjs.map +1 -1
- package/dist/PrismicText.js +1 -1
- package/dist/PrismicText.js.map +1 -1
- package/dist/PrismicToolbar.cjs +1 -1
- package/dist/PrismicToolbar.cjs.map +1 -1
- package/dist/PrismicToolbar.js +1 -1
- package/dist/PrismicToolbar.js.map +1 -1
- package/dist/SliceZone.cjs +27 -37
- package/dist/SliceZone.cjs.map +1 -1
- package/dist/SliceZone.d.ts +36 -33
- package/dist/SliceZone.js +27 -20
- package/dist/SliceZone.js.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.cjs +2 -2
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.js +2 -2
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.cjs +2 -2
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.js +2 -2
- package/dist/_node_modules/@prismicio/client/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs +12 -3
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js +12 -3
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asText.cjs +11 -2
- package/dist/_node_modules/@prismicio/client/dist/helpers/asText.cjs.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asText.js +11 -2
- package/dist/_node_modules/@prismicio/client/dist/helpers/asText.js.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js.map +1 -1
- package/dist/clientHooks.cjs.map +1 -1
- package/dist/clientHooks.d.ts +44 -116
- package/dist/clientHooks.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/react-server/PrismicLink.cjs +1 -1
- package/dist/react-server/PrismicLink.cjs.map +1 -1
- package/dist/react-server/PrismicLink.js +1 -1
- package/dist/react-server/PrismicLink.js.map +1 -1
- package/dist/react-server/PrismicRichText.cjs +1 -1
- package/dist/react-server/PrismicRichText.cjs.map +1 -1
- package/dist/react-server/PrismicRichText.js +1 -1
- package/dist/react-server/PrismicRichText.js.map +1 -1
- package/package.json +30 -18
- package/src/PrismicImage.tsx +4 -1
- package/src/PrismicText.tsx +4 -1
- package/src/PrismicToolbar.tsx +1 -1
- package/src/SliceZone.tsx +111 -93
- package/src/clientHooks.ts +26 -26
- package/src/index.ts +0 -2
- package/src/react-server/PrismicLink.tsx +4 -1
- package/src/react-server/PrismicRichText.tsx +4 -1
- package/src/lib/processPolyfill.ts +0 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichText.js","sources":["../../../src/react-server/PrismicRichText.tsx"],"sourcesContent":["import * as React from \"react\";\nimport * as prismic from \"@prismicio/client\";\nimport * as prismicR from \"@prismicio/richtext\";\n\nimport { JSXFunctionSerializer, JSXMapSerializer } from \"../types\";\nimport { LinkProps, PrismicLink } from \"./PrismicLink\";\nimport { devMsg } from \"../lib/devMsg\";\n\n/**\n * Props for `<PrismicRichText>`.\n */\nexport type PrismicRichTextProps<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,\n> = {\n\t/**\n\t * The Prismic Rich Text field to render.\n\t */\n\tfield: prismic.RichTextField | null | undefined;\n\n\t/**\n\t * The Link Resolver used to resolve links.\n\t *\n\t * @remarks\n\t * If your app uses Route Resolvers when querying for your Prismic\n\t * repository's content, a Link Resolver does not need to be provided.\n\t * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * A map or function that maps a Rich Text block to a React component.\n\t *\n\t * @remarks\n\t * Prefer using a map serializer over the function serializer when possible.\n\t * The map serializer is simpler to maintain.\n\t * @example A map serializer.\n\t *\n\t * ```jsx\n\t * {\n\t * heading1: ({children}) => <Heading>{children}</Heading>\n\t * }\n\t * ```\n\t *\n\t * @example A function serializer.\n\t *\n\t * ```jsx\n\t * (type, node, content, children) => {\n\t * \tswitch (type) {\n\t * \t\tcase \"heading1\": {\n\t * \t\t\treturn <Heading>{children}</Heading>;\n\t * \t\t}\n\t * \t}\n\t * };\n\t * ```\n\t */\n\tcomponents?: JSXMapSerializer | JSXFunctionSerializer;\n\n\t/**\n\t * The React component rendered for links when the URL is internal.\n\t *\n\t * @defaultValue `<a>`\n\t */\n\tinternalLinkComponent?: React.ComponentType<LinkProps>;\n\n\t/**\n\t * The React component rendered for links when the URL is external.\n\t *\n\t * @defaultValue `<a>`\n\t */\n\texternalLinkComponent?: React.ComponentType<LinkProps>;\n\n\t/**\n\t * The value to be rendered when the field is empty. If a fallback is not\n\t * given, `null` will be rendered.\n\t */\n\tfallback?: React.ReactNode;\n};\n\ntype CreateDefaultSerializerArgs<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,\n> = {\n\tlinkResolver: LinkResolverFunction | undefined;\n\tinternalLinkComponent?: React.ComponentType<LinkProps>;\n\texternalLinkComponent?: React.ComponentType<LinkProps>;\n};\n\nconst createDefaultSerializer = <\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any>,\n>(\n\targs: CreateDefaultSerializerArgs<LinkResolverFunction>,\n): JSXFunctionSerializer =>\n\tprismicR.wrapMapSerializer({\n\t\theading1: ({ children, key }) => <h1 key={key}>{children}</h1>,\n\t\theading2: ({ children, key }) => <h2 key={key}>{children}</h2>,\n\t\theading3: ({ children, key }) => <h3 key={key}>{children}</h3>,\n\t\theading4: ({ children, key }) => <h4 key={key}>{children}</h4>,\n\t\theading5: ({ children, key }) => <h5 key={key}>{children}</h5>,\n\t\theading6: ({ children, key }) => <h6 key={key}>{children}</h6>,\n\t\tparagraph: ({ children, key }) => <p key={key}>{children}</p>,\n\t\tpreformatted: ({ node, key }) => <pre key={key}>{node.text}</pre>,\n\t\tstrong: ({ children, key }) => <strong key={key}>{children}</strong>,\n\t\tem: ({ children, key }) => <em key={key}>{children}</em>,\n\t\tlistItem: ({ children, key }) => <li key={key}>{children}</li>,\n\t\toListItem: ({ children, key }) => <li key={key}>{children}</li>,\n\t\tlist: ({ children, key }) => <ul key={key}>{children}</ul>,\n\t\toList: ({ children, key }) => <ol key={key}>{children}</ol>,\n\t\timage: ({ node, key }) => {\n\t\t\tconst img = (\n\t\t\t\t<img\n\t\t\t\t\tsrc={node.url}\n\t\t\t\t\talt={node.alt ?? undefined}\n\t\t\t\t\tdata-copyright={node.copyright ? node.copyright : undefined}\n\t\t\t\t/>\n\t\t\t);\n\n\t\t\treturn (\n\t\t\t\t<p key={key} className=\"block-img\">\n\t\t\t\t\t{node.linkTo ? (\n\t\t\t\t\t\t<PrismicLink\n\t\t\t\t\t\t\tlinkResolver={args.linkResolver}\n\t\t\t\t\t\t\tinternalComponent={args.internalLinkComponent}\n\t\t\t\t\t\t\texternalComponent={args.externalLinkComponent}\n\t\t\t\t\t\t\tfield={node.linkTo}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{img}\n\t\t\t\t\t\t</PrismicLink>\n\t\t\t\t\t) : (\n\t\t\t\t\t\timg\n\t\t\t\t\t)}\n\t\t\t\t</p>\n\t\t\t);\n\t\t},\n\t\tembed: ({ node, key }) => (\n\t\t\t<div\n\t\t\t\tkey={key}\n\t\t\t\tdata-oembed={node.oembed.embed_url}\n\t\t\t\tdata-oembed-type={node.oembed.type}\n\t\t\t\tdata-oembed-provider={node.oembed.provider_name}\n\t\t\t\tdangerouslySetInnerHTML={{ __html: node.oembed.html ?? \"\" }}\n\t\t\t/>\n\t\t),\n\t\thyperlink: ({ node, children, key }) => (\n\t\t\t<PrismicLink\n\t\t\t\tkey={key}\n\t\t\t\tfield={node.data}\n\t\t\t\tlinkResolver={args.linkResolver}\n\t\t\t\tinternalComponent={args.internalLinkComponent}\n\t\t\t\texternalComponent={args.externalLinkComponent}\n\t\t\t>\n\t\t\t\t{children}\n\t\t\t</PrismicLink>\n\t\t),\n\t\tlabel: ({ node, children, key }) => (\n\t\t\t<span key={key} className={node.data.label}>\n\t\t\t\t{children}\n\t\t\t</span>\n\t\t),\n\t\tspan: ({ text, key }) => {\n\t\t\tconst result: React.ReactNode[] = [];\n\n\t\t\tlet i = 0;\n\t\t\tfor (const line of text.split(\"\\n\")) {\n\t\t\t\tif (i > 0) {\n\t\t\t\t\tresult.push(<br key={`${i}__break`} />);\n\t\t\t\t}\n\n\t\t\t\tresult.push(<React.Fragment key={`${i}__line`}>{line}</React.Fragment>);\n\n\t\t\t\ti++;\n\t\t\t}\n\n\t\t\treturn <React.Fragment key={key}>{result}</React.Fragment>;\n\t\t},\n\t});\n\n/**\n * React component that renders content from a Prismic Rich Text field. By\n * default, HTML elements are rendered for each piece of content. A `heading1`\n * block will render an `<h1>` HTML element, for example. Links will use\n * `<PrismicLink>` by default which can be customized using the\n * `internalLinkComponent` and `externalLinkComponent` props.\n *\n * To customize the components that are rendered, provide a map or function\n * serializer to the `components` prop.\n *\n * @remarks\n * This component returns a React fragment with no wrapping element around the\n * content. If you need a wrapper, add a component around `<PrismicRichText>`.\n * @example Rendering a Rich Text field using the default HTMl elements.\n *\n * ```jsx\n * <PrismicRichText field={document.data.content} />;\n * ```\n *\n * @example Rendering a Rich Text field using a custom set of React components.\n *\n * ```jsx\n * <PrismicRichText\n * \tfield={document.data.content}\n * \tcomponents={{\n * \t\theading1: ({ children }) => <Heading>{children}</Heading>,\n * \t}}\n * />;\n * ```\n *\n * @param props - Props for the component.\n *\n * @returns The Rich Text field's content as React components.\n *\n * @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}\n * @see Learn about Rich Text serializers {@link https://prismic.io/docs/core-concepts/html-serializer}\n */\nexport function PrismicRichText<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any>,\n>({\n\tlinkResolver,\n\tfield,\n\tfallback,\n\tcomponents,\n\texternalLinkComponent,\n\tinternalLinkComponent,\n\t...restProps\n}: PrismicRichTextProps<LinkResolverFunction>): JSX.Element | null {\n\treturn React.useMemo(() => {\n\t\tif (process.env.NODE_ENV === \"development\") {\n\t\t\tif (\"className\" in restProps) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[PrismicRichText] className cannot be passed to <PrismicRichText> since it renders an array without a wrapping component. For more details, see ${devMsg(\n\t\t\t\t\t\t\"classname-is-not-a-valid-prop\",\n\t\t\t\t\t)}.`,\n\t\t\t\t\tfield,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tif (prismic.isFilled.richText(field)) {\n\t\t\tconst serializer = prismicR.composeSerializers(\n\t\t\t\ttypeof components === \"object\"\n\t\t\t\t\t? prismicR.wrapMapSerializer(components)\n\t\t\t\t\t: components,\n\t\t\t\tcreateDefaultSerializer({\n\t\t\t\t\tlinkResolver,\n\t\t\t\t\tinternalLinkComponent,\n\t\t\t\t\texternalLinkComponent,\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\t// The serializer is wrapped in a higher-order function\n\t\t\t// that automatically applies a key to React Elements\n\t\t\t// if one is not already given.\n\t\t\tconst serialized = prismicR.serialize<JSX.Element>(\n\t\t\t\tfield,\n\t\t\t\t(type, node, text, children, key) => {\n\t\t\t\t\tconst result = serializer(type, node, text, children, key);\n\n\t\t\t\t\tif (React.isValidElement(result) && result.key == null) {\n\t\t\t\t\t\treturn React.cloneElement(result, { key });\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t);\n\n\t\t\treturn <>{serialized}</>;\n\t\t} else {\n\t\t\treturn fallback != null ? <>{fallback}</> : null;\n\t\t}\n\t}, [\n\t\tfield,\n\t\tinternalLinkComponent,\n\t\texternalLinkComponent,\n\t\tcomponents,\n\t\tlinkResolver,\n\t\tfallback,\n\t]);\n}\n"],"names":["prismic.isFilled.richText"],"mappings":";;;;;;AAwFA,MAAM,0BAA0B,CAI/B,SAEA,SAAS,kBAAkB;AAAA,EAC1B,UAAU,CAAC,EAAE,UAAU,IAAU,MAAA,oBAAC,MAAc,EAAA,SAAA,GAAN,GAAe;AAAA,EACzD,UAAU,CAAC,EAAE,UAAU,IAAU,MAAA,oBAAC,MAAc,EAAA,SAAA,GAAN,GAAe;AAAA,EACzD,UAAU,CAAC,EAAE,UAAU,IAAU,MAAA,oBAAC,MAAc,EAAA,SAAA,GAAN,GAAe;AAAA,EACzD,UAAU,CAAC,EAAE,UAAU,IAAU,MAAA,oBAAC,MAAc,EAAA,SAAA,GAAN,GAAe;AAAA,EACzD,UAAU,CAAC,EAAE,UAAU,IAAU,MAAA,oBAAC,MAAc,EAAA,SAAA,GAAN,GAAe;AAAA,EACzD,UAAU,CAAC,EAAE,UAAU,IAAU,MAAA,oBAAC,MAAc,EAAA,SAAA,GAAN,GAAe;AAAA,EACzD,WAAW,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UAAU,GAAG;AAEzD,YAAI,MAAM,eAAe,MAAM,KAAK,OAAO,OAAO,MAAM;AACvD,iBAAO,MAAM,aAAa,QAAQ,EAAE,IAAK,CAAA;AAAA,QAAA,OACnC;AACC,iBAAA;AAAA,QACP;AAAA,MAAA,CACD;AAGF,6CAAU,UAAW,WAAA,CAAA;AAAA,IAAA,OACf;AACN,aAAO,YAAY,OAAU,oBAAA,UAAA,EAAA,UAAA,UAAS,IAAM;AAAA,IAC5C;AAAA,EAAA,GACC;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACA;AACF;"}
|
|
1
|
+
{"version":3,"file":"PrismicRichText.js","sources":["../../../src/react-server/PrismicRichText.tsx"],"sourcesContent":["import * as React from \"react\";\nimport * as prismic from \"@prismicio/client\";\nimport * as prismicR from \"@prismicio/richtext\";\n\nimport { JSXFunctionSerializer, JSXMapSerializer } from \"../types\";\nimport { LinkProps, PrismicLink } from \"./PrismicLink\";\nimport { devMsg } from \"../lib/devMsg\";\n\n/**\n * Props for `<PrismicRichText>`.\n */\nexport type PrismicRichTextProps<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,\n> = {\n\t/**\n\t * The Prismic Rich Text field to render.\n\t */\n\tfield: prismic.RichTextField | null | undefined;\n\n\t/**\n\t * The Link Resolver used to resolve links.\n\t *\n\t * @remarks\n\t * If your app uses Route Resolvers when querying for your Prismic\n\t * repository's content, a Link Resolver does not need to be provided.\n\t * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * A map or function that maps a Rich Text block to a React component.\n\t *\n\t * @remarks\n\t * Prefer using a map serializer over the function serializer when possible.\n\t * The map serializer is simpler to maintain.\n\t * @example A map serializer.\n\t *\n\t * ```jsx\n\t * {\n\t * heading1: ({children}) => <Heading>{children}</Heading>\n\t * }\n\t * ```\n\t *\n\t * @example A function serializer.\n\t *\n\t * ```jsx\n\t * (type, node, content, children) => {\n\t * \tswitch (type) {\n\t * \t\tcase \"heading1\": {\n\t * \t\t\treturn <Heading>{children}</Heading>;\n\t * \t\t}\n\t * \t}\n\t * };\n\t * ```\n\t */\n\tcomponents?: JSXMapSerializer | JSXFunctionSerializer;\n\n\t/**\n\t * The React component rendered for links when the URL is internal.\n\t *\n\t * @defaultValue `<a>`\n\t */\n\tinternalLinkComponent?: React.ComponentType<LinkProps>;\n\n\t/**\n\t * The React component rendered for links when the URL is external.\n\t *\n\t * @defaultValue `<a>`\n\t */\n\texternalLinkComponent?: React.ComponentType<LinkProps>;\n\n\t/**\n\t * The value to be rendered when the field is empty. If a fallback is not\n\t * given, `null` will be rendered.\n\t */\n\tfallback?: React.ReactNode;\n};\n\ntype CreateDefaultSerializerArgs<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,\n> = {\n\tlinkResolver: LinkResolverFunction | undefined;\n\tinternalLinkComponent?: React.ComponentType<LinkProps>;\n\texternalLinkComponent?: React.ComponentType<LinkProps>;\n};\n\nconst createDefaultSerializer = <\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any>,\n>(\n\targs: CreateDefaultSerializerArgs<LinkResolverFunction>,\n): JSXFunctionSerializer =>\n\tprismicR.wrapMapSerializer({\n\t\theading1: ({ children, key }) => <h1 key={key}>{children}</h1>,\n\t\theading2: ({ children, key }) => <h2 key={key}>{children}</h2>,\n\t\theading3: ({ children, key }) => <h3 key={key}>{children}</h3>,\n\t\theading4: ({ children, key }) => <h4 key={key}>{children}</h4>,\n\t\theading5: ({ children, key }) => <h5 key={key}>{children}</h5>,\n\t\theading6: ({ children, key }) => <h6 key={key}>{children}</h6>,\n\t\tparagraph: ({ children, key }) => <p key={key}>{children}</p>,\n\t\tpreformatted: ({ node, key }) => <pre key={key}>{node.text}</pre>,\n\t\tstrong: ({ children, key }) => <strong key={key}>{children}</strong>,\n\t\tem: ({ children, key }) => <em key={key}>{children}</em>,\n\t\tlistItem: ({ children, key }) => <li key={key}>{children}</li>,\n\t\toListItem: ({ children, key }) => <li key={key}>{children}</li>,\n\t\tlist: ({ children, key }) => <ul key={key}>{children}</ul>,\n\t\toList: ({ children, key }) => <ol key={key}>{children}</ol>,\n\t\timage: ({ node, key }) => {\n\t\t\tconst img = (\n\t\t\t\t<img\n\t\t\t\t\tsrc={node.url}\n\t\t\t\t\talt={node.alt ?? undefined}\n\t\t\t\t\tdata-copyright={node.copyright ? node.copyright : undefined}\n\t\t\t\t/>\n\t\t\t);\n\n\t\t\treturn (\n\t\t\t\t<p key={key} className=\"block-img\">\n\t\t\t\t\t{node.linkTo ? (\n\t\t\t\t\t\t<PrismicLink\n\t\t\t\t\t\t\tlinkResolver={args.linkResolver}\n\t\t\t\t\t\t\tinternalComponent={args.internalLinkComponent}\n\t\t\t\t\t\t\texternalComponent={args.externalLinkComponent}\n\t\t\t\t\t\t\tfield={node.linkTo}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{img}\n\t\t\t\t\t\t</PrismicLink>\n\t\t\t\t\t) : (\n\t\t\t\t\t\timg\n\t\t\t\t\t)}\n\t\t\t\t</p>\n\t\t\t);\n\t\t},\n\t\tembed: ({ node, key }) => (\n\t\t\t<div\n\t\t\t\tkey={key}\n\t\t\t\tdata-oembed={node.oembed.embed_url}\n\t\t\t\tdata-oembed-type={node.oembed.type}\n\t\t\t\tdata-oembed-provider={node.oembed.provider_name}\n\t\t\t\tdangerouslySetInnerHTML={{ __html: node.oembed.html ?? \"\" }}\n\t\t\t/>\n\t\t),\n\t\thyperlink: ({ node, children, key }) => (\n\t\t\t<PrismicLink\n\t\t\t\tkey={key}\n\t\t\t\tfield={node.data}\n\t\t\t\tlinkResolver={args.linkResolver}\n\t\t\t\tinternalComponent={args.internalLinkComponent}\n\t\t\t\texternalComponent={args.externalLinkComponent}\n\t\t\t>\n\t\t\t\t{children}\n\t\t\t</PrismicLink>\n\t\t),\n\t\tlabel: ({ node, children, key }) => (\n\t\t\t<span key={key} className={node.data.label}>\n\t\t\t\t{children}\n\t\t\t</span>\n\t\t),\n\t\tspan: ({ text, key }) => {\n\t\t\tconst result: React.ReactNode[] = [];\n\n\t\t\tlet i = 0;\n\t\t\tfor (const line of text.split(\"\\n\")) {\n\t\t\t\tif (i > 0) {\n\t\t\t\t\tresult.push(<br key={`${i}__break`} />);\n\t\t\t\t}\n\n\t\t\t\tresult.push(<React.Fragment key={`${i}__line`}>{line}</React.Fragment>);\n\n\t\t\t\ti++;\n\t\t\t}\n\n\t\t\treturn <React.Fragment key={key}>{result}</React.Fragment>;\n\t\t},\n\t});\n\n/**\n * React component that renders content from a Prismic Rich Text field. By\n * default, HTML elements are rendered for each piece of content. A `heading1`\n * block will render an `<h1>` HTML element, for example. Links will use\n * `<PrismicLink>` by default which can be customized using the\n * `internalLinkComponent` and `externalLinkComponent` props.\n *\n * To customize the components that are rendered, provide a map or function\n * serializer to the `components` prop.\n *\n * @remarks\n * This component returns a React fragment with no wrapping element around the\n * content. If you need a wrapper, add a component around `<PrismicRichText>`.\n * @example Rendering a Rich Text field using the default HTMl elements.\n *\n * ```jsx\n * <PrismicRichText field={document.data.content} />;\n * ```\n *\n * @example Rendering a Rich Text field using a custom set of React components.\n *\n * ```jsx\n * <PrismicRichText\n * \tfield={document.data.content}\n * \tcomponents={{\n * \t\theading1: ({ children }) => <Heading>{children}</Heading>,\n * \t}}\n * />;\n * ```\n *\n * @param props - Props for the component.\n *\n * @returns The Rich Text field's content as React components.\n *\n * @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}\n * @see Learn about Rich Text serializers {@link https://prismic.io/docs/core-concepts/html-serializer}\n */\nexport function PrismicRichText<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tLinkResolverFunction extends prismic.LinkResolverFunction<any>,\n>({\n\tlinkResolver,\n\tfield,\n\tfallback,\n\tcomponents,\n\texternalLinkComponent,\n\tinternalLinkComponent,\n\t...restProps\n}: PrismicRichTextProps<LinkResolverFunction>): JSX.Element | null {\n\treturn React.useMemo(() => {\n\t\tif (\n\t\t\ttypeof process !== \"undefined\" &&\n\t\t\tprocess.env.NODE_ENV === \"development\"\n\t\t) {\n\t\t\tif (\"className\" in restProps) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[PrismicRichText] className cannot be passed to <PrismicRichText> since it renders an array without a wrapping component. For more details, see ${devMsg(\n\t\t\t\t\t\t\"classname-is-not-a-valid-prop\",\n\t\t\t\t\t)}.`,\n\t\t\t\t\tfield,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tif (prismic.isFilled.richText(field)) {\n\t\t\tconst serializer = prismicR.composeSerializers(\n\t\t\t\ttypeof components === \"object\"\n\t\t\t\t\t? prismicR.wrapMapSerializer(components)\n\t\t\t\t\t: components,\n\t\t\t\tcreateDefaultSerializer({\n\t\t\t\t\tlinkResolver,\n\t\t\t\t\tinternalLinkComponent,\n\t\t\t\t\texternalLinkComponent,\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\t// The serializer is wrapped in a higher-order function\n\t\t\t// that automatically applies a key to React Elements\n\t\t\t// if one is not already given.\n\t\t\tconst serialized = prismicR.serialize<JSX.Element>(\n\t\t\t\tfield,\n\t\t\t\t(type, node, text, children, key) => {\n\t\t\t\t\tconst result = serializer(type, node, text, children, key);\n\n\t\t\t\t\tif (React.isValidElement(result) && result.key == null) {\n\t\t\t\t\t\treturn React.cloneElement(result, { key });\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t);\n\n\t\t\treturn <>{serialized}</>;\n\t\t} else {\n\t\t\treturn fallback != null ? <>{fallback}</> : null;\n\t\t}\n\t}, [\n\t\tfield,\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|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "React components and hooks to fetch and present Prismic content",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -19,12 +19,24 @@
|
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
21
|
"react-server": {
|
|
22
|
-
"
|
|
23
|
-
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./dist/react-server/index.d.ts",
|
|
24
|
+
"default": "./dist/react-server.cjs"
|
|
25
|
+
},
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/react-server/index.d.ts",
|
|
28
|
+
"default": "./dist/react-server.js"
|
|
29
|
+
}
|
|
24
30
|
},
|
|
25
31
|
"default": {
|
|
26
|
-
"
|
|
27
|
-
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.cjs"
|
|
35
|
+
},
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
}
|
|
28
40
|
}
|
|
29
41
|
},
|
|
30
42
|
"./package.json": "./package.json"
|
|
@@ -54,41 +66,41 @@
|
|
|
54
66
|
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
|
|
55
67
|
},
|
|
56
68
|
"dependencies": {
|
|
57
|
-
"@prismicio/richtext": "^2.1.
|
|
69
|
+
"@prismicio/richtext": "^2.1.5"
|
|
58
70
|
},
|
|
59
71
|
"devDependencies": {
|
|
60
|
-
"@prismicio/client": "^7.
|
|
61
|
-
"@prismicio/mock": "^0.
|
|
72
|
+
"@prismicio/client": "^7.1.0",
|
|
73
|
+
"@prismicio/mock": "^0.3.1",
|
|
62
74
|
"@size-limit/preset-small-lib": "^8.2.4",
|
|
63
75
|
"@testing-library/react": "^14.0.0",
|
|
64
76
|
"@types/node-fetch": "^3.0.3",
|
|
65
|
-
"@types/react": "^18.
|
|
77
|
+
"@types/react": "^18.2.9",
|
|
66
78
|
"@types/react-test-renderer": "^18.0.0",
|
|
67
|
-
"@typescript-eslint/eslint-plugin": "^5.59.
|
|
68
|
-
"@typescript-eslint/parser": "^5.59.
|
|
79
|
+
"@typescript-eslint/eslint-plugin": "^5.59.9",
|
|
80
|
+
"@typescript-eslint/parser": "^5.59.9",
|
|
69
81
|
"@vitejs/plugin-react": "^4.0.0",
|
|
70
|
-
"@vitest/coverage-
|
|
71
|
-
"eslint": "^8.
|
|
82
|
+
"@vitest/coverage-v8": "^0.32.0",
|
|
83
|
+
"eslint": "^8.42.0",
|
|
72
84
|
"eslint-config-prettier": "^8.8.0",
|
|
73
85
|
"eslint-plugin-prettier": "^4.2.1",
|
|
74
86
|
"eslint-plugin-react": "^7.32.2",
|
|
75
87
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
76
88
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
77
|
-
"happy-dom": "^9.
|
|
89
|
+
"happy-dom": "^9.20.3",
|
|
78
90
|
"msw": "^1.2.1",
|
|
79
91
|
"node-fetch": "^3.3.1",
|
|
80
|
-
"prettier": "^2.8.
|
|
92
|
+
"prettier": "^2.8.8",
|
|
81
93
|
"prettier-plugin-jsdoc": "^0.4.2",
|
|
82
94
|
"react": "^18.2.0",
|
|
83
95
|
"react-dom": "^18.2.0",
|
|
84
96
|
"react-test-renderer": "^18.2.0",
|
|
85
|
-
"rollup-plugin-preserve-directives": "^0.
|
|
97
|
+
"rollup-plugin-preserve-directives": "^0.2.0",
|
|
86
98
|
"size-limit": "^8.2.4",
|
|
87
99
|
"standard-version": "^9.5.0",
|
|
88
100
|
"typescript": "^5.0.4",
|
|
89
|
-
"vite": "^4.3.
|
|
101
|
+
"vite": "^4.3.9",
|
|
90
102
|
"vite-plugin-sdk": "^0.1.1",
|
|
91
|
-
"vitest": "^0.
|
|
103
|
+
"vitest": "^0.32.0"
|
|
92
104
|
},
|
|
93
105
|
"peerDependencies": {
|
|
94
106
|
"@prismicio/client": "^6 || ^7",
|
package/src/PrismicImage.tsx
CHANGED
|
@@ -116,7 +116,10 @@ export const PrismicImage = React.forwardRef(function PrismicImage(
|
|
|
116
116
|
...restProps
|
|
117
117
|
} = props;
|
|
118
118
|
|
|
119
|
-
if (
|
|
119
|
+
if (
|
|
120
|
+
typeof process !== "undefined" &&
|
|
121
|
+
process.env.NODE_ENV === "development"
|
|
122
|
+
) {
|
|
120
123
|
if (typeof alt === "string" && props.alt !== "") {
|
|
121
124
|
console.warn(
|
|
122
125
|
`[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(
|
package/src/PrismicText.tsx
CHANGED
|
@@ -44,7 +44,10 @@ export type PrismicTextProps = {
|
|
|
44
44
|
* @see Learn about Rich Text fields {@link https://prismic.io/docs/core-concepts/rich-text-title}
|
|
45
45
|
*/
|
|
46
46
|
export const PrismicText = (props: PrismicTextProps): JSX.Element | null => {
|
|
47
|
-
if (
|
|
47
|
+
if (
|
|
48
|
+
typeof process !== "undefined" &&
|
|
49
|
+
process.env.NODE_ENV === "development"
|
|
50
|
+
) {
|
|
48
51
|
if ("className" in props) {
|
|
49
52
|
console.warn(
|
|
50
53
|
`[PrismicText] className cannot be passed to <PrismicText> since it renders plain text without a wrapping component. For more details, see ${devMsg(
|
package/src/PrismicToolbar.tsx
CHANGED
|
@@ -52,7 +52,7 @@ export const PrismicToolbar = ({
|
|
|
52
52
|
// environments.
|
|
53
53
|
//
|
|
54
54
|
// @see https://github.com/capricorn86/happy-dom/blob/02ae081e36f990c06171eda44f9d885fd9413d73/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElement.ts#L191-L209
|
|
55
|
-
if (process.env.NODE_ENV === "test") {
|
|
55
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
|
|
56
56
|
// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
|
|
57
57
|
script._evaluateScript = false;
|
|
58
58
|
}
|
package/src/SliceZone.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ComponentType } from "react";
|
|
2
2
|
import * as prismic from "@prismicio/client";
|
|
3
3
|
|
|
4
4
|
import { pascalCase, PascalCase } from "./lib/pascalCase";
|
|
@@ -8,55 +8,61 @@ import { pascalCase, PascalCase } from "./lib/pascalCase";
|
|
|
8
8
|
*
|
|
9
9
|
* @typeParam Slice - The Slice from which the type will be extracted.
|
|
10
10
|
*/
|
|
11
|
-
type ExtractSliceType<
|
|
12
|
-
?
|
|
13
|
-
:
|
|
14
|
-
?
|
|
11
|
+
type ExtractSliceType<TSlice extends SliceLike> = TSlice extends prismic.Slice
|
|
12
|
+
? TSlice["slice_type"]
|
|
13
|
+
: TSlice extends SliceLikeGraphQL
|
|
14
|
+
? TSlice["type"]
|
|
15
15
|
: never;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* The minimum required properties to represent a Prismic Slice from the Prismic
|
|
19
|
-
* Rest API V2 for the
|
|
20
|
-
*
|
|
21
|
-
* If using Prismic's Rest API V2, use the `Slice` export from
|
|
22
|
-
* `@prismicio/types` for a full interface.
|
|
19
|
+
* Rest API V2 for the `unstable_mapSliceZone()` helper.
|
|
23
20
|
*
|
|
24
21
|
* @typeParam SliceType - Type name of the Slice.
|
|
25
22
|
*/
|
|
26
|
-
export type SliceLikeRestV2<
|
|
27
|
-
|
|
28
|
-
id
|
|
29
|
-
|
|
23
|
+
export type SliceLikeRestV2<TSliceType extends string = string> = Pick<
|
|
24
|
+
prismic.Slice<TSliceType>,
|
|
25
|
+
"id" | "slice_type"
|
|
26
|
+
>;
|
|
30
27
|
|
|
31
28
|
/**
|
|
32
29
|
* The minimum required properties to represent a Prismic Slice from the Prismic
|
|
33
|
-
* GraphQL API for the
|
|
30
|
+
* GraphQL API for the `unstable_mapSliceZone()` helper.
|
|
34
31
|
*
|
|
35
32
|
* @typeParam SliceType - Type name of the Slice.
|
|
36
33
|
*/
|
|
37
|
-
export type SliceLikeGraphQL<
|
|
38
|
-
type: prismic.Slice<
|
|
34
|
+
export type SliceLikeGraphQL<TSliceType extends string = string> = {
|
|
35
|
+
type: prismic.Slice<TSliceType>["slice_type"];
|
|
39
36
|
};
|
|
40
37
|
|
|
41
38
|
/**
|
|
42
39
|
* The minimum required properties to represent a Prismic Slice for the
|
|
43
|
-
*
|
|
40
|
+
* `unstable_mapSliceZone()` helper.
|
|
44
41
|
*
|
|
45
42
|
* If using Prismic's Rest API V2, use the `Slice` export from
|
|
46
|
-
* `@prismicio/
|
|
43
|
+
* `@prismicio/client` for a full interface.
|
|
47
44
|
*
|
|
48
45
|
* @typeParam SliceType - Type name of the Slice.
|
|
49
46
|
*/
|
|
50
|
-
export type SliceLike<
|
|
51
|
-
| SliceLikeRestV2<
|
|
52
|
-
| SliceLikeGraphQL<
|
|
47
|
+
export type SliceLike<TSliceType extends string = string> = (
|
|
48
|
+
| SliceLikeRestV2<TSliceType>
|
|
49
|
+
| SliceLikeGraphQL<TSliceType>
|
|
50
|
+
) & {
|
|
51
|
+
/**
|
|
52
|
+
* If `true`, this Slice has been modified from its original value using a
|
|
53
|
+
* mapper and `@prismicio/client`'s `mapSliceZone()`.
|
|
54
|
+
*
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
__mapped?: true;
|
|
58
|
+
};
|
|
53
59
|
|
|
54
60
|
/**
|
|
55
|
-
* A looser version of the `SliceZone` type from `@prismicio/
|
|
61
|
+
* A looser version of the `SliceZone` type from `@prismicio/client` using
|
|
56
62
|
* `SliceLike`.
|
|
57
63
|
*
|
|
58
64
|
* If using Prismic's Rest API V2, use the `SliceZone` export from
|
|
59
|
-
* `@prismicio/
|
|
65
|
+
* `@prismicio/client` for the full type.
|
|
60
66
|
*
|
|
61
67
|
* @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
|
|
62
68
|
*/
|
|
@@ -73,7 +79,7 @@ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
|
|
|
73
79
|
*/
|
|
74
80
|
export type SliceComponentProps<
|
|
75
81
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
76
|
-
TSlice extends SliceLike =
|
|
82
|
+
TSlice extends SliceLike = SliceLike,
|
|
77
83
|
TContext = unknown,
|
|
78
84
|
> = {
|
|
79
85
|
/**
|
|
@@ -93,7 +99,9 @@ export type SliceComponentProps<
|
|
|
93
99
|
// reference limtiations. If we had another generic to determine the full
|
|
94
100
|
// union of Slice types, it would include TSlice. This causes TypeScript to
|
|
95
101
|
// throw a compilation error.
|
|
96
|
-
slices: SliceZoneLike<
|
|
102
|
+
slices: SliceZoneLike<
|
|
103
|
+
TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2
|
|
104
|
+
>;
|
|
97
105
|
|
|
98
106
|
/**
|
|
99
107
|
* Arbitrary data passed to `<SliceZone>` and made available to all Slice
|
|
@@ -145,35 +153,6 @@ export type SliceZoneComponents<
|
|
|
145
153
|
>;
|
|
146
154
|
};
|
|
147
155
|
|
|
148
|
-
/**
|
|
149
|
-
* This Slice component can be used as a reminder to provide a proper
|
|
150
|
-
* implementation.
|
|
151
|
-
*
|
|
152
|
-
* This is also the default React component rendered when a component mapping
|
|
153
|
-
* cannot be found in `<SliceZone>`.
|
|
154
|
-
*/
|
|
155
|
-
export const TODOSliceComponent = <TSlice extends SliceLike, TContext>({
|
|
156
|
-
slice,
|
|
157
|
-
}: SliceComponentProps<TSlice, TContext>): JSX.Element | null => {
|
|
158
|
-
if (process.env.NODE_ENV === "development") {
|
|
159
|
-
const type = "slice_type" in slice ? slice.slice_type : slice.type;
|
|
160
|
-
|
|
161
|
-
console.warn(
|
|
162
|
-
`[SliceZone] Could not find a component for Slice type "${type}"`,
|
|
163
|
-
slice,
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
return (
|
|
167
|
-
<section data-slice-zone-todo-component="" data-slice-type={type}>
|
|
168
|
-
Could not find a component for Slice type “{type}
|
|
169
|
-
”
|
|
170
|
-
</section>
|
|
171
|
-
);
|
|
172
|
-
} else {
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
156
|
/**
|
|
178
157
|
* Arguments for a `<SliceZone>` `resolver` function.
|
|
179
158
|
*/
|
|
@@ -212,15 +191,10 @@ type SliceZoneResolverArgs<
|
|
|
212
191
|
export type SliceZoneResolver<
|
|
213
192
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
214
193
|
TSlice extends SliceLike = any,
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
any,
|
|
220
|
-
TContext
|
|
221
|
-
>
|
|
222
|
-
| undefined
|
|
223
|
-
| null;
|
|
194
|
+
> = (
|
|
195
|
+
args: SliceZoneResolverArgs<TSlice>,
|
|
196
|
+
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
197
|
+
ComponentType<any> | undefined | null;
|
|
224
198
|
|
|
225
199
|
/**
|
|
226
200
|
* React props for the `<SliceZone>` component.
|
|
@@ -238,7 +212,7 @@ export type SliceZoneProps<TContext = unknown> = {
|
|
|
238
212
|
* A record mapping Slice types to React components.
|
|
239
213
|
*/
|
|
240
214
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
241
|
-
components?: Record<string,
|
|
215
|
+
components?: Record<string, ComponentType<any>>;
|
|
242
216
|
|
|
243
217
|
/**
|
|
244
218
|
* A function that determines the rendered React component for each Slice in
|
|
@@ -251,14 +225,14 @@ export type SliceZoneProps<TContext = unknown> = {
|
|
|
251
225
|
* @returns The React component to render for a Slice.
|
|
252
226
|
*/
|
|
253
227
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
254
|
-
resolver?: SliceZoneResolver<any
|
|
228
|
+
resolver?: SliceZoneResolver<any>;
|
|
255
229
|
|
|
256
230
|
/**
|
|
257
231
|
* The React component rendered if a component mapping from the `components`
|
|
258
232
|
* prop cannot be found.
|
|
259
233
|
*/
|
|
260
234
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
261
|
-
defaultComponent?:
|
|
235
|
+
defaultComponent?: ComponentType<SliceComponentProps<any, TContext>>;
|
|
262
236
|
|
|
263
237
|
/**
|
|
264
238
|
* Arbitrary data made available to all Slice components.
|
|
@@ -266,6 +240,38 @@ export type SliceZoneProps<TContext = unknown> = {
|
|
|
266
240
|
context?: TContext;
|
|
267
241
|
};
|
|
268
242
|
|
|
243
|
+
/**
|
|
244
|
+
* This Slice component can be used as a reminder to provide a proper
|
|
245
|
+
* implementation.
|
|
246
|
+
*
|
|
247
|
+
* This is also the default React component rendered when a component mapping
|
|
248
|
+
* cannot be found in `<SliceZone>`.
|
|
249
|
+
*/
|
|
250
|
+
export const TODOSliceComponent = <TSlice extends SliceLike, TContext>({
|
|
251
|
+
slice,
|
|
252
|
+
}: SliceComponentProps<TSlice, TContext>): JSX.Element | null => {
|
|
253
|
+
if (
|
|
254
|
+
typeof process !== "undefined" &&
|
|
255
|
+
process.env.NODE_ENV === "development"
|
|
256
|
+
) {
|
|
257
|
+
const type = "slice_type" in slice ? slice.slice_type : slice.type;
|
|
258
|
+
|
|
259
|
+
console.warn(
|
|
260
|
+
`[SliceZone] Could not find a component for Slice type "${type}"`,
|
|
261
|
+
slice,
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
return (
|
|
265
|
+
<section data-slice-zone-todo-component="" data-slice-type={type}>
|
|
266
|
+
Could not find a component for Slice type “{type}
|
|
267
|
+
”
|
|
268
|
+
</section>
|
|
269
|
+
);
|
|
270
|
+
} else {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
269
275
|
/**
|
|
270
276
|
* Renders content from a Prismic Slice Zone using React components for each
|
|
271
277
|
* type of Slice.
|
|
@@ -281,38 +287,50 @@ export type SliceZoneProps<TContext = unknown> = {
|
|
|
281
287
|
*
|
|
282
288
|
* @see Learn about Prismic Slices and Slice Zones {@link https://prismic.io/docs/core-concepts/slices}
|
|
283
289
|
*/
|
|
284
|
-
export
|
|
290
|
+
export function SliceZone<TContext>({
|
|
285
291
|
slices = [],
|
|
286
292
|
components = {},
|
|
287
293
|
resolver,
|
|
288
294
|
defaultComponent = TODOSliceComponent,
|
|
289
295
|
context = {} as TContext,
|
|
290
|
-
}: SliceZoneProps<TContext>)
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
296
|
+
}: SliceZoneProps<TContext>) {
|
|
297
|
+
// TODO: Remove in v3 when the `resolver` prop is removed.
|
|
298
|
+
if (process.env.NODE_ENV === "development") {
|
|
299
|
+
if (resolver) {
|
|
300
|
+
console.warn(
|
|
301
|
+
"The `resolver` prop is deprecated. Please replace it with a components map using the `components` prop.",
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const renderedSlices = slices.map((slice, index) => {
|
|
307
|
+
const type = "slice_type" in slice ? slice.slice_type : slice.type;
|
|
308
|
+
|
|
309
|
+
let Comp = components[type as keyof typeof components] || defaultComponent;
|
|
310
|
+
|
|
311
|
+
// TODO: Remove `resolver` in v3 in favor of `components`.
|
|
312
|
+
if (resolver) {
|
|
313
|
+
const resolvedComp = resolver({
|
|
314
|
+
slice,
|
|
315
|
+
sliceName: pascalCase(type),
|
|
316
|
+
i: index,
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
if (resolvedComp) {
|
|
320
|
+
Comp = resolvedComp as typeof Comp;
|
|
309
321
|
}
|
|
322
|
+
}
|
|
310
323
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
324
|
+
const key =
|
|
325
|
+
"id" in slice && slice.id
|
|
326
|
+
? slice.id
|
|
327
|
+
: `${index}-${JSON.stringify(slice)}`;
|
|
315
328
|
|
|
329
|
+
if (slice.__mapped) {
|
|
330
|
+
const { __mapped, ...mappedProps } = slice;
|
|
331
|
+
|
|
332
|
+
return <Comp key={key} {...mappedProps} />;
|
|
333
|
+
} else {
|
|
316
334
|
return (
|
|
317
335
|
<Comp
|
|
318
336
|
key={key}
|
|
@@ -322,8 +340,8 @@ export const SliceZone = <TContext,>({
|
|
|
322
340
|
context={context}
|
|
323
341
|
/>
|
|
324
342
|
);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
343
|
+
}
|
|
344
|
+
});
|
|
327
345
|
|
|
328
346
|
return <>{renderedSlices}</>;
|
|
329
|
-
}
|
|
347
|
+
}
|