Skip to content

Commit 9b1325a

Browse files
authored
Made the list opened if tag is selected (#223)
Made the list opened if tag is selected
1 parent f77f83b commit 9b1325a

3 files changed

Lines changed: 73 additions & 8 deletions

File tree

website/modules/asset/ui/src/clientSideFiltering.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
// Client-side filtering for case studies with page reload
22

3+
// No persistence needed for current requirement ("open if tag is selected").
4+
5+
const hasSelectedTagsInCategory = function (filterType) {
6+
const selectedTags = document.querySelectorAll(
7+
`#filter-content-${filterType} .tag-item.active`,
8+
);
9+
return selectedTags.length > 0;
10+
};
11+
12+
const isDesktop = function () {
13+
return window.innerWidth > 1024;
14+
};
15+
16+
const updateCategoriesVisibility = function () {
17+
const checkboxes = document.querySelectorAll('.filter-category__toggle');
18+
checkboxes.forEach(function (checkbox) {
19+
const filterType = checkbox.id.replace('filter-toggle-', '');
20+
const hasSelectedTags = hasSelectedTagsInCategory(filterType);
21+
const isIndustryCategory = filterType === 'industry';
22+
23+
const shouldBeOpen = hasSelectedTags || (isIndustryCategory && isDesktop());
24+
if (shouldBeOpen && !checkbox.checked) {
25+
checkbox.checked = true;
26+
const button = document.querySelector(`label[for="${checkbox.id}"]`);
27+
if (button) {
28+
button.setAttribute('aria-expanded', 'true');
29+
}
30+
} else if (!shouldBeOpen && checkbox.checked) {
31+
checkbox.checked = false;
32+
const button = document.querySelector(`label[for="${checkbox.id}"]`);
33+
if (button) {
34+
button.setAttribute('aria-expanded', 'false');
35+
}
36+
}
37+
});
38+
};
39+
40+
const initializeCategoriesVisibility = function () {
41+
updateCategoriesVisibility();
42+
};
43+
344
const shouldInterceptClick = function (link) {
445
const href = link.getAttribute('href');
546
return (
@@ -32,7 +73,6 @@ const handleFilterClick = function (event) {
3273
history.pushState({ clientSideFilter: true, url: newUrl }, '', newUrl);
3374
window.location.reload();
3475
} catch {
35-
// Fallback to default navigation if URL construction fails
3676
window.location.href = href;
3777
}
3878
};
@@ -43,11 +83,34 @@ const handlePopState = function (event) {
4383
}
4484
};
4585

86+
const handleResize = function () {
87+
updateCategoriesVisibility();
88+
};
89+
4690
export const initClientSideFiltering = function () {
4791
if (!document.querySelector('.cs_list')) {
4892
return;
4993
}
5094

95+
initializeCategoriesVisibility();
96+
5197
window.addEventListener('popstate', handlePopState);
98+
window.addEventListener('resize', handleResize);
5299
document.addEventListener('click', handleFilterClick);
100+
101+
// Keep aria-expanded in sync with checkbox state
102+
document.addEventListener('change', function (event) {
103+
const checkbox = event.target.closest('.filter-category__toggle');
104+
if (!checkbox) {
105+
return;
106+
}
107+
const button = document.querySelector(`label[for="${checkbox.id}"]`);
108+
if (button) {
109+
if (checkbox.checked) {
110+
button.setAttribute('aria-expanded', 'true');
111+
} else {
112+
button.setAttribute('aria-expanded', 'false');
113+
}
114+
}
115+
});
53116
};

website/modules/asset/ui/src/scss/_cases.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,14 +1286,14 @@
12861286
}
12871287

12881288
&__content {
1289-
position: absolute;
1290-
left: 50%;
1291-
top: 50%;
1289+
position: sticky;
1290+
left: 20px;
1291+
right: 20px;
1292+
top: 90px;
12921293
width: 90vw;
12931294
max-width: 400px;
1294-
min-width: 320px;
1295+
min-width: 300px;
12951296
background: #fff;
1296-
transform: translate(-50%, -50%);
12971297
z-index: 2;
12981298
padding: 0 16px 24px;
12991299
max-height: 90vh;

website/modules/case-studies-page/views/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@
9595
tags and tags.length %}
9696
<div class="filter-section">
9797
<!-- prettier-ignore -->
98+
{% set hasActiveFilter = data.query[filterType] %}
99+
{% set shouldBeOpen = loop.first or hasActiveFilter %}
98100
<input
99101
type="checkbox"
100102
id="filter-toggle-{{ filterType }}"
101103
class="filter-category__toggle"
102-
{% if loop.first %}checked{% endif %}
104+
{% if shouldBeOpen %}checked{% endif %}
103105
aria-labelledby="filter-category-{{ filterType }}"
104106
/>
105107
<div class="filter-category" id="filter-category-{{ filterType }}">
@@ -109,7 +111,7 @@
109111
class="filter-category__expand-button"
110112
role="button"
111113
tabindex="0"
112-
aria-expanded="{% if loop.first %}true{% else %}false{% endif %}"
114+
aria-expanded="{% if shouldBeOpen %}true{% else %}false{% endif %}"
113115
aria-controls="filter-content-{{ filterType }}"
114116
aria-label="Toggle {{ filterLabel }} filter section"
115117
onkeydown="return true;"

0 commit comments

Comments
 (0)