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

345 lines
9.3 KiB
TypeScript
Raw Normal View History

2019-11-21 23:21:09 +08:00
import {
getCurrentInstance,
SetupContext,
ComponentOptions
} from '../component'
import {
cloneVNode,
Comment,
isSameVNodeType,
VNode,
VNodeChildren
} from '../vnode'
import { warn } from '../warning'
import { isKeepAlive } from './KeepAlive'
import { toRaw } from '@vue/reactivity'
import { callWithAsyncErrorHandling, ErrorCodes } from '../errorHandling'
import { ShapeFlags } from '../shapeFlags'
2019-11-25 07:37:59 +08:00
import { onBeforeUnmount, onMounted } from '../apiLifecycle'
2019-11-25 05:00:46 +08:00
export interface BaseTransitionProps {
mode?: 'in-out' | 'out-in' | 'default'
appear?: boolean
2019-11-23 12:32:53 +08:00
// If true, indicates this is a transition that doesn't actually insert/remove
// the element, but toggles the show / hidden status instead.
// The transition hooks are injected, but will be skipped by the renderer.
// Instead, a custom directive can control the transition by calling the
// injected hooks (e.g. v-show).
persisted?: boolean
2019-11-23 12:32:53 +08:00
// Hooks. Using camel case for easier usage in render functions & JSX.
2019-11-25 07:37:59 +08:00
// In templates these can be written as @before-enter="xxx" as prop names
// are camelized.
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 interface TransitionHooks {
persisted: boolean
beforeEnter(el: object): void
enter(el: object): void
leave(el: object, remove: () => void): void
afterLeave?(): void
delayLeave?(delayedLeave: () => void): void
delayedLeave?(): void
}
2019-11-23 06:10:17 +08:00
type TransitionHookCaller = (
hook: ((el: any) => void) | undefined,
args?: any[]
) => void
type PendingCallback = (cancelled?: boolean) => void
2019-11-25 07:37:59 +08:00
interface TransitionState {
isMounted: boolean
isLeaving: boolean
isUnmounting: boolean
// Track pending leave callbacks for children of the same key.
// This is used to force remove leaving a child when a new copy is entering.
leavingVNodes: Record<string, VNode>
}
interface TransitionElement {
// in persisted mode (e.g. v-show), the same element is toggled, so the
// pending enter/leave callbacks may need to cancalled if the state is toggled
// before it finishes.
_enterCb?: PendingCallback
_leaveCb?: PendingCallback
2019-11-23 06:10:17 +08:00
}
2019-11-25 05:00:46 +08:00
const BaseTransitionImpl = {
name: `BaseTransition`,
2019-11-25 05:00:46 +08:00
setup(props: BaseTransitionProps, { slots }: SetupContext) {
const instance = getCurrentInstance()!
2019-11-25 07:37:59 +08:00
const state: TransitionState = {
isMounted: false,
isLeaving: false,
isUnmounting: false,
leavingVNodes: Object.create(null)
2019-11-25 07:37:59 +08:00
}
onMounted(() => {
state.isMounted = true
})
onBeforeUnmount(() => {
state.isUnmounting = true
})
2019-11-23 06:10:17 +08:00
const callTransitionHook: TransitionHookCaller = (hook, args) => {
hook &&
callWithAsyncErrorHandling(
hook,
instance,
ErrorCodes.TRANSITION_HOOK,
args
)
}
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 or component. 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.
const child = children[0]
2019-11-25 07:37:59 +08:00
if (state.isLeaving) {
return emptyPlaceholder(child)
}
// in the case of <transition><keep-alive/></transition>, we need to
// compare the type of the kept-alive children.
const innerChild = getKeepAliveChild(child)
if (!innerChild) {
return emptyPlaceholder(child)
}
2019-11-25 07:37:59 +08:00
const enterHooks = (innerChild.transition = resolveTransitionHooks(
innerChild,
rawProps,
2019-11-25 07:37:59 +08:00
state,
callTransitionHook
))
const oldChild = instance.subTree
const oldInnerChild = oldChild && getKeepAliveChild(oldChild)
// handle mode
if (
oldInnerChild &&
oldInnerChild.type !== Comment &&
!isSameVNodeType(innerChild, oldInnerChild)
) {
const prevHooks = oldInnerChild.transition!
const leavingHooks = resolveTransitionHooks(
oldInnerChild,
rawProps,
state,
callTransitionHook
)
// update old tree's hooks in case of dynamic transition
setTransitionHooks(oldInnerChild, leavingHooks)
// switching between different views
if (mode === 'out-in') {
2019-11-25 07:37:59 +08:00
state.isLeaving = true
// return placeholder node and queue update when leave finishes
leavingHooks.afterLeave = () => {
2019-11-25 07:37:59 +08:00
state.isLeaving = false
instance.update()
}
return emptyPlaceholder(child)
} else if (mode === 'in-out') {
delete prevHooks.delayedLeave
leavingHooks.delayLeave = delayedLeave => {
enterHooks.delayedLeave = delayedLeave
}
}
}
return child
}
}
2019-11-21 23:21:09 +08:00
}
if (__DEV__) {
2019-11-25 05:00:46 +08:00
;(BaseTransitionImpl as ComponentOptions).props = {
mode: String,
appear: Boolean,
persisted: Boolean,
// enter
onBeforeEnter: Function,
onEnter: Function,
onAfterEnter: Function,
onEnterCancelled: Function,
// leave
onBeforeLeave: Function,
onLeave: Function,
onAfterLeave: Function,
onLeaveCancelled: Function
}
}
2019-11-21 23:21:09 +08:00
// export the public type for h/tsx inference
// also to avoid inline import() in generated d.ts files
2019-11-25 05:00:46 +08:00
export const BaseTransition = (BaseTransitionImpl as any) as {
2019-11-21 23:21:09 +08:00
new (): {
2019-11-25 05:00:46 +08:00
$props: BaseTransitionProps
2019-11-21 23:21:09 +08:00
}
}
2019-11-23 07:09:26 +08:00
// The transition hooks are attached to the vnode as vnode.transition
// and will be called at appropriate timing in the renderer.
2019-11-23 06:10:17 +08:00
function resolveTransitionHooks(
vnode: VNode,
{
appear,
persisted = false,
onBeforeEnter,
onEnter,
onAfterEnter,
onEnterCancelled,
onBeforeLeave,
onLeave,
onAfterLeave,
onLeaveCancelled
2019-11-25 05:00:46 +08:00
}: BaseTransitionProps,
2019-11-25 07:37:59 +08:00
state: TransitionState,
callHook: TransitionHookCaller
2019-11-23 06:10:17 +08:00
): TransitionHooks {
const { leavingVNodes } = state
const key = String(vnode.key)
const hooks: TransitionHooks = {
persisted,
beforeEnter(el: TransitionElement) {
2019-11-25 07:37:59 +08:00
if (!appear && !state.isMounted) {
return
2019-11-23 06:10:17 +08:00
}
// for same element (v-show)
if (el._leaveCb) {
el._leaveCb(true /* cancelled */)
}
// for toggled element with same key (v-if)
const leavingVNode = leavingVNodes[key]
if (
leavingVNode &&
isSameVNodeType(vnode, leavingVNode) &&
leavingVNode.el._leaveCb
) {
// force early removal (not cancelled)
leavingVNode.el._leaveCb()
}
2019-11-23 06:10:17 +08:00
callHook(onBeforeEnter, [el])
},
2019-11-23 06:10:17 +08:00
enter(el: TransitionElement) {
2019-11-25 07:37:59 +08:00
if (!appear && !state.isMounted) {
return
}
let called = false
const afterEnter = (el._enterCb = (cancelled?) => {
if (called) return
called = true
2019-11-23 06:10:17 +08:00
if (cancelled) {
callHook(onEnterCancelled, [el])
} else {
callHook(onAfterEnter, [el])
}
if (hooks.delayedLeave) {
hooks.delayedLeave()
}
el._enterCb = undefined
2019-11-23 06:10:17 +08:00
})
if (onEnter) {
2019-11-23 06:10:17 +08:00
onEnter(el, afterEnter)
} else {
2019-11-23 06:10:17 +08:00
afterEnter()
}
},
2019-11-23 06:10:17 +08:00
leave(el: TransitionElement, remove) {
const key = String(vnode.key)
if (el._enterCb) {
el._enterCb(true /* cancelled */)
2019-11-25 07:37:59 +08:00
}
if (state.isUnmounting) {
return remove()
2019-11-23 06:10:17 +08:00
}
callHook(onBeforeLeave, [el])
let called = false
const afterLeave = (el._leaveCb = (cancelled?) => {
if (called) return
called = true
2019-11-23 06:10:17 +08:00
remove()
if (cancelled) {
callHook(onLeaveCancelled, [el])
} else {
callHook(onAfterLeave, [el])
}
el._leaveCb = undefined
delete leavingVNodes[key]
2019-11-23 06:10:17 +08:00
})
leavingVNodes[key] = vnode
if (onLeave) {
2019-11-23 06:10:17 +08:00
onLeave(el, afterLeave)
} else {
afterLeave()
}
}
}
return hooks
}
// 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 emptyPlaceholder(vnode: VNode): VNode | undefined {
if (isKeepAlive(vnode)) {
vnode = cloneVNode(vnode)
vnode.children = null
return vnode
}
}
function getKeepAliveChild(vnode: VNode): VNode | undefined {
return isKeepAlive(vnode)
? vnode.children
? ((vnode.children as VNodeChildren)[0] as VNode)
: undefined
: vnode
}
export function setTransitionHooks(vnode: VNode, hooks: TransitionHooks) {
if (vnode.shapeFlag & ShapeFlags.COMPONENT && vnode.component) {
setTransitionHooks(vnode.component.subTree, hooks)
} else {
vnode.transition = hooks
}
}