Skip to content

Commit 76c38bc

Browse files
committed
fix(sidebar): remove scrollbar when sidebar-minimized
- fix(sidebar) remove scrollbar when `sidebar-minimized` - refactor(sidebar) extract `makeScrollbar()` and `destroyScrollbar()`
1 parent 689a311 commit 76c38bc

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

js/src/sidebar.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const Sidebar = (($) => {
7777
class Sidebar {
7878
constructor(element) {
7979
this._element = element
80+
this.ps = null
8081
this.perfectScrollbar(Event.INIT)
8182
this.setActiveLink()
8283
this._addEventListeners()
@@ -92,47 +93,45 @@ const Sidebar = (($) => {
9293

9394
perfectScrollbar(event) {
9495
if (typeof PerfectScrollbar !== 'undefined') {
95-
let ps
96-
9796
if (event === Event.INIT && !document.body.classList.contains(ClassName.SIDEBAR_MINIMIZED)) {
98-
ps = new PerfectScrollbar(document.querySelector(Selector.NAVIGATION_CONTAINER), {
99-
suppressScrollX: true
100-
})
97+
this.ps = this.makeScrollbar()
10198
}
10299

103100
if (event === Event.DESTROY) {
104-
ps = new PerfectScrollbar(document.querySelector(Selector.NAVIGATION_CONTAINER), {
105-
suppressScrollX: true
106-
})
107-
ps.destroy()
108-
ps = null
101+
this.destroyScrollbar()
109102
}
110103

111104
if (event === Event.TOGGLE) {
112105
if (document.body.classList.contains(ClassName.SIDEBAR_MINIMIZED)) {
113-
ps = new PerfectScrollbar(document.querySelector(Selector.NAVIGATION_CONTAINER), {
114-
suppressScrollX: true
115-
})
116-
ps.destroy()
117-
ps = null
106+
this.destroyScrollbar()
118107
} else {
119-
ps = new PerfectScrollbar(document.querySelector(Selector.NAVIGATION_CONTAINER), {
120-
suppressScrollX: true
121-
})
108+
this.ps = this.makeScrollbar()
122109
}
123110
}
124111

125-
if (event === Event.UPDATE) {
112+
if (event === Event.UPDATE && !document.body.classList.contains(ClassName.SIDEBAR_MINIMIZED)) {
126113
// ToDo: Add smooth transition
127114
setTimeout(() => {
128-
ps = new PerfectScrollbar(document.querySelector(Selector.NAVIGATION_CONTAINER), {
129-
suppressScrollX: true
130-
})
115+
this.destroyScrollbar()
116+
this.ps = this.makeScrollbar()
131117
}, Default.transition)
132118
}
133119
}
134120
}
135121

122+
makeScrollbar(container = Selector.NAVIGATION_CONTAINER) {
123+
return new PerfectScrollbar(document.querySelector(container), {
124+
suppressScrollX: true
125+
})
126+
}
127+
128+
destroyScrollbar() {
129+
if (this.ps) {
130+
this.ps.destroy()
131+
this.ps = null
132+
}
133+
}
134+
136135
setActiveLink() {
137136
$(Selector.NAVIGATION).find(Selector.NAV_LINK).each((key, value) => {
138137
let link = value

0 commit comments

Comments
 (0)