DEV Community

Discussion on: Building a Reaction Component

Collapse
 
lexlohr profile image
Alex Lohr

Instead of aria-hidden, I would prefer role="presentation", which is for that exact use case.

Collapse
 
grahamthedev profile image
GrahamTheDev

aria-hidden removes it from the accessibility tree entirely, role="presentation" purely removes roles from an item to give it no semantic meaning.

To be fair here it probably doesn't matter as either way and support is pretty similar for both attributes so this is one of those very rare "personal preference" instances I suppose, both would be accessible.

Thread Thread
 
lexlohr profile image
Alex Lohr

I prefer role="presentation", because it conveys more semantic meaning than "hide it from assistive technology".

Thread Thread
 
grahamthedev profile image
GrahamTheDev

It is semantically incorrect to use role="presentation" here.

The icon has meaning, it isn't accompanied by a visible label.

role="presentation" is not really designed for this scenario, it is designed for either:

  • removing semantic meaning from an element (back in the days when tables were used for layout for example)
  • marking a graphic or image as decorative (background images).

Neither of the above are true.

Where role="presentation" is applicable is if you have a button with text and an icon, the icon adds nothing of value so it is presentational. That is not the case here as the icon is the label.

The most semantically correct way of creating this control would be to use the <title> and or <description> on the SVG as the label information (and using aria-labelledby etc. to ensure it is as robust as possible.

When the icon is the sole source of information you either have to give it alt attributes (or <title> which is the equivalent in SVG) or hide it entirely and replace it with a label for screen readers that makes sense.

At the end of the day, both options will work, but if we really want to be semantically correct the following is the proper way of doing it:

<label>
    <input type="checkbox" name="reaction-heart" value="75" style="--c:75" />
    <svg role="image" aria-labelledby="unicorn-title" viewBox="0 0 24 24">
         <title id="unicorn-title">React with a unicorn to this article? 75 others have given it a unicorn</title>
         [...]
    </svg>
    <span aria-hidden="true">75</span>
  </label>
  <label>
Enter fullscreen mode Exit fullscreen mode

That is the best option from a semantics perspective.

Now in the scenario I suggested we instead decided to create a label just for screen readers. In that scenario it is entirely correct to use aria-hidden as we want to remove the item we are replacing from the accessibility tree.

As I said, both options work so we are splitting hairs here, but it certainly isn't semantically correct to mark an item as presentational if it is the source of information.