Skip to content

Commit

Permalink
Merge pull request #157 from DFE-Digital/chore/simplify-hiding-js-ele…
Browse files Browse the repository at this point in the history
…ments

chore: Simplify hiding js-only elements
  • Loading branch information
katie-gardner authored Oct 2, 2024
2 parents 4075583 + 4abb884 commit c14201d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
26 changes: 1 addition & 25 deletions src/Dfe.ContentSupport.Web/Views/Content/CsIndex.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,4 @@
addPrintButtonEventListener();
</script>
}

<script nonce="@Context.Items["nonce"]" defer>
const hiddenClass = "govuk-visually-hidden";
//Some elements should remain hidden
const idsToNotShow = ["support-links"];
//Remove hidden visibility CSS class if JS enabled
const removeHiddenClassFromElements = () => {
const hiddenElements = Array.from(document.querySelectorAll(`.${hiddenClass}`));
for(const element of hiddenElements){
if(idsToNotShow.indexOf(element.id) > -1){
continue;
}
element.classList.remove(hiddenClass);
//Also unset "aria-hidden" attribute
element.ariaHidden = undefined;
}
}
removeHiddenClassFromElements();
</script>
}
}
9 changes: 8 additions & 1 deletion src/Dfe.ContentSupport.Web/Views/Shared/_BodyEnd.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<script defer type="module" nonce="@Context.Items["nonce"]">
import * as govuk from '/js/govuk-frontend.min.js';
govuk.initAll();
</script>
function showHiddenElement(element) {
element.classList.remove("govuk-visually-hidden");
element.ariaHidden = undefined;
}
// unhide all js-only elements when js is enabled
[...document.getElementsByClassName("js-only")].forEach(showHiddenElement)
</script>
2 changes: 1 addition & 1 deletion src/Dfe.ContentSupport.Web/Views/Shared/_Feedback.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@if (track)
{
<div class="dfe-feedback-banner govuk-!-margin-top-9 govuk-visually-hidden" aria-hidden="true" id="feedback-banner">
<div class="dfe-feedback-banner govuk-!-margin-top-9 js-only govuk-visually-hidden" aria-hidden="true" id="feedback-banner">
<div class="dfe-feedback-banner--content">
<form id="feedbackForm">
<div class="dfe-feedback-banner--content-questions">
Expand Down
2 changes: 1 addition & 1 deletion src/Dfe.ContentSupport.Web/Views/Shared/_Print.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="print-button govuk-visually-hidden" aria-hidden="true">
<div class="js-only print-button govuk-visually-hidden" aria-hidden="true">
<button class="govuk-link print-link-button" data-module="print-link" id="print-link" >Print this page</button>
</div>

0 comments on commit c14201d

Please sign in to comment.