Sorry for the length of the code to reproduce. The problem is that the google maps API seems to not forward certain key presses to an element in shadow DOM. If you load the following code (supplying your own API key), pan the map with the arrow keys, then try to type in the top input on the screen you'll see that the 'm', 'k', and arrow keys (at least) do not make it to the input. Strangely, the keypress
and keydown
events for those keys get triggered on the input, but not, importantly, input
. The arrow keys continue to be able to pan the map despite it no longer having focus. Typing into the bottom input (in regular DOM) works as expected.
NOTE:
- if you don't pan the map with the arrow keys the input works as expected.
- if you don't place the input in shadow DOM it works as expected.
So the problem seems to be that once the map gets keyboard focus it refuses to relinquish it. I tried giving the div with the input a tabindex
and focus
ing it. Does not resolve the problem.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1.0">
<meta charset="utf-8"/>
<style>
html, body {
font-family: sans-serif;
width: 100%;
height: 100%;
margin: 0;
}
#map-div {
width: 100%;
height: 100%;
}
#shadow {
position: fixed;
top: 50px;
left: 50px;
z-index: 1000;
}
#input {
position: fixed;
top: 100px;
left: 50px;
z-index: 1000;
}
</style>
</head>
<body>
<div id="shadow"></div>
<input id="input"></input>
<div id="map-div"></div>
<script>
document.querySelector('#shadow')
.attachShadow({mode:'open'})
.appendChild(document.createElement('input'));
window._mapsLoaded = () => {
let map = new google.maps.Map(document.querySelector('#map-div'), {
center: new google.maps.LatLng(39.75, -86.16),
zoom: 11,
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=$API_KEY&callback=_mapsLoaded"></script>
</body>
</html>