Skip to content

Commit

Permalink
refactor: use React.ElementType over string | React.ComponentType
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Dec 16, 2021
1 parent e2d1af4 commit 19094f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
29 changes: 8 additions & 21 deletions src/PrismicLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import { isInternalURL } from "./lib/isInternalURL";

import { usePrismicContext } from "./usePrismicContext";

type ComponentProps<T> = T extends React.ComponentType<infer U>
? U
: T extends keyof JSX.IntrinsicElements
? React.ComponentProps<T>
: unknown;

/**
* Props provided to a component when rendered with `<PrismicLink>`.
*/
Expand Down Expand Up @@ -43,15 +37,12 @@ export interface LinkProps {
* Props for `<PrismicLink>`.
*/
export type PrismicLinkProps<
InternalComponent extends string | React.ComponentType<LinkProps> =
| string
| React.ComponentType<LinkProps>,
ExternalComponent extends string | React.ComponentType<LinkProps> =
| string
| React.ComponentType<LinkProps>,
InternalComponent extends React.ElementType<LinkProps> = React.ElementType<LinkProps>,
ExternalComponent extends React.ElementType<LinkProps> = React.ElementType<LinkProps>,
LinkResolverFunction extends prismicH.LinkResolverFunction = prismicH.LinkResolverFunction,
> = Omit<
ComponentProps<InternalComponent> & ComponentProps<ExternalComponent>,
React.ComponentProps<InternalComponent> &
React.ComponentProps<ExternalComponent>,
keyof LinkProps
> & {
/**
Expand Down Expand Up @@ -142,12 +133,8 @@ const defaultExternalComponent = "a";
* link is internal or external.
*/
export const PrismicLink = <
InternalComponent extends
| string
| React.ComponentType<LinkProps> = typeof defaultInternalComponent,
ExternalComponent extends
| string
| React.ComponentType<LinkProps> = typeof defaultExternalComponent,
InternalComponent extends React.ElementType<LinkProps> = typeof defaultInternalComponent,
ExternalComponent extends React.ElementType<LinkProps> = typeof defaultExternalComponent,
LinkResolverFunction extends prismicH.LinkResolverFunction = prismicH.LinkResolverFunction,
>(
props: PrismicLinkProps<
Expand Down Expand Up @@ -180,12 +167,12 @@ export const PrismicLink = <
const rel =
props.rel || (target === "_blank" ? "noopener noreferrer" : undefined);

const InternalComponent =
const InternalComponent: React.ElementType<LinkProps> =
props.internalComponent ||
context.internalLinkComponent ||
defaultInternalComponent;

const ExternalComponent =
const ExternalComponent: React.ElementType<LinkProps> =
props.externalComponent ||
context.externalLinkComponent ||
defaultExternalComponent;
Expand Down
4 changes: 2 additions & 2 deletions src/PrismicProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export type PrismicContextValue = {
/**
* The component rendered by `<PrismicLink>` for internal URLs. Defaults to `<a>`.
*/
internalLinkComponent?: string | React.ComponentType<LinkProps>;
internalLinkComponent?: React.ElementType<LinkProps>;

/**
* The component rendered by `<PrismicLink>` for external URLs. Defaults to `<a>`.
*/
externalLinkComponent?: string | React.ComponentType<LinkProps>;
externalLinkComponent?: React.ElementType<LinkProps>;

/**
* Children for the component.
Expand Down

0 comments on commit 19094f6

Please sign in to comment.