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

Popup keyboard navigation bug when dismissing popup #726

Merged
merged 13 commits into from
Jan 31, 2023
9 changes: 7 additions & 2 deletions src/mapml/features/featureGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export var FeatureGroup = L.FeatureGroup.extend({
* @private
*/
_handleFocus: function(e) {
// tab, shift, cr, esc, up, left, down, right,
if(([9, 16, 27, 37, 38, 39, 40].includes(e.keyCode)) && e.type === "keydown"){
let index = this._map.featureIndex.currentIndex;
// Down/right arrow keys replicate tabbing through the feature index
Expand Down Expand Up @@ -94,13 +95,17 @@ export var FeatureGroup = L.FeatureGroup.extend({
obj._map.featureIndex.inBoundFeatures[0].path.setAttribute("tabindex", 0);
}, 0);
}
// tab, shift, cr, esc, right, left, up, down,
// 1, 2, 3, 4, 5, 6, 7 (featureIndexOverlay available index items
// [8, 9] being allocated to next, previous menu items).
} else if (!([9, 16, 13, 27, 37, 38, 39, 40, 49, 50, 51, 52, 53, 54, 55].includes(e.keyCode))){
this._map.featureIndex.currentIndex = 0;
this._map.featureIndex.inBoundFeatures[0].path.focus();
}


// 27 added so that the tooltip opens when dismissing popup with 'esc' key
if(e.target.tagName.toUpperCase() !== "G") return;
if(([9, 13, 16, 37, 38, 39, 40, 49, 50, 51, 52, 53, 54, 55].includes(e.keyCode)) && e.type === "keyup") {
if(([9, 13, 16, 37, 38, 39, 40, 49, 50, 51, 52, 53, 54, 55, 27].includes(e.keyCode)) && e.type === "keyup") {
AliyanH marked this conversation as resolved.
Show resolved Hide resolved
this.openTooltip();
} else if (e.keyCode === 13 || e.keyCode === 32){
this.closeTooltip();
Expand Down
8 changes: 5 additions & 3 deletions src/mapml/layers/MapLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,17 +1330,19 @@ export var MapMLLayer = L.Layer.extend({
let path = focusEvent.originalEvent.path || focusEvent.originalEvent.composedPath();
let isTab = focusEvent.originalEvent.keyCode === 9,
shiftPressed = focusEvent.originalEvent.shiftKey;
if((path[0].classList.contains("leaflet-popup-close-button") && isTab && !shiftPressed) || focusEvent.originalEvent.keyCode === 27){
if ((path[0].classList.contains("leaflet-popup-close-button") && isTab && !shiftPressed) ||
focusEvent.originalEvent.keyCode === 27 ||
path[0].classList.contains("leaflet-popup-close-button") && focusEvent.originalEvent.keyCode === 13){
setTimeout(() => {
L.DomEvent.stop(focusEvent);
map.closePopup(popup);
group.focus();
L.DomEvent.stop(focusEvent);
}, 0);
} else if ((path[0].title==="Focus Map" || path[0].classList.contains("mapml-popup-content")) && isTab && shiftPressed){
setTimeout(() => { //timeout needed so focus of the feature is done even after the keypressup event occurs
L.DomEvent.stop(focusEvent);
map.closePopup(popup);
group.focus();
L.DomEvent.stop(focusEvent);
}, 0);
}
}
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/core/popupTabNavigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,47 @@ test.describe("Playwright Keyboard Navigation + Query Layer Tests" , () => {
expect(f).toEqual("M330 83L586 83L586 339L330 339z");
});

test("Tooltip appears after pressing esc key", async () => {
await page.keyboard.press("Enter");
await page.waitForTimeout(500);
await page.keyboard.down("Escape"); // focus back on feature
await page.waitForTimeout(500);
AliyanH marked this conversation as resolved.
Show resolved Hide resolved
await page.keyboard.up("Escape");
await page.waitForTimeout(500);

const h = await page.evaluateHandle(() => document.querySelector("mapml-viewer"));
const nh = await page.evaluateHandle(doc => doc.shadowRoot, h);
const rh = await page.evaluateHandle(root => root.activeElement.querySelector(".leaflet-interactive"), nh);
const f = await (await page.evaluateHandle(elem => elem.getAttribute("d"), rh)).jsonValue();

let tooltipCount = await page.$eval("mapml-viewer .leaflet-tooltip-pane", div => div.childElementCount);
expect(tooltipCount).toEqual(1);
expect(f).toEqual("M330 83L586 83L586 339L330 339z");
});

test("Tooltip appears after pressing enter on close button", async () => {
await page.keyboard.press("Enter"); // focus back into popup
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab"); // focus on x button
await page.keyboard.down("Enter"); // press x button
await page.keyboard.up("Enter");
await page.waitForTimeout(500);

const h = await page.evaluateHandle(() => document.querySelector("mapml-viewer"));
const nh = await page.evaluateHandle(doc => doc.shadowRoot, h);
const rh = await page.evaluateHandle(root => root.activeElement.querySelector(".leaflet-interactive"), nh);
const f = await (await page.evaluateHandle(elem => elem.getAttribute("d"), rh)).jsonValue();

let tooltipCount = await page.$eval("mapml-viewer .leaflet-tooltip-pane", div => div.childElementCount);
expect(tooltipCount).toEqual(1);
expect(f).toEqual("M330 83L586 83L586 339L330 339z");

});

test("Next feature button focuses next feature", async () => {
await page.keyboard.press("Enter"); // popup with link
await page.waitForTimeout(500);
Expand Down