We are working to implement a keyboard map click to open a map popup. With the map application element focused, the user can pan using the arrow keys and press enter or spacebar to get a popup for the center of the map (where we float a crosshairs). After the enter or spacebar event, it is very useful to change the focus to the popup so the user can quickly access controls for collapsing, docking, and closing the popup without having to tab through all of the other actionable items (e.g. widget buttons) first. It is also useful to focus first on the popup title so a screen reader reads the title after the click.
Please see this codepen: https://codepen.io/jeffreinhart/pen/LYajqNW
This should be easy enough, right? Just wrap the popup title text in a div with a class to select and a tabindex:
`<div tabindex='0' class='popup-title'>Reverse geocode: ${lon}, ${lat}</div>`,
But then, we need to wait for the SDK to add that element to the DOM before we can set focus, so we wait for the waitForElm() function to complete (line 113). That function uses a mutation observer to check if an element has been added to the DOM, a link to the SO description is provided in the function definition.
At this point, popupTitleElement.focus() on line 116 should work. But it doesn’t. I cannot figure out what else I need to wait for to be able to focus this element. Best I could come up with is a setInterval() that stops when the element finally gets focus.
Anybody have any idea what else is happening that could be blocking setting focus or a better solution than setInterval()?