-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Lint rules don't allow links with javascript:void href, or any alternative. #4141
Comments
|
This doesn't work as it means that the href isn't passed to the dom, meaning that the style of the anchor doesn't get the link attributes. |
I'm experiencing pretty much the same as you, I get this error from my navbar links: Confuses the hell outta me, but the page does display. |
My apologies, maybe we should consider loosening this rule... |
Could you please tell me these reasons? I recall using this technique in early 2000s when I was not familiar even with jQuery. Not sure if it is viable now. |
I use href='# ' (notice the space after #) which retains the css of a tag and doesn't alter the URL in any other way a normal # would do.
Just for visually design purposes when the route to that specific is either not defined or not implemented. I'd rather use the href attribute than add a class to use cursor: pointer |
Since there is no other way to make an anchor tag clickable I'd be curious why anyone would want this rule to exist, let alone be the default, and not have a way to override it. |
bump 👍 |
Clicking the links in the left panel has the same effect as clicking on the marker. Note, this causes a warning in React: "Links must not point to "#". Use a more descriptive href or use a button instead jsx-a11y/href-no-hash" This has been raised as an issue on GitHub, and it looks as though the developers are considering removing this warning, so for the meantime, this code will continue to throw the warning. For more information, see: facebook/create-react-app#4141
Would be really nice to remove that rule. Currently, in react-scripts 1.x it is not enforced, so having a big codebase it is hard to try out 2.0 version, without significate changes in your code. |
Yeah, for Skip to content component's link had to use Overall I'd agree to turn this rule off. |
I think instead of disabling this line we need to properly explain how to style buttons like links. For example: .ButtonLink {
background-color: transparent;
border: 1px solid transparent;
cursor: pointer;
}
.ButtonLink:hover, .ButtonLink:focus {
text-decoration: underline;
} Then use a button: <button className='ButtonLink'>Hello</button> AFAIK that would correctly behave in an accessible way without hacks. |
Because So I suggest:
Now the button can be used inline in text as one can with I also inverted how the underline works from that of Bootstrap to display as links by default with the underline removed on hover. However the other option can just as easily be done. |
To get what @peterbraden asked, I just write "#/" as href value:
The warning is gone, and the output HTML is valid. |
It's still incorrect use of an anchor tag. Why not use button? |
Maybe some clarification is in order as to why this rule exists and why it is a good idea to follow it instead of finding ways to go around it. I currently have a PR open that will make the error message clearer and also show where to find more information. The current docs for that can be found here https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md. It contains an explanation of why this rule exists. But to expand on it, abusing an Here I am referring to any user that uses a keyboard to navigate the internet. That includes users who rely on screen reader software to use the web. Exact statistics are hard to find due to the privacy impact it would have, but one study shows that the number of users likely to use a keyboard alone just due to a disability being about 7% of the working adults in the UK, US and Canada. https://www.powermapper.com/blog/website-accessibility-disability-statistics/ This does not even include those who choose to use a keyboard or sustained a temporary disability like a broken arm etc. The point is we are talking about millions and millions of people. If an If the By using the fix @gaearon suggests above and turning it into a button, EVERYONE can interact with it. All keyboard users can interact with it while screen reader users hear "Button" and expect it to perform an action and not a navigation. All that, without having to hack the The rule is not there to be an irritation but to try and assist us in making applications that can be used by the widest group of users possible :-) |
I'm aware of the rationale. In fact the desire to keep semantic navigation elements as anchors rather than use a button was the entire reason I didn't just add an onClick to a different element. |
Thank you for responding. Semantic navigation elements are very important indeed. But the reason I thought to add some information is that the browser implementation of the Without an For sighted keyboard-only users there would be absolutely no way to interact with the element, even if it has an So the discussion is not about the semantics of the element itself, but if people can even use it at all. By giving it an So the suggestion would be:
|
To clarify, the situation that I initially had, which differs from other peoples cases here, was an anchor that had a null href for a time, but never had an onClick. It was a pure navigational element that needed a null href while some async logic happened. But this was months ago, I found a workaround, I no longer really care about this ticket. Strong lint rules are annoying when you have an edge-case, such as mine. |
You can always |
You can eslint disable, you can use one of the many hacks in this thread, you can give up and use a span, or you can eject and change the lint rules. Many options, each requiring cognitive load, for what ultimately is unimportant. Life is short, I'm assuming this is WONTFIX. Closing the issue to save my inbox. |
You can also unsubscribe :-) I'd feel better about closing after https://github.com/evcohen/eslint-plugin-jsx-a11y/pull/486 gets out and we update our lint message to be more specific and useful. But either is fine. |
For a variety of valid reasons, you may want to have html anchors with a href that is null for a time. One way of doing this is
href='javascript:void(0)'
, however this triggers:Script URL is a form of eval no-script-url" lint error.
Another way is
href='#'
. This triggers:Links must not point to "#". Use a more descriptive href or use a button instead jsx-a11y/href-no-hash
Another way is
<a href>
, This triggers:Warning: Received 'true' for a non-boolean attribute 'href'.
Are there ways to achieve the desired effect without lint errors, otherwise can one of these be removed?
See:
#808
The text was updated successfully, but these errors were encountered: