@@ -28,20 +28,25 @@ const DATA_KEY = 'coreui.sidebar'
28
28
const EVENT_KEY = `.${ DATA_KEY } `
29
29
const DATA_API_KEY = '.data-api'
30
30
31
- const Default = { }
31
+ const Default = {
32
+ breakpoint : false
33
+ }
32
34
33
- const DefaultType = { }
35
+ const DefaultType = {
36
+ breakpoint : '(boolean|string)'
37
+ }
34
38
35
39
const CLASS_NAME_BACKDROP = 'sidebar-backdrop'
36
40
const CLASS_NAME_FADE = 'fade'
37
41
const CLASS_NAME_SHOW = 'show'
42
+ const CLASS_NAME_SIDEBAR = 'sidebar'
38
43
const CLASS_NAME_SIDEBAR_NARROW = 'sidebar-narrow'
39
44
const CLASS_NAME_SIDEBAR_OVERLAID = 'sidebar-overlaid'
40
- const CLASS_NAME_SIDEBAR_SHOW = 'sidebar-show'
41
45
const CLASS_NAME_SIDEBAR_NARROW_UNFOLDABLE = 'sidebar-narrow-unfoldable'
42
46
43
47
// eslint-disable-next-line prefer-regex-literals
44
48
const REGEXP_SIDEBAR_SHOW = new RegExp ( 'sidebar.*show' )
49
+ const REGEXP_SIDEBAR_SHOW_BREAKPOINT = / s i d e b a r - ( s m | m d | l g | x l | x x l ) - s h o w /
45
50
46
51
const EVENT_HIDE = `hide${ EVENT_KEY } `
47
52
const EVENT_HIDDEN = `hidden${ EVENT_KEY } `
@@ -51,8 +56,9 @@ const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
51
56
const EVENT_LOAD_DATA_API = `load${ EVENT_KEY } ${ DATA_API_KEY } `
52
57
53
58
const SELECTOR_DATA_CLOSE = '[data-coreui-close="sidebar"]'
59
+ const SELECTOR_DATA_TOGGLE = '[data-coreui-toggle]'
60
+
54
61
const SELECTOR_SIDEBAR = '.sidebar'
55
- const SELECTOR_SIDEBAR_TOGGLER = '.sidebar-toggler'
56
62
57
63
/**
58
64
* ------------------------------------------------------------------------
@@ -64,6 +70,7 @@ class Sidebar extends BaseComponent {
64
70
constructor ( element , config ) {
65
71
super ( element )
66
72
this . _config = this . _getConfig ( config )
73
+ this . _breakpoint = this . _getBreakpoint ( )
67
74
this . _show = this . _isVisible ( )
68
75
this . _mobile = this . _isMobile ( )
69
76
this . _overlaid = this . _isOverlaid ( )
@@ -91,12 +98,10 @@ class Sidebar extends BaseComponent {
91
98
EventHandler . trigger ( this . _element , EVENT_SHOW )
92
99
93
100
if ( typeof breakpoint !== 'undefined' ) {
94
- this . _element . classList . add ( breakpoint )
101
+ this . _breakpoint = breakpoint
95
102
}
96
103
97
- if ( typeof breakpoint === 'undefined' || this . _isMobile ( ) ) {
98
- this . _element . classList . add ( CLASS_NAME_SIDEBAR_SHOW )
99
- }
104
+ this . _element . classList . add ( this . _createShowClass ( ) )
100
105
101
106
if ( this . _isMobile ( ) ) {
102
107
this . _showBackdrop ( )
@@ -119,22 +124,14 @@ class Sidebar extends BaseComponent {
119
124
emulateTransitionEnd ( this . _element , transitionDuration )
120
125
}
121
126
122
- hide ( breakpoint ) {
127
+ hide ( ) {
123
128
EventHandler . trigger ( this . _element , EVENT_HIDE )
124
129
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
+ } )
138
135
139
136
if ( this . _isMobile ( ) ) {
140
137
this . _removeBackdrop ( )
@@ -159,11 +156,17 @@ class Sidebar extends BaseComponent {
159
156
160
157
toggle ( breakpoint ) {
161
158
if ( this . _show ) {
162
- this . hide ( breakpoint )
159
+ this . hide ( )
163
160
return
164
161
}
165
162
166
- this . show ( breakpoint )
163
+ if ( breakpoint === 'all' ) {
164
+ this . _breakpoint = false
165
+ this . show ( this . _breakpoint )
166
+ return
167
+ }
168
+
169
+ this . show ( )
167
170
}
168
171
169
172
narrow ( ) {
@@ -230,6 +233,27 @@ class Sidebar extends BaseComponent {
230
233
return config
231
234
}
232
235
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
+
233
257
_isMobile ( ) {
234
258
return Boolean ( window . getComputedStyle ( this . _element , null ) . getPropertyValue ( '--is-mobile' ) )
235
259
}
@@ -270,7 +294,7 @@ class Sidebar extends BaseComponent {
270
294
}
271
295
272
296
// eslint-disable-next-line no-warning-comments
273
- // TODO: ta metoda nie zawsze działa poprawnie
297
+ // TODO: this method is not bulletproof
274
298
_isVisible ( ) {
275
299
const rect = this . _element . getBoundingClientRect ( )
276
300
return (
@@ -331,7 +355,7 @@ class Sidebar extends BaseComponent {
331
355
this . _addClickOutListener ( )
332
356
}
333
357
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 => {
335
359
event . preventDefault ( )
336
360
const toggle = Manipulator . getDataAttribute ( event . target , 'toggle' )
337
361
0 commit comments