-
-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option for passed hrefs (as in Next.js) (anchor-is-valid) #402
Comments
Here's a workaround (i.e., disabling the rule's "jsx-a11y/anchor-is-valid": [ "error", {
"components": [ "Link" ],
"specialLink": [ "hrefLeft", "hrefRight" ],
"aspects": [ "invalidHref", "preferButton" ]
}] |
That sounds like a really strange API; if it’s a Link why would it need an |
It's indeed a strange API. The reason they did it like this is probably so the Link element can be rendered as any, not only an anchor. But there's an |
Btw; yep, what you said is literally what the Next.js Link does... https://github.com/zeit/next.js/blob/canary/lib/link.js#L149 |
I’m not sure why they wouldn’t accept a component (or string “a”) so they can render the element directly rather than cloning :-/ Either way I’m not sure that supporting this generically is something either possible or reasonable to do. |
I see... Thanks! |
I’ll leave this open if somebody has an idea of how to handle it, but hardcoding an arbitrary library’s strange convention is a nonstarter. |
Yeah, this is really a fault of the next.js |
This will disable all linting
|
@T04435 looking for the same solution. |
In response to @sk22 adding the following to
Does this also mean, eslint is suggesting to use I would rather turn off the rule to make it work with Next.js. |
If it has a URL to navigate to, it should be an |
But why not? Button can be used to redirect user to some page. For example the form usually has a "submit" button and "cancel" - and end user by pressing cancel button is waiting to be redirected out of the form and "submit" button also should submit and redirect somewhere. It's a natural behavior. |
@FDiskas because that’s not how semantic html works. Buttons are for submitting forms, and that’s not navigation. Cancel buttons shouldn’t exist; that should either be a link, or the normal browsers’ reload button. |
The suggested linting workaround to disable the noHref check works, but there are additional checks done on While this is obviously a weirdness with Next.js' Link component inserting the |
An alternative (but hacky) solution is to enable the
|
jsx-eslint/eslint-plugin-jsx-a11y#402 next.js의 link컴포넌트를 사용하기 위해서 eslint 설정변경
I just put my hacky solution up as a gist - I've been using a wrapper component that internally creates the |
As usually you won't use the Link component on many pages (mostly only within a Navigation or Layout component), you could also easily disable the next line: return (
<Link href={yourUrl}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a {...yourProps}>{yourLinkName}</a>
</Link>
); |
@wheelmaker24 Agreed, a one-by-one disable or the I don't think those are going to be efficient if you're building a site that uses links in more contexts, though, like an ecommerce or a webapp. If you have Links all over the place you're basically stuck either turning off the rule in your config or pulling in a wrapper component to work around the issue until Next or ESLint can fix this. :-\ |
There's nothing we can really do except hardcoding the next.js pattern, which is all kinds of brittle. The proper solution is for next.js to follow the react best practice that long predates their existence: avoid patterns that necessitate cloning elements. |
No disagreement on what the ideal solution is @ljharb, but I disagree that hardcoding an exception for the Next.js pattern is the only non-brittle solution. If we could not only add certain components to the scope of this rule (as the |
@zackdotcomputer true. but then users would be able to exclude |
If you're worried about missing un-nested If you're just worried about developers being able to disable |
Hmm. I suppose if:
then perhaps the risk is minimal, and the only downside would be that we'd have to maintain a rule option forever because a single framework made a poor design choice and hasn't fixed it after a number of years :-/ |
Since I'm having trouble reading the tone of voice on your post as to whether you will or won't consider adding this option, I'm going to open a PR to add a "Case: I use Next.js Link Components" to the description of this rule so that folks can more easily find this thread of workarounds. If this option winds up getting added, that case can be updated to point users to it. |
This is my work around
|
@zackdotcomputer your tone read is accurate, as I'm not convinced either way myself, as every outcome seems differently terrible. I'll take a look at the PR. |
I know this problem from nextjs, but just started using |
@chiptus I noticed that the new NextJS eslint env turns off this rule since it can't be configured to work with their API. I think the unfortunate end state here for at least the near future is that this rule will have to be disabled if one is using a framework that follows the "wrapper component takes the href" pattern. That pattern doesn't seem to be going away and there doesn't appear to be any movement to alter this rule to support it. |
jsx-eslint/eslint-plugin-jsx-a11y#402 If the <a> tag wrapped by <Link href="xx" passHref> doesn't have the href attribute set, eslint complains.
I'm also looking 👀 for a way to deal with this. My current work around looks like this: import Link, { LinkProps } from "next/link";
import React, { FC } from "react";
type NextLinkProps = LinkProps & {
href: string;
className?: string;
};
/**
* Standard way of using the Next's `Link` tag together with the `a` tag
*/
const NextLink: FC<NextLinkProps> = ({ href, className, children, ...rest }) => (
<Link href={href} {...rest}>
<a href={href} className={className}>
{children}
</a>
</Link>
);
export default NextLink; Usage: <NextLink href={"/register"} className="font-medium">
Register
</NextLink> |
This seems resolved, both on the next side in a future major and on this side via config. I’ll reopen if that’s not the case. |
Could you link to the respective changes in Next and config you're referring to if you have them handy? |
The needed config is mentioned multiple times upthread. |
I'd suggest adding an option for
a
tags that don't have anhref
because its containing component passes thehref
to it.For example, Next.js' Link component takes the
href
but requires ana
tag as a child:Creating such structure triggers the anchor-is-valid rule:
At the moment, I don't see any practical usage of the anchor-is-valid rule that allows passing the href to the child.
A possible fix could be to check if the parent component (i.e. Link) has the
passHref
prop set (which is optional in Next.js when the child is ana
tag without an ownhref
).Other than that, the rule could just check for the
<Link href><a></Link>
structure.The text was updated successfully, but these errors were encountered: