Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions examples/nextjs-with-typescript/src/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useRouter } from 'next/router';
import NextLink, { LinkProps as NextLinkProps } from 'next/link';
import MuiLink, { LinkProps as MuiLinkProps } from '@material-ui/core/Link';

type NextComposedProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & NextLinkProps;
type NextComposedProps = Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> &
NextLinkProps;

const NextComposed = React.forwardRef<HTMLAnchorElement, NextComposedProps>((props, ref) => {
const { as, href, replace, scroll, passHref, shallow, prefetch, ...other } = props;
Expand All @@ -31,29 +32,39 @@ interface LinkPropsBase {
naked?: boolean;
}

type LinkProps = LinkPropsBase & NextComposedProps & Omit<MuiLinkProps, 'ref'>;
export type LinkProps = LinkPropsBase & NextComposedProps & Omit<MuiLinkProps, 'href'>;

// A styled version of the Next.js Link component:
// https://nextjs.org/docs/#with-link
function Link(props: LinkProps) {
const {
href,
activeClassName = 'active',
className: classNameProps,
innerRef,
naked,
...other
} = props;
const router = useRouter();

const router = useRouter();
const pathname = typeof href === 'string' ? href : href.pathname;
const className = clsx(classNameProps, {
[activeClassName]: router.pathname === props.href && activeClassName,
[activeClassName]: router.pathname === pathname && activeClassName,
});

if (naked) {
return <NextComposed className={className} ref={innerRef} {...other} />;
return <NextComposed className={className} ref={innerRef} href={href} {...other} />;
}

return <MuiLink component={NextComposed} className={className} ref={innerRef} {...other} />;
return (
<MuiLink
component={NextComposed}
className={className}
ref={innerRef}
href={href as string}
{...other}
/>
);
}

export default React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => (
Expand Down
20 changes: 12 additions & 8 deletions examples/nextjs/src/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,43 @@ const NextComposed = React.forwardRef(function NextComposed(props, ref) {
});

NextComposed.propTypes = {
as: PropTypes.string,
href: PropTypes.string,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
prefetch: PropTypes.bool,
};

// A styled version of the Next.js Link component:
// https://nextjs.org/docs/#with-link
function Link(props) {
const {
href,
activeClassName = 'active',
className: classNameProps,
innerRef,
naked,
...other
} = props;
const router = useRouter();

const router = useRouter();
const pathname = typeof href === 'string' ? href : href.pathname;
const className = clsx(classNameProps, {
[activeClassName]: router.pathname === props.href && activeClassName,
[activeClassName]: router.pathname === pathname && activeClassName,
});

if (naked) {
return <NextComposed className={className} ref={innerRef} {...other} />;
return <NextComposed className={className} ref={innerRef} href={href} {...other} />;
}

return <MuiLink component={NextComposed} className={className} ref={innerRef} {...other} />;
return (
<MuiLink component={NextComposed} className={className} ref={innerRef} href={href} {...other} />
);
}

Link.propTypes = {
activeClassName: PropTypes.string,
as: PropTypes.string,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
className: PropTypes.string,
href: PropTypes.string,
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
naked: PropTypes.bool,
onClick: PropTypes.func,
Expand Down