Explorar el Código

fix(Transition): prevent unmounted block from being inserted after transition leave

daiwei hace 4 meses
padre
commit
f9a9fadc6d

+ 10 - 1
packages/runtime-vapor/src/block.ts

@@ -79,6 +79,7 @@ export function insert(
   parent: ParentNode & { $fc?: Node | null },
   parent: ParentNode & { $fc?: Node | null },
   anchor: Node | null | 0 = null, // 0 means prepend
   anchor: Node | null | 0 = null, // 0 means prepend
   moveType: MoveType = MoveType.ENTER,
   moveType: MoveType = MoveType.ENTER,
+  parentComponent?: VaporComponentInstance,
   parentSuspense?: any, // TODO Suspense
   parentSuspense?: any, // TODO Suspense
 ): void {
 ): void {
   anchor = anchor === 0 ? parent.$fc || _child(parent) : anchor
   anchor = anchor === 0 ? parent.$fc || _child(parent) : anchor
@@ -98,7 +99,15 @@ export function insert(
         action(
         action(
           block,
           block,
           (block as TransitionBlock).$transition as TransitionHooks,
           (block as TransitionBlock).$transition as TransitionHooks,
-          () => parent.insertBefore(block, anchor as Node),
+          () => {
+            // if the component is unmounted after leave finish, remove the block
+            // to avoid retaining a detached node.
+            if (moveType === MoveType.LEAVE && parentComponent!.isUnmounted) {
+              block.remove()
+            } else {
+              parent.insertBefore(block, anchor as Node)
+            }
+          },
           parentSuspense,
           parentSuspense,
         )
         )
       } else {
       } else {

+ 1 - 1
packages/runtime-vapor/src/components/KeepAlive.ts

@@ -359,7 +359,7 @@ export function deactivate(
   instance: VaporComponentInstance,
   instance: VaporComponentInstance,
   container: ParentNode,
   container: ParentNode,
 ): void {
 ): void {
-  insert(instance.block, container, null, MoveType.LEAVE)
+  insert(instance.block, container, null, MoveType.LEAVE, instance)
 
 
   queuePostFlushCb(() => {
   queuePostFlushCb(() => {
     if (instance.da) invokeArrayFns(instance.da)
     if (instance.da) invokeArrayFns(instance.da)