2019-11-21 07:04:44 +08:00
|
|
|
import { createComponent } from '../apiCreateComponent'
|
2019-11-21 10:56:17 +08:00
|
|
|
import { getCurrentInstance, ComponentInternalInstance } from '../component'
|
|
|
|
import { cloneVNode, Comment, isSameVNodeType, VNode } from '../vnode'
|
2019-11-21 07:04:44 +08:00
|
|
|
import { warn } from '../warning'
|
|
|
|
import { isKeepAlive } from './KeepAlive'
|
|
|
|
import { toRaw } from '@vue/reactivity'
|
|
|
|
import { onMounted } from '../apiLifecycle'
|
2019-11-21 10:56:17 +08:00
|
|
|
import { callWithAsyncErrorHandling, ErrorCodes } from '../errorHandling'
|
|
|
|
import { ShapeFlags } from '../shapeFlags'
|
2019-11-21 07:04:44 +08:00
|
|
|
|
|
|
|
// Using camel case here makes it easier to use in render functions & JSX.
|
|
|
|
// In templates these will be written as @before-enter="xxx"
|
|
|
|
// The compiler has special handling to convert them into the proper cases.
|
|
|
|
export interface TransitionProps {
|
|
|
|
mode?: 'in-out' | 'out-in' | 'default'
|
|
|
|
appear?: boolean
|
|
|
|
// enter
|
|
|
|
onBeforeEnter?: (el: any) => void
|
|
|
|
onEnter?: (el: any, done: () => void) => void
|
|
|
|
onAfterEnter?: (el: any) => void
|
|
|
|
onEnterCancelled?: (el: any) => void
|
|
|
|
// leave
|
|
|
|
onBeforeLeave?: (el: any) => void
|
|
|
|
onLeave?: (el: any, done: () => void) => void
|
|
|
|
onAfterLeave?: (el: any) => void
|
|
|
|
onLeaveCancelled?: (el: any) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Transition = createComponent({
|
|
|
|
name: `Transition`,
|
|
|
|
setup(props: TransitionProps, { slots }) {
|
|
|
|
const instance = getCurrentInstance()!
|
|
|
|
let isLeaving = false
|
|
|
|
let isMounted = false
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
isMounted = true
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
const children = slots.default && slots.default()
|
|
|
|
if (!children || !children.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// warn multiple elements
|
|
|
|
if (__DEV__ && children.length > 1) {
|
|
|
|
warn(
|
|
|
|
'<transition> can only be used on a single element. Use ' +
|
|
|
|
'<transition-group> for lists.'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// there's no need to track reactivity for these props so use the raw
|
|
|
|
// props for a bit better perf
|
|
|
|
const rawProps = toRaw(props)
|
|
|
|
const { mode } = rawProps
|
|
|
|
// check mode
|
|
|
|
if (__DEV__ && mode && !['in-out', 'out-in', 'default'].includes(mode)) {
|
|
|
|
warn(`invalid <transition> mode: ${mode}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point children has a guaranteed length of 1.
|
2019-11-21 10:56:17 +08:00
|
|
|
const child = children[0]
|
2019-11-21 07:04:44 +08:00
|
|
|
if (isLeaving) {
|
2019-11-21 10:56:17 +08:00
|
|
|
return placeholder(child)
|
2019-11-21 07:04:44 +08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:56:17 +08:00
|
|
|
let delayedLeave: (() => void) | undefined
|
|
|
|
const performDelayedLeave = () => delayedLeave && delayedLeave()
|
|
|
|
const transitionData = (child.transition = resolveTransitionData(
|
|
|
|
instance,
|
|
|
|
rawProps,
|
|
|
|
isMounted,
|
|
|
|
performDelayedLeave
|
|
|
|
))
|
|
|
|
|
2019-11-21 07:04:44 +08:00
|
|
|
// clone old subTree because we need to modify it
|
|
|
|
const oldChild = instance.subTree
|
|
|
|
? (instance.subTree = cloneVNode(instance.subTree))
|
|
|
|
: null
|
|
|
|
|
|
|
|
// handle mode
|
|
|
|
if (
|
|
|
|
oldChild &&
|
2019-11-21 10:56:17 +08:00
|
|
|
!isSameVNodeType(child, oldChild) &&
|
2019-11-21 07:04:44 +08:00
|
|
|
oldChild.type !== Comment
|
|
|
|
) {
|
|
|
|
// update old tree's hooks in case of dynamic transition
|
2019-11-21 10:56:17 +08:00
|
|
|
// need to do this recursively in case of HOCs
|
|
|
|
updateHOCTransitionData(oldChild, transitionData)
|
2019-11-21 07:04:44 +08:00
|
|
|
// switching between different views
|
|
|
|
if (mode === 'out-in') {
|
|
|
|
isLeaving = true
|
|
|
|
// return placeholder node and queue update when leave finishes
|
2019-11-21 10:56:17 +08:00
|
|
|
transitionData.afterLeave = () => {
|
|
|
|
isLeaving = false
|
|
|
|
instance.update()
|
|
|
|
}
|
|
|
|
return placeholder(child)
|
2019-11-21 07:04:44 +08:00
|
|
|
} else if (mode === 'in-out') {
|
2019-11-21 10:56:17 +08:00
|
|
|
transitionData.delayLeave = performLeave => {
|
|
|
|
delayedLeave = performLeave
|
|
|
|
}
|
2019-11-21 07:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:56:17 +08:00
|
|
|
return child
|
2019-11-21 07:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
;(Transition as any).props = {
|
|
|
|
mode: String,
|
|
|
|
appear: Boolean,
|
|
|
|
// enter
|
|
|
|
onBeforeEnter: Function,
|
|
|
|
onEnter: Function,
|
|
|
|
onAfterEnter: Function,
|
|
|
|
onEnterCancelled: Function,
|
|
|
|
// leave
|
|
|
|
onBeforeLeave: Function,
|
|
|
|
onLeave: Function,
|
|
|
|
onAfterLeave: Function,
|
|
|
|
onLeaveCancelled: Function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:56:17 +08:00
|
|
|
export interface TransitionData {
|
|
|
|
beforeEnter(el: object): void
|
|
|
|
enter(el: object): void
|
|
|
|
leave(el: object, remove: () => void): void
|
|
|
|
afterLeave?(): void
|
|
|
|
delayLeave?(performLeave: () => void): void
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveTransitionData(
|
|
|
|
instance: ComponentInternalInstance,
|
2019-11-21 07:04:44 +08:00
|
|
|
{
|
|
|
|
appear,
|
|
|
|
onBeforeEnter,
|
|
|
|
onEnter,
|
|
|
|
onAfterEnter,
|
|
|
|
onEnterCancelled,
|
|
|
|
onBeforeLeave,
|
|
|
|
onLeave,
|
|
|
|
onAfterLeave,
|
|
|
|
onLeaveCancelled
|
|
|
|
}: TransitionProps,
|
|
|
|
isMounted: boolean,
|
2019-11-21 10:56:17 +08:00
|
|
|
performDelayedLeave: () => void
|
|
|
|
): TransitionData {
|
2019-11-21 07:04:44 +08:00
|
|
|
// TODO handle cancel hooks
|
|
|
|
return {
|
2019-11-21 10:56:17 +08:00
|
|
|
beforeEnter(el) {
|
2019-11-21 07:04:44 +08:00
|
|
|
if (!isMounted && !appear) {
|
|
|
|
return
|
|
|
|
}
|
2019-11-21 10:56:17 +08:00
|
|
|
onBeforeEnter &&
|
|
|
|
callWithAsyncErrorHandling(
|
|
|
|
onBeforeEnter,
|
|
|
|
instance,
|
|
|
|
ErrorCodes.TRANSITION_HOOK,
|
|
|
|
[el]
|
|
|
|
)
|
2019-11-21 07:04:44 +08:00
|
|
|
},
|
2019-11-21 10:56:17 +08:00
|
|
|
enter(el) {
|
2019-11-21 07:04:44 +08:00
|
|
|
if (!isMounted && !appear) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const done = () => {
|
2019-11-21 10:56:17 +08:00
|
|
|
onAfterEnter &&
|
|
|
|
callWithAsyncErrorHandling(
|
|
|
|
onAfterEnter,
|
|
|
|
instance,
|
|
|
|
ErrorCodes.TRANSITION_HOOK,
|
|
|
|
[el]
|
|
|
|
)
|
|
|
|
performDelayedLeave()
|
2019-11-21 07:04:44 +08:00
|
|
|
}
|
|
|
|
if (onEnter) {
|
|
|
|
onEnter(el, done)
|
|
|
|
} else {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
},
|
2019-11-21 10:56:17 +08:00
|
|
|
leave(el, remove) {
|
|
|
|
onBeforeLeave &&
|
|
|
|
callWithAsyncErrorHandling(
|
|
|
|
onBeforeLeave,
|
|
|
|
instance,
|
|
|
|
ErrorCodes.TRANSITION_HOOK,
|
|
|
|
[el]
|
|
|
|
)
|
|
|
|
const afterLeave = () =>
|
|
|
|
onAfterLeave &&
|
|
|
|
callWithAsyncErrorHandling(
|
|
|
|
onAfterLeave,
|
|
|
|
instance,
|
|
|
|
ErrorCodes.TRANSITION_HOOK,
|
|
|
|
[el]
|
|
|
|
)
|
2019-11-21 07:04:44 +08:00
|
|
|
if (onLeave) {
|
|
|
|
onLeave(el, () => {
|
|
|
|
remove()
|
2019-11-21 10:56:17 +08:00
|
|
|
afterLeave()
|
2019-11-21 07:04:44 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
remove()
|
2019-11-21 10:56:17 +08:00
|
|
|
afterLeave()
|
2019-11-21 07:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the placeholder really only handles one special case: KeepAlive
|
|
|
|
// in the case of a KeepAlive in a leave phase we need to return a KeepAlive
|
|
|
|
// placeholder with empty content to avoid the KeepAlive instance from being
|
|
|
|
// unmounted.
|
|
|
|
function placeholder(vnode: VNode): VNode | undefined {
|
|
|
|
if (isKeepAlive(vnode)) {
|
|
|
|
vnode = cloneVNode(vnode)
|
|
|
|
vnode.children = null
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 10:56:17 +08:00
|
|
|
|
|
|
|
function updateHOCTransitionData(vnode: VNode, data: TransitionData) {
|
|
|
|
if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
|
|
|
|
updateHOCTransitionData(vnode.component!.subTree, data)
|
|
|
|
} else {
|
|
|
|
vnode.transition = data
|
|
|
|
}
|
|
|
|
}
|