Skip to content

Commit

Permalink
chore: πŸ”§ typedRoutesγ‚’η„‘εŠΉεŒ–γ—γŸ (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReoHakase committed Mar 4, 2024
1 parent ad785d9 commit 3fced9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
3 changes: 0 additions & 3 deletions apps/website/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/** @type {import('next').NextConfig} */
const config = {
experimental: {
typedRoutes: true,
},
images: {
remotePatterns: [
{
Expand Down
34 changes: 16 additions & 18 deletions packages/core/component/link/link.presenter.tsx
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;

0 comments on commit 3fced9a

Please sign in to comment.