Skip to content

Commit ba6a166

Browse files
committed
feat: add backdrop to CSidebar
1 parent 48d7ad1 commit ba6a166

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/components/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,15 @@ export declare class CScrollbar extends Vue {
462462
export declare class CSidebar extends Vue {
463463
fixed: boolean
464464
unfoldable: boolean
465+
overlaid: boolean
465466
breakpoint: [string, boolean]
466467
minimize: boolean
467468
show: [boolean, string]
469+
size: string
468470
hideOnMobileClick: boolean
469471
aside: boolean
470472
colorScheme: string
471473
dropdownMode: string
472-
size: string
473-
overlaid: boolean
474474
}
475475

476476
export declare class CSidebarBrand extends Vue {

src/components/sidebar/CSidebar.vue

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,40 @@ export default {
6565
watch: {
6666
show (val) {
6767
this.open = val
68+
},
69+
open (val, oldVal) {
70+
const backdrop = document.getElementsByClassName('c-sidebar-backdrop')[0]
71+
if (val === true) {
72+
backdrop.className = 'c-sidebar-backdrop c-show'
73+
} else if (oldVal === true) {
74+
backdrop.className = 'c-sidebar-backdrop d-none'
75+
}
76+
}
77+
},
78+
mounted () {
79+
const backdrop = document.createElement('div')
80+
if (this.open === true) {
81+
backdrop.className = 'c-sidebar-backdrop c-show'
82+
} else {
83+
backdrop.className = 'c-sidebar-backdrop d-none'
6884
}
85+
document.body.appendChild(backdrop)
86+
backdrop.addEventListener('click', this.closeSidebar)
87+
},
88+
beforeDestroy () {
89+
const backdrop = document.getElementsByClassName('c-sidebar-backdrop')[0]
90+
backdrop.removeEventListener('click', this.closeSidebar)
91+
document.body.removeChild(backdrop)
6992
},
7093
computed: {
7194
sidebarClasses () {
95+
const haveResponsiveClass = this.breakpoint && this.open === 'responsive'
7296
return [
7397
'c-sidebar',
7498
`c-sidebar-${this.colorScheme}`,
7599
{
76100
'c-sidebar-show': this.open === true,
77-
[`c-sidebar-${this.breakpoint}-show`]: this.open === 'responsive',
101+
[`c-sidebar-${this.breakpoint}-show`]: haveResponsiveClass,
78102
'c-sidebar-fixed': this.fixed,
79103
'c-sidebar-right': this.aside,
80104
'c-sidebar-minimized': this.minimize && !this.unfoldable,
@@ -93,10 +117,13 @@ export default {
93117
this.hideOnMobileClick &&
94118
this.isOnMobile()
95119
) {
96-
this.open = 'responsive'
97-
this.$emit('update:show', 'responsive')
120+
this.closeSidebar()
98121
}
99122
},
123+
closeSidebar () {
124+
this.open = 'responsive'
125+
this.$emit('update:show', 'responsive')
126+
},
100127
isOnMobile () {
101128
return Boolean(getComputedStyle(this.$el).getPropertyValue('--is-mobile'))
102129
}

0 commit comments

Comments
 (0)