Skip to content

Commit db11c6d

Browse files
committedJul 11, 2021
feat(CSidebar): update responsive behavior
1 parent bf8fcec commit db11c6d

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed
 

‎src/components/sidebar/CSidebar.tsx

+30-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TODO: check if element is visible after toggle
2+
13
import React, {
24
forwardRef,
35
HTMLAttributes,
@@ -12,6 +14,7 @@ import classNames from 'classnames'
1214

1315
import { Breakpoints } from '../Types'
1416
import { useForkedRef } from '../../utils/hooks'
17+
import { CBackdrop } from '../backdrop/CBackdrop'
1518

1619
export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
1720
/**
@@ -42,14 +45,14 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
4245
* Make any sidebar self hiding across all viewports or pick a maximum breakpoint with which to have a self hiding up to. [docs]
4346
*/
4447
selfHiding?: Breakpoints | boolean
45-
/**
46-
* Show self hidden sidebar. [docs]
47-
*/
48-
show?: boolean
4948
/**
5049
* Expand narrowed sidebar on hover. [docs]
5150
*/
5251
unfoldable?: boolean
52+
/**
53+
* Toggle the visibility of sidebar component. [docs]
54+
*/
55+
visible?: boolean
5356
}
5457

5558
export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
@@ -64,15 +67,15 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
6467
position,
6568
selfHiding,
6669
unfoldable,
67-
show,
70+
visible,
6871
...rest
6972
},
7073
ref,
7174
) => {
7275
const sidebarRef = useRef<HTMLDivElement>(null)
7376
const forkedRef = useForkedRef(ref, sidebarRef)
74-
const [_show, setShow] = useState(show)
7577
const [mobile, setMobile] = useState(false)
78+
const [_visible, setVisible] = useState(visible)
7679

7780
const isOnMobile = (element: React.RefObject<HTMLDivElement>) =>
7881
Boolean(
@@ -84,14 +87,14 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
8487
})
8588

8689
useEffect(() => {
87-
setShow(show)
90+
setVisible(visible)
8891
setMobile(isOnMobile(sidebarRef))
89-
}, [show])
92+
}, [visible])
9093

9194
useEffect(() => {
9295
setMobile(isOnMobile(sidebarRef))
93-
_show && onShow && onShow()
94-
}, [_show])
96+
_visible && onShow && onShow()
97+
}, [_visible])
9598

9699
useEffect(() => {
97100
window.addEventListener('mouseup', handleClickOutside)
@@ -106,19 +109,27 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
106109
})
107110

108111
const handleHide = () => {
109-
if (_show) {
110-
setShow(false)
112+
if (_visible) {
113+
setVisible(false)
111114
onHide && onHide()
112115
}
113116
}
114117

115118
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+
) {
117124
handleHide()
118125
}
119126
}
120127
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+
) {
122133
handleHide()
123134
}
124135
}
@@ -140,7 +151,8 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
140151
[`sidebar-${position}`]: position,
141152
[`sidebar-self-hiding${typeof selfHiding !== 'boolean' && '-' + selfHiding}`]: selfHiding,
142153
'sidebar-narrow-unfoldable': unfoldable,
143-
show: _show,
154+
show: _visible,
155+
hide: !_visible,
144156
},
145157
className,
146158
)
@@ -151,8 +163,9 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
151163
{children}
152164
</div>
153165
{typeof window !== 'undefined' &&
166+
mobile &&
154167
createPortal(
155-
mobile && _show && <div className="sidebar-backdrop fade show"></div>,
168+
<CBackdrop className="sidebar-backdrop" visible={_visible} />,
156169
document.body,
157170
)}
158171
</>
@@ -172,8 +185,8 @@ CSidebar.propTypes = {
172185
PropTypes.bool,
173186
PropTypes.oneOf<'sm' | 'md' | 'lg' | 'xl' | 'xxl'>(['sm', 'md', 'lg', 'xl', 'xxl']),
174187
]),
175-
show: PropTypes.bool,
176188
unfoldable: PropTypes.bool,
189+
visible: PropTypes.bool,
177190
}
178191

179192
CSidebar.displayName = 'CSidebar'

0 commit comments

Comments
 (0)
Please sign in to comment.