From fff5fa9fa7b70de45ef11f4a819445b9e7db490a Mon Sep 17 00:00:00 2001 From: chenjiangui <674149625@qq.com> Date: Tue, 28 Dec 2021 19:57:43 +0800 Subject: [PATCH] fix(swipe): delay load cause swipe error (#10052) (#10069) --- packages/vant/src/swipe/Swipe.tsx | 56 ++++++++++++++++++------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/packages/vant/src/swipe/Swipe.tsx b/packages/vant/src/swipe/Swipe.tsx index 55f772aa9..782f1aa65 100644 --- a/packages/vant/src/swipe/Swipe.tsx +++ b/packages/vant/src/swipe/Swipe.tsx @@ -8,9 +8,10 @@ import { onDeactivated, onBeforeUnmount, defineComponent, - type InjectionKey, - type CSSProperties, + nextTick, type ExtractPropTypes, + type CSSProperties, + type InjectionKey, } from 'vue'; // Utils @@ -248,28 +249,37 @@ export default defineComponent({ return; } - if (!isHidden(root)) { - const rect = { - width: root.value.offsetWidth, - height: root.value.offsetHeight, - }; - state.rect = rect; - state.width = +(props.width ?? rect.width); - state.height = +(props.height ?? rect.height); + const cb = () => { + if (!isHidden(root)) { + const rect = { + width: root.value!.offsetWidth, + height: root.value!.offsetHeight, + }; + state.rect = rect; + state.width = +(props.width ?? rect.width); + state.height = +(props.height ?? rect.height); + } + + if (count.value) { + active = Math.min(count.value - 1, active); + } + + state.active = active; + state.swiping = true; + state.offset = getTargetOffset(active); + children.forEach((swipe) => { + swipe.setOffset(0); + }); + + autoplay(); + }; + + // issue: https://github.com/youzan/vant/issues/10052 + if (isHidden(root)) { + nextTick().then(cb); + } else { + cb(); } - - if (count.value) { - active = Math.min(count.value - 1, active); - } - - state.active = active; - state.swiping = true; - state.offset = getTargetOffset(active); - children.forEach((swipe) => { - swipe.setOffset(0); - }); - - autoplay(); }; const resize = () => initialize(state.active);