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

feat: Add support for collapsing page content under headings (resolves #1671) #1692

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/_includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<script src="/assets/scripts/lite-yt-embed.js"></script>
<link rel="preload" href="/lib/infusion/dist/infusion-uio.js" as="script">

{% if expandableHeading %}
<script src="/assets/scripts/expandable-heading.js"></script>
{% endif %}

<!-- CSS files for UI Options-->
<link rel="stylesheet" type="text/css" href="/lib/infusion/src/framework/core/css/fluid.css"/>
<link rel="stylesheet" type="text/css" href="/lib/infusion/src/framework/preferences/css/Enactors.css"/>
Expand Down
10 changes: 10 additions & 0 deletions src/admin/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ collections:
- label: Body
name: body
widget: markdown
- label: Expandable H2
name: expandableHeading
widget: boolean
default: false
hint: Make every H2s on the page hide/show their content.
- name: initiatives
label: Initiatives
label_singular: Initiative
Expand Down Expand Up @@ -116,6 +121,11 @@ collections:
- label: Tags
name: tags
widget: list
- label: Expandable H2
name: expandableHeading
widget: boolean
default: false
hint: Make every H2s on the page hide/show their content.
- name: events
label: Events
label_singular: Event
Expand Down
44 changes: 44 additions & 0 deletions src/assets/scripts/expandable-heading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const renderExpandableHeaddingIcon = (isExpanded) => {
return (isExpanded ?
"<svg xmlns=\"http://www.w3.org/2000/svg\" xml:space=\"preserve\" transform=\"rotate(90)\" viewBox=\"0 0 330 330\"><path d=\"m250.606 154.389-150-149.996c-5.857-5.858-15.355-5.858-21.213.001-5.857 5.858-5.857 15.355.001 21.213l139.393 139.39L79.393 304.394c-5.857 5.858-5.857 15.355.001 21.213C82.322 328.536 86.161 330 90 330s7.678-1.464 10.607-4.394l149.999-150.004a14.996 14.996 0 0 0 0-21.213z\"/></svg>" :
"<svg xmlns=\"http://www.w3.org/2000/svg\" xml:space=\"preserve\" viewBox=\"0 0 270 330\"><path d=\"m250.606 154.389-150-149.996c-5.857-5.858-15.355-5.858-21.213.001-5.857 5.858-5.857 15.355.001 21.213l139.393 139.39L79.393 304.394c-5.857 5.858-5.857 15.355.001 21.213C82.322 328.536 86.161 330 90 330s7.678-1.464 10.607-4.394l149.999-150.004a14.996 14.996 0 0 0 0-21.213z\"/></svg>"
);
};

const displayHeadingContent = (content, expanded) => {
while (content && !content.nodeName.startsWith("H")) {
if (!expanded) {
content.setAttribute("hidden", "true");
} else {
content.removeAttribute("hidden");
}
content = content.nextElementSibling;
}
};

window.addEventListener("load", () => {
document.querySelectorAll("main article h2:not(#tags)").forEach(heading => {
heading.classList.add("expandable-heading");
heading.innerHTML =
`
<button class="expandable-heading__toggle"
aria-expanded="false">
<div class="expandable-heading__icon">
${renderExpandableHeaddingIcon(false)}
</div>
${heading.textContent}
</button>
`;
displayHeadingContent(heading.nextElementSibling, false);
});

document.querySelectorAll(".expandable-heading__toggle").forEach((expandableHeading) => {
expandableHeading.addEventListener("click", (e) => {
const expanded = e.currentTarget.getAttribute("aria-expanded") === "true";
e.currentTarget.setAttribute("aria-expanded", !expanded);
e.currentTarget.querySelector(".expandable-heading__icon").innerHTML =
renderExpandableHeaddingIcon(!expanded);
displayHeadingContent(e.currentTarget.parentNode.nextElementSibling, !expanded);
});
});
});
2 changes: 2 additions & 0 deletions src/assets/styles/base/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ button:not(
.fl-prefsEditor-reset,
.fl-switchUI-control,
.lty-playbtn,
.expandable-heading__toggle,
.search-button) {
background-color: $green-light;
block-size: rem(54);
Expand All @@ -128,6 +129,7 @@ button:not(
.fl-prefsEditor-reset,
.fl-switchUI-control,
.lty-playbtn,
.expandable-heading__toggle,
.search-button,
.expander__toggle)::before {
block-size: rem(54);
Expand Down
1 change: 1 addition & 0 deletions src/assets/styles/components/_components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
@import "resources";
@import "search-container";
@import "tile-item";
@import "expandable-heading";
23 changes: 23 additions & 0 deletions src/assets/styles/components/_expandable-heading.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.expandable-heading {
inline-size: 100%;

&:has(button:focus),
&:hover {
background-color: #fadc90;
color: $green-dark;
outline: 0;
}
}

.expandable-heading__toggle {
align-items: center;
background-color: unset;
border: none;
display: flex;
gap: rem(12);
inline-size: 100%;

svg {
block-size: rem(24);
}
}
1 change: 1 addition & 0 deletions src/collections/initiatives/a-clusive-success-story.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tags:
- Disability
- Education
- CISL
expandableHeading: true
---
![Clusive logo](https://www.cast.org/binaries/content/gallery/castm/impact/clusive-story-16x9.jpg/clusive-story-16x9.jpg/aem%3Amedium)

Expand Down