Skip to content

Commit 238590a

Browse files
committed
refactor: update togglers
1 parent e07c7ec commit 238590a

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

js/src/sidebar.js

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,25 @@ const DATA_KEY = 'coreui.sidebar'
2828
const EVENT_KEY = `.${DATA_KEY}`
2929
const DATA_API_KEY = '.data-api'
3030

31-
const Default = {}
31+
const Default = {
32+
breakpoint: false
33+
}
3234

33-
const DefaultType = {}
35+
const DefaultType = {
36+
breakpoint: '(boolean|string)'
37+
}
3438

3539
const CLASS_NAME_BACKDROP = 'sidebar-backdrop'
3640
const CLASS_NAME_FADE = 'fade'
3741
const CLASS_NAME_SHOW = 'show'
42+
const CLASS_NAME_SIDEBAR = 'sidebar'
3843
const CLASS_NAME_SIDEBAR_NARROW = 'sidebar-narrow'
3944
const CLASS_NAME_SIDEBAR_OVERLAID = 'sidebar-overlaid'
40-
const CLASS_NAME_SIDEBAR_SHOW = 'sidebar-show'
4145
const CLASS_NAME_SIDEBAR_NARROW_UNFOLDABLE = 'sidebar-narrow-unfoldable'
4246

4347
// eslint-disable-next-line prefer-regex-literals
4448
const REGEXP_SIDEBAR_SHOW = new RegExp('sidebar.*show')
49+
const REGEXP_SIDEBAR_SHOW_BREAKPOINT = /sidebar-(sm|md|lg|xl|xxl)-show/
4550

4651
const EVENT_HIDE = `hide${EVENT_KEY}`
4752
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
@@ -51,8 +56,9 @@ const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
5156
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
5257

5358
const SELECTOR_DATA_CLOSE = '[data-coreui-close="sidebar"]'
59+
const SELECTOR_DATA_TOGGLE = '[data-coreui-toggle]'
60+
5461
const SELECTOR_SIDEBAR = '.sidebar'
55-
const SELECTOR_SIDEBAR_TOGGLER = '.sidebar-toggler'
5662

5763
/**
5864
* ------------------------------------------------------------------------
@@ -64,6 +70,7 @@ class Sidebar extends BaseComponent {
6470
constructor(element, config) {
6571
super(element)
6672
this._config = this._getConfig(config)
73+
this._breakpoint = this._getBreakpoint()
6774
this._show = this._isVisible()
6875
this._mobile = this._isMobile()
6976
this._overlaid = this._isOverlaid()
@@ -91,12 +98,10 @@ class Sidebar extends BaseComponent {
9198
EventHandler.trigger(this._element, EVENT_SHOW)
9299

93100
if (typeof breakpoint !== 'undefined') {
94-
this._element.classList.add(breakpoint)
101+
this._breakpoint = breakpoint
95102
}
96103

97-
if (typeof breakpoint === 'undefined' || this._isMobile()) {
98-
this._element.classList.add(CLASS_NAME_SIDEBAR_SHOW)
99-
}
104+
this._element.classList.add(this._createShowClass())
100105

101106
if (this._isMobile()) {
102107
this._showBackdrop()
@@ -119,22 +124,14 @@ class Sidebar extends BaseComponent {
119124
emulateTransitionEnd(this._element, transitionDuration)
120125
}
121126

122-
hide(breakpoint) {
127+
hide() {
123128
EventHandler.trigger(this._element, EVENT_HIDE)
124129

125-
if (typeof breakpoint !== 'undefined') {
126-
this._element.classList.remove(breakpoint)
127-
return
128-
}
129-
130-
if (typeof breakpoint === 'undefined' || this._isMobile()) {
131-
// eslint-disable-next-line unicorn/prefer-spread
132-
Array.from(this._element.classList).forEach(className => {
133-
if (className.match(REGEXP_SIDEBAR_SHOW)) {
134-
this._element.classList.remove(className)
135-
}
136-
})
137-
}
130+
this._element.classList.forEach(className => {
131+
if (className.match(REGEXP_SIDEBAR_SHOW)) {
132+
this._element.classList.remove(className)
133+
}
134+
})
138135

139136
if (this._isMobile()) {
140137
this._removeBackdrop()
@@ -159,11 +156,17 @@ class Sidebar extends BaseComponent {
159156

160157
toggle(breakpoint) {
161158
if (this._show) {
162-
this.hide(breakpoint)
159+
this.hide()
163160
return
164161
}
165162

166-
this.show(breakpoint)
163+
if (breakpoint === 'all') {
164+
this._breakpoint = false
165+
this.show(this._breakpoint)
166+
return
167+
}
168+
169+
this.show()
167170
}
168171

169172
narrow() {
@@ -230,6 +233,27 @@ class Sidebar extends BaseComponent {
230233
return config
231234
}
232235

236+
_getBreakpoint() {
237+
if (this._config.breakpoint) {
238+
return this._config.breakpoint
239+
}
240+
241+
const breakpoint = this._element.className.match(REGEXP_SIDEBAR_SHOW_BREAKPOINT)
242+
if (breakpoint) {
243+
return breakpoint[1]
244+
}
245+
246+
return false
247+
}
248+
249+
_createShowClass() {
250+
if (this._breakpoint && !this._isMobile()) {
251+
return `${CLASS_NAME_SIDEBAR}-${this._breakpoint}-${CLASS_NAME_SHOW}`
252+
}
253+
254+
return `${CLASS_NAME_SIDEBAR}-${CLASS_NAME_SHOW}`
255+
}
256+
233257
_isMobile() {
234258
return Boolean(window.getComputedStyle(this._element, null).getPropertyValue('--is-mobile'))
235259
}
@@ -270,7 +294,7 @@ class Sidebar extends BaseComponent {
270294
}
271295

272296
// eslint-disable-next-line no-warning-comments
273-
// TODO: ta metoda nie zawsze działa poprawnie
297+
// TODO: this method is not bulletproof
274298
_isVisible() {
275299
const rect = this._element.getBoundingClientRect()
276300
return (
@@ -331,7 +355,7 @@ class Sidebar extends BaseComponent {
331355
this._addClickOutListener()
332356
}
333357

334-
EventHandler.on(this._element, EVENT_CLICK_DATA_API, SELECTOR_SIDEBAR_TOGGLER, event => {
358+
EventHandler.on(this._element, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
335359
event.preventDefault()
336360
const toggle = Manipulator.getDataAttribute(event.target, 'toggle')
337361

0 commit comments

Comments
 (0)