You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.8 KiB
71 lines
1.8 KiB
3 years ago
|
import * as React from "react"
|
||
|
import clsx from 'clsx'
|
||
|
import { useRouter } from 'next/router'
|
||
|
import NextLink from 'next/link'
|
||
|
import MuiLink from '@mui/material/Link'
|
||
|
|
||
|
export const NextLinkComposed = React.forwardRef(function NextLinkComposed(props: any, ref) {
|
||
|
const { to, linkAs, href, replace, scroll, passHref, shallow, prefetch, locale, ...other } = props
|
||
|
|
||
|
return (
|
||
|
<NextLink
|
||
|
href={to}
|
||
|
prefetch={prefetch}
|
||
|
as={linkAs}
|
||
|
replace={replace}
|
||
|
scroll={scroll}
|
||
|
shallow={shallow}
|
||
|
passHref={passHref}
|
||
|
locale={locale}
|
||
|
>
|
||
|
<a ref={ref} {...other} />
|
||
|
</NextLink>
|
||
|
)
|
||
|
})
|
||
|
|
||
|
// A styled version of the Next.js Link component:
|
||
|
// https://nextjs.org/docs/#with-link
|
||
|
const Link = React.forwardRef(function Link(props: any, ref) {
|
||
|
const {
|
||
|
activeClassName = "active",
|
||
|
as: linkAs,
|
||
|
className: classNameProps,
|
||
|
href,
|
||
|
noLinkStyle,
|
||
|
role, // Links don't have roles.
|
||
|
...other
|
||
|
} = props
|
||
|
|
||
|
const router = useRouter()
|
||
|
const pathname: string = typeof href === "string" ? href : href.pathname
|
||
|
const className = clsx(classNameProps, {
|
||
|
[activeClassName]: router.pathname === pathname && activeClassName,
|
||
|
})
|
||
|
|
||
|
const isExternal: boolean = typeof href === "string" && (href.indexOf('http') === 0 || href.indexOf('mailto:') === 0)
|
||
|
|
||
|
if (isExternal) {
|
||
|
if (noLinkStyle) {
|
||
|
return <a className={className} href={href} ref={ref} {...other} />
|
||
|
}
|
||
|
|
||
|
return <MuiLink className={className} href={href} ref={ref} {...other} />
|
||
|
}
|
||
|
|
||
|
if (noLinkStyle) {
|
||
|
return <NextLinkComposed className={className} ref={ref} to={href} {...other} />
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
<MuiLink
|
||
|
component={NextLinkComposed}
|
||
|
linkAs={linkAs}
|
||
|
className={className}
|
||
|
ref={ref}
|
||
|
to={href}
|
||
|
{...other}
|
||
|
/>
|
||
|
)
|
||
|
})
|
||
|
|
||
|
export default Link
|