Skip to content

A11y: Warning on missing href on anchor tag when role="button" #1719

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

Closed
char opened this issue Sep 9, 2018 · 12 comments
Closed

A11y: Warning on missing href on anchor tag when role="button" #1719

char opened this issue Sep 9, 2018 · 12 comments

Comments

@char
Copy link

char commented Sep 9, 2018

At the moment, svelte will warn if an <a role="button" {...}>{...}</a> tag is missing an href attribute, but, changing these tags to span (and thus, getting rid of the warning) is a detriment to accessibility.

Should svelte warn if an href attribute is missing on an anchor whose role is button?

@mrkishi
Copy link
Member

mrkishi commented Oct 1, 2018

An anchor tag isn't a button. I vote it should warn on missing hrefs.

@char
Copy link
Author

char commented Oct 1, 2018

Please consider these examples:

<a role="button" class="nav-link dropdown-toggle" href="#" id="navbarDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  Dropdown
</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
  <span aria-hidden="true"></span>
  <span aria-hidden="true"></span>
  <span aria-hidden="true"></span>
</a>

@furstenberg
Copy link

YES! Svelte should warn in that case.

As far as I know you can’t emit the href attribute from an anchor tag.

It’s simple: use button element for “actions” and anchor element for navigation. With a valid href of course.

Those are bad examples when it comes to accessibility. They should both be a button with type=“button” to prevent it from “submitting”.

Try those examples in a screen reader and compare it to using a button element instead.

@Rich-Harris
Copy link
Member

Responded over on #1763. My inclination is to close this issue — just use a <button>, that's what it's for! — but I'm curious about what you mean when you say that it hinders a11y in #1763 (comment)?

@RedHatter
Copy link
Contributor

Just noting here that the specs say its valid to have an a tag without a href.

The href attribute on a and area elements is not required; when those elements do not have href attributes they do not create hyperlinks.

I tend to omit the href when the a tag becomes disabled and therefore shouldn't be navigable.

@Conduitry
Copy link
Member

I just checked, and neither Chrome nor Firefox let you tab to an <a role='button'> with no href - so I agree this should remain a warning, and am closing this.

@spiffytech
Copy link

Is there an option for disabling these warnings? I'm trying to use Bulma, which depends on un-anchored <a> tags a lot and doesn't give the option of using <button>. Right now with Svelte I'm stuck with a bunch of warnings I cannot fix without wholesale abandoning popular frameworks.

@Conduitry
Copy link
Member

Conduitry commented Jul 15, 2019

If you are using rollup-plugin-svelte, there's an onwarn option you can use. If you are using svelte-loader, there's a similar option, but it's currently broken because of a bug which has a pending PR.

Edit: The svelte-loader fix has been published.

@ps23Rick
Copy link

ps23Rick commented Feb 6, 2020

By the way.. I ran into the same thing.. What I found worked w/ Bulma was the following :

<label class="navbar-link">About</label>

instead of

<a class="navbar-link">About-Us</a>

Using doesn't alter the appearance on the loaded page like using did for me -- it worked but the rendered font label was really small.. Using seems to work just fine -- no warnings, no issues.

@igbanam
Copy link

igbanam commented Feb 22, 2022

a11y-label-has-associated-control: A11y: A form label must be associated with a control.

Gotta use that button @ps23Rick 🙂

@igbanam
Copy link

igbanam commented Feb 22, 2022

I found this workaround though

<a href={null}>Silences Compiler</a>

@jarecsni
Copy link

The null workaround by @igbanam doesnt seem to render the link properly (no anchor like styling on hover).

For me this worked fine:

<a href={'#'}>Click me</a>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants