简体   繁体   中英

What is the best way to announce some text to screen readers (ie. role="alert", aria-live="assertive/polite")?

I'm in the process of creating a searchable combobox component. It must be 100% accessible, so I want to tell screen readers how many results there are available when they open the component's options dropdown, and when they are changing its filter term.

The component looks something like this:

成分

I think I will probably need a live region. I know two ways of doing this:

1) role="alert"

This is AFAIK the safest option: it used to work years ago already, and it is pretty well supported by all major browsers (Chrome/Edge, FF) and screen readers (JAWS, NVDA, VoiceOver/iOS, Talkback).

The bad thing: it is "rude", ie. it interrupts the screen reader's current announcement. In addition, some browsers add an ugly "Alert" as a prefix to each such announcement.

So for example, if the options dropdown is expanded (the user presses Down key after focusing the filter input), then two things happen at the same time:

  • The filter input's aria-expanded is set from false to true , which makes the screen reader announce expanded
  • The dropdown is now visible, containing a live region with text 4 of 12 options available, starting with "Badminton"

If this live region is a role="alert" , then it potentially interrupts (⚡️) the expanded announcement, which feels like a hiccup from the screen reader, ie. expa⚡️ 4 of 12 options... .

2) aria-live="polite"

This is the more gentle option: it will append the live region's content to the current announcement of the screen reader (instead of interrupting it), ie. expanded 4 of 12 options... .

Sadly, this is not supported with JAWS+Chrome, as far as my tests show - can anyone confirm this? If so, this is extremely lame, taking into account that its the most used desktop combination, and behind both are multi-national (and probably multi-billion) companies.

Conclusion

I can't just use the gentle aria-live everywhere, I need to figure out which browser is used, and if it's Chrome, I need to reside to role="alert" .

But there are more problems: there seem to be serious differences between how browsers (and/or screen readers) handle live regions:

  • Some have problems recognising a live region if it's first hidden (ie. hidden attribute, or display: none ) and then made visible. It will then simply not be treated as live region.
  • Some immediately announce the content of such a made-visible live region, while others just ignore their initial content (they will only announce a change to it).
  • Some announce a role="alert" element even when it's already there when the page is loaded, others don't (and I think the latter ones are right, because as far as I understand, live regions should announce changes of content, not content that's initially there already)

So this topic is much more complex than I hoped. The simplest solution would probably be the following: just append a visually hidden role="alert" element at the end of the DOM each time an announcement is be made. Then, a few seconds later, remove it again. But this feels ugly to me. I'd rather have my live region right inside my component, where also visible users can see it, or at least parts of it: on the screenshot you can see 4 of 12 options for filter "d" , but to screen readers, the same text is announced, plus some more details, ie. starting with option "Badminton" . Keeping visual and screen reader announcement in the same place feels like good practice to me, so I will not easily forget about the latter ones, or I will spot some bugs quickly (that would otherwise may be not visible).

So my question is: is there maybe an existing script or library which solves this problem in a solid way and provides optimal experience for the most common browser / screen reader combos?

Thank you.

Polite live regions should be preferred. As you have noticed yourself, alert interrupts current speech, it's rude. The prefix "Alert." automatically added might even feel a little scary at times.

As you have noticed, polite live regions are working a little different on each browser + screen reader combinations. Some of these differences are intentional, others are just bugs, and others are simply due to the fact that the official specification doesn't clearly define any particular behavior. This makes the whole thing, as you have said, actually quite complicated. Sadly I have no universally working solution. In order to handle all cases, you need to make user agent detection.

Let's look at various issues or points of attention we have with live regions today (in may 2022). I have no official reference, the following is only my own observations along the years:

Live region detection

A live regions is successfully taken into account as such only when aria-live attribute is present when the element is added to the DOM , and when it is visible by screen readers .

This, in particular, means that it isn't taken into account when the aria-live attribute is set after the element has been added in the DOM, when the element is display:none, visibility:hidden, when attributes aria-hidden or hidden are set, or when the size is 0. Even if hidding attributes are removed later on, it doesn't work.

Announcement time

The only two cases when the content of a live region has to be announced are:

  • When the element is added in the DOM
  • When the content of the element has changed

For all other cases, it isn't well defined in the specification. In particular, initial content present directly in HTML code may not be announced, and as far as I have already observed, none of the browsers I have already used ever announce initial content at page load.

Interruptions

When an assertive live region is added, speech is normally always interrupted. Polite live regions updates should never interrupt speech.

However, with many browsers + screen readers combinations, a content currently being read is shut up:

  • When the aria-live element is removed from the DOM
  • When its content is emptied

This is for example the case with VoiceOver both under iOS and mac.

So if you have multiple messages to be spoken potentially quick in succession, you shouldn't just replace the older message by the newer one. You can adopt several strategies:

  • Simply append new messages to the already existing live region
  • Create new live regions each time
  • Clear live region content or remove it from the DOM only if the last messages is older than a few seconds ago

But, be careful, that's not all.

Repeat or not repeat

With some browsers + screen readers combinations, if you simply append new messages to be announced at the end of a live region, the whole content is repeated over and over again from the beginning. In short, the whole content is read each time there is a change, even parts that haven't changed.

IN theory, the standard provides the two attributes aria-atomic and aria-relevant to control this behavior. Defaults values are aria-atopic=false and aria-relevant=additions, meaning that only the new content has to be read, and content that hasn't changed shouldn't be read again.

However, in practice:

  • Chrome with both Jaws and NVDA ignore these attributes completely. The whole content is always read at every change no matter what.
  • With Firefox and Jaws, the aria-live region no longer works when aria-relevant attribute is explicitly set
  • With at least some versions of Safari, the aria-live region doesn't work if aria-relevant=additions isn't explicitly set
  • With some versions of Chrome, if for any reason you want to speak twice the same message, replacing content doesn't work, even if you replace first by the empty string and then by the content again

Are we safe if we create a new live region for each message? Spoiler alert: no!

Announcement order

When several live regions are added to the DOM or updated quickly, nothing says which one has to be announced first.

  • Safari iOS seem to handle it correctly, ie read content in the order of update. I don't know on mac.
  • With Chrome and both Jaws and NVDA, updating quickly several live regions seem to make them being read in reverse order of their position in the DOM.

Conclusion

At present, it looks like the safest is to create new polite live regions for each message. You can reuse them, but only if new messages don't arrive too quickly, or if it doesn't matter if they are occasionally interrupted.

For your particular case, I would say that a single reused polite live region is sufficient. But of course the best is to test as many combinations as possible.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM