1
+ // TODO: check if element is visible after toggle
2
+
1
3
import React , {
2
4
forwardRef ,
3
5
HTMLAttributes ,
@@ -12,6 +14,7 @@ import classNames from 'classnames'
12
14
13
15
import { Breakpoints } from '../Types'
14
16
import { useForkedRef } from '../../utils/hooks'
17
+ import { CBackdrop } from '../backdrop/CBackdrop'
15
18
16
19
export interface CSidebarProps extends HTMLAttributes < HTMLDivElement > {
17
20
/**
@@ -42,14 +45,14 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
42
45
* Make any sidebar self hiding across all viewports or pick a maximum breakpoint with which to have a self hiding up to. [docs]
43
46
*/
44
47
selfHiding ?: Breakpoints | boolean
45
- /**
46
- * Show self hidden sidebar. [docs]
47
- */
48
- show ?: boolean
49
48
/**
50
49
* Expand narrowed sidebar on hover. [docs]
51
50
*/
52
51
unfoldable ?: boolean
52
+ /**
53
+ * Toggle the visibility of sidebar component. [docs]
54
+ */
55
+ visible ?: boolean
53
56
}
54
57
55
58
export const CSidebar = forwardRef < HTMLDivElement , CSidebarProps > (
@@ -64,15 +67,15 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
64
67
position,
65
68
selfHiding,
66
69
unfoldable,
67
- show ,
70
+ visible ,
68
71
...rest
69
72
} ,
70
73
ref ,
71
74
) => {
72
75
const sidebarRef = useRef < HTMLDivElement > ( null )
73
76
const forkedRef = useForkedRef ( ref , sidebarRef )
74
- const [ _show , setShow ] = useState ( show )
75
77
const [ mobile , setMobile ] = useState ( false )
78
+ const [ _visible , setVisible ] = useState ( visible )
76
79
77
80
const isOnMobile = ( element : React . RefObject < HTMLDivElement > ) =>
78
81
Boolean (
@@ -84,14 +87,14 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
84
87
} )
85
88
86
89
useEffect ( ( ) => {
87
- setShow ( show )
90
+ setVisible ( visible )
88
91
setMobile ( isOnMobile ( sidebarRef ) )
89
- } , [ show ] )
92
+ } , [ visible ] )
90
93
91
94
useEffect ( ( ) => {
92
95
setMobile ( isOnMobile ( sidebarRef ) )
93
- _show && onShow && onShow ( )
94
- } , [ _show ] )
96
+ _visible && onShow && onShow ( )
97
+ } , [ _visible ] )
95
98
96
99
useEffect ( ( ) => {
97
100
window . addEventListener ( 'mouseup' , handleClickOutside )
@@ -106,19 +109,27 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
106
109
} )
107
110
108
111
const handleHide = ( ) => {
109
- if ( _show ) {
110
- setShow ( false )
112
+ if ( _visible ) {
113
+ setVisible ( false )
111
114
onHide && onHide ( )
112
115
}
113
116
}
114
117
115
118
const handleKeyup = ( event : Event ) => {
116
- if ( sidebarRef . current && ! sidebarRef . current . contains ( event . target as HTMLElement ) ) {
119
+ if (
120
+ mobile &&
121
+ sidebarRef . current &&
122
+ ! sidebarRef . current . contains ( event . target as HTMLElement )
123
+ ) {
117
124
handleHide ( )
118
125
}
119
126
}
120
127
const handleClickOutside = ( event : Event ) => {
121
- if ( sidebarRef . current && ! sidebarRef . current . contains ( event . target as HTMLElement ) ) {
128
+ if (
129
+ mobile &&
130
+ sidebarRef . current &&
131
+ ! sidebarRef . current . contains ( event . target as HTMLElement )
132
+ ) {
122
133
handleHide ( )
123
134
}
124
135
}
@@ -140,7 +151,8 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
140
151
[ `sidebar-${ position } ` ] : position ,
141
152
[ `sidebar-self-hiding${ typeof selfHiding !== 'boolean' && '-' + selfHiding } ` ] : selfHiding ,
142
153
'sidebar-narrow-unfoldable' : unfoldable ,
143
- show : _show ,
154
+ show : _visible ,
155
+ hide : ! _visible ,
144
156
} ,
145
157
className ,
146
158
)
@@ -151,8 +163,9 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
151
163
{ children }
152
164
</ div >
153
165
{ typeof window !== 'undefined' &&
166
+ mobile &&
154
167
createPortal (
155
- mobile && _show && < div className = "sidebar-backdrop fade show" > </ div > ,
168
+ < CBackdrop className = "sidebar-backdrop" visible = { _visible } / >,
156
169
document . body ,
157
170
) }
158
171
</ >
@@ -172,8 +185,8 @@ CSidebar.propTypes = {
172
185
PropTypes . bool ,
173
186
PropTypes . oneOf < 'sm' | 'md' | 'lg' | 'xl' | 'xxl' > ( [ 'sm' , 'md' , 'lg' , 'xl' , 'xxl' ] ) ,
174
187
] ) ,
175
- show : PropTypes . bool ,
176
188
unfoldable : PropTypes . bool ,
189
+ visible : PropTypes . bool ,
177
190
}
178
191
179
192
CSidebar . displayName = 'CSidebar'
0 commit comments