@@ -2,17 +2,45 @@ const sidebar = document.getElementById("nav");
22const button = document . getElementById ( "hamburger-button" ) ;
33
44if ( 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
0 commit comments