Skip to content

Commit 50f8661

Browse files
committed
feat: added internal CScrollbar component allowing menu animation
1 parent c1c3033 commit 50f8661

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

src/CScrollbar.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React, { useState, createRef, useEffect } from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import PerfectScrollbar from 'perfect-scrollbar'
5+
import 'perfect-scrollbar/css/perfect-scrollbar.css'
6+
7+
//component - CoreUI / CScrollbar
8+
const CScrollbar = props => {
9+
10+
const {
11+
tag: Tag,
12+
className,
13+
//
14+
innerRef,
15+
settings,
16+
switcher,
17+
...attributes
18+
} = props
19+
20+
const [instance, setInstance] = useState()
21+
const ref = createRef()
22+
innerRef && innerRef(ref)
23+
24+
useEffect(() => {
25+
switcher ? init() : uninit()
26+
}, [switcher])
27+
28+
useEffect(() => uninit(), [])
29+
30+
const init = () => {
31+
if (!instance) {
32+
createPerfectScrollbar()
33+
}
34+
}
35+
36+
const createPerfectScrollbar = () => {
37+
setInstance(new PerfectScrollbar(ref.current, settings))
38+
}
39+
40+
const uninit = () => {
41+
if (instance) {
42+
instance.destroy()
43+
setInstance(null)
44+
}
45+
}
46+
47+
// render
48+
return (
49+
<Tag
50+
className={classNames(className)}
51+
style={{ position: 'relative' }}
52+
{...attributes}
53+
ref={ref}
54+
/>
55+
)
56+
}
57+
58+
CScrollbar.propTypes = {
59+
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
60+
className: PropTypes.string,
61+
//
62+
settings: PropTypes.object,
63+
switcher: PropTypes.bool,
64+
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string])
65+
};
66+
67+
CScrollbar.defaultProps = {
68+
tag: 'div'
69+
};
70+
71+
export default CScrollbar

src/CSidebarNav.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { useContext } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
4-
import PerfectScrollbar from 'react-perfect-scrollbar'
4+
import CScrollbar from './CScrollbar'
55
import 'react-perfect-scrollbar/dist/css/styles.css'
66
import { Context } from './CSidebar'
7-
//component - CoreUI / CSidebarNav
87

8+
//component - CoreUI / CSidebarNav
99
const CSidebarNav = props => {
1010

1111
const {
@@ -22,17 +22,14 @@ const CSidebarNav = props => {
2222
//state
2323

2424
const isRtl = getComputedStyle(document.querySelector('html')).direction === 'rtl'
25-
return !scrollbarExist ?
26-
<ul className={navClasses} {...attributes} ref={innerRef}>
27-
{props.children}
28-
</ul> :
29-
<PerfectScrollbar
30-
options={{ suppressScrollX: !isRtl }}
31-
className={navClasses}
32-
ref={innerRef}
33-
component="ul"
34-
{...attributes}
35-
/>
25+
return <CScrollbar
26+
settings={{ suppressScrollX: !isRtl }}
27+
className={navClasses}
28+
innerRef={innerRef}
29+
switcher={scrollbarExist}
30+
tag="ul"
31+
{...attributes}
32+
/>
3633
}
3734

3835
CSidebarNav.propTypes = {

0 commit comments

Comments
 (0)