-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Next.js 13 compatible version of Link adapter
- Loading branch information
1 parent
c1c3647
commit 1d3877c
Showing
2 changed files
with
149 additions
and
50 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import clsx from 'clsx'; | ||
import { usePathname } from 'next/navigation'; | ||
import NextLink, { LinkProps as NextLinkProps } from 'next/link'; | ||
import MaterialLink, { LinkProps as MaterialLinkProps } 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<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'>, | ||
Omit<NextLinkProps, 'href' | 'as' | 'passHref' | 'onMouseEnter' | 'onClick' | 'onTouchStart'> { | ||
to: NextLinkProps['href']; | ||
linkAs?: NextLinkProps['as']; | ||
} | ||
|
||
export const NextLinkComposed = React.forwardRef<HTMLAnchorElement, NextLinkComposedProps>( | ||
function NextLinkComposed(props, ref) { | ||
const { to, linkAs, ...other } = props; | ||
|
||
return <NextLink as={linkAs} ref={ref} {...other} href={to} />; | ||
}, | ||
); | ||
|
||
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<NextLinkComposedProps, 'to' | 'linkAs' | 'href'> & | ||
Omit<MaterialLinkProps, 'href'>; | ||
|
||
// A styled version of the Next.js Link component: | ||
// https://nextjs.org/docs/app/api-reference/components/link | ||
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(function Link(props, ref) { | ||
const { | ||
activeClassName = 'active', | ||
as, | ||
className: classNameProp, | ||
href, | ||
linkAs: linkAsProp, | ||
locale, | ||
noLinkStyle, | ||
prefetch, | ||
replace, | ||
role, // Link don't have roles. | ||
scroll, | ||
shallow, | ||
...other | ||
} = props; | ||
|
||
const routerPathname = usePathname(); | ||
const pathname = typeof href === 'string' ? href : href.pathname; | ||
const className = clsx(classNameProp, { | ||
[activeClassName]: routerPathname === pathname && activeClassName, | ||
}); | ||
|
||
const isExternal = | ||
typeof href === 'string' && (href.startsWith('http') || href.startsWith('mailto:')); | ||
|
||
if (isExternal) { | ||
if (noLinkStyle) { | ||
return <Anchor className={className} href={href} ref={ref} {...other} />; | ||
} | ||
|
||
return <MaterialLink className={className} href={href} ref={ref} {...other} />; | ||
} | ||
|
||
const linkAs = linkAsProp || as; | ||
const nextjsProps = { | ||
to: href, | ||
linkAs, | ||
replace, | ||
scroll, | ||
shallow, | ||
prefetch, | ||
locale, | ||
}; | ||
|
||
if (noLinkStyle) { | ||
return <NextLinkComposed className={className} ref={ref} {...nextjsProps} {...other} />; | ||
} | ||
|
||
return ( | ||
<MaterialLink | ||
component={NextLinkComposed} | ||
className={className} | ||
ref={ref} | ||
{...nextjsProps} | ||
{...other} | ||
/> | ||
); | ||
}); | ||
|
||
export default Link; |