-
Notifications
You must be signed in to change notification settings - Fork 0
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-1044: Accordion-block open all / close all functionality #511
Changes from 3 commits
f8e44c0
40ebf55
49671b3
26cd7cb
d64574a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,8 @@ export default class Accordion { | |||||
|
||||||
if ( this.mainContainer ) { | ||||||
|
||||||
this.openAllButton = document.querySelectorAll( '.accordion__open-all' ); | ||||||
this.closeAllButton = document.querySelectorAll( '.accordion__close-all' ); | ||||||
this.dropdownTogglers = document.querySelectorAll( '.accordion__title-button' ); | ||||||
this.dropdowns = document.querySelectorAll( '.accordion__content' ); | ||||||
|
||||||
|
@@ -46,6 +48,154 @@ 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 ] | ||||||
) | ||||||
); | ||||||
|
||||||
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' ); | ||||||
|
||||||
for ( let x = 0; x < togglers.length; x++ ) { | ||||||
togglers[ x ].addEventListener( | ||||||
'click', | ||||||
() => this.updateButtonStates( | ||||||
this.mainContainer[ i ], | ||||||
this.openAllButton[ i ], | ||||||
this.closeAllButton[ i ] | ||||||
) | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Opens all dropdowns. | ||||||
* | ||||||
* @param {HTMLButtonElement} mainContainer Main container for accordion dropdowns. | ||||||
* @param {HTMLButtonElement} openAllButton The open all -button that was clicked. | ||||||
* @param {HTMLButtonElement} closeAllButton The close all -button to be shown. | ||||||
* | ||||||
* @return {void} | ||||||
*/ | ||||||
openAllDropdowns( mainContainer, openAllButton, closeAllButton ) { | ||||||
const dropdowns = mainContainer.getElementsByClassName( 'accordion__title-button' ); | ||||||
|
||||||
for ( let i = 0; i < dropdowns.length; i++ ) { | ||||||
const containerId = dropdowns[ i ].getAttribute( 'aria-controls' ); | ||||||
const dropDownContent = document.querySelector( `#${ containerId }` ); | ||||||
const textOpen = dropdowns[ i ].querySelector( '.icon-text--open' ); | ||||||
const textClose = dropdowns[ i ].querySelector( '.icon-text--close' ); | ||||||
|
||||||
textOpen.setAttribute( 'aria-hidden', 'true' ); | ||||||
textClose.setAttribute( 'aria-hidden', 'false' ); | ||||||
dropdowns[ i ].setAttribute( 'aria-expanded', 'true' ); | ||||||
if ( ! dropdowns[ i ].classList.contains( 'active-accordion' ) ) { | ||||||
dropdowns[ i ].classList.add( 'active-accordion' ); | ||||||
} | ||||||
|
||||||
dropDownContent.classList.remove( 'is-hidden' ); | ||||||
|
||||||
if ( ! dropdowns[ i ].classList.contains( 'is-hidden' ) | ||||||
&& ! dropdowns[ i ].classList.contains( 'accordion--table-initialized' ) ) { | ||||||
|
||||||
const accordionTables = dropDownContent.getElementsByTagName( 'table' ); | ||||||
|
||||||
if ( accordionTables.length > 0 ) { | ||||||
new Indicate( accordionTables, { arrows: true } ); | ||||||
|
||||||
dropdowns[ i ].classList.add( 'accordion--table-initialized' ); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
closeAllButton.classList.remove( 'is-hidden' ); | ||||||
openAllButton.classList.add( 'is-hidden' ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Closes all dropdowns. | ||||||
* | ||||||
* @param {HTMLButtonElement} mainContainer Main container for accordion dropdowns. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @param {HTMLButtonElement} closeAllButton The close all -button that was clicked. | ||||||
* @param {HTMLButtonElement} openAllButton The open all -button to be shown. | ||||||
* | ||||||
* @return {void} | ||||||
*/ | ||||||
closeAllDropdowns( mainContainer, closeAllButton, openAllButton ) { | ||||||
const dropdowns = mainContainer.getElementsByClassName( 'accordion__title-button' ); | ||||||
|
||||||
for ( let i = 0; i < dropdowns.length; i++ ) { | ||||||
const containerId = dropdowns[ i ].getAttribute( 'aria-controls' ); | ||||||
const dropDownContent = document.querySelector( `#${ containerId }` ); | ||||||
const textOpen = dropdowns[ i ].querySelector( '.icon-text--open' ); | ||||||
const textClose = dropdowns[ i ].querySelector( '.icon-text--close' ); | ||||||
|
||||||
textOpen.setAttribute( 'aria-hidden', 'false' ); | ||||||
textClose.setAttribute( 'aria-hidden', 'true' ); | ||||||
dropdowns[ i ].setAttribute( 'aria-expanded', 'false' ); | ||||||
if ( dropdowns[ i ].classList.contains( 'active-accordion' ) ) { | ||||||
dropdowns[ i ].classList.remove( 'active-accordion' ); | ||||||
} | ||||||
|
||||||
dropDownContent.classList.add( 'is-hidden' ); | ||||||
|
||||||
if ( dropdowns[ i ].classList.contains( 'is-hidden' ) | ||||||
&& dropdowns[ i ].classList.contains( 'accordion--table-initialized' ) ) { | ||||||
|
||||||
const accordionTables = dropDownContent.getElementsByTagName( 'table' ); | ||||||
|
||||||
if ( accordionTables.length > 0 ) { | ||||||
new Indicate( accordionTables, { arrows: true } ); | ||||||
|
||||||
dropdowns[ i ].classList.remove( 'accordion--table-initialized' ); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
openAllButton.classList.remove( 'is-hidden' ); | ||||||
closeAllButton.classList.add( 'is-hidden' ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Updates "Close all" or "Open all" -button states depending on open accordion dropdowns. | ||||||
* | ||||||
* @param {HTMLButtonElement} mainContainer Main container for accordion dropdowns. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @param {HTMLButtonElement} openAllButton The open all -button. | ||||||
* @param {HTMLButtonElement} closeAllButton The close all -button. | ||||||
* | ||||||
* @return {void} | ||||||
*/ | ||||||
updateButtonStates( mainContainer, openAllButton, closeAllButton ) { | ||||||
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' ); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -58,7 +208,11 @@ export default class Accordion { | |||||
toggleDropdown( clickedToggler ) { | ||||||
const containerId = clickedToggler.getAttribute( 'aria-controls' ); | ||||||
const dropDownContent = document.querySelector( `#${ containerId }` ); | ||||||
const textOpen = clickedToggler.querySelector( '.icon-text--open' ); | ||||||
const textClose = clickedToggler.querySelector( '.icon-text--close' ); | ||||||
|
||||||
this.toggleAriaHidden( textOpen ); | ||||||
this.toggleAriaHidden( textClose ); | ||||||
this.toggleAriaExpanded( clickedToggler ); | ||||||
dropDownContent.classList.toggle( 'is-hidden' ); | ||||||
|
||||||
|
@@ -96,6 +250,18 @@ export default class Accordion { | |||||
|
||||||
} | ||||||
|
||||||
/** | ||||||
* Get the icon-texts aria-hidden current state and set a new opposite state to it. | ||||||
* | ||||||
* @param {HTMLElement} iconText The icon-text element. | ||||||
* | ||||||
* @return {void} | ||||||
*/ | ||||||
toggleAriaHidden( iconText ) { | ||||||
const ariaHiddenState = iconText.getAttribute( 'aria-hidden' ) === 'false' ? true : false; | ||||||
iconText.setAttribute( 'aria-hidden', ariaHiddenState ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Add an unique identifier to each accordion. | ||||||
* | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
.accordion { | ||
&__open-all, | ||
&__close-all { | ||
color: $primary !important; | ||
background-color: transparent !important; | ||
border: none !important; | ||
border-radius: 0 !important; | ||
|
||
.icon { | ||
margin-left: $theme-spacing-three-quarters !important; | ||
} | ||
|
||
&:hover { | ||
filter: none !important; | ||
box-shadow: none !important; | ||
text-decoration: underline !important; | ||
Comment on lines
+5
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Voisi kommentoida, miksi käytetty !important. |
||
} | ||
} | ||
|
||
&__description { | ||
@include from($tablet) { | ||
max-width: 75%; | ||
|
@@ -33,6 +51,14 @@ | |
.icon { | ||
transform: rotate(180deg); | ||
} | ||
|
||
.icon-text--open { | ||
display: none; | ||
} | ||
|
||
.icon-text--close { | ||
display: inline-block; | ||
} | ||
} | ||
|
||
&:hover, | ||
|
@@ -42,8 +68,19 @@ | |
} | ||
|
||
&__title-icon { | ||
.icon { | ||
transition: transform ease-in .2s; | ||
align-items: center; | ||
} | ||
|
||
&__icon-text { | ||
color: $primary; | ||
font-size: $base-size; | ||
|
||
&.icon-text--open { | ||
display: inline-block; | ||
} | ||
|
||
&.icon-text--close { | ||
display: none; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?