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

Tabbing to a phantom element? #268

Closed
suspiration opened this issue Feb 4, 2021 · 4 comments · Fixed by #269 or #270
Closed

Tabbing to a phantom element? #268

suspiration opened this issue Feb 4, 2021 · 4 comments · Fixed by #269 or #270

Comments

@suspiration
Copy link

I have this codesandbox: https://codesandbox.io/s/material-ui-accessibility-workaround-forked-gd7ok?file=/src/App.tsx

Once you've opened the dialog, there is a textfield and a button. I set FocusTrap inside the dialog around the textfield and button. When I tab between the elements, it goes to the textfield, then button, then to some phantom element before wrapping back to textfield. Any idea? Thanks in advanced!

trapfocus

@stefcameron
Copy link
Member

Hi, @suspiration , thanks for reaching out! I check it out and the problem seems to be the fragment you have as the child of the FocusTrap. The "phantom" element is the body itself, which gets focused after the close button, so the trap is effectively not working in this case. I haven't debugged it, but I think you end-up in the silent "else" clause of this IF statement, which means the focus trap doesn't even get created.

If you change your fragment for a <div>...</div>, it works great.

Fragments are special, and FocusTrap expects a single child element. Clearly, the fragment satisfies all the conditions of a single child in the render() function, but I think we end-up with [undefined] here: https://github.com/focus-trap/focus-trap-react/blob/master/src/focus-trap-react.js#L158

And then it would explain why https://github.com/focus-trap/focus-trap-react/blob/master/src/focus-trap-react.js#L75 ends-up with nodesExist === false and we never setup the trap.

First thought is FocusTrap could throw at line 158 if element === undefined, indicating a fragment, and we just don't support that (since we never have anyway).

Otherwise, FocusTrap does support multiple container elements, but that's not what we have here inside the fragment. We just have multiple "leaf" children. Nothing to build a focus trap around. So we'd have to maybe look up for the nearest parent and use that as the container, but I'm not sure what consequences there may be out in the wild.

I'm leaning on the side of throwing an exception to point out that fragments aren't supported.

If you have any thoughts, please share!

@suspiration
Copy link
Author

Thanks @stefcameron that worked!

I think throwing an error and adding in documentation that fragments cannot be used as a single child element would be helpful for other users who run into the same issue. I'm going to leave this open for you to add the changes against, unless you want it closed.

@stefcameron
Copy link
Member

Thanks @stefcameron that worked!

My pleasure!

I think throwing an error and adding in documentation that fragments cannot be used as a single child element would be helpful for other users who run into the same issue. I'm going to leave this open for you to add the changes against, unless you want it closed.

👍

stefcameron added a commit that referenced this issue Feb 6, 2021
Fixes #268

Right now, this silently "fails" and appears to work, but it's actually not
working because the trap isn't activated, because it couldn't find a DOM
element for the Fragment.

This change throws an error if a child is given and it's a Fragment.
@stefcameron
Copy link
Member

Fixing with #269

stefcameron added a commit that referenced this issue Feb 6, 2021
Fixes #268

Right now, this silently "fails" and appears to work, but it's actually not
working because the trap isn't activated, because it couldn't find a DOM
element for the Fragment.

This change throws an error if a child is given and it's a Fragment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants