Skip to content

Commit

Permalink
Merge pull request #201 from localgovdrupal/2.x
Browse files Browse the repository at this point in the history
2.4.10 release
  • Loading branch information
finnlewis authored Oct 1, 2024
2 parents c2a598e + ba3be61 commit 8e9ef1c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
text-align: left;
}

.accordion-pane__title button[hidden] {
display: none;
}

.accordion--initialised .accordion-pane__content {
display: none;
}
Expand Down
132 changes: 73 additions & 59 deletions modules/localgov_subsites_paragraphs/js/localgov-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,83 +117,97 @@
const pane = accordionPanes[i];
const content = pane.querySelectorAll('.accordion-pane__content');
const title = pane.querySelectorAll('.accordion-pane__title');
const titleText = title[0].textContent;
const button = document.createElement('button');
const text = document.createTextNode(titleText);
const button = title[0].querySelector('button');
const heading = title[0].querySelector('.accordion-pane__heading');
const id = `accordion-content-${index}-${i}`;

// Add id attribute to all pane content elements.
content[0].setAttribute('id', id);

// Add show/hide button to each accordion title.
button.appendChild(text);
// Add an initially hidden icon which can be used if required to make accordions fit GDS standard
button.innerHTML += "<span class='accordion-icon' aria-hidden='true'></span>";
button.setAttribute('aria-expanded', 'false');
button.setAttribute('aria-controls', id);

// Add click event listener to the show/hide button.
button.addEventListener('click', e => {
const targetPaneId = e.target.getAttribute('aria-controls');
const targetPane = accordion.querySelectorAll(`#${targetPaneId}`);
const openPane = accordion.querySelectorAll(`.${openClass}`);

// Check the current state of the button and the content it controls.
if (e.target.getAttribute('aria-expanded') === 'false') {
// Close currently open pane.
if (openPane.length && !allowMultiple) {
const openPaneId = openPane[0].getAttribute('id');
const openPaneButton = accordion.querySelectorAll(
`[aria-controls="${openPaneId}"]`,
);

collapsePane(openPaneButton[0], openPane[0]);
}
// Hide default Heading text
if (heading) {
heading.hidden = true;
};

if (button) {
// Add aria-controls id to button and un-hide
button.setAttribute('aria-controls', id);
button.hidden = false;

// Add click event listener to the show/hide button.
button.addEventListener('click', e => {
const targetPaneId = e.target.getAttribute('aria-controls');
const targetPane = accordion.querySelectorAll(`#${targetPaneId}`);
const openPane = accordion.querySelectorAll(`.${openClass}`);

// Show new pane.
expandPane(e.target, targetPane[0]);
} else {
// If target pane is currently open, close it.
collapsePane(e.target, targetPane[0]);
}
// Check the current state of the button and the content it controls.
if (e.target.getAttribute('aria-expanded') === 'false') {
// Close currently open pane.
if (openPane.length && !allowMultiple) {
const openPaneId = openPane[0].getAttribute('id');
const openPaneButton = accordion.querySelectorAll(
`[aria-controls="${openPaneId}"]`,
);

if (showHideButton) {
const accordionState = getAccordionState();
const toggleState = showHideButton.getAttribute('aria-expanded') === 'true';
collapsePane(openPaneButton[0], openPane[0]);
}

if (
(accordionState === 1 && !toggleState) ||
(!accordionState && toggleState)
) {
toggleAll();
// Show new pane.
expandPane(e.target, targetPane[0]);
} else {
// If target pane is currently open, close it.
collapsePane(e.target, targetPane[0]);
}
}
});

if (displayShowHide) {
showHideButton = accordion.querySelector('.accordion-toggle-all');
showHideButton.hidden = false;
showHideButton.addEventListener('click', toggleAll);
showHideButtonLabel = showHideButton.querySelector('.accordion-text');
}
if (showHideButton) {
const accordionState = getAccordionState();
const toggleState = showHideButton.getAttribute('aria-expanded') === 'true';

if (
(accordionState === 1 && !toggleState) ||
(!accordionState && toggleState)
) {
toggleAll();
}
}
});
};

// Add init class.
accordion.classList.add(initClass);
if (button) {
if (displayShowHide) {
showHideButton = accordion.querySelector('.accordion-toggle-all');
showHideButton.hidden = false;
showHideButton.addEventListener('click', toggleAll);
showHideButtonLabel = showHideButton.querySelector('.accordion-text');
}

// Add init class.
accordion.classList.add(initClass);
};

// Add show/hide button to each accordion pane title element.
title[0].children[0].innerHTML = '';
title[0].children[0].appendChild(button);
}
};

const destroy = () => {
for (let i = 0; i < numberOfPanes; i++) {
// Remove buttons from accordion pane titles.
// Remove id attributes from buttons in accordion pane titles.
const button = accordion
.querySelectorAll('.accordion-pane__title')
[i].querySelectorAll('button');
if (button.length > 0) {
button[0].outerHTML = button[0].innerHTML;
.querySelectorAll('.accordion-pane__title')[i]
.querySelector('button')
.removeAttribute('id');

// Hide buttons in accordion pane titles.
if (button) {
button.hidden = true;
}

// Un-hide default heading text
const heading = accordion
.querySelectorAll('.accordion-pane__title')[i]
.querySelector('.accordion-pane__heading');

if (heading) {
heading.hidden = false;
}

// Remove id attributes from pane content elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ function localgov_subsites_paragraphs_preprocess_paragraph(&$variables) {
$heading_level = $paragraph->get('localgov_heading_level')->value;
if (is_string($heading_level)) {
$variables['heading'] = _localgov_subsites_paragraphs_create_heading($heading_text, $heading_level);
$variables['heading_text'] = $heading_text;
$variables['heading_level'] = $heading_level;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
<div{{ attributes.addClass(classes) }}>
{% block content %}
<div class="accordion-pane__title">
{{ heading }}
<{{ heading_level }}>
<span class="accordion-pane__heading">{{ heading_text }}</span>
<button aria-expanded="false" hidden>
{{ heading_text }}
<span class='accordion-icon' aria-hidden='true'></span>
</button>
</{{ heading_level }}>
</div>
<div class="accordion-pane__content">
{{ content.localgov_body_text }}
Expand Down

0 comments on commit 8e9ef1c

Please sign in to comment.