Created
May 7, 2021 10:33
-
-
Save cezarneaga/9006b07ce20fd9ed7878e3efc97ccc50 to your computer and use it in GitHub Desktop.
Next link with Chakra in TS
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
import type { ReactNode } from 'react' | |
import { forwardRef } from 'react' | |
import type { LinkProps as ChakraLinkProps } from '@chakra-ui/react' | |
import { Link as ChakraLink } from '@chakra-ui/react' | |
import type { LinkProps as NextLinkProps } from 'next/dist/client/link' | |
import NextLink from 'next/link' | |
import { useRouter } from 'next/router' | |
export type WithChildren<Props = {}> = Props & { | |
children: ReactNode | |
} | |
export type InternalLinkProps = | |
// enforce having children | |
WithChildren & | |
// `href` is included in `NextLinkProps`, `children` are taken care of | |
Omit<ChakraLinkProps, 'href' | 'children'> & | |
// `passHref` must be passed, `as` is proxied to not collide with chakras as | |
Omit<NextLinkProps, 'passHref' | 'as'> & { | |
linkAs?: NextLinkProps['as'] | |
} | |
export const InternalLink = forwardRef<HTMLAnchorElement, InternalLinkProps>( | |
({ href, shallow, children, prefetch, replace, scroll, linkAs, ...rest }, forwardRef) => { | |
const linkProps: NextLinkProps = { | |
as: linkAs, | |
href, | |
prefetch, | |
replace, | |
scroll, | |
shallow, | |
} | |
const { pathname } = useRouter() | |
const isActive = pathname === href | |
return ( | |
<NextLink {...linkProps} passHref> | |
<ChakraLink {...rest} ref={forwardRef} aria-current={isActive ? 'page' : undefined} _hover={{ textDecoration: 'none' }}> | |
{children} | |
</ChakraLink> | |
</NextLink> | |
) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment