Skip to content

Commit

Permalink
[example] Make sure next.js Links can accept url objects as href (mui…
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored and m4theushw committed Jan 6, 2020
1 parent 2061039 commit 5f9ccb1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
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

0 comments on commit 5f9ccb1

Please sign in to comment.