-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve accessibility of modal dialogs (#974)
* improve details modal accessibility * improve tag modal accessibility * fix overlays in archive and shared pages * update tests * use buttons for closing dialogs * replace description list * hide preview image from screen readers * update tests
- Loading branch information
1 parent
2973812
commit 17442ee
Showing
18 changed files
with
369 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
let keyboardActive = false; | ||
|
||
window.addEventListener( | ||
"keydown", | ||
() => { | ||
keyboardActive = true; | ||
}, | ||
{ capture: true }, | ||
); | ||
|
||
window.addEventListener( | ||
"mousedown", | ||
() => { | ||
keyboardActive = false; | ||
}, | ||
{ capture: true }, | ||
); | ||
|
||
export function isKeyboardActive() { | ||
return keyboardActive; | ||
} | ||
|
||
export class FocusTrapController { | ||
constructor(element) { | ||
this.element = element; | ||
this.focusableElements = this.element.querySelectorAll( | ||
'a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])', | ||
); | ||
this.firstFocusableElement = this.focusableElements[0]; | ||
this.lastFocusableElement = | ||
this.focusableElements[this.focusableElements.length - 1]; | ||
|
||
this.onKeyDown = this.onKeyDown.bind(this); | ||
|
||
this.firstFocusableElement.focus({ focusVisible: keyboardActive }); | ||
this.element.addEventListener("keydown", this.onKeyDown); | ||
} | ||
|
||
destroy() { | ||
this.element.removeEventListener("keydown", this.onKeyDown); | ||
} | ||
|
||
onKeyDown(event) { | ||
if (event.key !== "Tab") { | ||
return; | ||
} | ||
if (event.shiftKey) { | ||
if (document.activeElement === this.firstFocusableElement) { | ||
event.preventDefault(); | ||
this.lastFocusableElement.focus(); | ||
} | ||
} else { | ||
if (document.activeElement === this.lastFocusableElement) { | ||
event.preventDefault(); | ||
this.firstFocusableElement.focus(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Behavior } from "./index"; | ||
import { FocusTrapController } from "./focus-utils"; | ||
|
||
export class ModalBehavior extends Behavior { | ||
constructor(element) { | ||
super(element); | ||
|
||
this.onClose = this.onClose.bind(this); | ||
this.onKeyDown = this.onKeyDown.bind(this); | ||
|
||
this.overlay = element.querySelector(".modal-overlay"); | ||
this.closeButton = element.querySelector(".modal-header .close"); | ||
|
||
this.overlay.addEventListener("click", this.onClose); | ||
this.closeButton.addEventListener("click", this.onClose); | ||
document.addEventListener("keydown", this.onKeyDown); | ||
|
||
this.setupInert(); | ||
this.focusTrap = new FocusTrapController( | ||
element.querySelector(".modal-container"), | ||
); | ||
} | ||
|
||
destroy() { | ||
this.overlay.removeEventListener("click", this.onClose); | ||
this.closeButton.removeEventListener("click", this.onClose); | ||
document.removeEventListener("keydown", this.onKeyDown); | ||
|
||
this.clearInert(); | ||
this.focusTrap.destroy(); | ||
} | ||
|
||
setupInert() { | ||
// Inert all other elements on the page | ||
document | ||
.querySelectorAll("body > *:not(.modals)") | ||
.forEach((el) => el.setAttribute("inert", "")); | ||
} | ||
|
||
clearInert() { | ||
// Clear inert attribute from all elements to allow focus outside the modal again | ||
document | ||
.querySelectorAll("body > *") | ||
.forEach((el) => el.removeAttribute("inert")); | ||
} | ||
|
||
onKeyDown(event) { | ||
// Skip if event occurred within an input element | ||
const targetNodeName = event.target.nodeName; | ||
const isInputTarget = | ||
targetNodeName === "INPUT" || | ||
targetNodeName === "SELECT" || | ||
targetNodeName === "TEXTAREA"; | ||
|
||
if (isInputTarget) { | ||
return; | ||
} | ||
|
||
if (event.key === "Escape") { | ||
this.onClose(event); | ||
} | ||
} | ||
|
||
onClose(event) { | ||
event.preventDefault(); | ||
this.element.classList.add("closing"); | ||
this.element.addEventListener( | ||
"animationend", | ||
(event) => { | ||
if (event.animationName === "fade-out") { | ||
this.doClose(); | ||
} | ||
}, | ||
{ once: true }, | ||
); | ||
} | ||
|
||
doClose() { | ||
this.element.remove(); | ||
this.clearInert(); | ||
this.element.dispatchEvent(new CustomEvent("modal:close")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ | |
margin: 0; | ||
} | ||
|
||
& button.close { | ||
& .close { | ||
background: none; | ||
border: none; | ||
padding: 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.