@@ -7,13 +7,19 @@ import {
77} from './component'
88import { createComment , createTextNode } from './dom/node'
99import { EffectScope , pauseTracking , resetTracking } from '@vue/reactivity'
10+ import {
11+ type TransitionHooks ,
12+ applyTransitionEnter ,
13+ applyTransitionLeave ,
14+ } from '@vue/runtime-dom'
1015
11- export type Block =
16+ export type Block = (
1217 | Node
1318 | VaporFragment
1419 | DynamicFragment
1520 | VaporComponentInstance
1621 | Block [ ]
22+ ) & { transition ?: TransitionHooks }
1723
1824export type BlockFn = ( ...args : any [ ] ) => Block
1925
@@ -22,6 +28,7 @@ export class VaporFragment {
2228 anchor ?: Node
2329 insert ?: ( parent : ParentNode , anchor : Node | null ) => void
2430 remove ?: ( parent ?: ParentNode ) => void
31+ transition ?: TransitionHooks
2532
2633 constructor ( nodes : Block ) {
2734 this . nodes = nodes
@@ -52,13 +59,13 @@ export class DynamicFragment extends VaporFragment {
5259 // teardown previous branch
5360 if ( this . scope ) {
5461 this . scope . stop ( )
55- parent && remove ( this . nodes , parent )
62+ parent && remove ( this . nodes , parent , this . transition )
5663 }
5764
5865 if ( render ) {
5966 this . scope = new EffectScope ( )
6067 this . nodes = this . scope . run ( render ) || [ ]
61- if ( parent ) insert ( this . nodes , parent , this . anchor )
68+ if ( parent ) insert ( this . nodes , parent , this . anchor , this . transition )
6269 } else {
6370 this . scope = undefined
6471 this . nodes = [ ]
@@ -69,7 +76,7 @@ export class DynamicFragment extends VaporFragment {
6976 this . nodes =
7077 ( this . scope || ( this . scope = new EffectScope ( ) ) ) . run ( this . fallback ) ||
7178 [ ]
72- parent && insert ( this . nodes , parent , this . anchor )
79+ parent && insert ( this . nodes , parent , this . anchor , this . transition )
7380 }
7481
7582 resetTracking ( )
@@ -106,12 +113,23 @@ export function insert(
106113 block : Block ,
107114 parent : ParentNode ,
108115 anchor : Node | null | 0 = null , // 0 means prepend
116+ transition : TransitionHooks | undefined = block . transition ,
117+ parentSuspense ?: any , // TODO Suspense
109118) : void {
110119 anchor = anchor === 0 ? parent . firstChild : anchor
111120 if ( block instanceof Node ) {
112- parent . insertBefore ( block , anchor )
121+ if ( transition ) {
122+ applyTransitionEnter (
123+ block ,
124+ transition ,
125+ ( ) => parent . insertBefore ( block , anchor ) ,
126+ parentSuspense ,
127+ )
128+ } else {
129+ parent . insertBefore ( block , anchor )
130+ }
113131 } else if ( isVaporComponent ( block ) ) {
114- mountComponent ( block , parent , anchor )
132+ mountComponent ( block , parent , anchor , transition )
115133 } else if ( isArray ( block ) ) {
116134 for ( let i = 0 ; i < block . length ; i ++ ) {
117135 insert ( block [ i ] , parent , anchor )
@@ -121,7 +139,7 @@ export function insert(
121139 if ( block . insert ) {
122140 block . insert ( parent , anchor )
123141 } else {
124- insert ( block . nodes , parent , anchor )
142+ insert ( block . nodes , parent , anchor , block . transition )
125143 }
126144 if ( block . anchor ) insert ( block . anchor , parent , anchor )
127145 }
@@ -132,11 +150,23 @@ export function prepend(parent: ParentNode, ...blocks: Block[]): void {
132150 while ( i -- ) insert ( blocks [ i ] , parent , 0 )
133151}
134152
135- export function remove ( block : Block , parent ?: ParentNode ) : void {
153+ export function remove (
154+ block : Block ,
155+ parent ?: ParentNode ,
156+ transition : TransitionHooks | undefined = block . transition ,
157+ ) : void {
136158 if ( block instanceof Node ) {
137- parent && parent . removeChild ( block )
159+ if ( transition ) {
160+ applyTransitionLeave (
161+ block ,
162+ transition ,
163+ ( ) => parent && parent . removeChild ( block ) ,
164+ )
165+ } else {
166+ parent && parent . removeChild ( block )
167+ }
138168 } else if ( isVaporComponent ( block ) ) {
139- unmountComponent ( block , parent )
169+ unmountComponent ( block , parent , transition )
140170 } else if ( isArray ( block ) ) {
141171 for ( let i = 0 ; i < block . length ; i ++ ) {
142172 remove ( block [ i ] , parent )
@@ -146,7 +176,7 @@ export function remove(block: Block, parent?: ParentNode): void {
146176 if ( block . remove ) {
147177 block . remove ( parent )
148178 } else {
149- remove ( block . nodes , parent )
179+ remove ( block . nodes , parent , block . transition )
150180 }
151181 if ( block . anchor ) remove ( block . anchor , parent )
152182 if ( ( block as DynamicFragment ) . scope ) {
0 commit comments