import * as React from 'react' import clsx from 'clsx' import { useRouter } from 'next/router' import NextLink, { LinkProps as NextLinkProps } from 'next/link' import MuiLink, { LinkProps as MuiLinkProps } from '@mui/material/Link' import { styled } from '@mui/material/styles' // Add support for the sx prop for consistency with the other branches. const Anchor = styled('a')({}) interface NextLinkComposedProps extends Omit, 'href'>, Omit { to: NextLinkProps['href'] linkAs?: NextLinkProps['as'] } export const NextLinkComposed = React.forwardRef( // eslint-disable-next-line prefer-arrow-callback function NextLinkComposed(props, ref) { const { to, linkAs, replace, scroll, shallow, prefetch, locale, ...other } = props return ( ) }, ) export type LinkProps = { activeClassName?: string as?: NextLinkProps['as'] href: NextLinkProps['href'] linkAs?: NextLinkProps['as'] // Useful when the as prop is shallow by styled(). noLinkStyle?: boolean } & Omit & Omit // A styled version of the Next.js Link component: // https://nextjs.org/docs/api-reference/next/link const Link = React.forwardRef( // eslint-disable-next-line prefer-arrow-callback function Link(props, ref) { const { activeClassName = 'active', as, className: classNameProps, href, linkAs: linkAsProp, locale, noLinkStyle, prefetch, replace, role, // Link don't have roles. scroll, shallow, ...other } = props const router = useRouter() const pathname = typeof href === 'string' ? href : href.pathname const className = clsx(classNameProps, { [activeClassName]: router.pathname === pathname && activeClassName, }) const isExternal = typeof href === 'string' && (href.indexOf('http') === 0 || href.indexOf('mailto:') === 0) if (isExternal) { if (noLinkStyle) { return } return } const linkAs = linkAsProp || as const nextjsProps = { to: href, linkAs, replace, scroll, shallow, prefetch, locale } if (noLinkStyle) { return } return ( ) }) export default Link