Skip to content

Commit 848d649

Browse files
committed
fix: drawer throw error when unmounted
1 parent c1102df commit 848d649

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

components/vc-drawer/src/Drawer.js

+57-53
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ const Drawer = defineComponent({
242242
const { placement, levelMove, duration, ease, getContainer } = this.$props;
243243
if (!windowIsUndefined) {
244244
this.levelDom.forEach(dom => {
245-
if (this.isOpenChange || openTransition) {
245+
if (dom && (this.isOpenChange || openTransition)) {
246246
/* eslint no-param-reassign: "error" */
247247
dom.style.transition = `transform ${duration} ${ease}`;
248248
addEventListener(dom, transitionEnd, this.trnasitionEnd);
@@ -275,27 +275,29 @@ const Drawer = defineComponent({
275275
if (right) {
276276
document.body.style.position = 'relative';
277277
document.body.style.width = `calc(100% - ${right}px)`;
278-
this.dom.style.transition = 'none';
279-
switch (placement) {
280-
case 'right':
281-
this.dom.style.transform = `translateX(-${right}px)`;
282-
this.dom.style.msTransform = `translateX(-${right}px)`;
283-
break;
284-
case 'top':
285-
case 'bottom':
286-
this.dom.style.width = `calc(100% - ${right}px)`;
287-
this.dom.style.transform = 'translateZ(0)';
288-
break;
289-
default:
290-
break;
291-
}
292278
clearTimeout(this.timeout);
293-
this.timeout = setTimeout(() => {
294-
this.dom.style.transition = `${trannsformTransition},${widthTransition}`;
295-
this.dom.style.width = '';
296-
this.dom.style.transform = '';
297-
this.dom.style.msTransform = '';
298-
});
279+
if (this.dom) {
280+
this.dom.style.transition = 'none';
281+
switch (placement) {
282+
case 'right':
283+
this.dom.style.transform = `translateX(-${right}px)`;
284+
this.dom.style.msTransform = `translateX(-${right}px)`;
285+
break;
286+
case 'top':
287+
case 'bottom':
288+
this.dom.style.width = `calc(100% - ${right}px)`;
289+
this.dom.style.transform = 'translateZ(0)';
290+
break;
291+
default:
292+
break;
293+
}
294+
this.timeout = setTimeout(() => {
295+
this.dom.style.transition = `${trannsformTransition},${widthTransition}`;
296+
this.dom.style.width = '';
297+
this.dom.style.transform = '';
298+
this.dom.style.msTransform = '';
299+
});
300+
}
299301
}
300302
// 手机禁滚
301303
domArray.forEach((item, i) => {
@@ -317,41 +319,43 @@ const Drawer = defineComponent({
317319
if (transitionStr) {
318320
document.body.style.overflowX = 'hidden';
319321
}
320-
this.dom.style.transition = 'none';
321-
let heightTransition;
322-
switch (placement) {
323-
case 'right': {
324-
this.dom.style.transform = `translateX(${right}px)`;
325-
this.dom.style.msTransform = `translateX(${right}px)`;
326-
this.dom.style.width = '100%';
327-
widthTransition = `width 0s ${ease} ${duration}`;
328-
if (this.maskDom) {
329-
this.maskDom.style.left = `-${right}px`;
330-
this.maskDom.style.width = `calc(100% + ${right}px)`;
322+
if (placement === 'right' && this.maskDom) {
323+
this.maskDom.style.left = `-${right}px`;
324+
this.maskDom.style.width = `calc(100% + ${right}px)`;
325+
}
326+
clearTimeout(this.timeout);
327+
if (this.dom) {
328+
this.dom.style.transition = 'none';
329+
let heightTransition;
330+
switch (placement) {
331+
case 'right': {
332+
this.dom.style.transform = `translateX(${right}px)`;
333+
this.dom.style.msTransform = `translateX(${right}px)`;
334+
this.dom.style.width = '100%';
335+
widthTransition = `width 0s ${ease} ${duration}`;
336+
break;
331337
}
332-
break;
333-
}
334-
case 'top':
335-
case 'bottom': {
336-
this.dom.style.width = `calc(100% + ${right}px)`;
337-
this.dom.style.height = '100%';
338-
this.dom.style.transform = 'translateZ(0)';
339-
heightTransition = `height 0s ${ease} ${duration}`;
340-
break;
338+
case 'top':
339+
case 'bottom': {
340+
this.dom.style.width = `calc(100% + ${right}px)`;
341+
this.dom.style.height = '100%';
342+
this.dom.style.transform = 'translateZ(0)';
343+
heightTransition = `height 0s ${ease} ${duration}`;
344+
break;
345+
}
346+
default:
347+
break;
341348
}
342-
default:
343-
break;
349+
this.timeout = setTimeout(() => {
350+
this.dom.style.transition = `${trannsformTransition},${
351+
heightTransition ? `${heightTransition},` : ''
352+
}${widthTransition}`;
353+
this.dom.style.transform = '';
354+
this.dom.style.msTransform = '';
355+
this.dom.style.width = '';
356+
this.dom.style.height = '';
357+
});
344358
}
345-
clearTimeout(this.timeout);
346-
this.timeout = setTimeout(() => {
347-
this.dom.style.transition = `${trannsformTransition},${
348-
heightTransition ? `${heightTransition},` : ''
349-
}${widthTransition}`;
350-
this.dom.style.transform = '';
351-
this.dom.style.msTransform = '';
352-
this.dom.style.width = '';
353-
this.dom.style.height = '';
354-
});
355359
}
356360
domArray.forEach((item, i) => {
357361
if (!item) {

0 commit comments

Comments
 (0)