@@ -16,10 +16,12 @@ import { warn } from '../warning'
16
16
import { isKeepAlive } from './KeepAlive'
17
17
import { toRaw } from '@vue/reactivity'
18
18
import { callWithAsyncErrorHandling , ErrorCodes } from '../errorHandling'
19
- import { ShapeFlags , PatchFlags } from '@vue/shared'
19
+ import { ShapeFlags , PatchFlags , isArray } from '@vue/shared'
20
20
import { onBeforeUnmount , onMounted } from '../apiLifecycle'
21
21
import { RendererElement } from '../renderer'
22
22
23
+ type Hook < T = ( ) => void > = T | T [ ]
24
+
23
25
export interface BaseTransitionProps < HostElement = RendererElement > {
24
26
mode ?: 'in-out' | 'out-in' | 'default'
25
27
appear ?: boolean
@@ -34,20 +36,20 @@ export interface BaseTransitionProps<HostElement = RendererElement> {
34
36
// Hooks. Using camel case for easier usage in render functions & JSX.
35
37
// In templates these can be written as @before -enter="xxx" as prop names
36
38
// are camelized.
37
- onBeforeEnter ?: ( el : HostElement ) => void
38
- onEnter ?: ( el : HostElement , done : ( ) => void ) => void
39
- onAfterEnter ?: ( el : HostElement ) => void
40
- onEnterCancelled ?: ( el : HostElement ) => void
39
+ onBeforeEnter ?: Hook < ( el : HostElement ) => void >
40
+ onEnter ?: Hook < ( el : HostElement , done : ( ) => void ) => void >
41
+ onAfterEnter ?: Hook < ( el : HostElement ) => void >
42
+ onEnterCancelled ?: Hook < ( el : HostElement ) => void >
41
43
// leave
42
- onBeforeLeave ?: ( el : HostElement ) => void
43
- onLeave ?: ( el : HostElement , done : ( ) => void ) => void
44
- onAfterLeave ?: ( el : HostElement ) => void
45
- onLeaveCancelled ?: ( el : HostElement ) => void // only fired in persisted mode
44
+ onBeforeLeave ?: Hook < ( el : HostElement ) => void >
45
+ onLeave ?: Hook < ( el : HostElement , done : ( ) => void ) => void >
46
+ onAfterLeave ?: Hook < ( el : HostElement ) => void >
47
+ onLeaveCancelled ?: Hook < ( el : HostElement ) => void > // only fired in persisted mode
46
48
// appear
47
- onBeforeAppear ?: ( el : HostElement ) => void
48
- onAppear ?: ( el : HostElement , done : ( ) => void ) => void
49
- onAfterAppear ?: ( el : HostElement ) => void
50
- onAppearCancelled ?: ( el : HostElement ) => void
49
+ onBeforeAppear ?: Hook < ( el : HostElement ) => void >
50
+ onAppear ?: Hook < ( el : HostElement , done : ( ) => void ) => void >
51
+ onAfterAppear ?: Hook < ( el : HostElement ) => void >
52
+ onAppearCancelled ?: Hook < ( el : HostElement ) => void >
51
53
}
52
54
53
55
export interface TransitionHooks <
@@ -69,9 +71,9 @@ export interface TransitionHooks<
69
71
delayedLeave ?( ) : void
70
72
}
71
73
72
- export type TransitionHookCaller = (
73
- hook : ( ( el : any ) => void ) | Array < ( el : any ) => void > | undefined ,
74
- args ?: any [ ]
74
+ export type TransitionHookCaller = < T extends any [ ] = [ el : any ] > (
75
+ hook : Hook < ( ... args : T ) => void > | undefined ,
76
+ args ?: T
75
77
) => void
76
78
77
79
export type PendingCallback = ( cancelled ?: boolean ) => void
@@ -331,6 +333,19 @@ export function resolveTransitionHooks(
331
333
)
332
334
}
333
335
336
+ const callAsyncHook = (
337
+ hook : Hook < ( el : any , done : ( ) => void ) => void > ,
338
+ args : [ TransitionElement , ( ) => void ]
339
+ ) => {
340
+ const done = args [ 1 ]
341
+ callHook ( hook , args )
342
+ if ( isArray ( hook ) ) {
343
+ if ( hook . every ( hook => hook . length <= 1 ) ) done ( )
344
+ } else if ( hook . length <= 1 ) {
345
+ done ( )
346
+ }
347
+ }
348
+
334
349
const hooks : TransitionHooks < TransitionElement > = {
335
350
mode,
336
351
persisted,
@@ -388,10 +403,7 @@ export function resolveTransitionHooks(
388
403
el . _enterCb = undefined
389
404
} )
390
405
if ( hook ) {
391
- hook ( el , done )
392
- if ( hook . length <= 1 ) {
393
- done ( )
394
- }
406
+ callAsyncHook ( hook , [ el , done ] )
395
407
} else {
396
408
done ( )
397
409
}
@@ -423,10 +435,7 @@ export function resolveTransitionHooks(
423
435
} )
424
436
leavingVNodesCache [ key ] = vnode
425
437
if ( onLeave ) {
426
- onLeave ( el , done )
427
- if ( onLeave . length <= 1 ) {
428
- done ( )
429
- }
438
+ callAsyncHook ( onLeave , [ el , done ] )
430
439
} else {
431
440
done ( )
432
441
}
0 commit comments