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

TMS-1050: Accordion script fixes #178

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- TMS-1050: Add if-statements into accordion script to prevent breaking other elements functionality

## [1.8.14] - 2024-06-04

- TMS-1044: Accordion-block changes
Expand Down
70 changes: 45 additions & 25 deletions assets/scripts/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,27 @@ export default class Accordion {

if ( this.mainContainer ) {
for ( let i = 0; i < this.mainContainer.length; i++ ) {
this.openAllButton[ i ].addEventListener(
'click',
() => this.openAllDropdowns(
this.mainContainer[ i ],
this.openAllButton[ i ],
this.closeAllButton[ i ]
)
);
if ( this.openAllButton[ i ] ) {
this.openAllButton[ i ].addEventListener(
'click',
() => this.openAllDropdowns(
this.mainContainer[ i ],
this.openAllButton[ i ],
this.closeAllButton[ i ]
)
);
}

this.closeAllButton[ i ].addEventListener(
'click',
() => this.closeAllDropdowns(
this.mainContainer[ i ],
this.closeAllButton[ i ],
this.openAllButton[ i ]
)
);
if ( this.closeAllButton[ i ] ) {
this.closeAllButton[ i ].addEventListener(
'click',
() => this.closeAllDropdowns(
this.mainContainer[ i ],
this.closeAllButton[ i ],
this.openAllButton[ i ]
)
);
}

if ( this.dropdownTogglers ) {
const togglers = this.mainContainer[ i ].getElementsByClassName( 'accordion__title-button' );
Expand Down Expand Up @@ -99,6 +103,10 @@ export default class Accordion {
openAllDropdowns( mainContainer, openAllButton, closeAllButton ) {
const dropdowns = mainContainer.getElementsByClassName( 'accordion__title-button' );

if ( dropdowns.length === 0 ) {
return;
}

for ( let i = 0; i < dropdowns.length; i++ ) {
const containerId = dropdowns[ i ].getAttribute( 'aria-controls' );
const dropDownContent = document.querySelector( `#${ containerId }` );
Expand Down Expand Up @@ -143,6 +151,10 @@ export default class Accordion {
closeAllDropdowns( mainContainer, closeAllButton, openAllButton ) {
const dropdowns = mainContainer.getElementsByClassName( 'accordion__title-button' );

if ( dropdowns.length === 0 ) {
return;
}

for ( let i = 0; i < dropdowns.length; i++ ) {
const containerId = dropdowns[ i ].getAttribute( 'aria-controls' );
const dropDownContent = document.querySelector( `#${ containerId }` );
Expand Down Expand Up @@ -188,13 +200,15 @@ export default class Accordion {
const dropdowns = mainContainer.getElementsByClassName( 'accordion__title-button' );
const openDropdowns = mainContainer.getElementsByClassName( 'active-accordion' );

if ( openDropdowns.length === dropdowns.length ) {
closeAllButton.classList.remove( 'is-hidden' );
openAllButton.classList.add( 'is-hidden' );
}
else {
openAllButton.classList.remove( 'is-hidden' );
closeAllButton.classList.add( 'is-hidden' );
if ( openAllButton && closeAllButton ) {
if ( openDropdowns.length === dropdowns.length ) {
closeAllButton.classList.remove( 'is-hidden' );
openAllButton.classList.add( 'is-hidden' );
}
else {
openAllButton.classList.remove( 'is-hidden' );
closeAllButton.classList.add( 'is-hidden' );
}
}
}

Expand All @@ -211,8 +225,14 @@ export default class Accordion {
const textOpen = clickedToggler.querySelector( '.icon-text--open' );
const textClose = clickedToggler.querySelector( '.icon-text--close' );

this.toggleAriaHidden( textOpen );
this.toggleAriaHidden( textClose );
if ( textOpen ) {
this.toggleAriaHidden( textOpen );
}

if ( textClose ) {
this.toggleAriaHidden( textClose );
}

this.toggleAriaExpanded( clickedToggler );
dropDownContent.classList.toggle( 'is-hidden' );

Expand Down
Loading