aria-ease 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/aria-ease.d.ts +4 -3
- package/package.json +1 -1
- package/src/accordion/updateAccordionTriggerAriaAttributes.js +1 -3
- package/src/accordion/updateAccordionTriggerAriaAttributes.ts +2 -4
- package/src/handleKeyPress.js +10 -4
- package/src/handleKeyPress.ts +11 -4
- package/src/menu/makeMenuAccessible.js +3 -2
- package/src/menu/makeMenuAccessible.ts +3 -2
- package/src/menu/updateMenuTriggerAriaAttributes.js +1 -3
- package/src/menu/updateMenuTriggerAriaAttributes.ts +1 -3
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Out of the box accessibility utility package to develop production ready applica
|
|
|
10
10
|
|
|
11
11
|
Add accessibility to menu: menu can be a dropdown, side menu, slide navigation e.t.c. Basically any component that toggles display and has a list of interactive children items. The function creates a focus trap within the menu and focus can be navigated using the arrow keys. The escape key also closes the menu and returns the focus back to the trigger.
|
|
12
12
|
|
|
13
|
-
The makeMenuAccessible function takes
|
|
13
|
+
The makeMenuAccessible function takes three string arguments; the id of the menu, the class name of the children item of the menu, and the aria-label for the closed/hidden state of the menu. And should only be invoked after the menu has become visible or added to the DOM. When the menu is visible the first item of the menu is in focus and focus can be navigated using the arrow keys
|
|
14
14
|
|
|
15
15
|
The updateMenuTriggerAriaAttributes function take two string arguments; the id of the menu trigger, and the aria-label that will replace the current one in the DOM. Behind the scene the aria-expanded and aria-attributes are also updated based on the visibility of the menu.
|
|
16
16
|
|
|
@@ -26,7 +26,7 @@ const MenuExample = () => {
|
|
|
26
26
|
const menu = document.querySelector('#custom-menu');
|
|
27
27
|
if (getComputedStyle(menu).display === 'none') {
|
|
28
28
|
menu.style.display = 'block';
|
|
29
|
-
makeMenuAccessible('custom-menu', 'profile-menu-item');
|
|
29
|
+
makeMenuAccessible('custom-menu', 'profile-menu-item', 'Display profile menu);
|
|
30
30
|
updateMenuTriggerAriaAttributes('display-button', 'Hide profile menu');
|
|
31
31
|
} else {
|
|
32
32
|
cleanUpMenuEventListeners('custom-menu', 'profile-menu-item');
|
|
@@ -42,7 +42,7 @@ const MenuExample = () => {
|
|
|
42
42
|
id="display-button"
|
|
43
43
|
onMouseDown={toggleMenuDisplay}
|
|
44
44
|
aria-haspopup={true}
|
|
45
|
-
|
|
45
|
+
role="button"
|
|
46
46
|
aria-expanded={false}
|
|
47
47
|
aria-controls="custom-menu"
|
|
48
48
|
aria-label="Display profile menu"
|
package/aria-ease.d.ts
CHANGED
|
@@ -9,8 +9,9 @@ declare module 'aria-ease' {
|
|
|
9
9
|
* Adds keyboard interaction to toggle menu. The menu traps focus and can be interacted with using the keyboard. The first item of the menu has focus when menu appears.
|
|
10
10
|
* @param {string} menuId - The id of the menu.
|
|
11
11
|
* @param {string} menuItemClass - The class of the items that are children of the menu.
|
|
12
|
+
* @param {string} menuClosedStateAriaLabel The aria label for when the menu is closed and not displayed
|
|
12
13
|
*/
|
|
13
|
-
function makeMenuAccessible(menuId: string, menuItemClass: string): void;
|
|
14
|
+
function makeMenuAccessible(menuId: string, menuItemClass: string, menuClosedStateAriaLabel: string): void;
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Adds keyboard interaction to block. The block traps focus and can be interacted with using the keyboard.
|
|
@@ -20,7 +21,7 @@ declare module 'aria-ease' {
|
|
|
20
21
|
function makeBlockAccessible(blockId: string, blockItemClass: string);
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
|
-
* Updates the aria attributes of the menu trigger button. Trigger button element must possess the following aria attributes; aria-expanded
|
|
24
|
+
* Updates the aria attributes of the menu trigger button. Trigger button element must possess the following aria attributes; aria-expanded and aria-label.
|
|
24
25
|
* @param {string} triggerId The id of the trigger button that toggles the menu.
|
|
25
26
|
* @param {string} ariaLabel The aria-label to be updated.
|
|
26
27
|
*/
|
|
@@ -34,7 +35,7 @@ declare module 'aria-ease' {
|
|
|
34
35
|
function cleanUpMenuEventListeners(menuId: string, menuItemClass: string): void;
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
|
-
* Adds screen reader accessibility to accordions. Updates the aria attributes of the accordion trigger button. Trigger button element must possess the following aria attributes; aria-expanded
|
|
38
|
+
* Adds screen reader accessibility to accordions. Updates the aria attributes of the accordion trigger button. Trigger button element must possess the following aria attributes; aria-expanded and aria-label.
|
|
38
39
|
* @param {AccordionStates[]} accordionStates Array of objects containing accordion state information
|
|
39
40
|
* @param {string} accordionsClass The shared class of all the accordion triggers
|
|
40
41
|
* @param {number} currentClickedTriggerIndex Index of the currently clicked accordion trigger
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Adds screen reader accessibility to accordions. Updates the aria attributes of the accordion trigger button. Trigger button element must possess the following aria attributes; aria-expanded
|
|
2
|
+
* Adds screen reader accessibility to accordions. Updates the aria attributes of the accordion trigger button. Trigger button element must possess the following aria attributes; aria-expanded and aria-label.
|
|
3
3
|
* @param {AccordionStates[]} accordionStates Array of objects containing accordions state information
|
|
4
4
|
* @param {string} accordionsClass The shared class of all the accordion triggers
|
|
5
5
|
* @param {number} currentClickedTriggerIndex Index of the currently clicked accordion trigger
|
|
@@ -12,11 +12,9 @@ export function updateAccordionTriggerAriaAttributes(accordionStates, accordions
|
|
|
12
12
|
allAccordionTrigger.forEach(function (trigger, index) {
|
|
13
13
|
if (index === currentClickedTriggerIndex) {
|
|
14
14
|
trigger.setAttribute("aria-expanded", accordionStates[index].display ? 'true' : 'false');
|
|
15
|
-
trigger.setAttribute("aria-pressed", accordionStates[index].display ? 'true' : 'false');
|
|
16
15
|
}
|
|
17
16
|
else {
|
|
18
17
|
trigger.setAttribute("aria-expanded", 'false');
|
|
19
|
-
trigger.setAttribute("aria-pressed", 'false');
|
|
20
18
|
}
|
|
21
19
|
trigger.setAttribute("aria-label", accordionStates[index].display ? accordionStates[index].openedAriaLabel : accordionStates[index].closedAriaLabel);
|
|
22
20
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Adds screen reader accessibility to accordions. Updates the aria attributes of the accordion trigger button. Trigger button element must possess the following aria attributes; aria-expanded
|
|
2
|
+
* Adds screen reader accessibility to accordions. Updates the aria attributes of the accordion trigger button. Trigger button element must possess the following aria attributes; aria-expanded and aria-label.
|
|
3
3
|
* @param {AccordionStates[]} accordionStates Array of objects containing accordions state information
|
|
4
4
|
* @param {string} accordionsClass The shared class of all the accordion triggers
|
|
5
5
|
* @param {number} currentClickedTriggerIndex Index of the currently clicked accordion trigger
|
|
@@ -11,16 +11,14 @@ export function updateAccordionTriggerAriaAttributes(accordionStates: AccordionS
|
|
|
11
11
|
const allAccordionTrigger: HTMLElement[] = Array.from(document.querySelectorAll(`.${accordionsClass}`));
|
|
12
12
|
|
|
13
13
|
if ( !allAccordionTrigger) {
|
|
14
|
-
|
|
14
|
+
throw new Error('Invalid trigger class provided.');
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
allAccordionTrigger.forEach((trigger, index) => {
|
|
18
18
|
if (index === currentClickedTriggerIndex) {
|
|
19
19
|
trigger.setAttribute("aria-expanded", accordionStates[index].display ? 'true' : 'false');
|
|
20
|
-
trigger.setAttribute("aria-pressed", accordionStates[index].display ? 'true' : 'false');
|
|
21
20
|
} else {
|
|
22
21
|
trigger.setAttribute("aria-expanded", 'false');
|
|
23
|
-
trigger.setAttribute("aria-pressed", 'false');
|
|
24
22
|
}
|
|
25
23
|
|
|
26
24
|
trigger.setAttribute("aria-label", accordionStates[index].display ? accordionStates[index].openedAriaLabel : accordionStates[index].closedAriaLabel);
|
package/src/handleKeyPress.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { updateMenuTriggerAriaAttributes } from "./menu/updateMenuTriggerAriaAttributes";
|
|
2
|
+
function handleMenuEscapeKeyPress(menuElement, menuTriggerButton, menuClosedStateAriaLabel) {
|
|
3
|
+
menuElement.style.display = 'none';
|
|
4
|
+
var menuTriggerButtonId = menuTriggerButton.getAttribute('id');
|
|
5
|
+
updateMenuTriggerAriaAttributes("".concat(menuTriggerButtonId), "".concat(menuClosedStateAriaLabel));
|
|
6
|
+
}
|
|
7
|
+
export function handleKeyPress(event, elementItems, elementItemIndex, menuElementDiv, triggerButton, menuClosedStateAriaLabel) {
|
|
2
8
|
switch (event.key) {
|
|
3
9
|
case 'ArrowUp':
|
|
4
10
|
case 'ArrowLeft':
|
|
@@ -22,9 +28,9 @@ export function handleKeyPress(event, elementItems, elementItemIndex, elementDiv
|
|
|
22
28
|
break;
|
|
23
29
|
case 'Escape':
|
|
24
30
|
event.preventDefault();
|
|
25
|
-
if (
|
|
26
|
-
(getComputedStyle(
|
|
27
|
-
triggerButton
|
|
31
|
+
if (menuElementDiv && triggerButton && menuClosedStateAriaLabel) {
|
|
32
|
+
(getComputedStyle(menuElementDiv).display === 'block') ?
|
|
33
|
+
handleMenuEscapeKeyPress(menuElementDiv, triggerButton, menuClosedStateAriaLabel) :
|
|
28
34
|
null;
|
|
29
35
|
triggerButton.focus();
|
|
30
36
|
}
|
package/src/handleKeyPress.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { NodeListOfHTMLElement, HTMLElement } from "../Types";
|
|
2
|
+
import { updateMenuTriggerAriaAttributes } from "./menu/updateMenuTriggerAriaAttributes";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
function handleMenuEscapeKeyPress(menuElement: HTMLElement, menuTriggerButton: HTMLElement, menuClosedStateAriaLabel: string) {
|
|
5
|
+
menuElement.style.display = 'none';
|
|
6
|
+
const menuTriggerButtonId = menuTriggerButton.getAttribute('id');
|
|
7
|
+
updateMenuTriggerAriaAttributes(`${menuTriggerButtonId}`, `${menuClosedStateAriaLabel}`)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function handleKeyPress(event: KeyboardEvent, elementItems: NodeListOfHTMLElement, elementItemIndex: number, menuElementDiv?: HTMLElement | undefined, triggerButton?: HTMLElement | undefined, menuClosedStateAriaLabel?: string | undefined): void {
|
|
4
11
|
switch(event.key) {
|
|
5
12
|
case 'ArrowUp':
|
|
6
13
|
case 'ArrowLeft':
|
|
@@ -22,9 +29,9 @@ export function handleKeyPress(event: KeyboardEvent, elementItems: NodeListOfHTM
|
|
|
22
29
|
break;
|
|
23
30
|
case 'Escape':
|
|
24
31
|
event.preventDefault();
|
|
25
|
-
if(
|
|
26
|
-
(getComputedStyle(
|
|
27
|
-
triggerButton
|
|
32
|
+
if(menuElementDiv && triggerButton && menuClosedStateAriaLabel) {
|
|
33
|
+
(getComputedStyle(menuElementDiv).display === 'block') ?
|
|
34
|
+
handleMenuEscapeKeyPress(menuElementDiv, triggerButton, menuClosedStateAriaLabel) :
|
|
28
35
|
null
|
|
29
36
|
triggerButton.focus()
|
|
30
37
|
}
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* Adds keyboard interaction to toggle menu. The menu traps focus and can be interacted with using the keyboard. The first item of the menu has focus when menu appears.
|
|
3
3
|
* @param {string} menuId The id of the menu
|
|
4
4
|
* @param {string} menuItemClass The shared class of the items that are children of the menu
|
|
5
|
+
* @param {string} menuClosedStateAriaLabel The aria label for when the menu is closed and not displayed
|
|
5
6
|
*/
|
|
6
7
|
import { handleKeyPress } from '../handleKeyPress';
|
|
7
8
|
var eventListenersAdded = new Set();
|
|
8
|
-
export function makeMenuAccessible(menuId, menuItemClass) {
|
|
9
|
+
export function makeMenuAccessible(menuId, menuItemClass, menuClosedStateAriaLabel) {
|
|
9
10
|
var menuDiv = document.querySelector("#".concat(menuId));
|
|
10
11
|
var menuItems = menuDiv.querySelectorAll(".".concat(menuItemClass));
|
|
11
12
|
var triggerId = menuDiv.getAttribute('aria-labelledby');
|
|
@@ -14,7 +15,7 @@ export function makeMenuAccessible(menuId, menuItemClass) {
|
|
|
14
15
|
menuItems.forEach(function (menuItem, menuItemIndex) {
|
|
15
16
|
if (!eventListenersAdded.has(menuItem)) {
|
|
16
17
|
eventListenersAdded.add(menuItem);
|
|
17
|
-
menuItem.addEventListener('keydown', function (event) { return handleKeyPress(event, menuItems, menuItemIndex, menuDiv, triggerButton); });
|
|
18
|
+
menuItem.addEventListener('keydown', function (event) { return handleKeyPress(event, menuItems, menuItemIndex, menuDiv, triggerButton, menuClosedStateAriaLabel); });
|
|
18
19
|
}
|
|
19
20
|
});
|
|
20
21
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Adds keyboard interaction to toggle menu. The menu traps focus and can be interacted with using the keyboard. The first item of the menu has focus when menu appears.
|
|
3
3
|
* @param {string} menuId The id of the menu
|
|
4
4
|
* @param {string} menuItemClass The shared class of the items that are children of the menu
|
|
5
|
+
* @param {string} menuClosedStateAriaLabel The aria label for when the menu is closed and not displayed
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
8
|
import { HTMLElement, NodeListOfHTMLElement } from '../../Types'
|
|
@@ -9,7 +10,7 @@ import { handleKeyPress } from '../handleKeyPress';
|
|
|
9
10
|
|
|
10
11
|
let eventListenersAdded: Set<HTMLElement> = new Set();
|
|
11
12
|
|
|
12
|
-
export function makeMenuAccessible(menuId: string, menuItemClass: string): void {
|
|
13
|
+
export function makeMenuAccessible(menuId: string, menuItemClass: string, menuClosedStateAriaLabel: string): void {
|
|
13
14
|
const menuDiv: HTMLElement = document.querySelector(`#${menuId}`) as HTMLElement
|
|
14
15
|
const menuItems: NodeListOfHTMLElement = menuDiv.querySelectorAll(`.${menuItemClass}`)
|
|
15
16
|
|
|
@@ -20,7 +21,7 @@ export function makeMenuAccessible(menuId: string, menuItemClass: string): void
|
|
|
20
21
|
menuItems.forEach((menuItem: HTMLElement, menuItemIndex: number): void => {
|
|
21
22
|
if (!eventListenersAdded.has(menuItem)) {
|
|
22
23
|
eventListenersAdded.add(menuItem);
|
|
23
|
-
menuItem.addEventListener('keydown', (event: KeyboardEvent): void => handleKeyPress(event, menuItems, menuItemIndex, menuDiv, triggerButton))
|
|
24
|
+
menuItem.addEventListener('keydown', (event: KeyboardEvent): void => handleKeyPress(event, menuItems, menuItemIndex, menuDiv, triggerButton, menuClosedStateAriaLabel))
|
|
24
25
|
}
|
|
25
26
|
})
|
|
26
27
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Adds screen reader accessibility to menus. Updates the aria attributes of the menu trigger button. Trigger button element must possess the following aria attributes; aria-expanded
|
|
2
|
+
* Adds screen reader accessibility to menus. Updates the aria attributes of the menu trigger button. Trigger button element must possess the following aria attributes; aria-expanded and aria-label.
|
|
3
3
|
* @param {string} triggerId The id of the trigger button that toggles the menu.
|
|
4
4
|
* @param {string} ariaLabel The aria-label to be updated.
|
|
5
5
|
*/
|
|
@@ -8,12 +8,10 @@ export function updateMenuTriggerAriaAttributes(triggerId, ariaLabel) {
|
|
|
8
8
|
var currentAriaExpandedState = triggerButton.getAttribute('aria-expanded');
|
|
9
9
|
function triggerOpen(ariaLabel) {
|
|
10
10
|
triggerButton.setAttribute('aria-expanded', 'true');
|
|
11
|
-
triggerButton.setAttribute('aria-pressed', 'true');
|
|
12
11
|
triggerButton.setAttribute('aria-label', ariaLabel);
|
|
13
12
|
}
|
|
14
13
|
function triggerClose(ariaLabel) {
|
|
15
14
|
triggerButton.setAttribute('aria-expanded', 'false');
|
|
16
|
-
triggerButton.setAttribute('aria-pressed', 'false');
|
|
17
15
|
triggerButton.setAttribute('aria-label', ariaLabel);
|
|
18
16
|
}
|
|
19
17
|
(currentAriaExpandedState === 'false') ?
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Adds screen reader accessibility to menus. Updates the aria attributes of the menu trigger button. Trigger button element must possess the following aria attributes; aria-expanded
|
|
2
|
+
* Adds screen reader accessibility to menus. Updates the aria attributes of the menu trigger button. Trigger button element must possess the following aria attributes; aria-expanded and aria-label.
|
|
3
3
|
* @param {string} triggerId The id of the trigger button that toggles the menu.
|
|
4
4
|
* @param {string} ariaLabel The aria-label to be updated.
|
|
5
5
|
*/
|
|
@@ -13,13 +13,11 @@ export function updateMenuTriggerAriaAttributes(triggerId: string, ariaLabel: st
|
|
|
13
13
|
|
|
14
14
|
function triggerOpen(ariaLabel: string): void {
|
|
15
15
|
triggerButton.setAttribute('aria-expanded', 'true')
|
|
16
|
-
triggerButton.setAttribute('aria-pressed', 'true')
|
|
17
16
|
triggerButton.setAttribute('aria-label', ariaLabel)
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
function triggerClose(ariaLabel: string): void {
|
|
21
20
|
triggerButton.setAttribute('aria-expanded', 'false')
|
|
22
|
-
triggerButton.setAttribute('aria-pressed', 'false')
|
|
23
21
|
triggerButton.setAttribute('aria-label', ariaLabel)
|
|
24
22
|
}
|
|
25
23
|
|