Skip to content
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 aria-label in addition to the alt prop #30

Closed
brandonleboeuf opened this issue Oct 12, 2022 · 11 comments
Closed

Add aria-label in addition to the alt prop #30

brandonleboeuf opened this issue Oct 12, 2022 · 11 comments

Comments

@brandonleboeuf
Copy link

brandonleboeuf commented Oct 12, 2022

Browsers usually display the <title> as a tool tip, but for a11y, shouldn't this also have the aria-labelledby aria-label?

MDN refrence: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title

@rektdeckard
Copy link
Member

That's a good shout-out. We'll add this to the upcoming 2.0 release. Thanks @brandonleboeuf!

@rektdeckard
Copy link
Member

Wait, it looks like this attribute is for use when the element is labeled by visible text, which is not the case with icons. Are you sure there is an accessibility concern? I am pretty sure the <title> element achieves the desired effect for screen readers.

@brandonleboeuf
Copy link
Author

brandonleboeuf commented Jan 8, 2023

Good catch @rektdeckard ! I did mean aria-label, not aria-labelledby.

It is needed, as the icons are currently being skipped by the screen reader, even when there is something passed via the phosphor alt prop. As a work around, I have been adding a wrapper div with the aria-label around any phosphor icon that needs to be read by the screen reader.

From MDN on the title not being read by screen readers: "Text in a <title> element is not rendered as part of the graphic, but browsers usually display it as a tooltip. If an element can be described by visible text, it is recommended to reference that text with an aria-labelledby attribute rather than using the <title> element."

Though, looking at aria-labelledby, it does state to use aria-label instead if there is no visible text to reference, as you pointed out.

@brandonleboeuf brandonleboeuf changed the title Add aria-labelledby in addition to the alt prop Add aria-label in addition to the alt prop Jan 8, 2023
@brandonleboeuf brandonleboeuf changed the title Add aria-label in addition to the alt prop Add ~aria-labelledby ~ aria-label in addition to the alt prop Jan 8, 2023
@brandonleboeuf brandonleboeuf changed the title Add ~aria-labelledby ~ aria-label in addition to the alt prop Add ~aria-labelledby~ aria-label in addition to the alt prop Jan 8, 2023
@brandonleboeuf brandonleboeuf changed the title Add ~aria-labelledby~ aria-label in addition to the alt prop Add aria-label in addition to the alt prop Jan 8, 2023
@rektdeckard
Copy link
Member

rektdeckard commented Jan 8, 2023

Noted! Though, as our library docs mention, any and all valid SVG attributes you add to an icon will be applied to the rendered DOM node, so the real solution for you to just add that attribute to the icon yourself. Our title fix was specifically a convenience to address <svg> elements not respecting this attribute in the same way that HTML elements do, IE will not show a native tooltip on hover.

<House size={32} aria-label="An icon of a house" />

This will do you right. If you want both a tooltip and aria-label, add both, using alt for the title. Working example.

@brandonleboeuf
Copy link
Author

brandonleboeuf commented Jan 8, 2023

Thank you!

I think having the alt prop makes it feel like an <img> rather than an<svg> element, which would also make me think that the alt text would be all that's needed vs adding the aria-label?

It would be nice to have aria-label? added to Props section of the readme.md so that it eliminates that confusion since the docs currently state that the alt prop will "add accessible alt text to an icon".

@rektdeckard
Copy link
Member

This is good by me 👍

@benface
Copy link

benface commented Mar 7, 2024

I am a bit confused by the resolution here. What is the point of the alt prop if it's not to provide an accessible label to screen readers? 🤔 I feel pretty strongly that it should set aria-label on the <svg>, not a <title> (which is accessible in some browsers but not all, e.g. Safari based on my tests). Would you please reconsider @rektdeckard? 🙏 Happy to submit a PR.

@rektdeckard
Copy link
Member

rektdeckard commented Apr 24, 2024

@benface honestly, I don't think we should have done the alt thing to begin with. I took the suggestion in #22 and the changes made in #23 to mean that this would be accessible to screen readers. And though I don't like to add needless shorthands for things that have a perfectly good idiomatic/native way to do it, I merged it anyway. It seems this was not a good assumption.

The issues I see with modifying it further to add an aria-label attribute is that:

  1. You can already just add an aria-label to the icon as a regular prop, and this is what I'm arguing should be done
  2. What happens if you have both an alt and aria-label prop? Which takes precedence? Do you expect them to merge or overwrite? (discussed similar thing in Icon instance className overwrites context className #83)

Shorthands for things that are already short are bad. The thing I'd consider doing is deprecating the alt prop altogether, and recommending using the native HTML approach.

@benface
Copy link

benface commented Apr 24, 2024

Ah, I understand @rektdeckard. Good idea to deprecate it then. (I would personally like if aria-label was required for icons, but I understand that's an unpopular opinion.)

@rektdeckard
Copy link
Member

@benface I'll remove it from the IconProps type in the next minor version, but will probably keep the behavior around, just to safely deprecate.

Would like to educate myself a bit more on the accessibility best practices here, but it seems like there are definitely some cases where icons do not need to be labeled, such as when used for decorative purposes or made redundant by control/input/button labels. Not sure if I would want to force it.

@benface
Copy link

benface commented Apr 24, 2024

Yes @rektdeckard you are definitely right, but I prefer making it required so the developer is forced to think about it. And, if there is no need for a label, they can do alt="", like for img.

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

3 participants