1
1
<script lang="ts">
2
- import { defineComponent , onBeforeMount , onUnmounted , PropType , watch , ref , computed , onMounted , nextTick } from ' vue' ;
3
- import { clearAllBodyScrollLocks , disableBodyScroll , enableBodyScroll } from ' body-scroll-lock' ;
2
+ import {
3
+ defineComponent ,
4
+ onBeforeMount ,
5
+ onBeforeUnmount ,
6
+ PropType ,
7
+ watch ,
8
+ ref ,
9
+ computed ,
10
+ onMounted ,
11
+ nextTick ,
12
+ } from ' vue' ;
13
+ import { disableBodyScroll , enableBodyScroll } from ' body-scroll-lock' ;
4
14
import SidePanelCloseButton from ' ./SidePanelCloseButton.vue' ;
5
15
6
16
export default defineComponent ({
@@ -106,6 +116,7 @@ export default defineComponent({
106
116
const panelHeight = ref (0 );
107
117
const windowHeight = ref (0 );
108
118
const zIndex = ref <number >();
119
+ const isBodyAlreadyLocked = ref (false );
109
120
110
121
const calculateRightSize = async () => {
111
122
if (window ?.innerHeight > 0 ) windowHeight .value = window .innerHeight ;
@@ -147,9 +158,9 @@ export default defineComponent({
147
158
document .body .appendChild (teleportContainer );
148
159
});
149
160
150
- onUnmounted (() => {
151
- if (props .lockScroll ) {
152
- clearAllBodyScrollLocks ( );
161
+ onBeforeUnmount (() => {
162
+ if (props .lockScroll && panel . value && props . modelValue ) {
163
+ enableBodyScroll ( panel . value );
153
164
lockUnlockHtml (false );
154
165
}
155
166
if (teleportContainer ) document .body .removeChild (teleportContainer );
@@ -165,9 +176,14 @@ export default defineComponent({
165
176
166
177
watch (
167
178
() => [props .modelValue , panel .value ],
168
- (p ) => {
169
- const [isShown, panelEl] = p as [boolean , HTMLElement | null ];
179
+ (newP , oldP ) => {
180
+ const wasShown = oldP ? (oldP [0 ] as boolean ) : false ;
181
+ const [isShown, panelEl] = newP as [boolean , HTMLElement | null ];
182
+ const isOpening = isShown ;
183
+ const isClosing = wasShown && ! isShown ;
170
184
if (! panelEl ) return ;
185
+ if (isOpening ) isBodyAlreadyLocked .value = !! document .body .style .overflow ;
186
+
171
187
if (isShown ) {
172
188
if (props .lockScroll ) {
173
189
disableBodyScroll (panelEl , { reserveScrollBarGap: true });
@@ -178,14 +194,13 @@ export default defineComponent({
178
194
return ;
179
195
}
180
196
181
- if (props .lockScroll ) {
182
- setTimeout (() => {
183
- if (! panelEl ) return ;
184
- enableBodyScroll (panelEl );
185
- lockUnlockHtml (false );
186
- }, props .panelDuration );
187
- }
197
+ if (! props .lockScroll || ! isClosing || isBodyAlreadyLocked .value ) return ;
188
198
199
+ setTimeout (() => {
200
+ if (! panelEl ) return ;
201
+ enableBodyScroll (panelEl );
202
+ lockUnlockHtml (false );
203
+ }, props .panelDuration );
189
204
window .removeEventListener (' resize' , calculateRightSize );
190
205
},
191
206
{ immediate: true },
0 commit comments