Skip to content

Commit 8aa28c2

Browse files
committed
fix(Sidebar): click outside listener on mobile devices
1 parent 1d7264d commit 8aa28c2

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

js/src/sidebar.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
import {
99
defineJQueryPlugin,
10-
reflow,
1110
typeCheckConfig
1211
} from './util/index'
12+
import ScrollBarHelper from './util/scrollbar'
1313
import EventHandler from './dom/event-handler'
14-
import Manipulator from './dom/manipulator'
1514
import BaseComponent from './base-component'
15+
import Manipulator from './dom/manipulator'
16+
import Backdrop from './util/backdrop'
1617

1718
/**
1819
* ------------------------------------------------------------------------
@@ -30,7 +31,6 @@ const Default = {}
3031
const DefaultType = {}
3132

3233
const CLASS_NAME_BACKDROP = 'sidebar-backdrop'
33-
const CLASS_NAME_FADE = 'fade'
3434
const CLASS_NAME_HIDE = 'hide'
3535
const CLASS_NAME_SHOW = 'show'
3636
const CLASS_NAME_SIDEBAR_NARROW = 'sidebar-narrow'
@@ -66,8 +66,7 @@ class Sidebar extends BaseComponent {
6666
this._overlaid = this._isOverlaid()
6767
this._narrow = this._isNarrow()
6868
this._unfoldable = this._isUnfoldable()
69-
this._backdrop = null
70-
69+
this._backdrop = this._initializeBackDrop()
7170
this._addEventListeners()
7271
}
7372

@@ -96,7 +95,8 @@ class Sidebar extends BaseComponent {
9695

9796
if (this._isMobile()) {
9897
this._element.classList.add(CLASS_NAME_SHOW)
99-
this._showBackdrop()
98+
this._backdrop.show()
99+
new ScrollBarHelper().hide()
100100
}
101101

102102
const complete = () => {
@@ -121,7 +121,8 @@ class Sidebar extends BaseComponent {
121121
}
122122

123123
if (this._isMobile()) {
124-
this._removeBackdrop()
124+
this._backdrop.hide()
125+
new ScrollBarHelper().reset()
125126
} else {
126127
this._element.classList.add(CLASS_NAME_HIDE)
127128
}
@@ -207,6 +208,16 @@ class Sidebar extends BaseComponent {
207208
return config
208209
}
209210

211+
_initializeBackDrop() {
212+
return new Backdrop({
213+
className: CLASS_NAME_BACKDROP,
214+
isVisible: this._mobile,
215+
isAnimated: true,
216+
rootElement: this._element.parentNode,
217+
clickCallback: () => this.hide()
218+
})
219+
}
220+
210221
_isMobile() {
211222
return Boolean(window.getComputedStyle(this._element, null).getPropertyValue('--cui-is-mobile'))
212223
}
@@ -226,32 +237,14 @@ class Sidebar extends BaseComponent {
226237
_isVisible() {
227238
const rect = this._element.getBoundingClientRect()
228239
return (
229-
rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth)
240+
rect.top >= 0 && rect.left >= 0 && Math.floor(rect.bottom) <= (window.innerHeight || document.documentElement.clientHeight) && Math.floor(rect.right) <= (window.innerWidth || document.documentElement.clientWidth)
230241
)
231242
}
232243

233244
_addClassName(className) {
234245
this._element.classList.add(className)
235246
}
236247

237-
_removeBackdrop() {
238-
if (this._backdrop) {
239-
this._backdrop.remove()
240-
this._backdrop = null
241-
}
242-
}
243-
244-
_showBackdrop() {
245-
if (!this._backdrop) {
246-
this._backdrop = document.createElement('div')
247-
this._backdrop.className = CLASS_NAME_BACKDROP
248-
this._backdrop.classList.add(CLASS_NAME_FADE)
249-
document.body.append(this._backdrop)
250-
reflow(this._backdrop)
251-
this._backdrop.classList.add(CLASS_NAME_SHOW)
252-
}
253-
}
254-
255248
_clickOutListener(event, sidebar) {
256249
if (event.target.closest(SELECTOR_SIDEBAR) === null) {
257250
event.preventDefault()

0 commit comments

Comments
 (0)