@@ -4,6 +4,9 @@ import classNames from 'classnames';
4
4
import PropTypes from 'prop-types' ;
5
5
import PerfectScrollbar from 'react-perfect-scrollbar' ;
6
6
import 'react-perfect-scrollbar/dist/css/styles.css' ;
7
+ import '../css/scrollbar.css' ;
8
+
9
+ import LayoutHelper from './Shared/layout/layout'
7
10
8
11
const propTypes = {
9
12
children : PropTypes . node ,
@@ -39,17 +42,26 @@ class AppSidebarNav2 extends Component {
39
42
this . handleClick = this . handleClick . bind ( this ) ;
40
43
this . activeRoute = this . activeRoute . bind ( this ) ;
41
44
this . hideMobile = this . hideMobile . bind ( this ) ;
45
+
46
+ this . changes = null ;
47
+ this . state = { sidebarMinimized : false }
42
48
}
43
49
50
+ _scrollBarRef = null ;
51
+
44
52
handleClick ( e ) {
45
53
e . preventDefault ( ) ;
46
54
e . currentTarget . parentElement . classList . toggle ( 'open' ) ;
47
55
}
48
56
49
- activeRoute ( routeName , props ) {
57
+ isActiveRoute ( routeName , props ) {
50
58
return props . location . pathname . indexOf ( routeName ) > - 1
51
- ? 'nav-item nav-dropdown open'
52
- : 'nav-item nav-dropdown' ;
59
+ }
60
+
61
+ activeRoute ( routeName , props ) {
62
+ return this . isActiveRoute ( routeName , props ) ?
63
+ 'nav-item nav-dropdown open' :
64
+ 'nav-item nav-dropdown' ;
53
65
}
54
66
55
67
hideMobile ( ) {
@@ -148,6 +160,7 @@ class AppSidebarNav2 extends Component {
148
160
149
161
// nav link
150
162
navLink ( item , key , classes ) {
163
+ const ref = React . createRef ( ) ;
151
164
const url = item . url || '' ;
152
165
const itemIcon = < i className = { classes . icon } />
153
166
const itemBadge = this . navBadge ( item . badge )
@@ -171,7 +184,7 @@ class AppSidebarNav2 extends Component {
171
184
< RsNavLink href = { url } className = { classes . link } active { ...attributes } >
172
185
{ itemIcon } { item . name } { itemBadge }
173
186
</ RsNavLink > :
174
- < NavLink to = { url } className = { classes . link } activeClassName = "active" onClick = { this . hideMobile } { ...attributes } >
187
+ < NavLink to = { url } className = { classes . link } activeClassName = "active" onClick = { ( ) => this . hideMobile ( ref ) } ref = { ref } { ...attributes } >
175
188
{ itemIcon } { item . name } { itemBadge }
176
189
</ NavLink >
177
190
}
@@ -200,6 +213,45 @@ class AppSidebarNav2 extends Component {
200
213
return link . substring ( 0 , 4 ) === 'http' ;
201
214
}
202
215
216
+ observeDomMutations ( ) {
217
+ if ( window . MutationObserver ) {
218
+
219
+ // eslint-disable-next-line
220
+ this . changes = new MutationObserver ( ( mutations ) => {
221
+
222
+ const isSidebarMinimized = document . body . classList . contains ( 'sidebar-minimized' ) || false
223
+ this . setState ( { sidebarMinimized : isSidebarMinimized } )
224
+
225
+ LayoutHelper . sidebarPSToggle ( ! isSidebarMinimized )
226
+
227
+ } ) ;
228
+ const element = document . body ;
229
+ this . changes . observe ( element , {
230
+ attributes : true ,
231
+ attributeFilter : [ 'class' ]
232
+ } ) ;
233
+ }
234
+ window . addEventListener ( 'resize' , this . onResize ) ;
235
+ }
236
+
237
+ onResize ( ) {
238
+ LayoutHelper . sidebarPSToggle ( true )
239
+ }
240
+
241
+ componentDidMount ( ) {
242
+ this . observeDomMutations ( )
243
+ }
244
+
245
+ componentWillUnmount ( ) {
246
+ try {
247
+ this . changes . disconnect ( )
248
+ window . removeEventListener ( 'resize' , this . onResize ) ;
249
+ } catch ( ignore ) {
250
+ // eslint-disable-next-line
251
+ console . warn ( 'CoreUI SidebarNav failed to disconnect from MutationObserver' , ignore )
252
+ }
253
+ }
254
+
203
255
render ( ) {
204
256
const { className, children, navConfig, ...attributes } = this . props ;
205
257
@@ -208,18 +260,17 @@ class AppSidebarNav2 extends Component {
208
260
delete attributes . Tag
209
261
delete attributes . router
210
262
211
- const navClasses = classNames ( className , 'sidebar-nav' ) ;
263
+ const navClasses = classNames ( className , 'sidebar-nav' )
212
264
213
- // ToDo: find better rtl fix
214
- const isRtl = getComputedStyle ( document . documentElement ) . direction === 'rtl'
265
+ const options = Object . assign ( { } , { suppressScrollX : true , suppressScrollY : this . state . sidebarMinimized } )
215
266
216
267
// sidebar-nav root
217
268
return (
218
- < PerfectScrollbar className = { navClasses } { ...attributes } options = { { suppressScrollX : ! isRtl } } >
219
- < Nav >
220
- { children || this . navList ( navConfig . items ) }
221
- </ Nav >
222
- </ PerfectScrollbar >
269
+ < PerfectScrollbar className = { navClasses } { ...attributes } options = { options } ref = { ( ref ) => { this . _scrollBarRef = ref ; } } >
270
+ < Nav >
271
+ { children || this . navList ( navConfig . items ) }
272
+ </ Nav >
273
+ </ PerfectScrollbar >
223
274
) ;
224
275
}
225
276
}
0 commit comments