-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar2.js
92 lines (77 loc) · 2.64 KB
/
sidebar2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const leftToggle = document.querySelector(".left-toggle");
const rightToggle = document.querySelector(".right-toggle");
const articleNavigation = document.querySelector("#article-navigation");
const articlePreference = document.querySelector("#article-preference");
const body = document.body;
// Track if the left and right sidebar is open
let isLeftOpen = false;
let isRightOpen = false;
leftToggle.addEventListener("click", (e) => {
e.stopPropagation(); // Prevent the click event from reaching the document
isLeftOpen ? closeLeft() : openLeft();
});
rightToggle.addEventListener("click", (e) => {
e.stopPropagation(); // Prevent the click event from reaching the document
isRightOpen ? closeRight() : openRight();
});
/* */
document.addEventListener("click", (e) => {
// Check if the click is outside the left sidebar and the toggle button
if (!articleNavigation.contains(e.target) && e.target !== leftToggle) {
if (isLeftOpen) {
closeLeft();
}
}
});
function openLeft() {
leftToggle.setAttribute("aria-expanded", "true");
articleNavigation.setAttribute("data-state", "opened");
isLeftOpen = true; // Track left sidebar as open
}
function closeLeft() {
leftToggle.setAttribute("aria-expanded", "false");
articleNavigation.setAttribute("data-state", "closing");
// Listen for the end of the closing animation
/* articleNavigation.addEventListener('animationend', () => {
articleNavigation.setAttribute('data-state', "closed");
}, {once: true});*/
body.addEventListener(
"transitionend",
() => {
articleNavigation.setAttribute("data-state", "closed");
},
{ once: true }
);
isLeftOpen = false; // Track left sidebar as closed
}
document.addEventListener("click", (e) => {
// Check if the click is outside the left sidebar and the toggle button
if (!articlePreference.contains(e.target) && e.target !== rightToggle) {
if (isRightOpen) {
closeRight();
}
}
});
function openRight() {
rightToggle.setAttribute("aria-expanded", "true");
articlePreference.setAttribute("data-state", "opened");
isRightOpen = true; // Track right sidebar as open
}
function closeRight() {
rightToggle.setAttribute("aria-expanded", "false");
articlePreference.setAttribute("data-state", "closing");
// Listen for the end of the closing animation
/*
articlePreference.addEventListener('animationend', () => {
articlePreference.setAttribute('data-state', "closed");
}, {once: true});
*/
body.addEventListener(
"transitionend",
() => {
articlePreference.setAttribute("data-state", "closed");
},
{ once: true }
);
isRightOpen = false; // Track right sidebar as closed
}