@@ -66,29 +66,17 @@ export default {
6666 show (val ) {
6767 this .open = val
6868 },
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- }
69+ open (val ) {
70+ val === true ? this .createBackdrop () : this .removeBackdrop ()
7671 }
7772 },
7873 mounted () {
79- const backdrop = document .createElement (' div' )
8074 if (this .open === true ) {
81- backdrop .className = ' c-sidebar-backdrop c-show'
82- } else {
83- backdrop .className = ' c-sidebar-backdrop d-none'
75+ this .createBackdrop ()
8476 }
85- document .body .appendChild (backdrop)
86- backdrop .addEventListener (' click' , this .closeSidebar )
8777 },
8878 beforeDestroy () {
89- const backdrop = document .getElementsByClassName (' c-sidebar-backdrop' )[0 ]
90- backdrop .removeEventListener (' click' , this .closeSidebar )
91- document .body .removeChild (backdrop)
79+ this .removeBackdrop ()
9280 },
9381 computed: {
9482 sidebarClasses () {
@@ -116,11 +104,25 @@ export default {
116104 }
117105 },
118106 closeSidebar () {
119- this .open = ' responsive'
120- this .$emit (' update:show' , ' responsive ' )
107+ this .open = this . overlaid ? false : ' responsive'
108+ this .$emit (' update:show' , this . open )
121109 },
122110 isOnMobile () {
123111 return Boolean (getComputedStyle (this .$el ).getPropertyValue (' --is-mobile' ))
112+ },
113+ createBackdrop () {
114+ const backdrop = document .createElement (' div' )
115+ backdrop .className = ' c-sidebar-backdrop c-show'
116+ backdrop .id = this ._uid + ' backdrop'
117+ document .body .appendChild (backdrop)
118+ backdrop .addEventListener (' click' , this .closeSidebar )
119+ },
120+ removeBackdrop () {
121+ const backdrop = document .getElementById (this ._uid + ' backdrop' )
122+ if (backdrop) {
123+ backdrop .removeEventListener (' click' , this .closeSidebar )
124+ document .body .removeChild (backdrop)
125+ }
124126 }
125127 }
126128}
0 commit comments