nvda icon indicating copy to clipboard operation
nvda copied to clipboard

Setting focus to an element: NVDA doesn't announce it, when elements around it are set to aria-hidden!

Open jmuheim opened this issue 10 years ago • 31 comments

I want to create a proof of concept of a dialog box (without ARIA, only plain HTML and some basic JavaScript).

I want the focus to be set to the title of the dialog as soon as a button "open dialog" is pressed. To make the background invisible to the screenreader while the dialog is open, I set everything around the dialog to aria-hidden="true".

Interestingly, while this works nicely in JAWS and both FF/IE, NVDA seems to have problems with setting stuff to aria-hidden, as it doesn't announce the title of the dialog then (in both FF and IE). When I remove the line in the JavaScript that sets aria-hidden, NVDA announces stuff correctly.

A working demo is on CodePen:

http://codepen.io/jmuheim/pen/eZvyXq

Use the debug view for screenreader testing:

http://s.codepen.io/jmuheim/debug/eZvyXq

Can anybody tell me how I can force NVDA to announce the title even when I set aria-hidden to all elements around the dialog? I don't see any reason why it shouldn't.

jmuheim avatar Mar 21 '16 09:03 jmuheim

I have added a delay of 5secs to make sure the items are "really there", but this doesn't seem to solve the problem.

jmuheim avatar Mar 21 '16 14:03 jmuheim

