@@ -19,7 +19,16 @@ export type Block = (
1919 | DynamicFragment
2020 | VaporComponentInstance
2121 | Block [ ]
22- ) & { transition ?: TransitionHooks }
22+ ) &
23+ TransitionBlock
24+
25+ export type TransitionBlock = {
26+ transition ?: TransitionHooks
27+ applyLeavingHooks ?: (
28+ block : Block ,
29+ afterLeaveCb : ( ) => void ,
30+ ) => TransitionHooks
31+ }
2332
2433export type BlockFn = ( ...args : any [ ] ) => Block
2534
@@ -29,6 +38,10 @@ export class VaporFragment {
2938 insert ?: ( parent : ParentNode , anchor : Node | null ) => void
3039 remove ?: ( parent ?: ParentNode ) => void
3140 transition ?: TransitionHooks
41+ applyLeavingHooks ?: (
42+ block : Block ,
43+ afterLeaveCb : ( ) => void ,
44+ ) => TransitionHooks
3245
3346 constructor ( nodes : Block ) {
3447 this . nodes = nodes
@@ -56,29 +69,39 @@ export class DynamicFragment extends VaporFragment {
5669 pauseTracking ( )
5770 const parent = this . anchor . parentNode
5871
72+ const renderNewBranch = ( ) => {
73+ if ( render ) {
74+ this . scope = new EffectScope ( )
75+ this . nodes = this . scope . run ( render ) || [ ]
76+ if ( parent ) insert ( this . nodes , parent , this . anchor , this . transition )
77+ } else {
78+ this . scope = undefined
79+ this . nodes = [ ]
80+ }
81+
82+ if ( this . fallback && ! isValidBlock ( this . nodes ) ) {
83+ parent && remove ( this . nodes , parent , this . transition )
84+ this . nodes =
85+ ( this . scope || ( this . scope = new EffectScope ( ) ) ) . run ( this . fallback ) ||
86+ [ ]
87+ parent && insert ( this . nodes , parent , this . anchor , this . transition )
88+ }
89+ }
90+
5991 // teardown previous branch
6092 if ( this . scope ) {
6193 this . scope . stop ( )
62- parent && remove ( this . nodes , parent , this . transition )
63- }
64-
65- if ( render ) {
66- this . scope = new EffectScope ( )
67- this . nodes = this . scope . run ( render ) || [ ]
68- if ( parent ) insert ( this . nodes , parent , this . anchor , this . transition )
69- } else {
70- this . scope = undefined
71- this . nodes = [ ]
72- }
73-
74- if ( this . fallback && ! isValidBlock ( this . nodes ) ) {
75- parent && remove ( this . nodes , parent )
76- this . nodes =
77- ( this . scope || ( this . scope = new EffectScope ( ) ) ) . run ( this . fallback ) ||
78- [ ]
79- parent && insert ( this . nodes , parent , this . anchor , this . transition )
94+ if ( this . transition && this . transition . mode ) {
95+ const transition = this . applyLeavingHooks ! ( this . nodes , renderNewBranch )
96+ parent && remove ( this . nodes , parent , transition )
97+ resetTracking ( )
98+ return
99+ } else {
100+ parent && remove ( this . nodes , parent , this . transition )
101+ }
80102 }
81103
104+ renderNewBranch ( )
82105 resetTracking ( )
83106 }
84107}
0 commit comments