-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Transition hooks #1175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Transition hooks #1175
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ae8925
refactor(Transition): refactor transition hooks
underfin 0fee39d
refactor(Transition): update type
underfin 1e1a37f
refactor(Transition): add appear hooks into `BaseTransition` props
underfin ec6dafa
fix(Transition): fix test
underfin 7eec9b9
fix(Transition): fix hooks
underfin 6ae5ba0
fix(Transition): fix hooks
underfin 03d9cc4
fix(Transition): format code
underfin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,10 @@ import { | |
warn, | ||
FunctionalComponent, | ||
getCurrentInstance, | ||
callWithAsyncErrorHandling | ||
callWithAsyncErrorHandling, | ||
RendererElement, | ||
TransitionActiveHook, | ||
TransitionOtherHook | ||
} from '@vue/runtime-core' | ||
import { isObject } from '@vue/shared' | ||
import { ErrorCodes } from 'packages/runtime-core/src/errorHandling' | ||
|
@@ -30,6 +33,11 @@ export interface TransitionProps extends BaseTransitionProps<Element> { | |
leaveToClass?: string | ||
} | ||
|
||
export type TransitionHookCaller<HostElement = RendererElement> = ( | ||
hook?: TransitionOtherHook<HostElement> | TransitionActiveHook<HostElement>, | ||
args?: any[] | ||
) => void | ||
|
||
// DOM Transition is a higher-order-component based on the platform-agnostic | ||
// base Transition component, with DOM-specific logic. | ||
export const Transition: FunctionalComponent<TransitionProps> = ( | ||
|
@@ -71,24 +79,50 @@ export function resolveTransitionProps({ | |
leaveFromClass = `${name}-leave-from`, | ||
leaveActiveClass = `${name}-leave-active`, | ||
leaveToClass = `${name}-leave-to`, | ||
appear, | ||
onBeforeEnter, | ||
onEnter, | ||
onAfterEnter, | ||
onEnterCancelled, | ||
onBeforeLeave, | ||
onLeave, | ||
onAfterLeave, | ||
onLeaveCancelled, | ||
onBeforeAppear = onBeforeEnter, | ||
onAppear = onEnter, | ||
onAfterAppear = onAfterEnter, | ||
onAppearCancelled = onEnterCancelled, | ||
...baseProps | ||
}: TransitionProps): BaseTransitionProps<Element> { | ||
if (!css) { | ||
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 } = baseProps | ||
|
||
// is appearing | ||
const originEnterClass = [enterFromClass, enterActiveClass, enterToClass] | ||
const originEnterHook: any[] = [ | ||
onBeforeEnter, | ||
onEnter, | ||
onAfterEnter, | ||
onEnterCancelled | ||
] | ||
if (appear && !instance.isMounted) { | ||
enterFromClass = appearFromClass | ||
enterActiveClass = appearActiveClass | ||
enterToClass = appearToClass | ||
;[enterFromClass, enterActiveClass, enterToClass] = [ | ||
appearFromClass, | ||
appearActiveClass, | ||
appearToClass | ||
] | ||
;[onBeforeEnter, onEnter, onBeforeEnter, onEnterCancelled] = [ | ||
onBeforeAppear, | ||
onAppear, | ||
onAfterAppear, | ||
onAppearCancelled | ||
] | ||
} | ||
|
||
type Hook = (el: Element, done?: () => void) => void | ||
|
@@ -97,9 +131,15 @@ export function resolveTransitionProps({ | |
removeTransitionClass(el, enterToClass) | ||
removeTransitionClass(el, enterActiveClass) | ||
done && done() | ||
// reset enter class | ||
// reset enter class and hooks | ||
if (appear) { | ||
;[enterFromClass, enterActiveClass, enterToClass] = originEnterClass | ||
;[ | ||
onBeforeEnter, | ||
onEnter, | ||
onBeforeEnter, | ||
onEnterCancelled | ||
] = originEnterHook | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is fix bug which never called the related |
||
} | ||
|
||
|
@@ -109,23 +149,26 @@ export function resolveTransitionProps({ | |
done && done() | ||
} | ||
|
||
// only needed for user hooks called in nextFrame | ||
// sync errors are already handled by BaseTransition | ||
function callHookWithErrorHandling(hook: Hook, args: any[]) { | ||
callWithAsyncErrorHandling(hook, instance, ErrorCodes.TRANSITION_HOOK, args) | ||
const callHook: TransitionHookCaller<Element> = (hook, args) => { | ||
hook && | ||
callWithAsyncErrorHandling( | ||
hook, | ||
instance, | ||
ErrorCodes.TRANSITION_HOOK, | ||
args | ||
) | ||
} | ||
|
||
return { | ||
...baseProps, | ||
onBeforeEnter(el) { | ||
onBeforeEnter && onBeforeEnter(el) | ||
callHook(onBeforeEnter, [el]) | ||
addTransitionClass(el, enterActiveClass) | ||
addTransitionClass(el, enterFromClass) | ||
}, | ||
onEnter(el, done) { | ||
nextFrame(() => { | ||
const resolve = () => finishEnter(el, done) | ||
onEnter && callHookWithErrorHandling(onEnter as Hook, [el, resolve]) | ||
callHook(onEnter, [el, resolve]) | ||
removeTransitionClass(el, enterFromClass) | ||
addTransitionClass(el, enterToClass) | ||
if (!(onEnter && onEnter.length > 1)) { | ||
|
@@ -137,12 +180,18 @@ export function resolveTransitionProps({ | |
} | ||
}) | ||
}, | ||
onAfterEnter(el) { | ||
callHook(onAfterEnter, [el]) | ||
}, | ||
onBeforeLeave(el) { | ||
callHook(onBeforeLeave, [el]) | ||
}, | ||
onLeave(el, done) { | ||
addTransitionClass(el, leaveActiveClass) | ||
addTransitionClass(el, leaveFromClass) | ||
nextFrame(() => { | ||
const resolve = () => finishLeave(el, done) | ||
onLeave && callHookWithErrorHandling(onLeave as Hook, [el, resolve]) | ||
callHook(onLeave, [el, resolve]) | ||
removeTransitionClass(el, leaveFromClass) | ||
addTransitionClass(el, leaveToClass) | ||
if (!(onLeave && onLeave.length > 1)) { | ||
|
@@ -154,8 +203,17 @@ export function resolveTransitionProps({ | |
} | ||
}) | ||
}, | ||
onEnterCancelled: finishEnter, | ||
onLeaveCancelled: finishLeave | ||
onAfterLeave(el) { | ||
callHook(onAfterLeave, [el]) | ||
}, | ||
onEnterCancelled(el) { | ||
finishEnter(el) | ||
callHook(onEnterCancelled, [el]) | ||
}, | ||
onLeaveCancelled(el) { | ||
finishLeave(el) | ||
callHook(onLeaveCancelled, [el]) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fix the bug wihch never called |
||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's helpful for re-use type.But i'm not sure your opinion