@prismicio/react 3.2.2-pr.235.59c6d59 → 3.2.2-pr.235.5fa03d2
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/PrismicLink.js +4 -2
- package/dist/PrismicLink.js.map +1 -1
- package/dist/package.json.js +1 -1
- package/package.json +1 -1
- package/src/PrismicLink.tsx +9 -5
package/dist/PrismicLink.js
CHANGED
|
@@ -6,7 +6,7 @@ import { devMsg } from "./lib/devMsg.js";
|
|
|
6
6
|
const defaultInternalComponentConfigKey = Symbol.for("@prismicio/react/PrismicLink/defaultInternalComponent");
|
|
7
7
|
const defaultComponent = "a";
|
|
8
8
|
const PrismicLink = forwardRef(function PrismicLink2(props, ref) {
|
|
9
|
-
const { field, document: doc, linkResolver, internalComponent = globalThis[defaultInternalComponentConfigKey]
|
|
9
|
+
const { field, document: doc, linkResolver, internalComponent = globalThis[defaultInternalComponentConfigKey], externalComponent, children, ...restProps } = props;
|
|
10
10
|
if (DEV) {
|
|
11
11
|
if (field) {
|
|
12
12
|
if (!field.link_type) {
|
|
@@ -31,7 +31,9 @@ const PrismicLink = forwardRef(function PrismicLink2(props, ref) {
|
|
|
31
31
|
rel = restProps.rel;
|
|
32
32
|
}
|
|
33
33
|
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
34
|
-
const
|
|
34
|
+
const InternalComponent = internalComponent || defaultComponent;
|
|
35
|
+
const ExternalComponent = externalComponent || defaultComponent;
|
|
36
|
+
const Component = href ? isInternalURL(href) ? InternalComponent : ExternalComponent : InternalComponent;
|
|
35
37
|
return jsx(Component, { ref, ...attrs, ...restProps, href, rel, children: "children" in props ? children : field == null ? void 0 : field.text });
|
|
36
38
|
});
|
|
37
39
|
function isInternalURL(url) {
|
package/dist/PrismicLink.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicLink.js","sources":["../src/PrismicLink.tsx"],"sourcesContent":["import {\n\tComponentProps,\n\tComponentType,\n\tForwardedRef,\n\tHTMLAttributeAnchorTarget,\n\tReactNode,\n\tforwardRef,\n} from \"react\";\nimport {\n\ttype LinkField,\n\ttype LinkResolverFunction,\n\ttype PrismicDocument,\n\tasLinkAttrs,\n\ttype AsLinkAttrsConfig,\n} from \"@prismicio/client\";\nimport { DEV } from \"esm-env\";\n\nimport { devMsg } from \"./lib/devMsg.js\";\n\nconst defaultInternalComponentConfigKey = Symbol.for(\n\t\"@prismicio/react/PrismicLink/defaultInternalComponent\",\n);\n\n/** The default component rendered for internal and external links. */\nconst defaultComponent = \"a\";\n\n/** Props provided to a component when rendered with `<PrismicLink>`. */\nexport interface LinkProps {\n\t/** The URL to link. */\n\thref: string;\n\n\t/**\n\t * The `target` attribute for anchor elements. If the Prismic field is\n\t * configured to open in a new window, this prop defaults to `_blank`.\n\t */\n\ttarget?: HTMLAttributeAnchorTarget;\n\n\t/**\n\t * The `rel` attribute for anchor elements. If the `target` prop is set to\n\t * `\"_blank\"`, this prop defaults to `\"noopener noreferrer\"`.\n\t */\n\trel?: string;\n\n\t/** Children for the component. */\n\tchildren?: ReactNode;\n}\n\nexport type PrismicLinkProps<\n\tInternalComponentProps = ComponentProps<typeof defaultComponent>,\n\tExternalComponentProps = ComponentProps<typeof defaultComponent>,\n> = Omit<\n\tInternalComponentProps & ExternalComponentProps,\n\t\"rel\" | \"href\" | \"children\"\n> & {\n\t/**\n\t * The `rel` attribute for the link. By default, `\"noreferrer\"` is provided if\n\t * the link's URL is external. This prop can be provided a function to use the\n\t * link's metadata to determine the `rel` value.\n\t */\n\trel?: string | AsLinkAttrsConfig[\"rel\"];\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 *\n\t * @see Learn about link resolvers and route resolvers {@link https://prismic.io/docs/routes}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * The component rendered for internal URLs. Defaults to `<a>`.\n\t *\n\t * If your app uses a client-side router that requires a special Link\n\t * component, provide the Link component to this prop.\n\t */\n\tinternalComponent?: ComponentType<InternalComponentProps>;\n\n\t/** The component rendered for external URLs. Defaults to `<a>`. */\n\texternalComponent?: ComponentType<ExternalComponentProps>;\n\n\t/**\n\t * The children to render for the link. If no children are provided, the\n\t * link's `text` property will be used.\n\t */\n\tchildren?: ReactNode;\n} & (\n\t\t| {\n\t\t\t\tdocument: PrismicDocument | null | undefined;\n\t\t\t\thref?: never;\n\t\t\t\tfield?: never;\n\t\t }\n\t\t| {\n\t\t\t\tfield: LinkField | null | undefined;\n\t\t\t\thref?: never;\n\t\t\t\tdocument?: never;\n\t\t }\n\t\t| {\n\t\t\t\thref: LinkProps[\"href\"];\n\t\t\t\tfield?: LinkField | null | undefined;\n\t\t\t\tdocument?: never;\n\t\t }\n\t);\n\n/**\n * Renders a link from a Prismic link field or page.\n *\n * @example\n *\n * ```tsx\n * <PrismicLink field={slice.primary.link}>Click here</PrismicLink>;\n * ```\n *\n * @see Learn how to display links and use variants for styling: {@link https://prismic.io/docs/fields/link}\n */\nexport const PrismicLink = forwardRef(function PrismicLink<\n\tInternalComponentProps = ComponentProps<typeof defaultComponent>,\n\tExternalComponentProps = ComponentProps<typeof defaultComponent>,\n>(\n\tprops: PrismicLinkProps<InternalComponentProps, ExternalComponentProps>,\n\tref: ForwardedRef<Element>,\n) {\n\tconst {\n\t\tfield,\n\t\tdocument: doc,\n\t\tlinkResolver,\n\t\tinternalComponent = (\n\t\t\tglobalThis as {\n\t\t\t\t[defaultInternalComponentConfigKey]?: ComponentType<LinkProps>;\n\t\t\t}\n\t\t)[defaultInternalComponentConfigKey]
|
|
1
|
+
{"version":3,"file":"PrismicLink.js","sources":["../src/PrismicLink.tsx"],"sourcesContent":["import {\n\tComponentProps,\n\tComponentType,\n\tForwardedRef,\n\tHTMLAttributeAnchorTarget,\n\tReactNode,\n\tforwardRef,\n} from \"react\";\nimport {\n\ttype LinkField,\n\ttype LinkResolverFunction,\n\ttype PrismicDocument,\n\tasLinkAttrs,\n\ttype AsLinkAttrsConfig,\n} from \"@prismicio/client\";\nimport { DEV } from \"esm-env\";\n\nimport { devMsg } from \"./lib/devMsg.js\";\n\nconst defaultInternalComponentConfigKey = Symbol.for(\n\t\"@prismicio/react/PrismicLink/defaultInternalComponent\",\n);\n\n/** The default component rendered for internal and external links. */\nconst defaultComponent = \"a\";\n\n/** Props provided to a component when rendered with `<PrismicLink>`. */\nexport interface LinkProps {\n\t/** The URL to link. */\n\thref: string;\n\n\t/**\n\t * The `target` attribute for anchor elements. If the Prismic field is\n\t * configured to open in a new window, this prop defaults to `_blank`.\n\t */\n\ttarget?: HTMLAttributeAnchorTarget;\n\n\t/**\n\t * The `rel` attribute for anchor elements. If the `target` prop is set to\n\t * `\"_blank\"`, this prop defaults to `\"noopener noreferrer\"`.\n\t */\n\trel?: string;\n\n\t/** Children for the component. */\n\tchildren?: ReactNode;\n}\n\nexport type PrismicLinkProps<\n\tInternalComponentProps = ComponentProps<typeof defaultComponent>,\n\tExternalComponentProps = ComponentProps<typeof defaultComponent>,\n> = Omit<\n\tInternalComponentProps & ExternalComponentProps,\n\t\"rel\" | \"href\" | \"children\"\n> & {\n\t/**\n\t * The `rel` attribute for the link. By default, `\"noreferrer\"` is provided if\n\t * the link's URL is external. This prop can be provided a function to use the\n\t * link's metadata to determine the `rel` value.\n\t */\n\trel?: string | AsLinkAttrsConfig[\"rel\"];\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 *\n\t * @see Learn about link resolvers and route resolvers {@link https://prismic.io/docs/routes}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * The component rendered for internal URLs. Defaults to `<a>`.\n\t *\n\t * If your app uses a client-side router that requires a special Link\n\t * component, provide the Link component to this prop.\n\t */\n\tinternalComponent?: ComponentType<InternalComponentProps>;\n\n\t/** The component rendered for external URLs. Defaults to `<a>`. */\n\texternalComponent?: ComponentType<ExternalComponentProps>;\n\n\t/**\n\t * The children to render for the link. If no children are provided, the\n\t * link's `text` property will be used.\n\t */\n\tchildren?: ReactNode;\n} & (\n\t\t| {\n\t\t\t\tdocument: PrismicDocument | null | undefined;\n\t\t\t\thref?: never;\n\t\t\t\tfield?: never;\n\t\t }\n\t\t| {\n\t\t\t\tfield: LinkField | null | undefined;\n\t\t\t\thref?: never;\n\t\t\t\tdocument?: never;\n\t\t }\n\t\t| {\n\t\t\t\thref: LinkProps[\"href\"];\n\t\t\t\tfield?: LinkField | null | undefined;\n\t\t\t\tdocument?: never;\n\t\t }\n\t);\n\n/**\n * Renders a link from a Prismic link field or page.\n *\n * @example\n *\n * ```tsx\n * <PrismicLink field={slice.primary.link}>Click here</PrismicLink>;\n * ```\n *\n * @see Learn how to display links and use variants for styling: {@link https://prismic.io/docs/fields/link}\n */\nexport const PrismicLink = forwardRef(function PrismicLink<\n\tInternalComponentProps = ComponentProps<typeof defaultComponent>,\n\tExternalComponentProps = ComponentProps<typeof defaultComponent>,\n>(\n\tprops: PrismicLinkProps<InternalComponentProps, ExternalComponentProps>,\n\tref: ForwardedRef<Element>,\n) {\n\tconst {\n\t\tfield,\n\t\tdocument: doc,\n\t\tlinkResolver,\n\t\tinternalComponent = (\n\t\t\tglobalThis as {\n\t\t\t\t[defaultInternalComponentConfigKey]?: ComponentType<LinkProps>;\n\t\t\t}\n\t\t)[defaultInternalComponentConfigKey],\n\t\texternalComponent,\n\t\tchildren,\n\t\t...restProps\n\t} = props;\n\n\tif (DEV) {\n\t\tif (field) {\n\t\t\tif (!field.link_type) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[PrismicLink] This \"field\" prop value caused an error to be thrown.\\n`,\n\t\t\t\t\tfield,\n\t\t\t\t);\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`[PrismicLink] The provided field is missing required properties to properly render a link. The link will not render. For more details, see ${devMsg(\n\t\t\t\t\t\t\"missing-link-properties\",\n\t\t\t\t\t)}`,\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\t(\"text\" in field\n\t\t\t\t\t? Object.keys(field).length > 2\n\t\t\t\t\t: Object.keys(field).length > 1) &&\n\t\t\t\t!(\"url\" in field || \"uid\" in field || \"id\" in field)\n\t\t\t) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[PrismicLink] The provided field is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(\n\t\t\t\t\t\t\"missing-link-properties\",\n\t\t\t\t\t)}`,\n\t\t\t\t\tfield,\n\t\t\t\t);\n\t\t\t}\n\t\t} else if (doc) {\n\t\t\tif (!(\"url\" in doc || \"id\" in doc)) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`[PrismicLink] The provided document is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(\n\t\t\t\t\t\t\"missing-link-properties\",\n\t\t\t\t\t)}`,\n\t\t\t\t\tdoc,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tconst {\n\t\thref: computedHref,\n\t\trel: computedRel,\n\t\t...attrs\n\t} = asLinkAttrs(field ?? doc, {\n\t\tlinkResolver,\n\t\trel: typeof restProps.rel === \"function\" ? restProps.rel : undefined,\n\t});\n\n\tlet rel: string | undefined = computedRel;\n\tif (\"rel\" in restProps && typeof restProps.rel !== \"function\") {\n\t\trel = restProps.rel;\n\t}\n\n\tconst href = (\"href\" in restProps ? restProps.href : computedHref) || \"\";\n\n\tconst InternalComponent = (internalComponent ||\n\t\tdefaultComponent) as ComponentType<LinkProps>;\n\tconst ExternalComponent = (externalComponent ||\n\t\tdefaultComponent) as ComponentType<LinkProps>;\n\tconst Component = href\n\t\t? isInternalURL(href)\n\t\t\t? InternalComponent\n\t\t\t: ExternalComponent\n\t\t: InternalComponent;\n\n\treturn (\n\t\t<Component ref={ref} {...attrs} {...restProps} href={href} rel={rel}>\n\t\t\t{\"children\" in props ? children : field?.text}\n\t\t</Component>\n\t);\n}) as <\n\tInternalComponentProps = ComponentProps<typeof defaultComponent>,\n\tExternalComponentProps = ComponentProps<typeof defaultComponent>,\n>(\n\tprops: PrismicLinkProps<InternalComponentProps, ExternalComponentProps> & {\n\t\tref?: ForwardedRef<Element>;\n\t},\n) => ReactNode;\n\n/**\n * Determines if a URL is internal or external.\n *\n * @param url - The URL to check if internal or external.\n *\n * @returns `true` if `url` is internal, `false` otherwise.\n */\n// TODO: This does not detect all relative URLs as internal such as `about` or `./about`. This function assumes relative URLs start with a \"/\" or \"#\"`.\nexport function isInternalURL(url: string): boolean {\n\tconst isInternal = /^(\\/(?!\\/)|#)/.test(url);\n\tconst isSpecialLink = !isInternal && !/^https?:\\/\\//.test(url);\n\n\treturn isInternal && !isSpecialLink;\n}\n"],"names":["PrismicLink","_jsx"],"mappings":";;;;;AAmBA,MAAM,oCAAoC,OAAO,IAChD,uDAAuD;AAIxD,MAAM,mBAAmB;AA6FlB,MAAM,cAAc,WAAW,SAASA,aAI9C,OACA,KAA0B;AAE1B,QAAM,EACL,OACA,UAAU,KACV,cACA,oBACC,WAGC,iCAAiC,GACnC,mBACA,UACA,GAAG,UACA,IAAA;AAEJ,MAAI,KAAK;AACR,QAAI,OAAO;AACN,UAAA,CAAC,MAAM,WAAW;AACrB,gBAAQ,MACP;AAAA,GACA,KAAK;AAEN,cAAM,IAAI,MACT,8IAA8I,OAC7I,yBAAyB,CACzB,EAAE;AAAA,MAAA,YAGH,UAAU,QACR,OAAO,KAAK,KAAK,EAAE,SAAS,IAC5B,OAAO,KAAK,KAAK,EAAE,SAAS,MAC/B,EAAE,SAAS,SAAS,SAAS,SAAS,QAAQ,QAC7C;AACD,gBAAQ,KACP,uJAAuJ,OACtJ,yBAAyB,CACzB,IACD,KAAK;AAAA,MAAA;AAAA,eAGG,KAAK;AACf,UAAI,EAAE,SAAS,OAAO,QAAQ,MAAM;AACnC,gBAAQ,KACP,0JAA0J,OACzJ,yBAAyB,CACzB,IACD,GAAG;AAAA,MAAA;AAAA,IAEL;AAAA,EACD;AAGK,QAAA,EACL,MAAM,cACN,KAAK,aACL,GAAG,UACA,YAAY,SAAS,KAAK;AAAA,IAC7B;AAAA,IACA,KAAK,OAAO,UAAU,QAAQ,aAAa,UAAU,MAAM;AAAA,EAAA,CAC3D;AAED,MAAI,MAA0B;AAC9B,MAAI,SAAS,aAAa,OAAO,UAAU,QAAQ,YAAY;AAC9D,UAAM,UAAU;AAAA,EAAA;AAGjB,QAAM,QAAQ,UAAU,YAAY,UAAU,OAAO,iBAAiB;AAEtE,QAAM,oBAAqB,qBAC1B;AACD,QAAM,oBAAqB,qBAC1B;AACD,QAAM,YAAY,OACf,cAAc,IAAI,IACjB,oBACA,oBACD;AAEH,SACCC,IAAC,WAAS,EAAC,KAAc,GAAA,UAAW,WAAW,MAAY,eACzD,cAAc,QAAQ,WAAW,+BAAO,MAAI;AAGhD,CAAC;AAiBK,SAAU,cAAc,KAAW;AAClC,QAAA,aAAa,gBAAgB,KAAK,GAAG;AAC3C,QAAM,gBAAgB,CAAC,cAAc,CAAC,eAAe,KAAK,GAAG;AAE7D,SAAO,cAAc,CAAC;AACvB;"}
|
package/dist/package.json.js
CHANGED
package/package.json
CHANGED
package/src/PrismicLink.tsx
CHANGED
|
@@ -130,8 +130,8 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
130
130
|
globalThis as {
|
|
131
131
|
[defaultInternalComponentConfigKey]?: ComponentType<LinkProps>;
|
|
132
132
|
}
|
|
133
|
-
)[defaultInternalComponentConfigKey]
|
|
134
|
-
externalComponent
|
|
133
|
+
)[defaultInternalComponentConfigKey],
|
|
134
|
+
externalComponent,
|
|
135
135
|
children,
|
|
136
136
|
...restProps
|
|
137
137
|
} = props;
|
|
@@ -189,11 +189,15 @@ export const PrismicLink = forwardRef(function PrismicLink<
|
|
|
189
189
|
|
|
190
190
|
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
191
191
|
|
|
192
|
+
const InternalComponent = (internalComponent ||
|
|
193
|
+
defaultComponent) as ComponentType<LinkProps>;
|
|
194
|
+
const ExternalComponent = (externalComponent ||
|
|
195
|
+
defaultComponent) as ComponentType<LinkProps>;
|
|
192
196
|
const Component = href
|
|
193
197
|
? isInternalURL(href)
|
|
194
|
-
?
|
|
195
|
-
:
|
|
196
|
-
:
|
|
198
|
+
? InternalComponent
|
|
199
|
+
: ExternalComponent
|
|
200
|
+
: InternalComponent;
|
|
197
201
|
|
|
198
202
|
return (
|
|
199
203
|
<Component ref={ref} {...attrs} {...restProps} href={href} rel={rel}>
|