In short, this is a bug. :( However, the reason it occurs is a bit obscure (it took me a good 20 minutes to figure it out) and difficult to fix without breaking other use cases. Basically, in browse mode, NVDA reports focus changes only if the browse mode cursor isn't already within the focus. We do this because unlike JAWS, NVDA moves focus as you move the browse mode cursor. While this has some big advantages, we need to make sure we don't handle a focus change caused by moving the browse mode cursor.

In this example, when you hide the content, the browse mode cursor snaps to where the hidden content would be placed if it weren't hidden, which is the start of your content area. The dialog heading is at the start of the content area. So, when NVDA compares the browse mode cursor with the position of the heading, they match (the start of the content area), so it doesn't report the focus change.

The workaround would be to have some text (even a
) at the start of your content area that doesn't get hidden when the dialog appears. However, that's an unacceptable hack. The correct solution is for us to find a way to fix this, but I have no ideas at this stage as to how we can do that.

Dev note: We should consider this when looking at #2039 again.

jcsteh avatar Mar 22 '16 14:03 jcsteh

What if you just make the behavior that you describe optional, even if you leave it enabled by default?

On Mar 22, 2016, at 9:29 AM, James Teh [email protected] wrote:

In short, this is a bug. :( However, the reason it occurs is a bit obscure (it took me a good 20 minutes to figure it out) and difficult to fix without breaking other use cases. Basically, in browse mode, NVDA reports focus changes only if the browse mode cursor isn't already within the focus. We do this because unlike JAWS, NVDA moves focus as you move the browse mode cursor. While this has some big advantages, we need to make sure we don't handle a focus change caused by moving the browse mode cursor.

In this example, when you hide the content, the browse mode cursor snaps to where the hidden content would be placed if it weren't hidden, which is the start of your content area. The dialog heading is at the start of the content area. So, when NVDA compares the browse mode cursor with the position of the heading, they match (the start of the content area), so it doesn't report the focus change.

The workaround would be to have some text (even a
) at the start of your content area that doesn't get hidden when the dialog appears. However, that's an unacceptable hack. The correct solution is for us to find a way to fix this, but I have no ideas at this stage as to how we can do that.

Dev note: We should consider this when looking at #2039 again. — You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

Laughingthunder avatar Mar 22 '16 15:03 Laughingthunder

Thanks for this quick answer.

The workaround would be to have some text (even a
) at the start of your content area that doesn't get hidden when the dialog appears.

I've done this, and it leads NVDA to announce the element as "edit" and switch to focus mode. Not really what I intended. ;-)

See http://s.codepen.io/jmuheim/debug/WwjNoQ

jmuheim avatar Mar 22 '16 15:03 jmuheim

I've found a cross-browser compatible solution like so:

  1. Check for windows
isWindows: function () {
  var operating_system = navigator.platform;
  return (operating_system == "Win32");  // If os is windows, return true
}
  1. Add a 'dot' matching the background color, just above your revealed text input:
if (this.isWindows()) {
  this.ui.input.before("<div class='nvda_dot' style='color:white;line-height:0'>.</div>");
}
  1. Then when you choose to hide the text input, just remove the 'nvda_dot'
if (this.isWindows()) {
  this.ui.input.siblings('.nvda_dot').remove();
}

Cheers

seanriordan08 avatar Oct 14 '16 16:10 seanriordan08

I'm experiencing this same bug, as described by @jcsteh. Replacing an element on the page with another element and focusing that element will not cause NVDA to read out of the contents of the new element if it was in the same position as the element being replaced.

Here is a working example, where the new element is being appended, rather than replacing an existing element. NVDA will read the new element out loud.

https://codepen.io/s-robertson/pen/MrVBOG

And here is an example of the bug, where the new element replaces the button you click. NVDA will NOT read the new element:

https://codepen.io/s-robertson/pen/zpWamp

s-robertson avatar Jan 11 '18 16:01 s-robertson

Is there any plans to fix this soon? A customer of ours that uses NVDA as their screen reader raised a bug with us relating to our dropdown menus and how bad the keyboard support is. We've built them with accessibility in mind, and the focus issue only occurs once they turn NVDA on :(

I've tested in Narrator and Voiceover but they work perfectly. Could you let us know if there are plans to fix this bug?

seancurtis avatar Feb 21 '18 06:02 seancurtis

I'm also experiencing this bug, where an element that is originally hidden, replaces an item in the same position and receives focus. NVDA doesn't read out the contents of this focused element, whereas Voiceover on Mac reads this perfectly. We also build with accessibility in mind and it'll be great if there are plans to fix this!

wongvincent avatar Jun 06 '18 23:06 wongvincent

Are there any plans to fix this @jcsteh?

seancurtis avatar Jun 07 '18 00:06 seancurtis

I'm no longer a primary developer (I now work for Mozilla), so I'm not the right person to ask about this. Certainly, I'm not likely to get to this any time soon myself.

jcsteh avatar Jun 07 '18 01:06 jcsteh

Oh grats!

Maybe I should have mentioned @michaelDCurran, @feerrenrut, @lukaszgo1, or @leonardder instead (just going by recent commit history)

seancurtis avatar Jun 07 '18 01:06 seancurtis

@seancurtis Looking over the history of this issue, despite the fact that we understand the problem it's clear that it will be difficult to fix. Especially considering other use cases. We believe it is important to fix, and have given it an appropriate priority.

feerrenrut avatar Jun 07 '18 04:06 feerrenrut

@feerrenrut thanks for the reply. As a component builder who tries to make things as accessible as possible, focus management is a crucial thing we need to handle. It'd be awesome to have consistent support for things like programatic setting of focus across the various screen readers.

seancurtis avatar Jun 07 '18 05:06 seancurtis

I believe my current situation is related to this issue. In a React application, I am replacing some content with other content showing the result of the action. Because the React reconciler re-uses the DOM elements as far as possible, the net change in the DOM is that the container tags are re-used.

After displaying the result the code then sets focus on the container element. And NVDA does not announce anything. The focus remains inside the container and that is why I believe it is related.

If I set focus outside the container first the focus action is announced just fine.

I drilled it down to the following React code:

import React, { Component, Fragment } from "react";

class Section extends React.Component {
  constructor(props) {
    super(props);

    this.sectionRef = React.createRef();
  }

  componentDidMount() {
    this.sectionRef.current.focus();
  }

  render() {
    return (
      <section tabIndex="-1" ref={this.sectionRef} aria-labelledby="headingId">
        <h2 id="headingId">This should be read out</h2>
      </section>
    );
  }
}

class App extends Component {
  state = { showSection: false };

  onClickHandler = () => {
    this.setState(prevState => ({
      showSection: !prevState.showSection
    }));
  };

  render() {
    return (
      <Fragment>
        {this.state.showSection ? <Section /> : null}
        {this.state.showSection ? null : (
          <section>
            <button type="button" onClick={this.onClickHandler}>
              Show/hide
            </button>
          </section>
        )}
      </Fragment>
    );
  }
}

export default App;

If I remove the conditional render of the second section it also gets announced just fine.

P.S. The section tag around the button element is wrong HTML, I just did this to recreate the issues in as little code as possible.

AlmeroSteyn avatar Aug 28 '18 09:08 AlmeroSteyn

Any workarounds you guys have to force NVDA to read the focused element when it's replacing something?

epatpol avatar Oct 26 '18 14:10 epatpol

This is fixed by #8869 (now in NVDA alpha builds). I've verified the fix for @jmuheim's test case from https://github.com/nvaccess/nvda/issues/5825#issue-142291794 and @s-robertson's test case from https://github.com/nvaccess/nvda/issues/5825#issuecomment-356982511.

jcsteh avatar Oct 29 '18 01:10 jcsteh

Is there a possible regression here? When trying @s-robertson's test case with NVDA 2019.1.1 in Firefox NVDA is not reading out the "I am a message". Here's what I'm doing:

  • Click anywhere in the result frame
  • Hit tab once, the button is now focussed
  • Hit enter, the button is now clicked and swaps out the button for the message

Result: The message is not read. Is this expected?

simong avatar Jun 18 '19 15:06 simong

I am still having this issue as well, with Firefox and NVDA 2019.1.1.

nmorris2 avatar Jun 20 '19 16:06 nmorris2

It's caused by the fact that #8869 was reverted. This issue probably should be reopened cc @Adriani90

lukaszgo1 avatar Jun 20 '19 16:06 lukaszgo1

Thanks very much for reporting. I am reopening this issue. cc: @jcsteh, @michaelDCurran

Adriani90 avatar Jun 20 '19 17:06 Adriani90