Skip to content

Commit 7cee024

Browse files
committed
wip: handle mode
1 parent 41822e3 commit 7cee024

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

packages/runtime-core/src/components/BaseTransition.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,11 @@ export function resolveTransitionHooks(
392392
if (
393393
leavingVNode &&
394394
isSameVNodeType(vnode, leavingVNode) &&
395-
(leavingVNode.el as TransitionElement)[leaveCbKey]
395+
// TODO refactor
396+
((leavingVNode.el || leavingVNode) as TransitionElement)[leaveCbKey]
396397
) {
397398
// force early removal (not cancelled)
398-
;(leavingVNode.el as TransitionElement)[leaveCbKey]!()
399+
;((leavingVNode.el || leavingVNode) as TransitionElement)[leaveCbKey]!()
399400
}
400401
callHook(hook, [el])
401402
},

packages/runtime-vapor/src/block.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2433
export 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
}

packages/runtime-vapor/src/components/Transition.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import type { Block } from '../block'
1111
import { isVaporComponent } from '../component'
1212

1313
export const vaporTransitionImpl: VaporTransitionInterface = {
14-
applyTransition: (props: TransitionProps, slots: { default: () => any }) => {
14+
applyTransition: (
15+
props: TransitionProps,
16+
slots: { default: () => Block },
17+
) => {
1518
const children = slots.default && slots.default()
1619
if (!children) {
1720
return
@@ -30,7 +33,32 @@ export const vaporTransitionImpl: VaporTransitionInterface = {
3033
)
3134
setTransitionHooks(child, enterHooks)
3235

33-
// TODO handle mode
36+
const { mode } = props
37+
// TODO check mode
38+
39+
child.applyLeavingHooks = (block: Block, afterLeaveCb: () => void) => {
40+
let leavingHooks = resolveTransitionHooks(
41+
block as any,
42+
props,
43+
state,
44+
currentInstance!,
45+
)
46+
setTransitionHooks(block, leavingHooks)
47+
48+
if (mode === 'out-in') {
49+
state.isLeaving = true
50+
leavingHooks.afterLeave = () => {
51+
state.isLeaving = false
52+
afterLeaveCb()
53+
delete leavingHooks.afterLeave
54+
}
55+
} else if (mode === 'in-out') {
56+
leavingHooks.delayLeave = (block: Block, earlyRemove, delayedLeave) => {
57+
// TODO delay leave
58+
}
59+
}
60+
return leavingHooks
61+
}
3462

3563
return children
3664
},

0 commit comments

Comments
 (0)