Skip to content

Commit

Permalink
fix(transition): ensure manual style manipulation in transition leave…
Browse files Browse the repository at this point in the history
… hooks work

ref #2720
  • Loading branch information
yyx990803 committed Dec 3, 2020
1 parent 3016b81 commit cbaa380
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 146 deletions.
25 changes: 10 additions & 15 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,23 @@ export function resolveTransitionProps(
return extend(baseProps, {
onBeforeEnter(el) {
onBeforeEnter && onBeforeEnter(el)
addTransitionClass(el, enterActiveClass)
addTransitionClass(el, enterFromClass)
addTransitionClass(el, enterActiveClass)
},
onBeforeAppear(el) {
onBeforeAppear && onBeforeAppear(el)
addTransitionClass(el, appearActiveClass)
addTransitionClass(el, appearFromClass)
addTransitionClass(el, appearActiveClass)
},
onEnter: makeEnterHook(false),
onAppear: makeEnterHook(true),
onLeave(el, done) {
const resolve = () => finishLeave(el, done)
addTransitionClass(el, leaveActiveClass)
addTransitionClass(el, leaveFromClass)

const cachedTransition = (el as HTMLElement).style.transitionProperty
requestAnimationFrame(() => {
// ref #2531, #2593
// disabling the transition before nextFrame ensures styles from
// *-leave-from classes are applied instantly before the transition starts.
// ref #2712
// do this in an rAF to ensure styles from *-leave-active can trigger
// transition on the first frame when el has `transition` property itself.
;(el as HTMLElement).style.transitionProperty = 'none'
})
// force reflow so *-leave-from classes immediately take effect (#2593)
forceReflow()
addTransitionClass(el, leaveActiveClass)
nextFrame(() => {
;(el as HTMLElement).style.transitionProperty = cachedTransition
removeTransitionClass(el, leaveFromClass)
addTransitionClass(el, leaveToClass)
if (!(onLeave && onLeave.length > 1)) {
Expand Down Expand Up @@ -370,3 +360,8 @@ function getTimeout(delays: string[], durations: string[]): number {
function toMs(s: string): number {
return Number(s.slice(0, -1).replace(',', '.')) * 1000
}

// synchronously force layout to put elements into a certain state
export function forceReflow() {
return document.body.offsetHeight
}
8 changes: 2 additions & 6 deletions packages/runtime-dom/src/components/TransitionGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ElementWithTransition,
getTransitionInfo,
resolveTransitionProps,
TransitionPropsValidators
TransitionPropsValidators,
forceReflow
} from './Transition'
import {
Fragment,
Expand Down Expand Up @@ -172,11 +173,6 @@ function applyTranslation(c: VNode): VNode | undefined {
}
}

// this is put in a dedicated function to avoid the line from being treeshaken
function forceReflow() {
return document.body.offsetHeight
}

function hasCSSTransform(
el: ElementWithTransition,
root: Node,
Expand Down
Loading

0 comments on commit cbaa380

Please sign in to comment.