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

EMULSIF-437: Accordion: Screen readers announce hidden content of collapsed accordions #172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/components/accordion/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ Drupal.behaviors.accordion = {
const controls = context.querySelectorAll('.accordion__controls__item');
// Classes
const itemToggle = '.js-accordion-item__toggle';
const itemContent = '.accordion-item__content';
const itemState = 'data-accordion-expanded';
const buttonState = 'aria-expanded';
const contentState = 'aria-hidden';

// Function to expand an accordion item.
const expand = (item, button) => {
const expand = (item, button, content) => {
item.setAttribute(itemState, 'true');
button.setAttribute(buttonState, 'true');
content.setAttribute(contentState, 'false');
};

// Function to collapse an accordion item.
const collapse = (item, button) => {
const collapse = (item, button, content) => {
item.setAttribute(itemState, 'false');
button.setAttribute(buttonState, 'false');
content.setAttribute(contentState, 'true');
};

/* eslint-disable */
Expand Down Expand Up @@ -50,16 +54,17 @@ Drupal.behaviors.accordion = {
// Toggle accordion content when toggle is activated.
items.forEach((item) => {
const button = item.querySelector(itemToggle);
const content = item.querySelector(itemContent);

const anchor = item.id;
// eslint-disable-next-line
const newUrl = `${getUrl()}` + '#' + `${anchor}`;

// Hide all accordion content sections if JavaScript is enabled.
collapse(item, button);
collapse(item, button, content);

if (item.getAttribute('id') && item.getAttribute('id') === getAnchor()) {
expand(item, button);
expand(item, button, content);
}

button.addEventListener('click', () => {
Expand All @@ -74,8 +79,8 @@ Drupal.behaviors.accordion = {
}
// Toggle the item's state.
return button.getAttribute(buttonState) === 'true'
? collapse(item, button)
: expand(item, button);
? collapse(item, button, content)
: expand(item, button, content);
});
});

Expand All @@ -96,11 +101,12 @@ Drupal.behaviors.accordion = {
// Iterate over
allItems.forEach((item) => {
const button = item.querySelector(itemToggle);
const content = item.querySelector(itemContent);

if (action === false) {
collapse(item, button);
collapse(item, button, content);
} else {
expand(item, button);
expand(item, button, content);
}
});
});
Expand Down
Loading