Skip to content

Commit

Permalink
refactor(core): ♻️ simplify section handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair3149 committed May 21, 2024
1 parent a405d64 commit 593c640
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
6 changes: 3 additions & 3 deletions includes/Partials/BodyContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ final class BodyContent extends Partial {
*/

/**
* Class name for collapsible section wrappers
* Class name for section wrappers
*/
public const STYLE_COLLAPSIBLE_SECTION_CLASS = 'citizen-section-collapsible';
public const SECTION_CLASS = 'citizen-section';

/**
* List of tags that could be considered as section headers.
Expand Down Expand Up @@ -203,7 +203,7 @@ private function prepareHeading( DOMDocument $doc, DOMElement $heading ) {
*/
private function createSectionBodyElement( DOMDocument $doc, $sectionNumber ) {
$sectionBody = $doc->createElement( 'section' );
$sectionBody->setAttribute( 'class', self::STYLE_COLLAPSIBLE_SECTION_CLASS );
$sectionBody->setAttribute( 'class', self::SECTION_CLASS );
$sectionBody->setAttribute( 'id', 'citizen-section-collapsible-' . $sectionNumber );

return $sectionBody;
Expand Down
66 changes: 46 additions & 20 deletions resources/skins.citizen.scripts/sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,68 @@ function init( bodyContent ) {
}

const headings = bodyContent.querySelectorAll( '.citizen-section-heading' );
const sections = bodyContent.querySelectorAll( '.citizen-section-collapsible' );
const sections = bodyContent.querySelectorAll( '.citizen-section' );

const toggleAriaExpanded = ( headline ) => {
headline.toggleAttribute( 'aria-expanded' );
const setHeadlineAttributes = ( heading, collapsibleID, i ) => {
const headline = heading.querySelector( '.mw-headline' ) ||
heading.querySelector( '.mw-heading' );

};
if ( !headline ) {
return;
}

const setHeadlineAttributes = ( headline, collapsibleID ) => {
headline.setAttribute( 'tabindex', 0 );
headline.setAttribute( 'role', 'button' );
headline.setAttribute( 'aria-controls', collapsibleID );
headline.setAttribute( 'aria-expanded', true );
headline.setAttribute( 'data-mw-citizen-section-heading-index', i );
};

const handleClick = ( i ) => {
const collapsibleID = `citizen-section-collapsible-${ i + 1 }`;
const headline = headings[ i ].querySelector( '.citizen-section-heading .mw-headline' );
const toggleClasses = ( i ) => {
if ( sections[ i + 1 ] ) {
headings[ i ].classList.toggle( 'citizen-section-heading--collapsed' );
sections[ i + 1 ].classList.toggle( 'citizen-section--collapsed' );
}
};

if ( headline ) {
setHeadlineAttributes( headline, collapsibleID );
const toggleAriaExpanded = ( el ) => {
const isExpanded = el.getAttribute( 'aria-expanded' ) === 'true';
el.setAttribute( 'aria-expanded', isExpanded ? 'false' : 'true' );
};

headings[ i ].addEventListener( 'click', function ( e ) {
this.classList.toggle( 'citizen-section-heading--collapsed' );
sections[ i + 1 ].classList.toggle( 'citizen-section-collapsible--collapsed' );
toggleAriaExpanded( headline );
const onEditSectionClick = ( e ) => {
e.stopPropagation();
};

if ( e.target.closest( '.mw-editsection, .mw-editsection-like' ) ) {
e.stopPropagation();
}
} );
const handleClick = ( e ) => {
const target = e.target;
const isEditSection = target.closest( '.mw-editsection, .mw-editsection-like' );

if ( isEditSection ) {
onEditSectionClick( e );
return;
}

const heading = target.closest( '.citizen-section-heading' );

if ( heading ) {
const i = +heading.getAttribute( 'data-mw-citizen-section-heading-index' );
const headline = heading.querySelector( '.mw-headline' ) ||
heading.querySelector( '.mw-heading' );

if ( headline ) {
toggleClasses( i );
toggleAriaExpanded( headline );
}
}
};

for ( let i = 0; i < headings.length; i++ ) {
handleClick( i );
const headingsLength = headings.length;
for ( let i = 0; i < headingsLength; i++ ) {
setHeadlineAttributes( headings[ i ], `citizen-section-${ i + 1 }`, i );
}

bodyContent.addEventListener( 'click', handleClick, false );
}

module.exports = {
Expand Down
14 changes: 6 additions & 8 deletions resources/skins.citizen.styles/components/Sections.less
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@

// Fix button alignment
&-heading,
&-collapsible > h3,
&-collapsible > h4,
&-collapsible > h5,
&-collapsible > h6 {
> h3,
> h4,
> h5,
> h6 {
display: flex;
align-items: center;

Expand All @@ -73,10 +73,8 @@
}
}

&-collapsible {
&--collapsed {
display: none;
}
&--collapsed {
display: none;
}
}
}
Expand Down

0 comments on commit 593c640

Please sign in to comment.