Skip to content

Instantly share code, notes, and snippets.

@zackdotcomputer
Last active August 27, 2022 15:43
Show Gist options
  • Save zackdotcomputer/d7af9901e7db87364aad7fbfadb5c99b to your computer and use it in GitHub Desktop.
Save zackdotcomputer/d7af9901e7db87364aad7fbfadb5c99b to your computer and use it in GitHub Desktop.
Workaround for next/link vs <a> href accessibility lint issue
// Created by Zack Sheppard (@zackdotcomputer) on 1/19/2021
// Freely available under MIT License
// Workaround for https://github.com/vercel/next.js/issues/5533
import Link, { LinkProps } from "next/link";
import { AnchorHTMLAttributes, PropsWithChildren } from "react";
type PropTypes = LinkProps & Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href">;
/// A unified component for the next/link <Link> and a standard <a> anchor.
/// Will lift href and all other props from Link up to the Link.
/// Will automatically make an <a> tag containing the children and pass it remaining props.
const LinkTo = ({
children,
href,
as,
replace,
scroll,
shallow,
prefetch,
locale,
...anchorProps
}: PropsWithChildren<PropTypes>) => {
return (
// These props are lifted up to the `Link` element. All others are passed to the `<a>`
<Link {...{ href, as, replace, scroll, shallow, prefetch, locale }}>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<a {...anchorProps}>{children}</a>
</Link>
);
};
export default LinkTo;
@Igor-Iugin
Copy link

Hi made a similar solution but instead of AnchorHTMLAttributes I used HTMLAttributes. Just wanted to thank you for the solution :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment