@@ -74,24 +74,29 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
74
74
) => {
75
75
const sidebarRef = useRef < HTMLDivElement > ( null )
76
76
const forkedRef = useForkedRef ( ref , sidebarRef )
77
- const [ mobile , setMobile ] = useState ( false )
78
- const [ _visible , setVisible ] = useState ( visible )
77
+
79
78
const [ inViewport , setInViewport ] = useState < boolean > ( )
79
+ const [ mobile , setMobile ] = useState ( false )
80
+ const [ visibleMobile , setVisibleMobile ] = useState ( false )
81
+ const [ visibleDesktop , setVisibleDesktop ] = useState ( true )
80
82
81
83
useEffect ( ( ) => {
82
84
sidebarRef . current && setMobile ( isOnMobile ( sidebarRef . current ) )
83
-
84
- setVisible ( visible )
85
+ if ( visible !== undefined ) {
86
+ handleVisibleChange ( visible )
87
+ }
85
88
} , [ visible ] )
86
89
90
+
91
+
87
92
useEffect ( ( ) => {
88
93
inViewport !== undefined && onVisibleChange && onVisibleChange ( inViewport )
89
94
! inViewport && onHide && onHide ( )
90
95
inViewport && onShow && onShow ( )
91
96
} , [ inViewport ] )
92
97
93
98
useEffect ( ( ) => {
94
- mobile && visible && setVisible ( false )
99
+ mobile && setVisibleMobile ( false )
95
100
} , [ mobile ] )
96
101
97
102
useEffect ( ( ) => {
@@ -119,8 +124,17 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
119
124
}
120
125
} )
121
126
127
+ const handleVisibleChange = ( visible : boolean ) => {
128
+ if ( mobile ) {
129
+ setVisibleMobile ( visible )
130
+ return
131
+ }
132
+
133
+ setVisibleDesktop ( visible )
134
+ }
135
+
122
136
const handleHide = ( ) => {
123
- setVisible ( false )
137
+ handleVisibleChange ( false )
124
138
}
125
139
126
140
const handleResize = ( ) => {
@@ -167,8 +181,8 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
167
181
[ `sidebar-${ position } ` ] : position ,
168
182
[ `sidebar-${ size } ` ] : size ,
169
183
'sidebar-narrow-unfoldable' : unfoldable ,
170
- show : _visible === true && mobile ,
171
- hide : _visible === false && ! mobile ,
184
+ show : ( mobile && visibleMobile ) || ( overlaid && visibleDesktop ) ,
185
+ hide : ! visibleDesktop && ! mobile ,
172
186
} ,
173
187
className ,
174
188
) }
@@ -180,7 +194,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
180
194
{ typeof window !== 'undefined' &&
181
195
mobile &&
182
196
createPortal (
183
- < CBackdrop className = "sidebar-backdrop" visible = { _visible } /> ,
197
+ < CBackdrop className = "sidebar-backdrop" visible = { mobile && visibleMobile } /> ,
184
198
document . body ,
185
199
) }
186
200
</ >
0 commit comments