Skip to content

Commit 73f5edb

Browse files
committed
refactor(CSidebar): improve responsive behavior
1 parent 261ff5b commit 73f5edb

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

packages/coreui-react/src/components/sidebar/CSidebar.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
1313
* A string of all className you want applied to the component.
1414
*/
1515
className?: string
16+
/**
17+
* Sets if the color of text should be colored for a light or dark dark background.
18+
*
19+
* @type 'dark' | 'light'
20+
*/
21+
colorScheme?: 'dark' | 'light'
1622
/**
1723
* Make sidebar narrow.
1824
*/
@@ -33,6 +39,11 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
3339
* Set sidebar to overlaid variant.
3440
*/
3541
overlaid?: boolean
42+
/**
43+
* Components placement, there’s no default placement.
44+
* @type 'start' | 'end'
45+
*/
46+
placement?: 'start' | 'end'
3647
/**
3748
* Place sidebar in non-static positions.
3849
*/
@@ -59,11 +70,13 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
5970
{
6071
children,
6172
className,
73+
colorScheme,
6274
narrow,
6375
onHide,
6476
onShow,
6577
onVisibleChange,
6678
overlaid,
79+
placement,
6780
position,
6881
size,
6982
unfoldable,
@@ -77,18 +90,16 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
7790

7891
const [inViewport, setInViewport] = useState<boolean>()
7992
const [mobile, setMobile] = useState(false)
80-
const [visibleMobile, setVisibleMobile] = useState(false)
81-
const [visibleDesktop, setVisibleDesktop] = useState(true)
93+
const [visibleMobile, setVisibleMobile] = useState<boolean>(false)
94+
const [visibleDesktop, setVisibleDesktop] = useState<boolean>(
95+
visible !== undefined ? visible : overlaid ? false : true,
96+
)
8297

8398
useEffect(() => {
8499
sidebarRef.current && setMobile(isOnMobile(sidebarRef.current))
85-
if (visible !== undefined) {
86-
handleVisibleChange(visible)
87-
}
100+
visible !== undefined && handleVisibleChange(visible)
88101
}, [visible])
89102

90-
91-
92103
useEffect(() => {
93104
inViewport !== undefined && onVisibleChange && onVisibleChange(inViewport)
94105
!inViewport && onHide && onHide()
@@ -176,13 +187,15 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
176187
className={classNames(
177188
'sidebar',
178189
{
190+
[`sidebar-${colorScheme}`]: colorScheme,
179191
'sidebar-narrow': narrow,
180192
'sidebar-overlaid': overlaid,
193+
[`sidebar-${placement}`]: placement,
181194
[`sidebar-${position}`]: position,
182195
[`sidebar-${size}`]: size,
183196
'sidebar-narrow-unfoldable': unfoldable,
184197
show: (mobile && visibleMobile) || (overlaid && visibleDesktop),
185-
hide: !visibleDesktop && !mobile,
198+
hide: visibleDesktop === false && !mobile && !overlaid,
186199
},
187200
className,
188201
)}
@@ -205,11 +218,13 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
205218
CSidebar.propTypes = {
206219
children: PropTypes.node,
207220
className: PropTypes.string,
221+
colorScheme: PropTypes.oneOf(['dark', 'light']),
208222
narrow: PropTypes.bool,
209223
onHide: PropTypes.func,
210224
onShow: PropTypes.func,
211225
onVisibleChange: PropTypes.func,
212226
overlaid: PropTypes.bool,
227+
placement: PropTypes.oneOf(['start', 'end']),
213228
position: PropTypes.oneOf(['fixed', 'sticky']),
214229
size: PropTypes.oneOf(['sm', 'lg', 'xl']),
215230
unfoldable: PropTypes.bool,

0 commit comments

Comments
 (0)