react-retro-hit-counter icon indicating copy to clipboard operation
react-retro-hit-counter copied to clipboard

a11y support

Open joshwcomeau opened this issue 6 years ago • 2 comments

React Retro Hit Counter creates numbers using SVG segments and a canvas border. This is totally unfriendly to screen readers.

I have a few thoughts for how to make it more accessible:

  • Aria tags
  • Adding zero-opacity text over the hit counter, something like "Hit counter with N hits"?
  • Something else?

I don't really know what would be best. Open to suggestions!

joshwcomeau avatar Jan 28 '18 12:01 joshwcomeau

First of all, awesome to see you're thinking a11y @joshwcomeau!

Since the React Retro Hit Counter is intentionally presentational, with no user action expected, your work for serving a readable version for assistive tech can be pretty simple.

For a good, solid start, you'll want to have a textual version of the counter value available in the DOM. This could be as simple as a <p>1234</p> somewhere in your main wrapper. Though, I would definitely suggest a little more context - something like <p>This site has had 1234 visitors!</p> - to round out the cues that sighted users would grok automatically.

To visually hide the alternative text, you can use a similar approach to Bootstrap's .sr-only class, meant to retain the text as machine-readable. Something like this (grabbed from this handy post on the subject):

.screen-reader-only {
   position: absolute;
   height: 1px;
   width: 1px;
   clip: rect(1px 1px 1px 1px); // IE 6 and 7
   clip: rect(1px,1px,1px,1px);
   clip-path: polygon(0px 0px, 0px 0px, 0px 0px);
   -webkit-clip-path: polygon(0px 0px, 0px 0px, 0px 0px);
   overflow: hidden !important;
 }

Since the counter itself provides no content for non-sighted users and machines, I would even consider employing aria-hidden on a wrapper (that doesn't include the text element) to ensure that screen readers that follow the ARIA spec will skip it entirely. Something like this:

<div>
  <p class="screen-reader-only">This site may or may not have had 1234 visitors!</p>
  <div aria-hidden="true">...</div>
  <canvas aria-hidden="true">...</canvas>
</div>

If you're looking to mod the counter to change it's value while it is mounted (eg. syncing ongoing traffic while a user is mid-session), you could get into some fun with ARIA's live region options as well.

stephenbelyea avatar Feb 01 '18 22:02 stephenbelyea

Oh wow, awesome. Thanks so much for this trove of info!

Sounds pretty straightforward, and about what I expected. While in my own projects I haven't made the counter update after initial render, presumably others who use this project might!

Live regions seem really neat! This counter is just presentational, so presumably some folks might want to have it auto-update? I think I'll wait and see if this counter is used anywhere outside my own projects (which don't auto-update).

Thanks again for the info :)

joshwcomeau avatar Feb 05 '18 16:02 joshwcomeau