Click a button and we manually shift focus to the first focusable element in a “container”.
We trap the focus in this container, ultimately you loop in the container until you click a “close button”, and it takes you back to the open button. This is part of our accessibility.
The issue arises if the last "focusable element is actually hidden.
E.g. the + sign on the last link (which is a button) indicates that there’s a hidden link. That being hidden breaks the plugin. I’m not sure how to even approach this in Javascript. It’s a dynamic situation and sometimes the last focusable element actually is visible . There might be multiple links in that last dropdown.
Can you not test the element to see if it is hidden? element.style.visibility === 'hidden' is what is coming to mind. If it is not hidden, set focus. Otherwise move to the last element that is visible and set focus there.
Maybe I am not understanding. I am assuming here that in your example since the last element is hidden, it is not setting the “Info For…” element as the focusable element. If this is the scenario, again check if the last item is hidden, since it is, you would go to the previous element, check if it is hidden and since it is not, set that to be the item focused. If the last item is visible, then you would set the focus on that.
Either way, I think you just need to check if the element is hidden or not and know whether or not to focus it or some element before it.
What I expressed is vanilla. The style property of an element is straight out vanilla JS.
Ideally yes, in the keydown you can check the visibility of the element an do what you like. You can also just put the visibility check in your filter callback function. If it is not visible, then just don’t include it in focusableEls.
No need to alter the selector (it is already horrendously long). Just put the check in your filter callback. s.style.visibility === 'visible'. If it doesn’t meet the check, then it it is filtered out.