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

Remove accordion icon added by JS and add to twig instead #200

Merged
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
34 changes: 20 additions & 14 deletions modules/localgov_subsites_paragraphs/js/localgov-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,19 @@
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');
// Add aria-controls id to button and un-hide
button.setAttribute('aria-controls', id);
button.hidden = false;

// Hide default Heading text
heading.hidden = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
heading.hidden = true;
if (heading) {
heading.hidden = true;
}


// Add click event listener to the show/hide button.
button.addEventListener('click', e => {
Expand Down Expand Up @@ -188,13 +187,20 @@

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.
button.hidden = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
button.hidden = true;
button.hidden = true;
if (heading) {
heading.hidden = false;
}


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

// Remove id attributes from pane content elements.
accordionPanes[i]
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