@@ -65,16 +65,40 @@ export default {
65
65
watch: {
66
66
show (val ) {
67
67
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'
68
84
}
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)
69
92
},
70
93
computed: {
71
94
sidebarClasses () {
95
+ const haveResponsiveClass = this .breakpoint && this .open === ' responsive'
72
96
return [
73
97
' c-sidebar' ,
74
98
` c-sidebar-${ this .colorScheme } ` ,
75
99
{
76
100
' c-sidebar-show' : this .open === true ,
77
- [` c-sidebar-${ this .breakpoint } -show` ]: this . open === ' responsive ' ,
101
+ [` c-sidebar-${ this .breakpoint } -show` ]: haveResponsiveClass ,
78
102
' c-sidebar-fixed' : this .fixed ,
79
103
' c-sidebar-right' : this .aside ,
80
104
' c-sidebar-minimized' : this .minimize && ! this .unfoldable ,
@@ -93,10 +117,13 @@ export default {
93
117
this .hideOnMobileClick &&
94
118
this .isOnMobile ()
95
119
) {
96
- this .open = ' responsive'
97
- this .$emit (' update:show' , ' responsive' )
120
+ this .closeSidebar ()
98
121
}
99
122
},
123
+ closeSidebar () {
124
+ this .open = ' responsive'
125
+ this .$emit (' update:show' , ' responsive' )
126
+ },
100
127
isOnMobile () {
101
128
return Boolean (getComputedStyle (this .$el ).getPropertyValue (' --is-mobile' ))
102
129
}
0 commit comments