Skip to content

Commit c187d0b

Browse files
committed
refactor(CSidebar): update responsive behavior
1 parent d3abee6 commit c187d0b

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

packages/coreui-vue/src/components/sidebar/CSidebar.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ const isOnMobile = (element: HTMLDivElement) => {
1414
const CSidebar = defineComponent({
1515
name: 'CSidebar',
1616
props: {
17+
/**
18+
* Sets if the color of text should be colored for a light or dark dark background.
19+
*
20+
* @values 'dark', light'
21+
*/
22+
colorScheme: {
23+
type: String,
24+
default: undefined,
25+
validator: (value: string) => {
26+
return ['dark', 'light'].includes(value)
27+
},
28+
},
1729
/**
1830
* Make sidebar narrow.
1931
*/
@@ -22,6 +34,17 @@ const CSidebar = defineComponent({
2234
* Set sidebar to overlaid variant.
2335
*/
2436
overlaid: Boolean,
37+
/**
38+
* Components placement, there’s no default placement.
39+
* @values 'start', 'end'
40+
*/
41+
placement: {
42+
type: String,
43+
default: undefined,
44+
validator: (value: string) => {
45+
return ['start', 'end'].includes(value)
46+
},
47+
},
2548
/**
2649
* Place sidebar in non-static positions.
2750
*/
@@ -64,10 +87,16 @@ const CSidebar = defineComponent({
6487
'visible-change',
6588
],
6689
setup(props, { attrs, slots, emit }) {
67-
const mobile = ref()
68-
const inViewport = ref()
6990
const sidebarRef = ref()
70-
const visible = ref(props.visible)
91+
92+
const inViewport = ref()
93+
const mobile = ref()
94+
const visibleMobile = ref(false)
95+
const visibleDesktop = ref(
96+
props.visible !== undefined ? props.visible : props.overlaid ? false : true,
97+
)
98+
99+
// const visible = ref(props.visible)
71100

72101
watch(inViewport, () => {
73102
emit('visible-change', inViewport.value)
@@ -76,11 +105,14 @@ const CSidebar = defineComponent({
76105

77106
watch(
78107
() => props.visible,
79-
() => (visible.value = props.visible),
108+
() => props.visible !== undefined && handleVisibleChange(props.visible),
80109
)
81110

82111
watch(mobile, () => {
83-
if (mobile.value && visible.value) visible.value = false
112+
if (mobile.value) {
113+
console.log('mobile')
114+
visibleMobile.value = false
115+
}
84116
})
85117

86118
onMounted(() => {
@@ -109,8 +141,17 @@ const CSidebar = defineComponent({
109141
})
110142
})
111143

144+
const handleVisibleChange = (visible: boolean) => {
145+
if (mobile.value) {
146+
visibleMobile.value = visible
147+
return
148+
}
149+
150+
visibleDesktop.value = visible
151+
}
152+
112153
const handleHide = () => {
113-
visible.value = false
154+
handleVisibleChange(false)
114155
emit('visible-change', false)
115156
}
116157

@@ -146,13 +187,15 @@ const CSidebar = defineComponent({
146187
class: [
147188
'sidebar',
148189
{
190+
[`sidebar-${props.colorScheme}`]: props.colorScheme,
149191
'sidebar-narrow': props.narrow,
150192
'sidebar-overlaid': props.overlaid,
193+
[`sidebar-${props.placement}`]: props.placement,
151194
[`sidebar-${props.position}`]: props.position,
152195
[`sidebar-${props.size}`]: props.size,
153196
'sidebar-narrow-unfoldable': props.unfoldable,
154-
show: visible.value === true && mobile.value,
155-
hide: visible.value === false && !mobile.value,
197+
show: (mobile.value && visibleMobile.value) || (props.overlaid && visibleDesktop.value),
198+
hide: visibleDesktop.value === false && !mobile.value && !props.overlaid,
156199
},
157200
attrs.class,
158201
],
@@ -163,7 +206,7 @@ const CSidebar = defineComponent({
163206
mobile.value &&
164207
h(CBackdrop, {
165208
class: 'sidebar-backdrop',
166-
visible: props.visible,
209+
visible: visibleMobile.value,
167210
onClick: () => handleHide(),
168211
}),
169212
]

packages/docs/api/sidebar/CSidebar.api.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import CSidebar from '@coreui/vue/src/components/sidebar/CSidebar'
88

99
#### Props
1010

11-
| Prop name | Description | Type | Values | Default |
12-
| -------------- | ------------------------------------------------ | ------- | ------ | ------- |
13-
| **narrow** | Make sidebar narrow. | boolean | - | - |
14-
| **overlaid** | Set sidebar to overlaid variant. | boolean | - | - |
15-
| **position** | Place sidebar in non-static positions. | string | - | - |
16-
| **size** | Size the component small, large, or extra large. | string | - | - |
17-
| **unfoldable** | Expand narrowed sidebar on hover. | boolean | - | - |
18-
| **visible** | Toggle the visibility of sidebar component. | boolean | - | - |
11+
| Prop name | Description | Type | Values | Default |
12+
| ---------------- | -------------------------------------------------------------------------------- | ------- | ------------------ | ------- |
13+
| **color-scheme** | Sets if the color of text should be colored for a light or dark dark background. | string | `'dark'`, `light'` | - |
14+
| **narrow** | Make sidebar narrow. | boolean | - | - |
15+
| **overlaid** | Set sidebar to overlaid variant. | boolean | - | - |
16+
| **placement** | Components placement, there’s no default placement. | string | `'start'`, `'end'` | - |
17+
| **position** | Place sidebar in non-static positions. | string | - | - |
18+
| **size** | Size the component small, large, or extra large. | string | - | - |
19+
| **unfoldable** | Expand narrowed sidebar on hover. | boolean | - | - |
20+
| **visible** | Toggle the visibility of sidebar component. | boolean | - | - |
1921

2022
#### Events
2123

0 commit comments

Comments
 (0)