-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: π§
typedRoutes
γη‘εΉεγγ (#7)
- Loading branch information
Showing
2 changed files
with
16 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
import NextLink, { type LinkProps as NextLinkProps } from 'next/link'; | ||
import { type ComponentPropsWithoutRef, type ElementRef, type ReactNode, forwardRef } from 'react'; | ||
|
||
export type LinkProps<T> = | ||
export type LinkProps = | ||
| (ComponentPropsWithoutRef<typeof NextLink> & | ||
NextLinkProps<T> & { | ||
NextLinkProps & { | ||
external?: false; | ||
}) | ||
| (ComponentPropsWithoutRef<'a'> & { | ||
external: true; | ||
}); | ||
|
||
const ExoticLink = forwardRef<ElementRef<JSX.IntrinsicElements['a'] & typeof NextLink>, LinkProps<unknown>>( | ||
({ children, ...props }, ref): ReactNode => { | ||
if ('external' in props) { | ||
const { external, href, ...anchorProps } = props; | ||
|
||
return ( | ||
<a ref={ref} href={href?.toString()} target="_blank" rel="noopener noreferrer" {...anchorProps}> | ||
{children} | ||
</a> | ||
); | ||
} | ||
const ExoticLink = forwardRef<ElementRef<JSX.IntrinsicElements['a'] & typeof NextLink>, LinkProps>(({ children, ...props }, ref): ReactNode => { | ||
if ('external' in props) { | ||
const { external, href, ...anchorProps } = props; | ||
|
||
return ( | ||
<NextLink ref={ref} {...props}> | ||
<a ref={ref} href={href?.toString()} target="_blank" rel="noopener noreferrer" {...anchorProps}> | ||
{children} | ||
</NextLink> | ||
</a> | ||
); | ||
}, | ||
); | ||
} | ||
|
||
return ( | ||
<NextLink ref={ref} {...props}> | ||
{children} | ||
</NextLink> | ||
); | ||
}); | ||
|
||
ExoticLink.displayName = ExoticLink.name; | ||
|
||
export const Link = ExoticLink as <T>(props: LinkProps<T> & { ref?: ElementRef<JSX.IntrinsicElements['a'] & typeof NextLink> }) => ReactNode; | ||
export const Link = ExoticLink; |