Skip to content

Commit

Permalink
fix(transition): fix appear hooks handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 25, 2020
1 parent acd3156 commit 7ae70ea
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 64 deletions.
43 changes: 37 additions & 6 deletions packages/runtime-core/__tests__/components/BaseTransition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ function mockProps(extra: BaseTransitionProps = {}, withKeepAlive = false) {
}),
onAfterLeave: jest.fn(),
onLeaveCancelled: jest.fn(),
onBeforeAppear: jest.fn(),
onAppear: jest.fn((el, done) => {
cbs.doneEnter[serialize(el as TestElement)] = done
}),
onAfterAppear: jest.fn(),
onAppearCancelled: jest.fn(),
...extra
}
return {
Expand Down Expand Up @@ -132,8 +138,33 @@ function runTestWithKeepAlive(tester: TestFn) {
}

describe('BaseTransition', () => {
test('appear: true', () => {
const { props, cbs } = mockProps({ appear: true })
test('appear: true w/ appear hooks', () => {
const { props, cbs } = mockProps({
appear: true
})
mount(props, () => h('div'))
expect(props.onBeforeAppear).toHaveBeenCalledTimes(1)
expect(props.onAppear).toHaveBeenCalledTimes(1)
expect(props.onAfterAppear).not.toHaveBeenCalled()

// enter should not be called
expect(props.onBeforeEnter).not.toHaveBeenCalled()
expect(props.onEnter).not.toHaveBeenCalled()
expect(props.onAfterEnter).not.toHaveBeenCalled()

cbs.doneEnter[`<div></div>`]()
expect(props.onAfterAppear).toHaveBeenCalledTimes(1)
expect(props.onAfterEnter).not.toHaveBeenCalled()
})

test('appear: true w/ fallback to enter hooks', () => {
const { props, cbs } = mockProps({
appear: true,
onBeforeAppear: undefined,
onAppear: undefined,
onAfterAppear: undefined,
onAppearCancelled: undefined
})
mount(props, () => h('div'))
expect(props.onBeforeEnter).toHaveBeenCalledTimes(1)
expect(props.onEnter).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -207,11 +238,11 @@ describe('BaseTransition', () => {
const { hooks } = mockPersistedHooks()
mount(props, () => h('div', hooks))

expect(props.onBeforeEnter).toHaveBeenCalledTimes(1)
expect(props.onEnter).toHaveBeenCalledTimes(1)
expect(props.onAfterEnter).not.toHaveBeenCalled()
expect(props.onBeforeAppear).toHaveBeenCalledTimes(1)
expect(props.onAppear).toHaveBeenCalledTimes(1)
expect(props.onAfterAppear).not.toHaveBeenCalled()
cbs.doneEnter[`<div></div>`]()
expect(props.onAfterEnter).toHaveBeenCalledTimes(1)
expect(props.onAfterAppear).toHaveBeenCalledTimes(1)
})
})

Expand Down
58 changes: 44 additions & 14 deletions packages/runtime-core/src/components/BaseTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ export interface BaseTransitionProps<HostElement = RendererElement> {
onLeave?: (el: HostElement, done: () => void) => void
onAfterLeave?: (el: HostElement) => void
onLeaveCancelled?: (el: HostElement) => void // only fired in persisted mode
// appear
onBeforeAppear?: (el: HostElement) => void
onAppear?: (el: HostElement, done: () => void) => void
onAfterAppear?: (el: HostElement) => void
onAppearCancelled?: (el: HostElement) => void
}

export interface TransitionHooks<HostElement extends RendererElement = RendererElement> {
export interface TransitionHooks<
HostElement extends RendererElement = RendererElement
> {
persisted: boolean
beforeEnter(el: HostElement): void
enter(el: HostElement): void
Expand Down Expand Up @@ -115,7 +122,12 @@ const BaseTransitionImpl = {
onBeforeLeave: Function,
onLeave: Function,
onAfterLeave: Function,
onLeaveCancelled: Function
onLeaveCancelled: Function,
// appear
onBeforeAppear: Function,
onAppear: Function,
onAfterAppear: Function,
onAppearCancelled: Function
},

setup(props: BaseTransitionProps, { slots }: SetupContext) {
Expand Down Expand Up @@ -254,7 +266,11 @@ export function resolveTransitionHooks(
onBeforeLeave,
onLeave,
onAfterLeave,
onLeaveCancelled
onLeaveCancelled,
onBeforeAppear,
onAppear,
onAfterAppear,
onAppearCancelled
}: BaseTransitionProps<any>,
state: TransitionState,
instance: ComponentInternalInstance
Expand All @@ -275,8 +291,13 @@ export function resolveTransitionHooks(
const hooks: TransitionHooks<TransitionElement> = {
persisted,
beforeEnter(el) {
if (!appear && !state.isMounted) {
return
let hook = onBeforeEnter
if (!state.isMounted) {
if (appear) {
hook = onBeforeAppear || onBeforeEnter
} else {
return
}
}
// for same element (v-show)
if (el._leaveCb) {
Expand All @@ -292,31 +313,40 @@ export function resolveTransitionHooks(
// force early removal (not cancelled)
leavingVNode.el!._leaveCb()
}
callHook(onBeforeEnter, [el])
callHook(hook, [el])
},

enter(el) {
if (!appear && !state.isMounted) {
return
let hook = onEnter
let afterHook = onAfterEnter
let cancelHook = onEnterCancelled
if (!state.isMounted) {
if (appear) {
hook = onAppear || onEnter
afterHook = onAfterAppear || onAfterEnter
cancelHook = onAppearCancelled || onEnterCancelled
} else {
return
}
}
let called = false
const afterEnter = (el._enterCb = (cancelled?) => {
const done = (el._enterCb = (cancelled?) => {
if (called) return
called = true
if (cancelled) {
callHook(onEnterCancelled, [el])
callHook(cancelHook, [el])
} else {
callHook(onAfterEnter, [el])
callHook(afterHook, [el])
}
if (hooks.delayedLeave) {
hooks.delayedLeave()
}
el._enterCb = undefined
})
if (onEnter) {
onEnter(el, afterEnter)
if (hook) {
hook(el, done)
} else {
afterEnter()
done()
}
},

Expand Down
72 changes: 38 additions & 34 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,28 @@ export function resolveTransitionProps(
return baseProps
}

const originEnterClass = [enterFromClass, enterActiveClass, enterToClass]
const instance = getCurrentInstance()!
const durations = normalizeDuration(duration)
const enterDuration = durations && durations[0]
const leaveDuration = durations && durations[1]
const {
appear,
onBeforeEnter,
onEnter,
onLeave,
onEnterCancelled,
onLeaveCancelled
onLeave,
onLeaveCancelled,
onBeforeAppear,
onAppear,
onAppearCancelled
} = baseProps

// is appearing
if (appear && !instance.isMounted) {
enterFromClass = appearFromClass
enterActiveClass = appearActiveClass
enterToClass = appearToClass
}

type Hook =
| ((el: Element, done: () => void) => void)
| ((el: Element) => void)
type HookWithDone = (el: Element, done: () => void) => void
type Hook = HookWithDone | ((el: Element) => void)

const finishEnter = (el: Element, done?: () => void) => {
removeTransitionClass(el, enterToClass)
removeTransitionClass(el, enterActiveClass)
const finishEnter = (el: Element, isAppear: boolean, done?: () => void) => {
removeTransitionClass(el, isAppear ? appearToClass : enterToClass)
removeTransitionClass(el, isAppear ? appearActiveClass : enterActiveClass)
done && done()
// reset enter class
if (appear) {
;[enterFromClass, enterActiveClass, enterToClass] = originEnterClass
}
}

const finishLeave = (el: Element, done?: () => void) => {
Expand All @@ -147,27 +136,38 @@ export function resolveTransitionProps(
)
}

return extend(baseProps, {
onBeforeEnter(el) {
onBeforeEnter && onBeforeEnter(el)
addTransitionClass(el, enterActiveClass)
addTransitionClass(el, enterFromClass)
},
onEnter(el, done) {
const makeEnterHook = (isAppear: boolean): HookWithDone => {
const hook = isAppear ? onAppear : onEnter
return (el, done) => {
nextFrame(() => {
const resolve = () => finishEnter(el, done)
callHook(onEnter, [el, resolve])
removeTransitionClass(el, enterFromClass)
addTransitionClass(el, enterToClass)
if (!(onEnter && onEnter.length > 1)) {
const resolve = () => finishEnter(el, isAppear, done)
callHook(hook, [el, resolve])
removeTransitionClass(el, isAppear ? appearFromClass : enterFromClass)
addTransitionClass(el, isAppear ? appearToClass : enterToClass)
if (!(hook && hook.length > 1)) {
if (enterDuration) {
setTimeout(resolve, enterDuration)
} else {
whenTransitionEnds(el, type, resolve)
}
}
})
}
}

return extend(baseProps, {
onBeforeEnter(el) {
onBeforeEnter && onBeforeEnter(el)
addTransitionClass(el, enterActiveClass)
addTransitionClass(el, enterFromClass)
},
onBeforeAppear(el) {
onBeforeAppear && onBeforeAppear(el)
addTransitionClass(el, appearActiveClass)
addTransitionClass(el, appearFromClass)
},
onEnter: makeEnterHook(false),
onAppear: makeEnterHook(true),
onLeave(el, done) {
addTransitionClass(el, leaveActiveClass)
addTransitionClass(el, leaveFromClass)
Expand All @@ -186,9 +186,13 @@ export function resolveTransitionProps(
})
},
onEnterCancelled(el) {
finishEnter(el)
finishEnter(el, false)
onEnterCancelled && onEnterCancelled(el)
},
onAppearCancelled(el) {
finishEnter(el, true)
onAppearCancelled && onAppearCancelled(el)
},
onLeaveCancelled(el) {
finishLeave(el)
onLeaveCancelled && onLeaveCancelled(el)
Expand Down
27 changes: 17 additions & 10 deletions packages/vue/__tests__/Transition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ describe('e2e: Transition', () => {
test(
'transition on appear',
async () => {
await page().evaluate(async () => {
const appearClass = await page().evaluate(async () => {
const { createApp, ref } = (window as any).Vue
createApp({
template: `
Expand All @@ -468,9 +468,12 @@ describe('e2e: Transition', () => {
return { toggle, click }
}
}).mount('#app')
return Promise.resolve().then(() => {
return document.querySelector('.test')!.className.split(/\s+/g)
})
})
// appear
expect(await classList('.test')).toStrictEqual([
expect(appearClass).toStrictEqual([
'test',
'test-appear-active',
'test-appear-from'
Expand Down Expand Up @@ -598,13 +601,13 @@ describe('e2e: Transition', () => {
return document.querySelector('.test')!.className.split(/\s+/g)
})
})
// appear fixme spy called
// appear
expect(appearClass).toStrictEqual([
'test',
'test-appear-active',
'test-appear-from'
])
expect(beforeAppearSpy).not.toBeCalled()
expect(beforeAppearSpy).toBeCalled()
expect(onAppearSpy).not.toBeCalled()
expect(afterAppearSpy).not.toBeCalled()
await nextFrame()
Expand All @@ -613,11 +616,15 @@ describe('e2e: Transition', () => {
'test-appear-active',
'test-appear-to'
])
expect(onAppearSpy).not.toBeCalled()
expect(onAppearSpy).toBeCalled()
expect(afterAppearSpy).not.toBeCalled()
await transitionFinish()
expect(await html('#container')).toBe('<div class="test">content</div>')
expect(afterAppearSpy).not.toBeCalled()
expect(afterAppearSpy).toBeCalled()

expect(beforeEnterSpy).not.toBeCalled()
expect(onEnterSpy).not.toBeCalled()
expect(afterEnterSpy).not.toBeCalled()

// leave
expect(await classWhenTransitionStart()).toStrictEqual([
Expand All @@ -640,23 +647,23 @@ describe('e2e: Transition', () => {
expect(await html('#container')).toBe('<!--v-if-->')
expect(afterLeaveSpy).toBeCalled()

// enter fixme spy called
// enter
expect(await classWhenTransitionStart()).toStrictEqual([
'test',
'test-enter-active',
'test-enter-from'
])
expect(beforeEnterSpy).toBeCalled()
expect(onEnterSpy).toBeCalled()
expect(afterEnterSpy).toBeCalled()
expect(onEnterSpy).not.toBeCalled()
expect(afterEnterSpy).not.toBeCalled()
await nextFrame()
expect(await classList('.test')).toStrictEqual([
'test',
'test-enter-active',
'test-enter-to'
])
expect(onEnterSpy).toBeCalled()
expect(afterEnterSpy).toBeCalled()
expect(afterEnterSpy).not.toBeCalled()
await transitionFinish()
expect(await html('#container')).toBe('<div class="test">content</div>')
expect(afterEnterSpy).toBeCalled()
Expand Down

0 comments on commit 7ae70ea

Please sign in to comment.