-
Notifications
You must be signed in to change notification settings - Fork 112
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
Bug with selecting jumping to first tabbable from last if web component is involved. #1068
Comments
@simonxabris Thanks for bringing this to my attention, and for providing a repro sandbox. Very helpful. It's interesting that it works in reverse, but not forward direction. It gets stuck going forward, but doesn't going backward. The other thing I notice is that when the trap is first activated, the focusable/tabbable In your investigation (which I also appreciate!), what has happening when pressing TAB with focus on "Day 3" (after which we'd expect it to go to I would expect we would be in I'm suspicious of what might be happening with Line 564 in ed95b7c
|
When you initiate the focus trap in the reproduction, according to I inspected the line you linked and yes it does set the Thanks for the quick response by the way, le me know if you need any additional info! Also as I said before, I'm down to submit a PR if we can find the right solution. |
@simonxabris Thank you for explaining further. I think I have a clearer understanding of the issue:
So yes, I think your suggestion of looking into the active element on line 423 to see if there's a shadow would be a good solution and I'd certainly welcome the PR you've offered to make! 😄 |
Yep, what you described is exactly what's happening! I'll try to submit that PR in the coming days |
@simonxabris Your fix has been published in |
Description
I found an issue where if you put a focus trap on a web component, like you have a
my-modal
element, and also addtabindex="0"
to it to make it focusable/tabbable, when you cycle through the tabbable elements within the web component withTab
, you cannot jump back to the first element from the last.Expected behaviour
When I hit
Tab
on the last element, the containing web-component, which is the first tabbable element is focusedActual behaviour
Hitting
Tab
on the last element seemingly does nothingReproduction
More detailed steps on how you can see the issue are provided in this stackblitz
https://stackblitz.com/edit/vitejs-vite-mxdsxb?file=index.html
Suggestion
I identified that the issue comes from here:
focus-trap/index.js
Lines 423 to 425 in ed95b7c
The
node
that we want to focus is going to be the web-component itself, anddocument.activeElement
will also be the web-component, but that's not really true, because inside its shadow-root, there's another activeElement, but that's not visible from the outside this way.My proposed solution is that when doing this check, it should recursively check if
document.activeElement
has a shadow-root and if it does, check for theactiveElement
inside that.If someone from the project can confirm that this is a bug and my solution seems okay, I'm happy to submit a PR. If you think another solution would work better, I'm happy to work on that as well.
The text was updated successfully, but these errors were encountered: