Skip to content

Commit 22bd280

Browse files
update accordian to not animate on page load (#2467)
Co-authored-by: Josh Collinsworth <joshuajcollinsworth@gmail.com>
1 parent 8deda5a commit 22bd280

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

_components/SidebarNav.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,13 @@ export default function (data: Lume.Data) {
4747
{item.items && item.items.length > 0
4848
? (
4949
<>
50-
<label
51-
htmlFor={`sub-nav-toggle-${
52-
item.title.replaceAll(" ", "")
53-
}`}
54-
className="sub-nav-toggle block relative py-1 px-3 after:right-4 [font:inherit] after:translate-y-1/2 after:transition-transfor after:duration-100 after:ease-in after:[background:url(./img/chevron.svg)_no-repeat_center] after:-top-0.5 after:block after:w-4 after:h-4 after:absolute"
50+
<button
51+
type="button"
52+
data-accordion-toggle={item.title.replaceAll(" ", "")}
53+
className="sub-nav-toggle block relative py-1 px-3 after:right-4 [font:inherit] after:translate-y-1/2 after:transition-transform after:duration-100 after:ease-in after:[background:url(./img/chevron.svg)_no-repeat_center] after:-top-0.5 after:block after:w-4 after:h-4 after:absolute w-full text-left"
5554
>
5655
{item.title}
57-
<input
58-
type="checkbox"
59-
id={`sub-nav-toggle-${
60-
item.title.replaceAll(" ", "")
61-
}`}
62-
className="sub-nav-toggle-checkbox sr-only"
63-
/>
64-
</label>
56+
</button>
6557
<SidebarList>
6658
{item.items.map((subItem: any) => (
6759
<li key={subItem.href}>

static/js/sidebar.client.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,45 @@ const sidebar = document.getElementById("nav");
22
const button = document.getElementById("hamburger-button");
33

44
if (sidebar) {
5-
const checkboxes = document.querySelectorAll(".sub-nav-toggle-checkbox");
6-
checkboxes.forEach((checkbox) => {
7-
// on change of the checkbox, update the checked state in the local storage
8-
checkbox.addEventListener("change", () => {
9-
console.log("updated");
10-
localStorage.setItem(checkbox.id, checkbox.checked);
11-
});
5+
const toggleButtons = document.querySelectorAll(
6+
".sub-nav-toggle[data-accordion-toggle]",
7+
);
8+
9+
toggleButtons.forEach((toggleButton) => {
10+
const accordionId = toggleButton.getAttribute("data-accordion-toggle");
11+
const parentLi = toggleButton.closest("li");
12+
13+
// Set initial state from localStorage without animation
14+
const isExpanded =
15+
localStorage.getItem(`accordion-${accordionId}`) === "true";
16+
if (parentLi) {
17+
if (isExpanded) {
18+
parentLi.classList.add("expanded");
19+
toggleButton.setAttribute("aria-expanded", "true");
20+
} else {
21+
toggleButton.setAttribute("aria-expanded", "false");
22+
}
23+
}
1224

13-
// set the checked state of the checkbox based on the value in the local storage
14-
const checked = localStorage.getItem(checkbox.id) === "true";
15-
checkbox.checked = checked;
25+
// Add click event listener
26+
toggleButton.addEventListener("click", () => {
27+
if (parentLi) {
28+
// Add the user-interaction class to enable animations
29+
parentLi.classList.add("user-interaction");
30+
31+
const wasExpanded = parentLi.classList.contains("expanded");
32+
33+
if (wasExpanded) {
34+
parentLi.classList.remove("expanded");
35+
toggleButton.setAttribute("aria-expanded", "false");
36+
localStorage.setItem(`accordion-${accordionId}`, "false");
37+
} else {
38+
parentLi.classList.add("expanded");
39+
toggleButton.setAttribute("aria-expanded", "true");
40+
localStorage.setItem(`accordion-${accordionId}`, "true");
41+
}
42+
}
43+
});
1644
});
1745
}
1846

styles.css

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,41 +1401,41 @@
14011401
li:has(> &) {
14021402
display: grid;
14031403
grid-template-rows: max-content 0fr;
1404-
transition: grid 0.25s cubic-bezier(0.165, 0.84, 0.44, 1);
1404+
/* Only animate when user interaction class is present */
1405+
}
1406+
1407+
/* Apply transitions when li has user-interaction class */
1408+
li.user-interaction:has(> &) {
1409+
transition: grid-template-rows 0.25s cubic-bezier(0.165, 0.84, 0.44, 1);
14051410
}
14061411

14071412
+ ul {
14081413
margin-left: 1rem !important;
14091414
}
14101415

1411-
li:has(> .sub-nav-toggle-checkbox:checked) {
1416+
/* Expanded state when li has the 'expanded' class */
1417+
li:has(> &).expanded {
14121418
grid-template-rows: max-content 1fr;
14131419
padding-bottom: 1rem;
14141420
}
14151421

1416-
li:has(> + ul:focus-within) {
1417-
grid-template-rows: max-content 1fr;
1418-
padding-bottom: 1rem;
1422+
/* Rotate chevron when expanded - only animate on user interaction */
1423+
li.expanded > &::after {
1424+
transform: rotate(90deg);
14191425
}
14201426

1421-
&:has(.sub-nav-toggle-checkbox:checked)::after {
1422-
transform: rotate(90deg);
1427+
li.user-interaction > &::after {
1428+
transition: transform 0.1s ease-in;
14231429
}
14241430

14251431
&:has(+ ul:focus-within)::after {
14261432
transform: rotate(90deg);
14271433
}
14281434
}
14291435

1430-
.sub-nav-toggle-checkbox {
1431-
li:has(> .sub-nav-toggle &:checked) {
1432-
grid-template-rows: max-content 1fr;
1433-
padding-bottom: 1rem;
1434-
}
1435-
1436-
.sub-nav-toggle:has(&:checked)::after {
1437-
transform: rotate(90deg);
1438-
}
1436+
li:has(.sub-nav-toggle + ul:focus-within) {
1437+
grid-template-rows: max-content 1fr;
1438+
padding-bottom: 1rem;
14391439
}
14401440

14411441
#hamburger-button[aria-pressed="true"] {

0 commit comments

Comments
 (0)