-
Notifications
You must be signed in to change notification settings - Fork 317
/
sidebar-drawer.js
228 lines (196 loc) · 5.71 KB
/
sidebar-drawer.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import throttle from 'lodash.throttle'
import { qs } from '../helpers'
const BREAKPOINT = 768
const ANIMATION_DURATION = 300
const SIDEBAR_TOGGLE_SELECTOR = '.sidebar-toggle'
const CONTENT_SELECTOR = '.content'
const userPref = {
CLOSED: 'closed',
OPEN: 'open',
NO_PREF: 'no_pref'
}
const SIDEBAR_CLASS = {
opened: 'sidebar-opened',
openingStart: 'sidebar-opening-start',
opening: 'sidebar-opening',
closed: 'sidebar-closed',
closingStart: 'sidebar-closing-start',
closing: 'sidebar-closing'
}
const SIDEBAR_CLASSES = Object.values(SIDEBAR_CLASS)
const state = {
// Keep track of the current timeout to clear it if needed
togglingTimeout: null,
// Record window width on resize to update sidebar state only when it actually changes
lastWindowWidth: window.innerWidth,
// No_PREF is defaults to OPEN behavior
sidebarPreference: userPref.NO_PREF
}
/**
* Initializes the toggleable sidebar drawer.
*/
export function initialize () {
setDefaultSidebarState()
observeResizing()
addEventListeners()
}
function observeResizing() {
const sidebarWidth = sessionStorage.getItem('sidebar_width')
if(sidebarWidth) {
setSidebarWidth(sidebarWidth)
}
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
setSidebarWidth(entry.contentRect.width);
}
});
resizeObserver.observe(document.getElementById('sidebar'));
}
function setSidebarWidth(width) {
sessionStorage.setItem('sidebar_width', width)
document.body.style.setProperty('--sidebarWidth', `${width}px`);
}
function setDefaultSidebarState () {
// check & set persistent session state
const persistentSessionState = sessionStorage.getItem('sidebar_state')
// set default for closed state only, so sidebar will still auto close on window resize
if (persistentSessionState === 'closed' || isScreenSmall()) {
setClass(SIDEBAR_CLASS.closed)
qs(SIDEBAR_TOGGLE_SELECTOR).setAttribute('aria-expanded', 'false')
} else {
setClass(SIDEBAR_CLASS.opened)
qs(SIDEBAR_TOGGLE_SELECTOR).setAttribute('aria-expanded', 'true')
}
}
function isScreenSmall () {
return window.matchMedia(`screen and (max-width: ${BREAKPOINT}px)`).matches
}
function setClass (...classes) {
document.body.classList.remove(...SIDEBAR_CLASSES)
document.body.classList.add(...classes)
}
function addEventListeners () {
qs(SIDEBAR_TOGGLE_SELECTOR).addEventListener('click', (event) => {
toggleSidebar()
setPreference()
})
qs(CONTENT_SELECTOR).addEventListener('click', (event) => {
closeSidebarIfSmallScreen()
})
window.addEventListener(
'resize',
throttle((event) => {
adoptSidebarToWindowSize()
}, 100)
)
}
/**
* Either opens or closes the sidebar depending on the current state.
*
* @returns {Promise} A promise resolving once the animation is finished.
*/
export function toggleSidebar () {
if (isSidebarOpen()) {
return closeSidebar()
} else {
return openSidebar()
}
}
function isSidebarOpen () {
return (
document.body.classList.contains(SIDEBAR_CLASS.opened) ||
document.body.classList.contains(SIDEBAR_CLASS.opening)
)
}
/**
* Opens the sidebar by applying an animation.
*
* @returns {Promise} A promise resolving once the animation is finished.
*/
export function openSidebar () {
clearTimeoutIfAny()
sessionStorage.setItem('sidebar_state', 'opened')
qs(SIDEBAR_TOGGLE_SELECTOR).setAttribute('aria-expanded', 'true')
requestAnimationFrame(() => {
setClass(SIDEBAR_CLASS.openingStart)
requestAnimationFrame(() => {
setClass(SIDEBAR_CLASS.opening)
return new Promise((resolve, reject) => {
state.togglingTimeout = setTimeout(() => {
setClass(SIDEBAR_CLASS.opened)
resolve()
}, ANIMATION_DURATION)
})
})
})
}
/**
* Closes the sidebar by applying an animation.
*
* @returns {Promise} A promise resolving once the animation is finished.
*/
export function closeSidebar () {
clearTimeoutIfAny()
sessionStorage.setItem('sidebar_state', 'closed')
qs(SIDEBAR_TOGGLE_SELECTOR).setAttribute('aria-expanded', 'false')
requestAnimationFrame(() => {
setClass(SIDEBAR_CLASS.closingStart)
requestAnimationFrame(() => {
setClass(SIDEBAR_CLASS.closing)
return new Promise((resolve, reject) => {
state.togglingTimeout = setTimeout(() => {
setClass(SIDEBAR_CLASS.closed)
resolve()
}, ANIMATION_DURATION)
})
})
})
}
function clearTimeoutIfAny () {
if (state.togglingTimeout) {
clearTimeout(state.togglingTimeout)
state.togglingTimeout = null
}
}
/**
* Handles updating the sidebar state on window resize
*
* WHEN the window width has changed
* AND the user sidebar preference is OPEN or NO_PREF
* THEN adjust the sidebar state according to screen size
*/
function adoptSidebarToWindowSize () {
// See https://github.com/elixir-lang/ex_doc/issues/736#issuecomment-307371291
if (state.lastWindowWidth !== window.innerWidth) {
state.lastWindowWidth = window.innerWidth
if (
state.sidebarPreference === userPref.OPEN ||
state.sidebarPreference === userPref.NO_PREF
) {
setDefaultSidebarState()
}
}
}
function closeSidebarIfSmallScreen () {
const sidebarCoversContent = isScreenSmall()
if (sidebarCoversContent && isSidebarOpen()) {
closeSidebar()
}
}
/**
* Track the sidebar preference for the user
*/
function setPreference () {
switch (state.sidebarPreference) {
case userPref.OPEN:
state.sidebarPreference = userPref.CLOSED
break
case userPref.CLOSED:
state.sidebarPreference = userPref.OPEN
break
case userPref.NO_PREF:
isSidebarOpen()
? (state.sidebarPreference = userPref.OPEN)
: (state.sidebarPreference = userPref.CLOSED)
}
}